Block Itirators and fireworks

Discussion in 'Plugin Development' started by XgXXSnipz, Sep 24, 2014.

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

    XgXXSnipz

    ok so I made a firework gun, all I need is to know how to make the firework do about 6.3 hearts of damage and play a firework effect(BTW I already tried the effect, nothing happens) code:
    Code:java
    1. Object[] dataStore = new Object[5];
    2.  
    3.  
    4.  
    5. public void onEnable(){
    6. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    7. }
    8.  
    9. public static Method getMethod(Class<?> cl, String method)
    10. {
    11. for (Method m : cl.getMethods()) {
    12. if (m.getName().equals(method)) {
    13. return m;
    14. }
    15. }
    16. return null;
    17.  
    18. }
    19.  
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onInteract(PlayerInteractEvent e) {
    24. final Player p = e.getPlayer();
    25. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    26. if (p.getItemInHand().getType() == Material.STICK) {
    27. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 5, 2));
    28.  
    29. p.playSound(p.getEyeLocation(), Sound.EXPLODE, 0.6F, 2.0F);
    30. p.getWorld().playEffect(p.getEyeLocation().add(p.getLocation().getDirection()), Effect.SMOKE, 4);
    31.  
    32.  
    33.  
    34.  
    35. for (Block block : p.getLineOfSight(null, 30)) {
    36.  
    37. try
    38. {
    39.  
    40.  
    41. Firework fw = (Firework)block.getWorld().spawn(block.getLocation(), Firework.class);
    42.  
    43. FireworkEffect effect = FireworkEffect.builder().trail(true).flicker(false).withColor(Color.RED).with(Type.BURST).build();
    44. FireworkMeta fwm = fw.getFireworkMeta();
    45. fwm.addEffect(effect);
    46.  
    47. if (dataStore[0] == null) {
    48. dataStore[0] = getMethod(block.getWorld().getClass(), "getHandle");
    49. }
    50. if (dataStore[2] == null) {
    51. dataStore[2] = getMethod(fw.getClass(), "getHandle");
    52. }
    53. dataStore[3] = ((Method)dataStore[0]).invoke(block.getWorld(), (Object[])null);
    54.  
    55. dataStore[4] = ((Method)dataStore[2]).invoke(fw, (Object[])null);
    56. if (dataStore[1] == null) {
    57. dataStore[1] = getMethod(dataStore[3].getClass(), "addParticle");
    58. }
    59. ((Method)dataStore[1]).invoke(dataStore[3], "fireworksSpark", block.getX(), block.getY(), block.getZ(), new Random().nextGaussian() * 0.05D, -(block.getZ() * 1.15D) * 0.5D, new Random().nextGaussian() * 0.05D);
    60. fw.setFireworkMeta(fwm);
    61. fw.remove();
    62. }
    63. catch (Exception ignore) {}
    64. }
    65. }
    66.  
    67. }
    68. }
    69. }
    70.  


    somone help please!

    ok nvm I got it to work, just I need it so the player that shoots wont take damage as well, im using explosions :D
    Code:java
    1. package me.JoeyLangston.AssualtRifle;
    2.  
    3. import java.lang.reflect.Method;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Color;
    8. import org.bukkit.Effect;
    9. import org.bukkit.FireworkEffect;
    10. import org.bukkit.FireworkEffect.Type;
    11. import org.bukkit.Material;
    12. import org.bukkit.Sound;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.entity.Firework;
    15. import org.bukkit.entity.LivingEntity;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.Action;
    20. import org.bukkit.event.inventory.InventoryOpenEvent;
    21. import org.bukkit.event.inventory.InventoryType;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.inventory.ItemStack;
    24. import org.bukkit.inventory.meta.FireworkMeta;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26. import org.bukkit.potion.PotionEffect;
    27. import org.bukkit.potion.PotionEffectType;
    28.  
    29. public class AssualtRifle extends JavaPlugin implements Listener {
    30.  
    31. Object[] dataStore = new Object[5];
    32.  
    33.  
    34.  
    35. public void onEnable(){
    36. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    37. }
    38.  
    39. public static Method getMethod(Class<?> cl, String method)
    40. {
    41. for (Method m : cl.getMethods()) {
    42. if (m.getName().equals(method)) {
    43. return m;
    44. }
    45. }
    46. return null;
    47.  
    48. }
    49.  
    50.  
    51. @SuppressWarnings("deprecation")
    52. @EventHandler
    53. public void onInteract(PlayerInteractEvent e) {
    54. final Player p = e.getPlayer();
    55. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    56. if (p.getItemInHand().getType() == Material.STICK) {
    57. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 5, 2));
    58.  
    59. p.playSound(p.getEyeLocation(), Sound.EXPLODE, 0.6F, 2.0F);
    60. p.getWorld().playEffect(p.getEyeLocation().add(p.getLocation().getDirection()), Effect.SMOKE, 4);
    61.  
    62.  
    63.  
    64.  
    65. for (Block block : p.getLineOfSight(null, 30)) {
    66.  
    67. try
    68. {
    69.  
    70.  
    71. Firework fw = (Firework)block.getWorld().spawn(block.getLocation(), Firework.class);
    72.  
    73.  
    74. FireworkEffect effect = FireworkEffect.builder().trail(true).flicker(false).withColor(Color.RED).with(Type.BURST).build();
    75. FireworkMeta fwm = fw.getFireworkMeta();
    76. fwm.addEffect(effect);
    77.  
    78. if (dataStore[0] == null) {
    79. dataStore[0] = getMethod(block.getWorld().getClass(), "getHandle");
    80. }
    81. if (dataStore[2] == null) {
    82. dataStore[2] = getMethod(fw.getClass(), "getHandle");
    83. }
    84. dataStore[3] = ((Method)dataStore[0]).invoke(block.getWorld(), (Object[])null);
    85.  
    86. dataStore[4] = ((Method)dataStore[2]).invoke(fw, (Object[])null);
    87. if (dataStore[1] == null) {
    88. dataStore[1] = getMethod(dataStore[3].getClass(), "addParticle");
    89. }
    90. ((Method)dataStore[1]).invoke(dataStore[3], "fireworksSpark", block.getX(), block.getY(), block.getZ(), new Random().nextGaussian() * 0.05D, -(block.getZ() * 1.15D) * 0.5D, new Random().nextGaussian() * 0.05D);
    91. fw.setFireworkMeta(fwm);
    92. fw.getWorld().createExplosion(fw.getLocation(), -2);
    93.  
    94. for(LivingEntity en : fw.getWorld().getEntitiesByClass(LivingEntity.class)){
    95. if(en.getLocation().distance(fw.getLocation()) <= 30)//or your distance{
    96. en.damage(7);
    97. en.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 50, 50));
    98. }
    99. fw.remove();
    100.  
    101. }
    102.  
    103. catch (Exception ignore) {}
    104. }
    105. }
    106. }
    107.  
    108.  
    109. }
    110.  
    111. @EventHandler
    112. public void onInventoryOpen(InventoryOpenEvent e) {
    113.  
    114.  
    115. Player p = (Player) e.getPlayer();
    116. if (e.getInventory().getType() == InventoryType.CHEST) {
    117. e.getPlayer().getInventory().addItem(new ItemStack(Material.values()[new Random().nextInt(Material.values().length)]));
    118. e.setCancelled(true);
    119. p.getLocation().getBlock().breakNaturally();
    120.  
    121.  
    122.  
    123. }
    124. return;
    125.  
    126.  
    127. // rawr
    128. }
    129. }
    130.  
    131.  


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

    Gamecube762

    Instead of creating an explosion, get the nearby entities to the firework, filter them, then deal damage to them.
     
  3. Offline

    XgXXSnipz

    Gamecube762 Hey ok i just realized that i dont like the way this works, now can you give me an example for the getting nearby entites and make them do damage? also! can you tell me how to play an effect to the place the entity takes damage from, eg. like I shoot a player and it plays a greenball right were he got shot thanks!
     
  4. Offline

    Gamecube762

  5. Offline

    XgXXSnipz

  6. Offline

    Gamecube762

    XgXXSnipz I'm sure you know the unwritten rule about spoon-feeding.

    Anyways, here is some pseudo code:
    Code:
    Entity someEntity
    Number some#
     
    for entities : someEntity.getNearbyEntities(some# some# some#)
    if entities is a living entity, then .damage(someDamageNumber) or .damage(someDamageNumber someEntityThatShotTheWeapon)
     
    
     
  7. Offline

    XgXXSnipz

    Gamecube762 kk thx, I didnt expect spoon feeding I just wanted an example
     
Thread Status:
Not open for further replies.

Share This Page