Solved Ignoring ItemMeta?

Discussion in 'Plugin Development' started by KarimAKL, Jun 18, 2018.

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


    What i want is for this line of code:
    2. player.getInventory().removeItem(new ItemStack(Material.DIRT, 1));

    to ignore ItemMeta. (Like if it has a displayname or lore) How would i do this? I've been trying for some time now but can't seem to find any way to do this. :/
  2. Offline


    What you should do is check if the material types are the same.
  3. Offline


    @CommonSenze Then what? I've been checking it alot of times but i always end up with needing to remove it no matter the ItemMeta. :/
  4. Offline



    1. player.getInventory().remove(Material.DIRT);

    This way removes all items that are the same Material. If you want a more specific way, like removing a specific itemstack, it is going to be more tricky.
  5. Offline


    @CommonSenze I just want to remove 1 dirt no matter what ItemMeta it has. (If it even has any)
  6. Offline


    Go through the players inventory in an int i = 0 for loop. Get the item in the certain slot, check if its not null, check the material is dirt, set the amount to the already determined amount minus one. <3
  7. Offline


    @CommonSenze Will try that now then, thank you. :)
    EDIT: I just tried this:
    2. public void removeItem(Player p) {
    3. PlayerInventory inv = p.getInventory();
    4. for (int i = 0; i < inv.getContents().length; i++) {
    5. if (inv.getItem(i) == null) {
    6. continue;
    7. }
    8. if (inv.getItem(i).getType() == Material.DIRT) {
    9. Integer amount = inv.getItem(i).getAmount();
    10. p.sendMessage("Amount: "+amount);
    11. if (amount > 0) {
    12. inv.getItem(i).setAmount(amount-1);
    13. }
    14. p.sendMessage("Amount: "+amount);
    15. }
    16. }
    17. }

    And it seems to work but i just have 2 problems now.
    1. It doesn't remove 1 from the amount if you only have 1 of the item. :/ (I put 2 messages in there for testing)
    2. It removes 1 from all the stacks of dirt i have, maybe something like this would block that?:
    2. for (int i = 0; i < 1; i++) {
    3. //Do something with 1 of the items.
    4. }

    EDIT: Nvm, didn't work. :p (At least not the way i tried/imagined it might work)
    Anyway, thanks for helping me finally get past that, it was actually pretty simple.. Now that i look at it i feel like an idiot, anyway thanks again. :7
    Last edited: Jun 18, 2018
  8. Offline


    Mark as solved
  9. Offline


    @MightyOne Whoops, my bad. :p Thank you for the reminder.
Thread Status:
Not open for further replies.

Share This Page