如何知道一个项目是否可以堆叠在库存中相同类型的其他项目上
How to know if an item can be stacked on other items of the same type on an inventory
所以我有一个播放器和一个商店插件。
商店试图给玩家 16 羊毛。
如果玩家有免费的 1 到 63 羊毛(Ofc 有另一个 inv space 免费)我的方法应该 return 正确。但是,如果玩家只有 63 羊毛而他的其他 space 装满了其他材料,方法应该 return 为假。我的问题是,当玩家在 inv.getContents() 中有 1、1、1 堆羊毛物品时,我的方法将无法正确计算。
这是我的代码:
public static boolean canStackItemToInventory(Player player, ItemStack item) {
Material type = item.getType();
int amount = item.getAmount();
if (type.equals(Material.BOW)) return false;
PlayerInventory inventory = player.getInventory();
int itemsNumber = 0;
for (ItemStack itemStack : inventory.getContents()) {
if (itemStack.getType()!=type) continue;
if (itemStack.getAmount()==itemStack.getMaxStackSize()) continue;
itemsNumber+=itemStack.getAmount();
}
itemsNumber+=amount;
return itemsNumber <= type.getMaxStackSize();
}
我写的这个函数应该 return true 或 false 取决于玩家是否可以持有 material type
的 amount
。我没有测试过,但它应该可以工作
public boolean hasFreeSpace(Material type, int amount, Player player) {
PlayerInventory inv = player.getInventory();
//check if the player has any free slots
if(inv.firstEmpty() == -1) {
return false;
}
//we will decrement this until we need no more space
int neededSpace = amount;
//look through each itemstack
for(ItemStack stack : inv.getContents()) {
//check if that type of item is the same
if(stack.getType() != type)
continue;
//how many more items until the stack is full
int untilStackIsFull = stack.getMaxStackSize() - stack.getAmount();
//if the stack is full we will basically be subtracting zero, otherwise we subtract off how much it can hold
neededSpace -= untilStackIsFull;
//if we have all the space needed return true
if(neededSpace <= 0)
return true;
}
return false;
}
所以我有一个播放器和一个商店插件。
商店试图给玩家 16 羊毛。
如果玩家有免费的 1 到 63 羊毛(Ofc 有另一个 inv space 免费)我的方法应该 return 正确。但是,如果玩家只有 63 羊毛而他的其他 space 装满了其他材料,方法应该 return 为假。我的问题是,当玩家在 inv.getContents() 中有 1、1、1 堆羊毛物品时,我的方法将无法正确计算。
这是我的代码:
public static boolean canStackItemToInventory(Player player, ItemStack item) {
Material type = item.getType();
int amount = item.getAmount();
if (type.equals(Material.BOW)) return false;
PlayerInventory inventory = player.getInventory();
int itemsNumber = 0;
for (ItemStack itemStack : inventory.getContents()) {
if (itemStack.getType()!=type) continue;
if (itemStack.getAmount()==itemStack.getMaxStackSize()) continue;
itemsNumber+=itemStack.getAmount();
}
itemsNumber+=amount;
return itemsNumber <= type.getMaxStackSize();
}
我写的这个函数应该 return true 或 false 取决于玩家是否可以持有 material type
的 amount
。我没有测试过,但它应该可以工作
public boolean hasFreeSpace(Material type, int amount, Player player) {
PlayerInventory inv = player.getInventory();
//check if the player has any free slots
if(inv.firstEmpty() == -1) {
return false;
}
//we will decrement this until we need no more space
int neededSpace = amount;
//look through each itemstack
for(ItemStack stack : inv.getContents()) {
//check if that type of item is the same
if(stack.getType() != type)
continue;
//how many more items until the stack is full
int untilStackIsFull = stack.getMaxStackSize() - stack.getAmount();
//if the stack is full we will basically be subtracting zero, otherwise we subtract off how much it can hold
neededSpace -= untilStackIsFull;
//if we have all the space needed return true
if(neededSpace <= 0)
return true;
}
return false;
}