Solved InventoryClickEvent doesn't remove item

Discussion in 'Plugin Development' started by alexisngo1605, Aug 16, 2015.

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

    alexisngo1605

    Hello ! So i'm trying to making a plugin and when you click in a bucket it remove this but when I click in the bucket nothing happen and i keep the item here the code

    Code:
    @SuppressWarnings("deprecation")
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getCurrentItem().getItemMeta() == null)
                return;
            ItemStack bucket = new ItemStack(Material.BUCKET);
            bucket.setAmount(e.getCurrentItem().getAmount());
            if (e.getCurrentItem().equals(bucket)) {
                e.getWhoClicked().getInventory().removeItem(e.getCurrentItem());
                e.getWhoClicked.updateInventory();
     
  2. Offline

    SkyleTyler1337

    try setting the item meta of the bucket
     
  3. Offline

    mine-care

    @alexisngo1605 Have you debuged?
    im not sure if that would work.
     
  4. Offline

    alexisngo1605

    Debugged ? Can you explain me please ^^' ?
     
  5. Offline

    Xtreme727

    I'm not sure, but instead of getItemMeta() == null, I think you would use the boolean hasItemMeta()
     
  6. Offline

    L3N

    The method equals() doesn't work well with Items/ItemStacks so you have two options:
    - You can use e.getCurrentItem().isSimilar(bucket); (Recommended)
    or
    - Manually check for their display names, lore, material etc.. (Not recommended)
     
    mine-care likes this.
  7. Offline

    mine-care

  8. Offline

    alexisngo1605

    I thought that could work but it's always not working.
    I put a message in the code and I just receive the message, the bucket is not removed !
    Does the cause is my SpigotAPI or the code ? (I use Spigot 1.7.10 R.01 1645)
    Code:
    @EventHandler
        public void  onInventoryClick(InventoryClickEvent e) {
            ItemStack bucket = new ItemStack(Material.BUCKET);
            bucket.setAmount(e.getCurrentItem().getAmount());
            if (e.getCurrentItem().isSimilar(bucket)) {
                e.getInventory().removeItem(e.getCurrentItem());
                ((Player) e.getWhoClicked()).updateInventory();
                ((CommandSender) e.getWhoClicked()).sendMessage("Test");
    The bucket only get removed in a furnace and not in the inventory !
     
    Last edited: Aug 17, 2015
  9. Offline

    L3N

    It has nothing to do with SpigotAPI but try to add in the last line
    Code:
    e.setCancelled(true);
     
  10. Offline

    au2001

    @alexisngo1605 If you use ItemStack#isSimilar(ItemStack), you don't have to setAmount.
    And maybe try e.setCurrentItem(null)?

    And Spigot isn't really supported here, you can use Bukkit 1.7 though.
     
  11. Offline

    alexisngo1605

    Thanks guys, I'm gonna try it when I can
     
  12. Offline

    L3N

    Reply if it helped, but @au2001 comment I think it's not e.setCurrentItem(null) but instead e.setCurrentItem(Material.AIR).
     
  13. Offline

    NickenatorDev

    Use:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
    
            ItemStack currentItem = event.getCurrentItem();
    
            Inventory playerInventory = player.getInventory();
    
            if (currentItem.getType() == Material.BUCKET) {
                if (currentItem.getAmount() == 1) {
                    playerInventory.remove(Material.BUCKET);
           
                    player.updateInventory();
                } else if (currentItem.getAmount() > 1) {
                    currentItem.setAmount(currentItem.getAmount() - 1);
           
                    player.updateInventory();
                }
            }
        }
    There is not too much wrong with the method that you are using except for the fact that you need to add a few checks into the method as well as a line of code that you should not have:

    Code:
    bucket.setAmount(e.getCurrentItem().getAmount());
    Even if you were to tweak that line of code a little bit it still may not work in some cases because of checks that you are missing in your method but the way that you have it is setting the item amount to it's current amount. Also, for any future references, instead of using:

    Code:
    if (event.getCurrentItem().getItemMeta() == null) {
        return;
    }
    Use:

    Code:
    if (!event.getCurrentItem().hasItemMeta()) {
        return;
    }
     
    Last edited: Aug 26, 2015
  14. Offline

    alexisngo1605

    Your code looks good thanks :)
     
  15. Offline

    au2001

    @NickenatorDev
    Code:
    if (currentItem.getAmount() <= 1)
        playerInventory.remove(currentItem);
    else
        currentItem.setAmount(currentItem.getAmount() - 1);
    
    player.updateInventory();
     
  16. Offline

    alexisngo1605

    My code is now perfect thanks a lot ! :)
     
Thread Status:
Not open for further replies.

Share This Page