From 099e6f47e8f2327d38da9aafa416ec20009a63cb Mon Sep 17 00:00:00 2001 From: mezz Date: Fri, 16 Oct 2015 11:56:22 -0700 Subject: [PATCH] Fix #893 Shift-Removing items from a machine prefers the hotbar --- .../java/forestry/core/utils/SlotUtil.java | 96 ++++++++++++------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/src/main/java/forestry/core/utils/SlotUtil.java b/src/main/java/forestry/core/utils/SlotUtil.java index c3afcf452..b7360148e 100644 --- a/src/main/java/forestry/core/utils/SlotUtil.java +++ b/src/main/java/forestry/core/utils/SlotUtil.java @@ -104,11 +104,7 @@ public abstract class SlotUtil { return shiftToHotbar(inventorySlots, stackInSlot); } } else { - boolean shifted = shiftToHotbar(inventorySlots, stackInSlot); - if (!shifted) { - shifted = shiftToPlayerInventoryNoHotbar(inventorySlots, stackInSlot); - } - return shifted; + return shiftToPlayerInventory(inventorySlots, stackInSlot); } } @@ -158,38 +154,31 @@ public abstract class SlotUtil { } private static boolean shiftItemStackToRange(List inventorySlots, ItemStack stackToShift, int start, int count) { - boolean changed = false; - if (stackToShift.isStackable()) { - for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < start + count; slotIndex++) { - Slot slot = (Slot) inventorySlots.get(slotIndex); - ItemStack stackInSlot = slot.getStack(); - if (stackInSlot != null && ItemStackUtil.isIdenticalItem(stackInSlot, stackToShift)) { - int resultingStackSize = stackInSlot.stackSize + stackToShift.stackSize; - int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); - if (resultingStackSize <= max) { - stackToShift.stackSize = 0; - stackInSlot.stackSize = resultingStackSize; - slot.onSlotChanged(); - changed = true; - } else if (stackInSlot.stackSize < max) { - stackToShift.stackSize -= max - stackInSlot.stackSize; - stackInSlot.stackSize = max; - slot.onSlotChanged(); - changed = true; - } - } - } + boolean changed = shiftItemStackToRangeMerge(inventorySlots, stackToShift, start, count); + changed |= shiftItemStackToRangeOpenSlots(inventorySlots, stackToShift, start, count); + return changed; + } + + private static boolean shiftItemStackToRangeMerge(List inventorySlots, ItemStack stackToShift, int start, int count) { + if (stackToShift == null || !stackToShift.isStackable() || stackToShift.stackSize <= 0) { + return false; } - if (stackToShift.stackSize > 0) { - for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < start + count; slotIndex++) { - Slot slot = (Slot) inventorySlots.get(slotIndex); - ItemStack stackInSlot = slot.getStack(); - if (stackInSlot == null) { - int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); - stackInSlot = stackToShift.copy(); - stackInSlot.stackSize = Math.min(stackToShift.stackSize, max); - stackToShift.stackSize -= stackInSlot.stackSize; - slot.putStack(stackInSlot); + + boolean changed = false; + for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < start + count; slotIndex++) { + Slot slot = (Slot) inventorySlots.get(slotIndex); + ItemStack stackInSlot = slot.getStack(); + if (stackInSlot != null && ItemStackUtil.isIdenticalItem(stackInSlot, stackToShift)) { + int resultingStackSize = stackInSlot.stackSize + stackToShift.stackSize; + int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); + if (resultingStackSize <= max) { + stackToShift.stackSize = 0; + stackInSlot.stackSize = resultingStackSize; + slot.onSlotChanged(); + changed = true; + } else if (stackInSlot.stackSize < max) { + stackToShift.stackSize -= max - stackInSlot.stackSize; + stackInSlot.stackSize = max; slot.onSlotChanged(); changed = true; } @@ -198,6 +187,28 @@ public abstract class SlotUtil { return changed; } + private static boolean shiftItemStackToRangeOpenSlots(List inventorySlots, ItemStack stackToShift, int start, int count) { + if (stackToShift == null || stackToShift.stackSize <= 0) { + return false; + } + + boolean changed = false; + for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < start + count; slotIndex++) { + Slot slot = (Slot) inventorySlots.get(slotIndex); + ItemStack stackInSlot = slot.getStack(); + if (stackInSlot == null) { + int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit()); + stackInSlot = stackToShift.copy(); + stackInSlot.stackSize = Math.min(stackToShift.stackSize, max); + stackToShift.stackSize -= stackInSlot.stackSize; + slot.putStack(stackInSlot); + slot.onSlotChanged(); + changed = true; + } + } + return changed; + } + private static final int playerInventorySize = 9 * 4; private static final int playerHotbarSize = 9; @@ -209,6 +220,19 @@ public abstract class SlotUtil { return SlotUtil.isSlotInRange(slotIndex, playerInventorySize - playerHotbarSize, playerInventorySize); } + private static boolean shiftToPlayerInventory(List inventorySlots, ItemStack stackInSlot) { + int playerHotbarStart = playerInventorySize - playerHotbarSize; + + // try to merge with existing stacks, hotbar first + boolean shifted = shiftItemStackToRangeMerge(inventorySlots, stackInSlot, playerHotbarStart, playerHotbarSize); + shifted |= shiftItemStackToRangeMerge(inventorySlots, stackInSlot, 0, playerHotbarStart); + + // shift to open slots, hotbar first + shifted |= shiftItemStackToRangeOpenSlots(inventorySlots, stackInSlot, playerHotbarStart, playerHotbarSize); + shifted |= shiftItemStackToRangeOpenSlots(inventorySlots, stackInSlot, 0, playerHotbarStart); + return shifted; + } + private static boolean shiftToPlayerInventoryNoHotbar(List inventorySlots, ItemStack stackInSlot) { int playerHotbarStart = playerInventorySize - playerHotbarSize; return shiftItemStackToRange(inventorySlots, stackInSlot, 0, playerHotbarStart);