提问者:小点点

Minecraft Spigot从库存中删除物品


8_R3,我目前正在商店系统上工作,当你点击一个物品购买时,4个铁锭应该从玩家的库存中移除。

inventory.remove(new ItemStack(getResource(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".type")), Integer.parseInt(Main.plugin.getConfigValue("shop." + e.getCurrentItem().getTypeId() + ".cost"))));

但是,此代码仅在正好有4个铁锭时才移除铁。我如何使它从不同的数量中移除4个铁锭,例如5个?

编辑:我尝试使用这段代码:

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

我目前收到一个空指针错误。我仍然需要帮助!


共2个答案

匿名用户

这部分代码会搜索一个类似“铁锭x4”的项目。如果它不相似,它不会改变它是什么,因为它只是不同。

你应该这样做:

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

匿名用户

这并不是一个完整的代码解决方案。但是,您可以这样做。

>

  • 为所需的铁量创建一个整数。

    创建一个for循环,循环遍历玩家库存,如果该插槽包含铁,那么:如果插槽中的数量大于铁整数,从该插槽中移除铁整数并给出欠款的项目。否则从铁整数中移除插槽中的数量并删除该插槽。

    我希望你能从中解决你的问题。