Bukkit从库存中移除物品 [英] Bukkit remove item from inventory

查看:23
本文介绍了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从库存中移除物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆