Checking if a players has an item then removing it

Discussion in 'Plugin Development' started by Ducyooo, Oct 12, 2020.

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

    Ducyooo

    Hello guys, I'm switching between inventories and it all works.. now i have this method to check if a player has "requirements" to craft an item it all worked fine but now, it seems when i switch to a new inventory the method calls but it stops in the first if.. the "clickedItem" that I'm giving to it IS an ItemStack but now it doesnt work can you please help?

    Code:
        private void craftUno(InventoryClickEvent event, Player player, ItemStack clickedItem, Material requirement, Material itemToGive, int number) {
    
            if (event.getCurrentItem().equals(clickedItem)) {
                event.setCancelled(true);
                if (player.getInventory().containsAtLeast(new ItemStack(requirement), number)) {
                    player.getInventory().addItem(new ItemStack(itemToGive));
                    player.getInventory().removeItem(new ItemStack(requirement, number));
                    Colorize.sendMessage(player, "&aYou crafted successfully a: " + event.getCurrentItem().getType().toString());
                    event.setCancelled(true);
                } else Colorize.sendMessage(player, "&cYou don't have the requirements to craft this item!");
                event.setCancelled(true);
            }
        }
     
  2. Offline

    Shqep

    I actually don't understand what you would like to do here but others may. It's better if you could paste your codes in a PHP tag or a Java Syntax tag, if you'd prefer to use other pasting sites, hastebin and hatebin are also pretty good. (Because highlighting like come on u wouldnt write codes in notepad and compile it later)

    What you said about your problem is really vague. Maybe elaborate and post full codes?
     
  3. Offline

    Strahan

    Bear in mind, the two ItemStacks need to be identical the way you are doing it. If the item is an item subject to wear for example, if it has one little bit of durability difference versus the item you are checking, it fails the equals check.

    I'd either use ItemStack#isSimilar or just write my own Boolean method to check whatever makes the item special manually, then return the evaluation.
     
Thread Status:
Not open for further replies.

Share This Page