本帖最后由 Mucrazy 于 2019-10-28 09:14 编辑
答案:(注3楼不是e.getAction()而是e.getHand() )ItemStack item = p.getItemInHand();
if (x == Action.PHYSICAL || e.getItem == null || is.getType() == Material.AIR || x == Action.LEFT_CLICK_BLOCK
|| x == Action.LEFT_CLICK_AIR || item == null || item.getType() == Material.AIR)
{
return;
}
int amount = item.getAmount();
if (e.getHand() == EquipmentSlot.HAND) {
if (amount - 1 <= 0) {
p.setItemInHand(null);
} else {
item.setAmount(amount - 1);
}
}
答案:(注3楼不是e.getAction()而是e.getHand() )ItemStack item = p.getItemInHand();
if (x == Action.PHYSICAL || e.getItem == null || is.getType() == Material.AIR || x == Action.LEFT_CLICK_BLOCK
|| x == Action.LEFT_CLICK_AIR || item == null || item.getType() == Material.AIR)
{
return;
}
int amount = item.getAmount();
if (e.getHand() == EquipmentSlot.HAND) {
if (amount - 1 <= 0) {
p.setItemInHand(null);
} else {
item.setAmount(amount - 1);
}
}
ItemStack.setAmount(int);
举个例子,将玩家手上物品数量减少1,如果数量为1则设为空:
复制代码
举个例子,将玩家手上物品数量减少1,如果数量为1则设为空:
- ItemStack item = player.getItemInHand(); // 获取手中物品
- if (item == null || item.getType() == Material.AIR) return; // 为空就返回
- int amount = item.getAmount(); // 获取当前数量
- if (amount - 1 <= 0) // 如果数量 - 1 小于等于 0
- player.setItemInHand(null); // 那么设置为空
- else
- item.setAmount(amount - 1); // 否则数量减去1个
lgou2w 发表于 2019-10-27 23:25
ItemStack.setAmount(int);
举个例子,将玩家手上物品数量减少1,如果数量为1则设为空:
...
补充
假设为1.8版本或以上有主副手版本
使用PlayerInteractEvent时记得判断event.getAction()==EquipmentSlot.Hand后再进行扣除物品
因为有了主副手PlayerInteractEvent会触发两次