Solved Bug by creating hide and unhide plugin.

Discussion in 'Plugin Development' started by Louisfx, Jul 28, 2014.

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

    Louisfx

    Hey guys,
    So I wanded to do a hide and unhide plugin.
    Here's the code that I have for the moment:
    Code:java
    1. @EventHandler
    2. public void onPlayeruseHide(PlayerInteractEvent e){
    3. Player p = (Player) e.getPlayer();
    4.  
    5.  
    6. if(p.getItemInHand().getType() == Material.SLIME_BALL){
    7. e.setCancelled(true);
    8. for(Player ph : Bukkit.getOnlinePlayers()){
    9. p.hidePlayer(ph);
    10. p.setItemInHand(new ItemStack(Material.MAGMA_CREAM));
    11. p.sendMessage(ChatColor.GREEN + "You hide the players");
    12. }
    13.  
    14. }
    15.  
    16. }
    17.  
    18. @EventHandler
    19. public void onPlayeruseUnHide(PlayerInteractEvent e){
    20. Player p = e.getPlayer();
    21.  
    22. if(p.getItemInHand().getType() == Material.MAGMA_CREAM){
    23. e.setCancelled(true);
    24. for(Player ph : Bukkit.getOnlinePlayers()){
    25. p.canSee(ph);
    26. p.setItemInHand(new ItemStack(Material.SLIME_BALL));
    27. p.sendMessage(ChatColor.GREEN + "You unhide the players");
    28. }
    29. }
    30. }

    But when I try it in game, it's working with the Slime Ball but when I use the Magma Cream it's telling me "You hide the players" and an other line "You hide the players".
    So if you find the problem, tell me the problem please.
    Thanks you for helping me(or trying to) :)
     
  2. Louisfx
    Don't use
    Code:
    p.canSee(ph);
    use
    Code:
    p.showPlayer(ph);
     
  3. Offline

    Garris0n

    You should use one event handler, not two. When the first one is called, you set the item in their hand to the other item. Then, when the second one is called, they're now holding the other item, so your if statement returns true and it executes as well.

    You also have another issue, and you can fix that by reading the JavaDocs.
    http://jd.bukkit.org/dev/doxygen/d5...Player.html#af5bce9b888154b2eca3dfb11d719a9c1
    http://jd.bukkit.org/dev/doxygen/d5...Player.html#a2264d53f85cbb325ba0d01f7377c6438
     
  4. Offline

    Louisfx

    Garris0n when I try with one Event Handler it's no working...
    Other solutions ?

    DJSkepter Do you have an other solution ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. (Now I know Garris0n is probably going to kill me for spoonfeeding but)
    Code:java
    1. @EventHandler
    2. public void onPlayeruseHide(PlayerInteractEvent e) {
    3. /* Unnecessary cast */
    4. // Player p = (Player) e.getPlayer();
    5. Player p = e.getPlayer();
    6. if (p.getItemInHand().getType().equals(Material.SLIME_BALL)) {
    7. e.setCancelled(true);
    8. for (Player ph : Bukkit.getOnlinePlayers()) {
    9. p.hidePlayer(ph);
    10. p.setItemInHand(new ItemStack(Material.MAGMA_CREAM));
    11. p.sendMessage(ChatColor.GREEN + "You hide the players");
    12. }
    13. } else if (p.getItemInHand().getType().equals(Material.MAGMA_CREAM)) {
    14. e.setCancelled(true);
    15. for (Player ph : Bukkit.getOnlinePlayers()) {
    16. /* Wrong method */
    17. // p.canSee(ph);
    18. p.showPlayer(ph);
    19. p.setItemInHand(new ItemStack(Material.SLIME_BALL));
    20. p.sendMessage(ChatColor.GREEN + "You unhide the players");
    21. }
    22. }
    23. }

    You may want to use a check to see what action it is.
     
  6. Offline

    Louisfx

    DJSkepter Thank you so much !
    You're the best ! :)
     
  7. Offline

    Garris0n

    ಠ_ಠ
     
    Jaaakee224, DJSkepter and Dragonphase like this.
  8. Few issues I have with this:
    • The item in the player's hand may be null.
    • Use event.getItem() instead of p.getItemInHand(), it exists for a reason.
    • Not sure if it'll make a difference, but you should check if the player does not equal itself, using usernames/UUIDs in the loop.
    • Why would you set the item in hand to a Magma Cream inside the loop? Every player loop, it's going to set itself.
    • You're sending the message INSIDE the loop, meaning for every player online, it'll send the player the message. Imagine if there were over 100 people in the Hub, like Hypixel. God, that'd spam. :eek:
     
    DJSkepter and Maved like this.
  9. KingFaris11 likes this.
  10. Offline

    Garris0n

    No, it won't, because CraftBukkit is extremely inconsistent and doesn't specifically turn null in half the methods...
     
  11. What? I don't get you there....
     
  12. Offline

    Louisfx

    KingFaris11 I tried it it's not sending a message to everybody, only the player who clicked will get the message.
    But thank you for trying to help me :)
     
  13. Offline

    Garris0n

  14. Oh yeah - that's what I was thinking (Air) but I didn't understand how it works. xD But still, the item could be null.

    Yes I know that, I didn't say it's going to send a message to everybody, I said it's going to send a message to the player who clicked as many times as the amount of players online.

    Ignore this Garrison since apparently you hate spoon-feeding:
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            if (!event.getAction().name().startsWith("RIGHT_") || event.getItem() == null) return;
            if (event.getItem().getType().equals(Material.SLIME_BALL)) {
                event.setCancelled(true);
                for (Player oP : event.getPlayer().getServer().getOnlinePlayers()) {
                    if (!event.getPlayer().getUniqueId().equals(oP.getUniqueId())) event.getPlayer().hidePlayer(oP);
                }
                event.getPlayer().setItemInHand(new ItemStack(Material.MAGMA_CREAM));
                event.getPlayer().sendMessage(ChatColor.GREEN + "You hid all players!");
            } else if (event.getItem().getType().equals(Material.MAGMA_CREAM)) {
                event.setCancelled(true);
                for (Player oP : event.getPlayer().getServer().getOnlinePlayers()) {
                    if (!event.getPlayer().getUniqueId().equals(oP.getUniqueId())) event.getPlayer().showPlayer(oP);
                }
                event.getPlayer().setItemInHand(new ItemStack(Material.SLIME_BALL));
                event.getPlayer().sendMessage(ChatColor.GREEN + "You've unhidden the players!");
            }
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page