// Iterate over the player's inventory
for (ItemStack itemStack : player.getInventory()) {
// Check that the ItemStack isn't null (empty/air slots) or if it's not a slot containing diamonds
if(itemStack == null || !itemStack.getType().equals(Material.DIAMOND)) {
// if so, skip running further logic on this stack
continue;
}
// Remove one from the amount, the ItemStack will be removed if it has 0 amount
itemStack.setAmount(itemStack.getAmount() - 1);
// Break out of the parent for loop to avoid removing more than we want
break;
}
This will only match a slot if both the type and the amount of the stack match
So this will only remove one diamond if the player has a slot containing only one diamond; it will not attempt to remove if they had two, for example.
Therefore, we need to do some leg work:
// Iterate over the player's inventory
for (ItemStack itemStack : player.getInventory()) {
// Check that the ItemStack isn't null (empty/air slots) or if it's not a slot containing diamonds
if(itemStack == null || !itemStack.getType().equals(Material.DIAMOND)) {
// if so, skip running further logic on this stack
continue;
}
// Remove one from the amount, the ItemStack will be removed if it has 0 amount
itemStack.setAmount(itemStack.getAmount() - 1);
// Break out of the parent for loop to avoid removing more than we want
break;
}
You could extract this logic into a method with a signature like reduceItemStackByAmount(Player player, Material material, int amount); however, you will need to add extra logic for when the amount is greater than a stack (64) and account for the stack size being smaller than the amount you're trying to remove. I'll leave this implementation up to you as a learning exercise.
发布评论
评论(1)
您的内部静态方法
remove intemfromineventory
在那里无法使用;您不能将方法直接嵌套在Java中。但是,无论如何不是必需的。查看,在实体的
库存
上有一个删除
方法,因此我们可以删除这样的项目:但是,如文档中所述:
因此,只有在玩家只有一个钻石的插槽时,才能去除一颗钻石;例如,如果他们有两个,它将不会尝试删除。
因此,我们需要做一些腿部工作:
您可以将此逻辑提取到具有签名的方法中,例如
delectItemstackbyamount(玩家播放器,材料,材料,int量)
;但是,当金额
大于堆栈(64)时,您将需要添加额外的逻辑,并说明堆栈尺寸小于您要删除的数量。作为学习练习,我将把这个实施留给您。PS For future StackOverflow questions, take the tour and read
Your inner static method
removeItemFromInventory
won't work there; you can't directly nest methods in Java. However, it's not necessary anyway.Looking at the Spigot documentation, there's a
remove
method on the entity'sInventory
, so we could remove an item like so:However, as noted in the documentation:
So this will only remove one diamond if the player has a slot containing only one diamond; it will not attempt to remove if they had two, for example.
Therefore, we need to do some leg work:
You could extract this logic into a method with a signature like
reduceItemStackByAmount(Player player, Material material, int amount)
; however, you will need to add extra logic for when theamount
is greater than a stack (64) and account for the stack size being smaller than the amount you're trying to remove. I'll leave this implementation up to you as a learning exercise.P.S. For future StackOverflow questions, take the tour and read How do I ask a good question? Also why you shouldn't post images of code.