Little-C
在给予玩家物品前想检测玩家背包剩余空间是否可以的装得下,否则就取消本次操作

SumCraft
本帖最后由 SumCraft 于 2022-4-20 10:42 编辑

获取玩家背包,然后用下面的方法.
  1. int firstEmpty()
  2. 返回第一个空格子的格子数.
  3. 原文: Returns the first empty Slot.

  4. 返回:
  5. 第一个空格子的格子数,-1就没有空格子
复制代码


或者使用这个方法,这方法 会返回一个map,里面是没有成功添加玩家背包的物品
  1. java.util.HashMap<java.lang.Integer,ItemStack> addItem(ItemStack... items)
  2.                                                 throws java.lang.IllegalArgumentException
  3. 在物品栏存放所给的物品堆.本方法将尽可能完美地尝试填充已有的但还未达到堆叠上限的物品堆 和空格子.
  4. 返回的HashMap包含不能存放的物品堆,键是你所给的参数的索引值, 值是你所给的参数指定索引处的物品堆.如果所有物品都被存放,将返回一个空HashMap.

  5. (以下翻译不保证准确性,自己试试呗)如果你传递的物品堆超过了物品的最大堆叠量,首先它们将被按 最大堆叠量[Material.getMaxStackSize()]添加一部分.如果当没有部分的物品堆剩余时, 物品堆将被以Inventory.getMaxStackSize()的数量分割,允许你超过那个物品的最大堆叠量.

  6. 已知在一些实现里本方法也将会设置输入的argument amount to the number of that item not placed in slots.

  7. 原文:Stores the given ItemStacks in the inventory. This will try to fill existing stacks and empty slots as well as it can.

  8. The returned HashMap contains what it couldn't store, where the key is the index of the parameter, and the value is the ItemStack at that index of the varargs parameter. If all items are stored, it will return an empty HashMap.

  9. If you pass in ItemStacks which exceed the maximum stack size for the Material, first they will be added to partial stacks where Material.getMaxStackSize() is not exceeded, up to Material.getMaxStackSize(). When there are no partial stacks left stacks will be split on Inventory.getMaxStackSize() allowing you to exceed the maximum stack size for that material.

  10. It is known that in some implementations this method will also set the inputted argument amount to the number of that item not placed in slots.

  11. 参数:
  12. items - 要存放的物品
  13. 返回:
  14. 包含不能被存放的物品的HashMap
  15. 抛出:
  16. java.lang.IllegalArgumentException - 如果items或在内的任何元素为null
复制代码



ai你yong不老
你可以去反编译一下QuickShop插件的代码,我记得里面有个ShopManager类里面有个方法叫getEmptySpace(Inventory)的方法,这个方法是获取一个背包里面剩余的空间,包括数堆等。数得很细,可以去看一下

javv1
都是大佬

1952110084
感谢大佬的代码,很有用

syn614211648
Inventory.addItem(ItemStack..)方法返回值为一个<Integer,ItemStack>的Map,存储了不能添加进去的物品(背包满了),如果仅仅是想要检测能否添加进去某些物品可以: p.getInventory().clone().addItem(ItemStack[]).isEmpty() 来判断是否包能加进去一些物品 或者可以看一下forge源码,(玩家捡东西时候的背包判空方法)

Toame
遍历下玩家背包 通常情况下如果遍历到空气 会直接报空这时候可以尝试添加try语句 这时候往catch一段塞代码就可以实现效果

LaotouY
public int getinvsize(Player p) {
      int size = 0;
      ItemStack[] var3 = p.getInventory().getStorageContents();
      int var4 = var3.length;

      for(int var5 = 0; var5 < var4; ++var5) {
         ItemStack is = var3[var5];
         if (is == null) {
            ++size;
         }
      }

      return size;
   }

RMSCA
本帖最后由 RMSCA 于 2022-7-19 22:59 编辑
  1. Inventory inventory = player.getInventory();            
  2. if (inventory.firstEmpty() == -1) {
  3.         player.getWorld().dropItem(player.getLocation(), event.getItem());
  4. } else {
  5.         inventory.addItem(event.getItem());
  6. }
复制代码


从我的代码里截下来的,可能跟你需求不太一样,但是检测玩家物品栏状态的这段可以参考一下

第一页 上一页 下一页 最后一页