Solved @EventHandler code happening three times

Discussion in 'Plugin Development' started by Pimp_like_me, Sep 25, 2013.

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

    Pimp_like_me

    I am creating a wizard pvp plugin but whenever players click with the item the code runs three times instead of just once like it is supposed to. I really need help fixing this.



    Code:java
    1. package Pimp;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.Location;
    4. import org.bukkit.World;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.EnderPearl;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.Snowball;
    10. import org.bukkit.entity.WitherSkull;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.EventPriority;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    15. import org.bukkit.event.entity.PlayerDeathEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17.  
    18. public class MyPlayerListener implements Listener {
    19. public static Pimp plugin;
    20. long LastRun = 0;
    21. long LastRunEnderpearl = 0;
    22.  
    23. @SuppressWarnings("deprecation")
    24. @EventHandler(priority = EventPriority.MONITOR)
    25. public void onPlayerInteract(PlayerInteractEvent event) {
    26. final Player player = event.getPlayer();
    27. int blockId = player.getItemInHand().getType().getId();
    28. if (blockId == 280) {
    29. if (LastRun < System.currentTimeMillis() - 1000) {
    30. player.throwSnowball();
    31. LastRun = System.currentTimeMillis();
    32. }
    33. } else if (blockId == 294) {
    34. Block block = player.getTargetBlock(null, 5);
    35. Location location = block.getLocation();
    36. World world = player.getWorld();
    37. if (LastRun < System.currentTimeMillis() - 1000) {
    38. world.strikeLightning(location);
    39. LastRun = System.currentTimeMillis();
    40. }
    41. } else if (blockId == 369) {
    42. if (LastRun < System.currentTimeMillis() - 1000) {
    43. player.launchProjectile(WitherSkull.class);
    44. LastRun = System.currentTimeMillis();
    45. }
    46. } else if (blockId == 406) {
    47. if (LastRunEnderpearl < System.currentTimeMillis() - 30000) {
    48. player.launchProjectile(EnderPearl.class);
    49. LastRunEnderpearl = System.currentTimeMillis();
    50. }
    51.  
    52. }
    53. }
    54.  
    55. @SuppressWarnings("deprecation")
    56. @EventHandler(priority = EventPriority.HIGHEST)
    57. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    58. if (event.getDamager() instanceof Snowball
    59. && event.getEntity() instanceof Player) {
    60. Entity ent = (Entity) event.getEntity();
    61. if (ent instanceof Player) {
    62. ((Player) ent).damage(5);
    63. }
    64. }
    65. }
    66. @EventHandler(priority = EventPriority.HIGHEST)
    67. public void onPlayerDeathEvent(PlayerDeathEvent event) {
    68. final Player player = (event.getEntity());
    69. event.setDeathMessage(ChatColor.GOLD + "" + player.getName() + ChatColor.GRAY + " has fallen!");
    70. return;
    71. }
    72.  
    73.  
    74. }
    75.  
     
  2. Offline

    KingMagnus

    Is the event supposed to happen when you right click? If so, use this:

    Code:java
    1. if(event.getAction() == Action.RIGHT_CLICK_AIR ||event.getAction() == Action.RIGHT_CLICK_BLOCK){
    2. final Player player = event.getPlayer();
    3. int blockId = player.getItemInHand().getType().getId();
    4. if (blockId == 280) {
    5. if(event.getAction() == Action.RIGHT_CLICK_AIR ||event.getAction() == Action.RIGHT_CLICK_BLOCK) if (LastRun < System.currentTimeMillis() - 1000) {
    6. player.throwSnowball();
    7. LastRun = System.currentTimeMillis();
    8. }
    9. } else if (blockId == 294) {
    10. Block block = player.getTargetBlock(null, 5);
    11. Location location = block.getLocation();
    12. World world = player.getWorld();
    13. if (LastRun < System.currentTimeMillis() - 1000) {
    14. world.strikeLightning(location);
    15. LastRun = System.currentTimeMillis();
    16. }
    17. } else if (blockId == 369) {
    18. if (LastRun < System.currentTimeMillis() - 1000) {
    19. player.launchProjectile(WitherSkull.class);
    20. LastRun = System.currentTimeMillis();
    21. }
    22. } else if (blockId == 406) {
    23. if (LastRunEnderpearl < System.currentTimeMillis() - 30000) {
    24. player.launchProjectile(EnderPearl.class);
    25. LastRunEnderpearl = System.currentTimeMillis();
    26. }
    27. }
    28. }
    29. }
     
  3. Offline

    Pimp_like_me

    No i want it either or and the way that i am making it its easier. But my problem is that the code from the interactions happens three times instead of one

    KingMagnus

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

    KingMagnus

    I just tested your code on my server and for left/right clicking anything it only did it once. Are you sure you didn't mess up anywhere?
     
  5. Offline

    amhokies

    You could have three different instances of your plugin running on your server.
     
    KingMagnus likes this.
  6. Offline

    KingMagnus

    I might just die laughing if this is your problem.
     
  7. Offline

    Pimp_like_me

    THANK YOU SO MUCH amhokies! #VirginiaTech
     
  8. Offline

    amhokies

    Glad I could help :)
     
    KingMagnus likes this.
Thread Status:
Not open for further replies.

Share This Page