Bukkit remove item from inventory(Bukkit从库存中移除物品)
本文介绍了Bukkit从库存中移除物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查玩家的库存中是否有物品,如果有,则删除其中一个。这是我现在拥有的:
Material ammomat = parseMaterial(plugin.getConfig().getString("game.ammo_material"));
ItemStack ammo = new ItemStack(ammomat, 1);
if(p.getInventory().contains(ammomat, 1)){
p.getInventory().removeItem(ammo);
p.updateInventory();
}
它获取他们是否拥有该项目,但不会删除一个项目。
如何从玩家的库存中删除一件物品?
推荐答案
如果您只想删除一件物品,您可以遍历玩家清单中的物品,然后检查材料是否与您想要的匹配。如果是,您可以从ItemStack中删除一项
它可能如下所示:
for(int i = 0; i < p.getInventory().getSize(); i++){
//get the ItemStack at slot i
ItemStack itm = p.getInventory().getItem(i);
//make sure the item is not null, and check if it's material is "mat"
if(itm != null && itm.getType().equals(mat){
//get the new amount of the item
int amt = itm.getAmount() - 1;
//set the amount of the item to "amt"
itm.setAmount(amt);
//set the item in the player's inventory at slot i to "itm" if the amount
//is > 0, and to null if it is <= 0
p.getInventory().setItem(i, amt > 0 ? itm : null);
//update the player's inventory
p.updateInventory();
//we're done, break out of the for loop
break;
}
}
因此,您的代码可能如下所示:
Material ammomat = parseMaterial(plugin.getConfig().getString("game.ammo_material"));
for(int i = 0; i < p.getInventory().getSize(); i++){
ItemStack itm = p.getInventory().getItem(i);
if(itm != null && itm.getType().equals(ammomat){
int amt = itm.getAmount() - 1;
itm.setAmount(amt);
p.getInventory().setItem(i, amt > 0 ? itm : null);
p.updateInventory();
break;
}
}
这篇关于Bukkit从库存中移除物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Bukkit从库存中移除物品


基础教程推荐
猜你喜欢
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01