Merge pull request #219 from RinesThaix/moreStackingRules

AbstractInventory: more StackingRule#apply usages instead of ItemStack#withAmount
This commit is contained in:
TheMode 2021-04-04 18:19:17 +02:00 committed by GitHub
commit 9055ca2380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -79,7 +79,7 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
itemStack = stackingRule.apply(itemStack, totalAmount - stackingRule.getMaxSize()); itemStack = stackingRule.apply(itemStack, totalAmount - stackingRule.getMaxSize());
} else { } else {
// Slot can accept the whole item // Slot can accept the whole item
itemChangesMap.put(i, inventoryItem.withAmount(totalAmount)); itemChangesMap.put(i, stackingRule.apply(inventoryItem, totalAmount));
itemStack = ItemStack.AIR; itemStack = ItemStack.AIR;
break; break;
} }
@ -172,7 +172,6 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
*/ */
public boolean takeItemStack(@NotNull ItemStack itemStack) { public boolean takeItemStack(@NotNull ItemStack itemStack) {
Int2ObjectMap<ItemStack> itemChangesMap = new Int2ObjectOpenHashMap<>(); Int2ObjectMap<ItemStack> itemChangesMap = new Int2ObjectOpenHashMap<>();
final StackingRule stackingRule = itemStack.getStackingRule(); final StackingRule stackingRule = itemStack.getStackingRule();
for (int i = 0; i < getInnerSize(); i++) { for (int i = 0; i < getInnerSize(); i++) {
ItemStack inventoryItem = getItemStack(i); ItemStack inventoryItem = getItemStack(i);
@ -188,7 +187,7 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
break; break;
} }
itemChangesMap.put(i, ItemStack.AIR); itemChangesMap.put(i, ItemStack.AIR);
itemStack = itemStack.withAmount(amount -> amount - itemAmount); itemStack = stackingRule.apply(itemStack, itemStackAmount - itemAmount);
if (stackingRule.getAmount(itemStack) == 0) { if (stackingRule.getAmount(itemStack) == 0) {
itemStack = ItemStack.AIR; itemStack = ItemStack.AIR;
break; break;
@ -231,6 +230,7 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
*/ */
public boolean canTakeItemStack(@NotNull ItemStack itemStack) { public boolean canTakeItemStack(@NotNull ItemStack itemStack) {
final StackingRule stackingRule = itemStack.getStackingRule(); final StackingRule stackingRule = itemStack.getStackingRule();
int amountLeft = stackingRule.getAmount(itemStack);
for (int i = 0; i < getInnerSize(); i++) { for (int i = 0; i < getInnerSize(); i++) {
ItemStack inventoryItem = getItemStack(i); ItemStack inventoryItem = getItemStack(i);
if (inventoryItem.isAir()) { if (inventoryItem.isAir()) {
@ -238,11 +238,10 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
} }
if (stackingRule.canBeStacked(itemStack, inventoryItem)) { if (stackingRule.canBeStacked(itemStack, inventoryItem)) {
final int itemAmount = stackingRule.getAmount(inventoryItem); final int itemAmount = stackingRule.getAmount(inventoryItem);
final int itemStackAmount = stackingRule.getAmount(itemStack); if (amountLeft <= itemAmount) {
if (itemStackAmount <= itemAmount) {
return true; return true;
} }
itemStack = itemStack.withAmount(amount -> amount - itemAmount); amountLeft -= itemAmount;
} }
} }
return false; return false;