Firework Hit

Discussion in 'Plugin Development' started by Jaaakee224, Aug 13, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Jaaakee224

    I'm working on a Railgun plugin which means, clicking a hoe will shoot fireworks and if it hits/goes very close to an entity it will kill them. Does anybody have an idea on how I would be able to accomplish this?
    Code:java
    1. package me.Jaaakee224.RailGun;
    2.  
    3. import java.lang.reflect.Method;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.Sound;
    9. import org.bukkit.block.Block;
    10. import org.bukkit.entity.Firework;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class RailGun extends JavaPlugin implements Listener {
    18.  
    19. int timer,id = 0;
    20. Random gen = new Random();
    21. public void onEnable(){
    22. this.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. @EventHandler
    27. public void onPlayerInteract(PlayerInteractEvent event) {
    28. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getPlayer().getItemInHand().equals(Material.WOOD_HOE))) {
    29. // event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.BLAZE_DEATH, 1, 9);
    30. event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.BLAZE_HIT, 2, 50);
    31. try {
    32. for(Block loc:event.getPlayer().getLineOfSight(null, 100)) {
    33. playFirework(loc.getLocation());
    34.  
    35. }
    36. } catch (Exception e) {
    37. // TODO Auto-generated catch block
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42.  
    43. private Object[] dataStore = new Object[5];
    44. public void playFirework(Location loc) throws Exception {
    45. Firework fw = (Firework) loc.getWorld().spawn(loc, Firework.class);
    46. if(dataStore[0] == null) dataStore[0] = getMethod(loc.getWorld().getClass(), "getHandle");
    47. if(dataStore[2] == null) dataStore[2] = getMethod(fw.getClass(), "getHandle");
    48. dataStore[3] = ((Method) dataStore[0]).invoke(loc.getWorld(), (Object[]) null);
    49. dataStore[4] = ((Method) dataStore[2]).invoke(fw, (Object[]) null);
    50. if(dataStore[1] == null) dataStore[1] = getMethod(dataStore[3].getClass(), "addParticle");
    51. ((Method) dataStore[1]).invoke(dataStore[3], new Object[] {"fireworksSpark", loc.getX(),loc.getY(),loc.getZ(),gen.nextGaussian() * 0.05D, -(loc.getZ()* 1.15D) * 0.5D, gen.nextGaussian() * 0.05D});
    52. fw.remove();
    53. }
    54. private Method getMethod(Class<?> cl, String method) {
    55. for(Method m : cl.getMethods()) if(m.getName().equals(method)) return m;
    56. return null;
    57. }
    58. }

    All help is appreciated :)
     
    PureGero likes this.
  2. Offline

    Jaaakee224

  3. Offline

    JordyPwner

    Jaaakee224 already looked at the quake plugin?

    Jaaakee224 Maybe this:

    The particle effects when you right click (shooting)(not the firework)
    Show Spoiler
    Code:java
    1. package com.Geekpower14.Quake.Utils;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.lang.reflect.Method;
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Player;
    7.  
    8. public enum ParticleEffects
    9. {
    10. HUGE_EXPLOSION("hugeexplosion"), LARGE_EXPLODE("largeexplode"), FIREWORKS_SPARK("fireworksSpark"), BUBBLE("bubble"), SUSPEND("suspend"), DEPTH_SUSPEND("depthSuspend"), TOWN_AURA("townaura"), CRIT("crit"), MAGIC_CRIT("magicCrit"), MOB_SPELL("mobSpell"), MOB_SPELL_AMBIENT("mobSpellAmbient"), SPELL("spell"), INSTANT_SPELL("instantSpell"), WITCH_MAGIC("witchMagic"), NOTE("note"), PORTAL("portal"), ENCHANTMENT_TABLE("enchantmenttable"), EXPLODE("explode"), FLAME("flame"), LAVA("lava"), FOOTSTEP("footstep"), SPLASH("splash"), LARGE_SMOKE("largesmoke"), CLOUD("cloud"), RED_DUST("reddust"), SNOWBALL_POOF("snowballpoof"), DRIP_WATER("dripWater"), DRIP_LAVA("dripLava"), SNOW_SHOVEL("snowshovel"), SLIME("slime"), HEART("heart"), ANGRY_VILLAGER("angryVillager"), HAPPY_VILLAGER("happerVillager"), ICONCRACK("iconcrack_"), ICONCRASH("iconcrack_0sqd"), TILECRACK("tilecrack_");
    11.  
    12. private Class<?> nmsPacketPlayOutWorldParticles = Reflection.getNMSClass("PacketPlayOutWorldParticles");
    13. private static Method getEntityHandle;
    14. private static Field getPlayerConnection;
    15. private static Method sendPacket;
    16. private String particleName;
    17.  
    18. static
    19. {
    20. try
    21. {
    22. getEntityHandle = Reflection.getOBCClass("entity.CraftPlayer").getMethod("getHandle", new Class[0]);
    23. getPlayerConnection = Reflection.getNMSClass("EntityPlayer").getDeclaredField("playerConnection");
    24. sendPacket = Reflection.getNMSClass("PlayerConnection").getMethod("sendPacket", new Class[] { Reflection.getNMSClass("Packet") });
    25. }
    26. catch (Exception e)
    27. {
    28. e.printStackTrace();
    29. }
    30. }
    31.  
    32. private ParticleEffects(String particleName)
    33. {
    34. this.particleName = particleName;
    35. }
    36.  
    37. public void sendToPlayer(Player player, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count)
    38. throws Exception
    39. {
    40. Object packet = this.nmsPacketPlayOutWorldParticles.newInstance();
    41. ReflectionUtilities.setValue(packet, "a", this.particleName);
    42. ReflectionUtilities.setValue(packet, "b", Float.valueOf((float)location.getX()));
    43. ReflectionUtilities.setValue(packet, "c", Float.valueOf((float)location.getY()));
    44. ReflectionUtilities.setValue(packet, "d", Float.valueOf((float)location.getZ()));
    45. ReflectionUtilities.setValue(packet, "e", Float.valueOf(offsetX));
    46. ReflectionUtilities.setValue(packet, "f", Float.valueOf(offsetY));
    47. ReflectionUtilities.setValue(packet, "g", Float.valueOf(offsetZ));
    48. ReflectionUtilities.setValue(packet, "h", Float.valueOf(speed));
    49. ReflectionUtilities.setValue(packet, "i", Integer.valueOf(count));
    50. sendPacket(packet, player);
    51. }
    52.  
    53. public static void sendPacket(Object packet, Player player)
    54. {
    55. try
    56. {
    57. Object nms_player = getEntityHandle.invoke(player, new Object[0]);
    58. Object nms_connection = getPlayerConnection.get(nms_player);
    59. sendPacket.invoke(nms_connection, new Object[] { packet });
    60. }
    61. catch (Exception e)
    62. {
    63. e.printStackTrace();
    64. }
    65. }
    66.  
    67. public static class ReflectionUtilities
    68. {
    69. public static void setValue(Object instance, String fieldName, Object value)
    70. throws Exception
    71. {
    72. Field field = instance.getClass().getDeclaredField(fieldName);
    73. field.setAccessible(true);
    74. field.set(instance, value);
    75. }
    76.  
    77. public static Object getValue(Object instance, String fieldName)
    78. throws Exception
    79. {
    80. Field field = instance.getClass().getDeclaredField(fieldName);
    81. field.setAccessible(true);
    82. return field.get(instance);
    83. }
    84. }
    85. }
    86.  


    The FireworkEffect on player:
    Show Spoiler

    Code:java
    1. package com.Geekpower14.Quake.Utils;
    2.  
    3. import com.Geekpower14.Quake.Quake;
    4. import java.lang.reflect.InvocationTargetException;
    5. import java.lang.reflect.Method;
    6. import org.bukkit.FireworkEffect;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.entity.Firework;
    10. import org.bukkit.inventory.meta.FireworkMeta;
    11.  
    12. public class FireworkEffectPlayer
    13. {
    14. public Quake plugin;
    15.  
    16. public FireworkEffectPlayer(Quake pl)
    17. {
    18. this.plugin = pl;
    19. }
    20.  
    21. private Method world_getHandle = null;
    22. private Method nms_world_broadcastEntityEffect = null;
    23. private Method firework_getHandle = null;
    24.  
    25. public void playFirework(World world, Location loc, FireworkEffect fe)
    26. throws Exception
    27. {
    28. Firework fw = (Firework)world.spawn(loc, Firework.class);
    29.  
    30. Object nms_world = null;
    31. Object nms_firework = null;
    32. if (this.world_getHandle == null)
    33. {
    34. this.world_getHandle = getMethod(world.getClass(), "getHandle");
    35. this.firework_getHandle = getMethod(fw.getClass(), "getHandle");
    36. }
    37. nms_world = this.world_getHandle.invoke(world, null);
    38. nms_firework = this.firework_getHandle.invoke(fw, null);
    39. if (this.nms_world_broadcastEntityEffect == null) {
    40. this.nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
    41. }
    42. FireworkMeta data = fw.getFireworkMeta();
    43.  
    44. data.clearEffects();
    45.  
    46. data.setPower(1);
    47.  
    48. data.addEffect(fe);
    49.  
    50. fw.setFireworkMeta(data);
    51.  
    52.  
    53.  
    54.  
    55. this.nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] { nms_firework, Byte.valueOf(17) });
    56.  
    57. fw.remove();
    58. }
    59.  
    60. public void playFirework(World world, Location loc)
    61. throws Exception
    62. {
    63. Firework fw = (Firework)world.spawn(loc, Firework.class);
    64. if (this.world_getHandle == null)
    65. {
    66. this.world_getHandle = getMethod(world.getClass(), "getHandle");
    67. this.firework_getHandle = getMethod(fw.getClass(), "getHandle");
    68. }
    69. Object nms_world = this.world_getHandle.invoke(world, null);
    70. Object nms_firework = this.firework_getHandle.invoke(fw, null);
    71. if (this.nms_world_broadcastEntityEffect == null) {
    72. this.nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
    73. }
    74. FireworkMeta data = fw.getFireworkMeta();
    75.  
    76. data.clearEffects();
    77.  
    78. data.setPower(1);
    79.  
    80. fw.setFireworkMeta(data);
    81. try
    82. {
    83. this.nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] { nms_firework, Byte.valueOf(17) });
    84. }
    85. {
    86. e.printStackTrace();
    87. }
    88. {
    89. e.printStackTrace();
    90. }
    91. {
    92. e.printStackTrace();
    93. }
    94. fw.remove();
    95. }
    96.  
    97. private static Method getMethod(Class<?> cl, String method)
    98. {
    99. for (Method m : cl.getMethods()) {
    100. if (m.getName().equals(method)) {
    101. return m;
    102. }
    103. }
    104. return null;
    105. }
    106. }
    107.  


    Thats what i got from the quake plugin :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Jaaakee224

    JordyPwner The code I gave is already working, meaning it shoots the line of effects exactly like Hypixel's. I don't want to copy from another plugin on Bukkit.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    mythbusterma

    Jaaakee224

    Wait 24 hours to bump.

    Also, you've already copied the entire idea from another plugin, what's a little code? That being said, you can check every so often if a firework is near an entity, and if it is, kill it.
     
  6. Offline

    Jaaakee224

    mythbusterma
    This isn't going into anything minigame related and this plugin won't be released. I'm just doing this for good practice.
     
  7. Offline

    mythbusterma

    Jaaakee224

    I never said anything about a minigame or releasing this? Look at the solution I gave you.
     
  8. Offline

    Jaaakee224

    mythbusterma
    Once I get home I will test this out. Sorry for the confusion last reply. Only reason I am copying this is to see if I can even do it. Challenging myself :)
     
Thread Status:
Not open for further replies.

Share This Page