Need help - Stop player from moving item in inventory

Discussion in 'Plugin Development' started by Maxx_Qc, Jul 7, 2015.

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

    Maxx_Qc

    Hi guys, I want to block every players from moving a certain item in their inventory.
    This is my code:
    Code:
    @EventHandler
        public void onInventoryClickEven(InventoryClickEvent event) {
            ItemStack LoveGun = new ItemStack(Material.STICK);
            ItemMeta LoveGunM = LoveGun.getItemMeta();
            LoveGunM.setDisplayName(colorize(LoveGunName));
            LoveGun.setItemMeta(LoveGunM);
            if(event.getCurrentItem().equals(LoveGun)){
                event.setCancelled(true);
                event.setResult(Result.DENY);
                event.setCursor(new ItemStack(Material.AIR));
                event.getWhoClicked().closeInventory();
            }
           
            if(event.isShiftClick() && (event.getSlot() == 4)) {
                event.setCancelled(true);
                event.setResult(Result.DENY);
                event.setCursor(new ItemStack(Material.AIR));
                event.getWhoClicked().closeInventory();
            }
        }
    The problem is when the player shift+click or click on a number (hotbar), the item get duplicated and then the inventory close. So they have 2 items instead of one. Can someone help me with this?
     
  2. Offline

    Hawktasard

    @Maxx_Qc
    This should work. I'm pretty sure it checks for durability tho so if your item isn't unbreakable then that might be a problem.
    Code:java
    1. if(itemStack.isSimilar(anotherItemStack)) {
    2. // TODO Put code in here.
    3. }
     
  3. Offline

    DoggyCode™

    Umm, don't know if this will work or not.. but try to close the inventory right after the event is cancelled. Like this:

    Code:
    
    @EventHandler
      public void onInventoryClickEven(InventoryClickEvent event) {
         Player p = (Player)event.getWhoClicked();
      ItemStack LoveGun = new ItemStack(Material.STICK);
      ItemMeta LoveGunM = LoveGun.getItemMeta();
      LoveGunM.setDisplayName(colorize(LoveGunName));
      LoveGun.setItemMeta(LoveGunM);
      if(event.getCurrentItem().equals(LoveGun)){
      event.setCancelled(true);
      p.closeInventory();
      event.setResult(Result.DENY);
      event.setCursor(new ItemStack(Material.AIR));
      }
       
      if(event.isShiftClick() && (event.getSlot() == 4)) {
      event.setCancelled(true);
      p.closeInventory();
      event.setResult(Result.DENY);
      event.setCursor(new ItemStack(Material.AIR));
      }
      }
    @Maxx_Qc
     
  4. Offline

    Agentleader1

    Don't attempt to use equals on item stacks as you are checking boolean if they're the same instance (which they're most likely not). Just make each item name in your game unique (Whatsoever), then check if the item has the correct display name. And all that junk about setting cursor to none is unnecessary. You just need to ice.setCancelled(true) and it does the work for you. Also if you can't make item names unique, try a == instead of .equals();
     
Thread Status:
Not open for further replies.

Share This Page