Right Click Event (Not sure whats going on)

Discussion in 'Spigot Plugin Development' started by MannyPlays, May 14, 2016.

  1. Basically I'm trying to make it toggle vanish and change the item but I have no idea what the code is (My brother has challenged me to do this :p)
    Code (Text):
    @EventHandler
          public void StaffVisibilityOff(PlayerInteractEvent event)
          {
              ItemStack visibilityon = new ItemStack(Material.REDSTONE_TORCH_ON);
              ItemMeta visibilitymeta = visibilityon.getItemMeta();
              visibilitymeta.setDisplayName(ChatColor.GREEN + "Visibility " + ChatColor.GOLD + "» " + ChatColor.GREEN + "Enabled" + ChatColor.GRAY + " (Right Click)");
              visibilitymeta.setLore(Arrays.asList( ChatColor.GRAY + "Enable/Disable your visibility"));
              visibilityon.setItemMeta(visibilitymeta);
              //-----------------------------------------
              ItemStack visibilityoff = new ItemStack(Material.LEVER);
              ItemMeta visibilityoffmeta = visibilityoff.getItemMeta();
              visibilityoffmeta.setDisplayName(ChatColor.GREEN + "Visibility " + ChatColor.GOLD + "» " + ChatColor.RED + "Disabled" + ChatColor.GRAY + " (Right Click)");
              visibilityoffmeta.setLore(Arrays.asList( ChatColor.GRAY + "Enable/Disable your visibility"));
              visibilityoff.setItemMeta(visibilityoffmeta);
             
             
              Player player = event.getPlayer();
             
                 if(player.getItemInHand().getType().equals(Material.REDSTONE_TORCH_ON)) {
                     if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                     if(player.getItemInHand().getItemMeta().getDisplayName().contains("Visibility")) {
                     if(!Staff.contains(player)) {
                         player.sendMessage(ChatColor.RED + "You are not allowed to use this item without being in Staff Mode!");
                         player.setItemInHand(null);
          }
                     if(Staff.contains(player)) {
                         if(VanishAPI.isInvisible(player) == true) {
                             player.setItemInHand(new ItemStack(visibilityoff));

                             VanishAPI.showPlayer(player);
                         }
                     }
                     }
                     
                     }
                 }
          }
     
  2. What's the problem exactly?
     
  3. Also, you might want to save the 2 items instead of creating new instances every time.
     
  4. It doesn't work I right clicked it instantly vanish then visible you see the lever for 1ms then it disappears
     
  5. Is this what you are looking for?
    Also just a quick tip use player.getInventory().setItemInMainHand(Item); vs player.setItemInHand() as it was depreciated.
    Code (Text):
    public class StaffVisibility implements Listener{
       
        ItemStack visibilityon = new ItemStack(Material.REDSTONE_TORCH_ON);
        ItemStack visibilityoff = new ItemStack(Material.LEVER);

        public StaffVisibility() {
            ItemMeta metaon = visibilityon.getItemMeta();
            metaon.setDisplayName(ChatColor.GREEN + "Visibility " + ChatColor.GOLD + "» " + ChatColor.GREEN + "Enabled" + ChatColor.GRAY + " (Right Click)");
            metaon.setLore(Arrays.asList( ChatColor.GRAY + "Enable/Disable your visibility"));
            visibilityon.setItemMeta(metaon);
           
            ItemMeta metaoff = visibilityoff.getItemMeta();
            metaoff.setDisplayName(ChatColor.GREEN + "Visibility " + ChatColor.GOLD + "» " + ChatColor.RED + "Disabled" + ChatColor.GRAY + " (Right Click)");
            metaoff.setLore(Arrays.asList( ChatColor.GRAY + "Enable/Disable your visibility"));
            visibilityoff.setItemMeta(metaoff);
           
           
           
        }
       
        @EventHandler
        public void StaffVisibilityOff(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(!event.getItem().equals(visibilityon) || !event.getItem().equals(visibilityoff)){
                return;
            }
            if(Staff.contains(player)){
                if(event.getItem().equals(visibilityon)){
                    VanishAPI.showPlayer(player);
                    player.getInventory().setItemInMainHand(visibilityoff);
                }else{
                    VanishAPI.hidePlayer(player);
                    player.getInventory().setItemInMainHand(visibilityoff);
                }
            }else{
                player.sendMessage(ChatColor.RED + "You are not allowed to use this item without being in Staff Mode!");
                player.getInventory().setItemInMainHand(null);
            }
        }

    }
     
  6. From my experience when right clicking a block it triggers the PlayerInteractEvent twice. Pretty sure its a bug.
     
  7. Starting with 1.9, the PlayerInteractEvent will fire twice per action. This is because we now have two hands. If you only want to listen to one hand, ignore the other like this:

    Code (Text):

    public void onInteract(PlayerInteractEvent event) {
        if (event.getHand() == null || event.getHand().equals(EquipmentSlot.OFF_HAND)) {
            return; // ignore offhand 2nd packet
        }

        // continue with event like normal here
    }
     
     
    • Useful Useful x 2
    • Informative Informative x 1
  8. Thank you for the information, that is very useful.
     
  9. Code (Java):
    if (event.getHand().equals(EquipmentSlot.HAND) && player.getInventory().getItemInMainHand().getType() == Material.COMPASS) {
       //Code
    }
    Could you just check it this way to make it easier? To see if it was your main hand, and you have a certain item in that hand.
     
  10. You could implement it any way you see fit. ^_^

    Just some people (like me) have a personal preference to not nest code unnecessarily like that. It makes it harder to read and debug.

    For some, this is easier to read:
    Code (Text):

    if (something) {
        if (somethingelse) {
            if (anothercheck) {
                if (andyetanothercheck) {
                    // do something.
                }
            }
        }
    }
     
    Most (including myself) find it extremely difficult to read and debug. For us, we invert the checks and simply return out.

    Code (Text):

    if (!something) {
        return; // something failed
    }
    if (!somethingelse) {
        return; // somethingelse failed
    }
    if (!anothercheck) {
        return; // anothercheck failed
    }
    if (!andyetanothercheck) {
        return; // andyetanothercheck failed
    }

    // finally do something
     
    With this method we aren't hunting down exit/failing points trying to debug something. Especially and larger amounts of code. No one likes to indent 30 times because nested blocks.. ^_^

    But, like I said, it really just boils down to personal preference. ;)
     
  11. yea...1.9 ....its 1.8 XD
     
  12. No idea then. I dont support 1.8 and neither does Spigot anymore :p
     
  13. @BillyGalbreath This thread may be old but I got a question :) When I use your e.gethand thingie I got a nosuchmethoderror. You know a fix?
     
  14. Choco

    Moderator

    1. Create a new thread if you know this one is old
    2. You're most likely not using 1.9. That method was only added after the 1.9 update
     
  15. Show us your code. It's definitely a method, so either you imported your library wrong, or something else happened. The method works for everyone else though.
     
  16. @GamerzKing ofcourse
    Code (Text):
    @EventHandler
        public void onRC(PlayerInteractEntityEvent e){
        if (e.getHand() == null || e.getHand().equals(EquipmentSlot.OFF_HAND)) return;
            if(e.getRightClicked() instanceof Player){
             
                Player target = (Player) e.getRightClicked();  
                target.setHealth(0.0);
             
            }
        }
    Error:

    http://imgur.com/a/oJRVM
     
  17. What version does your server use?