Solved PlayerInteractEvent Detection Issue

Discussion in 'Plugin Development' started by Prosfis, Jul 10, 2014.

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

    Prosfis

    I have two samples of my code. The first sample is the bit I'm having trouble with. It will not detect the event at all. I stripped it down to the basics for testing. There is virtually no difference between this code and the second fragment, but only the second works.

    Code:java
    1. @EventHandler(ignoreCancelled = true)
    2. public void onClick(PlayerInteractEvent event) {
    3. boolean cancelled = event.isCancelled();
    4. if (cancelled)
    5. event.setCancelled(false);
    6. boolean itemUsed = false;
    7. Player player = event.getPlayer();
    8. player.sendMessage("InteractEventTriggered");
    9. if (player.equals(summoner)) {
    10. Action act = event.getAction();
    11. if (act == Action.RIGHT_CLICK_AIR
    12. || act == Action.RIGHT_CLICK_BLOCK) {
    13. player.sendMessage("Right Click");
    14.  
    15. } else if (act == Action.LEFT_CLICK_AIR
    16. || act == Action.LEFT_CLICK_BLOCK) {
    17. player.sendMessage("Left Click");
    18. }
    19. if (itemUsed) {
    20. event.setCancelled(true);
    21. PlayerInteractEvent.getHandlerList().unregister(this);
    22. }
    23. }
    24. if(cancelled)
    25. event.setCancelled(cancelled);
    26. }



    This is the code that I derived the first bit from. They are the same. This event will always be called when the first is called, so that means the event will be canceled, but I cant seem to detect it after it is canceled.

    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event) {
    3. boolean itemUsed = false;
    4. Player player = event.getPlayer();
    5. Action act = event.getAction();
    6. if (act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) {
    7. ItemStack item = player.getItemInHand();
    8. if (item.hasItemMeta()) {
    9. ItemMeta im = item.getItemMeta();
    10. if (im.hasDisplayName()) {
    11. int pclass = player.getMetadata("RPGClass").get(0).asInt();
    12. String dispName = im.getDisplayName();
    13. switch (dispName) {
    14. case MagicItems.STAFF_NAME:
    15. magicItems.classStaff(player, pclass);
    16. itemUsed = true;
    17. break;
    18. case MagicItems.TIME_TURNER_NAME:
    19. magicItems.timeTurner(player, pclass, true);
    20. itemUsed = true;
    21. break;
    22. case MagicItems.WEATHER_DIAL_NAME:
    23. magicItems.weatherDial(player, pclass);
    24. itemUsed = true;
    25. break;
    26. case MagicItems.BLINDING_SAC_NAME:
    27. magicItems.blindingSac(player, pclass);
    28. itemUsed = true;
    29. break;
    30. case MagicItems.PLAYER_PORTAL_NAME:
    31. magicItems.playerPortal(player, pclass);
    32. itemUsed = true;
    33. break;
    34. case MagicItems.VANISHING_DUST_NAME:
    35. magicItems.vanishingDust(player, pclass);
    36. itemUsed = true;
    37. break;
    38. case MagicItems.IRON_BAR_NAME:
    39. magicItems.ironBar(player, pclass);
    40. itemUsed = true;
    41. break;
    42. case MagicItems.BOMB_NAME:
    43. magicItems.bomb(player, pclass);
    44. itemUsed = true;
    45. break;
    46. case MagicItems.DRAGON_ESSENCE_NAME:
    47. magicItems.dragonEssence(player, pclass);
    48. itemUsed = true;
    49. break;
    50. }
    51. }
    52. }
    53. } else if (act == Action.LEFT_CLICK_AIR
    54. || act == Action.LEFT_CLICK_BLOCK) {
    55. ItemStack item = player.getItemInHand();
    56. if (item.hasItemMeta()) {
    57. ItemMeta im = item.getItemMeta();
    58. if (im.hasDisplayName()) {
    59. int pclass = player.getMetadata("RPGClass").get(0).asInt();
    60. String dispName = im.getDisplayName();
    61. switch (dispName) {
    62. case MagicItems.TIME_TURNER_NAME:
    63. magicItems.timeTurner(player, pclass, false);
    64. itemUsed = true;
    65. break;
    66. }
    67. }
    68. }
    69. }
    70. if (itemUsed) {
    71. event.setCancelled(true);
    72. }
    73. }



    Can anyone fathom why the first will not work?

    Btw, the second fragment of code works every time.

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

    tommycake50

    Put debug messages, make sure all events are registered.
     
  3. Offline

    _Filip

    Post your entire class.
     
  4. Offline

    Prosfis

    tommycake50

    I did put debug messages in the first bit of code. I did mean to comment that the event only goes off when clicking blocks, but not air for the first.

    TheSpherret

    Code:
    package com.yahoo.prosfis.RPGClasses.ClassListeners;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.util.BlockIterator;
     
    import com.yahoo.prosfis.RPGClasses.MagicItems;
    import com.yahoo.prosfis.RPGClasses.Spells;
     
    public class MoveEntityListener implements Listener {
     
        private Player summoner;
        private LivingEntity target;
        private Spells spells;
     
        public MoveEntityListener(Player summoner, LivingEntity target,
                Spells spells) {
            this.summoner = summoner;
            this.target = target;
            this.spells = spells;
        }
     
        @EventHandler(ignoreCancelled = true)
        public void onClick(PlayerInteractEvent event) {
            boolean cancelled = event.isCancelled();
            if (cancelled)
                event.setCancelled(false);
            boolean itemUsed = false;
            Player player = event.getPlayer();
            player.sendMessage("InteractEventTriggered");
            if (player.equals(summoner)) {
                Action act = event.getAction();
                if (act == Action.RIGHT_CLICK_AIR
                        || act == Action.RIGHT_CLICK_BLOCK) {
                    player.sendMessage("Right Click");
                   
                } else if (act == Action.LEFT_CLICK_AIR
                        || act == Action.LEFT_CLICK_BLOCK) {
                    player.sendMessage("Left Click");
                }
                if (itemUsed) {
                    event.setCancelled(true);
                    PlayerInteractEvent.getHandlerList().unregister(this);
                }
            }
            if(cancelled)
                event.setCancelled(cancelled);
        }
    }
    Ignore the many imports. They are needed for the rest of the code that I deleted for the moment.

    Gah... I had a misunderstanding of how ignoreCancelled works. I was under the impression that it meant to ignore that the event was cancelled, but it means not to proceed if the event was canceled. Thanks for the replys.

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

    _Filip

    Did you register the event Listener class?
     
Thread Status:
Not open for further replies.

Share This Page