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!
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();