Sysy
Sysy

Reputation: 91

Minecraft Spigot Remove item from inventory

I'm making a bedwars plugin in spigot 1.8_R3, I'm currently working on the shop system, when you click on an item to purchase it, 4 iron ingots should be removed from the players inventory.

inventory.remove(new ItemStack(getResource(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".type")), Integer.parseInt(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".cost"))));

However, this code only removes the iron if there are exactly 4 iron ingots. How would I make it so 4 iron ingots would be removed from different amounts such as 5?

Edit : I tried to use this code :

    public void removeItemFromInventory(Inventory inv, ItemStack currentItem, Player p) {
        ItemStack item = new ItemStack(getResource(Main.plugin.getConfigValue("shop." + currentItem.getTypeId() + ".type")), Integer.parseInt(Main.plugin.getConfigValue("shop." + currentItem.getTypeId() + ".cost")));
        if(inv.contains(item)) { // contains the exact item
            inv.remove(item); // remove first time it find this item
        } else { // doesn't contains this item
            for(ItemStack invItem : inv.getContents()) {
                if(invItem.getType().equals(item.getType())) { // if it's this type of item.
                    // You can add other check specially for ItemMeta ...

                    int amount = invItem.getAmount(); // amount of actual item
                    int stay = item.getAmount(); // keep amount
                    if(amount > stay) { // too many item, just change amount
                        invItem.setAmount(amount - stay); // change amount to remove it
                        break; // stop loop
                    } else if(amount < stay) { // not enough item
                        invItem.setAmount(0); // you can also remove the item by setting air to this slot
                        item.setAmount(stay - amount); // reduce amount of item to delete
                    }
                }
            }
        }
        FastShop shop = new FastShop(p );
        p.closeInventory();
        p.openInventory(shop.getInventory());
    }

I'm currently getting a nullpointer error. I'm still need of help!

Upvotes: 1

Views: 3466

Answers (2)

UberSuperBoss
UberSuperBoss

Reputation: 77

This isn't exactly a full solution with code. However, you can do this.

  1. Create an integer for the amount of iron required.

  2. Create a for loop that iterates through a players inventory and if that slot contains iron then: if the amount in the slot is greater than the iron integer, remove the iron integer from that slot and give the item that is owed. else remove the amount in the slot from the iron integer and delete that slot.

I hope you can solve your issue from this.

Upvotes: 0

Elikill58
Elikill58

Reputation: 4908

This part of code will search an item similar to "Iron Ingot x4". If it's not similar, it doesn't change what is it because it's just different.

You should do like that :

ItemStack item = new ItemStack(getResource(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".type")), Integer.parseInt(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".cost")));
if(inv.contains(item)) { // contains the exact item
   inv.remove(item); // remove first time it find this item
} else { // doesn't contains this item
   for(ItemStack invItem : inv.getContents()) {
      if(invItem != null && invItem.getType().equals(item.getType()) { // if it's this type of item.
          // You can add other check specially for ItemMeta ...

          int amount = invItem.getAmount(); // amount of actual item
          int stay = item.getAmount(); // keep amount
          if(amount > stay) { // too many item, just change amount
             invItem.setAmount(amount - stay); // change amount to remove it
             break; // stop loop
          } else if(amount < stay) { // not enough item
             invItem.setAmount(0); // you can also remove the item by setting air to this slot
             item.setAmount(stay - amount); // reduce amount of item to delete
          }
      }
   }
}
player.updateInventory();

Upvotes: 1

Related Questions