Solved player interact event doesn't work

Discussion in 'Plugin Development' started by superjesse07, Apr 30, 2014.

Thread Status:
Not open for further replies.
  1. i'am making a clock that makes people disappear
    Code:java
    1. @EventHandler
    2. public void interactEvent(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. if(p.getItemInHand() == null|| p.getItemInHand().equals(Material.AIR)) return;
    6. ItemMeta meta1 = p.getItemInHand().getItemMeta();
    7. meta1.setDisplayName(ChatColor.YELLOW + "Hide Players");
    8. ItemMeta meta2 = p.getItemInHand().getItemMeta();
    9. meta2.setDisplayName(ChatColor.YELLOW + "Show Players");
    10. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    11. {
    12. if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.YELLOW + "Show Players"))
    13. {
    14. p.getItemInHand().setItemMeta(meta1);
    15. for(Player player : p.getWorld().getPlayers())
    16. {
    17. p.showPlayer(player);
    18. }
    19. }
    20. if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.YELLOW + "Hide Players"))
    21. {
    22. p.getItemInHand().setItemMeta(meta2);
    23. for(Player player : p.getWorld().getPlayers())
    24. {
    25. p.hidePlayer(player);
    26. }
    27. }
    28. }
    29. }

    but the interact event doesn't work right and it doesn't give an error

    show doesn't work

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

    mythbusterma

    Tell us more about what's not working, and try debugging it first.
     
  3. mythbusterma if i switched to Show Players then nothing happens if i right click
     
  4. Offline

    FabeGabeMC

    superjesse07 Loop through all the players on the server instead of in the world.
    ex:
    Code:java
    1. for(Player allP : Bukkit.getOnlinePlayers()){
    2. //Show
    3. p.showPlayer(allP);
    4. //Hide
    5. p.hidePlayer(allP);
    6. }
     
  5. Offline

    mythbusterma

    The code inside "if(p.getItemInHand().getMetaData.....)" is unrechable because there's no way the item's display name could be "ChatColor.YELLOW + 'Hide Players'" without first creating an object with that metadata.
     
  6. FabeGabeMC still doesn't work he doesn't get that i have a item in my hand with the name Show Players
     
  7. Offline

    CraftBang

    Did you register your event?
    Dd you try debugging like just a broadcast if the interract event is being used?
     
    Europia79 likes this.
  8. if the watch is named Hide Players it works fine
     
  9. Offline

    FabeGabeMC

    superjesse07 Ah i noticed.
    Make sure you set the meta first before the event triggers.
    Are you trying to do this with a specific item or something? That might be the case.
    Also, create the item if this (^^) is the case.
     
  10. CraftBang id did this and both messages show up
    Code:
            p.sendMessage("hi2");
            if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.YELLOW + "Hide Players"))
            {
                p.sendMessage("hi");
                p.getItemInHand().setItemMeta(meta2);
                for(Player player : Bukkit.getOnlinePlayers())
                {
                    p.hidePlayer(player);
                }
        }
    @CraftBang @FabeGabeMC @mythbusterma found it first the code looks for a item named show players then it looks for an item named hide players i just needed to put else by the second if and then it doesn't happen

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

    Licio123

    Try use Actions.Right_click...... before checking item in hand
     
Thread Status:
Not open for further replies.

Share This Page