ProjectileHitEvent

Discussion in 'Plugin Development' started by 17nhammond, Jan 11, 2014.

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

    17nhammond

    Hi,
    I have an event listening for a projectile hit. It works fine but I need to get the location of the block that the arrow is sticking into. I want the block to break after an arrow hits it.

    Thank you,
    NateD101

    Code:java
    1. package me.NateD101.GlassBreaker;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.entity.Arrow;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.ProjectileHitEvent;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class glassbreaker extends JavaPlugin implements Listener
    18. {
    19. Logger logger = Logger.getLogger("Minecraft");
    20. public static glassbreaker plugin;
    21.  
    22. @Override
    23.  
    24. public void onDisable()
    25. {
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. logger.info(pdfFile.getName() + " Has Been Disabled!");
    28. getServer().clearRecipes();
    29. }
    30.  
    31. public void onEnable()
    32.  
    33. {
    34. getServer().getPluginManager().registerEvents(this, this);
    35. PluginDescriptionFile pdfFile = this.getDescription();
    36. logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    37.  
    38. }
    39.  
    40. @EventHandler(priority = EventPriority.HIGH)
    41. public void onArrowHit(ProjectileHitEvent e)
    42. {
    43. System.out.println(e.getEntity());
    44. Entity ent = e.getEntity();
    45. if(ent instanceof Arrow)
    46. {
    47. System.out.println("Entity is an arrow!");
    48. Location loc = ent.getLocation().subtract(0, 1, 0);
    49. Block b = loc.getBlock();
    50. System.out.println("Location: " + loc + " | Block: " + b.getType());
    51. if(b.getType() == Material.DIAMOND_BLOCK)
    52. {
    53. System.out.println(b.getType());
    54. b.breakNaturally();
    55. }
    56. }
    57. }
    58. }
     
  2. Offline

    tommycake50

    The one that it is sticking into is the same one as Arrow.getLocation() isn't it?
     
  3. Offline

    17nhammond

    tommycake50
    When you type that it gets the occupying space of the arrow and not the block it is sticking into.
    Thanks!
    NateD101
     
  4. Offline

    tommycake50

    Well I guess you are going to have to get the direction that it is facing then get the block on the corresponding blockface of the airblock that the arrow is in.
     
Thread Status:
Not open for further replies.

Share This Page