Adjusted Shift code to support Phantom slots

Yes, this also was stolen from Railcraft. Its also much more robust than
the code BC was using.
This commit is contained in:
CovertJaguar 2013-05-27 18:08:17 -07:00
parent 6d6e05044c
commit 0a18ae34ff
4 changed files with 86 additions and 89 deletions

View file

@ -87,7 +87,7 @@ tile.refineryBlock=Refinery
tile.libraryBlock=Blueprint Library tile.libraryBlock=Blueprint Library
tile.laserBlock=Laser tile.laserBlock=Laser
tile.assemblyTableBlock=Assembly Table tile.assemblyTableBlock=Assembly Table
tile.assemblyWorkbenchBlock=Assembly Workbench tile.assemblyWorkbenchBlock=Advanced Crafting Table
tile.pathMarkerBlock=Path Marker tile.pathMarkerBlock=Path Marker
tile.blockHopper=Chute tile.blockHopper=Chute
tile.pumpBlock=Pump tile.pumpBlock=Pump

View file

@ -8,6 +8,7 @@
package buildcraft.core.gui; package buildcraft.core.gui;
import buildcraft.core.gui.slots.IPhantomSlot; import buildcraft.core.gui.slots.IPhantomSlot;
import buildcraft.core.gui.slots.SlotBase;
import buildcraft.core.inventory.StackMergeHelper; import buildcraft.core.inventory.StackMergeHelper;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
@ -17,7 +18,7 @@ import net.minecraft.item.ItemStack;
public abstract class BuildCraftContainer extends Container { public abstract class BuildCraftContainer extends Container {
private final StackMergeHelper STACK_MERGE = new StackMergeHelper(); private final StackMergeHelper MERGE_HELPER = new StackMergeHelper();
private int inventorySize; private int inventorySize;
public BuildCraftContainer(int inventorySize) { public BuildCraftContainer(int inventorySize) {
@ -58,7 +59,7 @@ public abstract class BuildCraftContainer extends Container {
adjustPhantomSlot(slot, mouseButton, modifier); adjustPhantomSlot(slot, mouseButton, modifier);
slot.onPickupFromSlot(player, playerInv.getItemStack()); slot.onPickupFromSlot(player, playerInv.getItemStack());
} else if (slot.isItemValid(stackHeld)) { } else if (slot.isItemValid(stackHeld)) {
if (STACK_MERGE.canStacksMerge(stackSlot, stackHeld)) { if (MERGE_HELPER.canStacksMerge(stackSlot, stackHeld)) {
adjustPhantomSlot(slot, mouseButton, modifier); adjustPhantomSlot(slot, mouseButton, modifier);
} else { } else {
fillPhantomSlot(slot, stackHeld, mouseButton, modifier); fillPhantomSlot(slot, stackHeld, mouseButton, modifier);
@ -105,27 +106,99 @@ public abstract class BuildCraftContainer extends Container {
slot.putStack(phantomStack); slot.putStack(phantomStack);
} }
protected boolean shiftItemStack(ItemStack stackToShift, int start, int end) {
boolean changed = false;
if (stackToShift.isStackable()) {
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) {
Slot slot = (Slot) inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot != null && MERGE_HELPER.canStacksMerge(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;
}
}
}
}
if (stackToShift.stackSize > 0) {
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; 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 boolean tryShiftItem(ItemStack stackToShift, int numSlots) {
for (int machineIndex = 0; machineIndex < numSlots - 9 * 4; machineIndex++) {
Slot slot = (Slot) inventorySlots.get(machineIndex);
if (slot instanceof SlotBase && !((SlotBase) slot).canShift()) {
continue;
}
if (slot instanceof IPhantomSlot) {
continue;
}
if (!slot.isItemValid(stackToShift)) {
continue;
}
if (shiftItemStack(stackToShift, machineIndex, machineIndex + 1)) {
return true;
}
}
return false;
}
@Override @Override
public ItemStack transferStackInSlot(EntityPlayer pl, int i) { public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
ItemStack itemstack = null; ItemStack originalStack = null;
Slot slot = (Slot) inventorySlots.get(i); Slot slot = (Slot) inventorySlots.get(slotIndex);
int numSlots = inventorySlots.size();
if (slot != null && slot.getHasStack()) { if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack(); ItemStack stackInSlot = slot.getStack();
itemstack = itemstack1.copy(); originalStack = stackInSlot.copy();
if (i < inventorySize) { if (slotIndex >= numSlots - 9 * 4 && tryShiftItem(stackInSlot, numSlots)) {
if (!mergeItemStack(itemstack1, inventorySize, inventorySlots.size(), true)) { // NOOP
} else if (slotIndex >= numSlots - 9 * 4 && slotIndex < numSlots - 9) {
if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots)) {
return null; return null;
} }
} else if (!mergeItemStack(itemstack1, 0, inventorySize, false)) { } else if (slotIndex >= numSlots - 9 && slotIndex < numSlots) {
if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots - 9)) {
return null;
}
} else if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots)) {
return null; return null;
} }
if (itemstack1.stackSize == 0) { slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.stackSize <= 0) {
slot.putStack(null); slot.putStack(null);
} else { } else {
slot.onSlotChanged(); slot.onSlotChanged();
} }
if (stackInSlot.stackSize == originalStack.stackSize) {
return null;
}
slot.onPickupFromSlot(player, stackInSlot);
} }
return itemstack; return originalStack;
} }
public int getInventorySize() { public int getInventorySize() {

View file

@ -1,38 +0,0 @@
package buildcraft.core.inventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
/**
* Manages input on ISidedInventory
*/
public class TransactorForgeSided extends TransactorSimple {
ISidedInventory sided;
public TransactorForgeSided(ISidedInventory inventory) {
super(inventory);
this.sided = inventory;
}
@Override
protected int getPartialSlot(ItemStack stack, ForgeDirection orientation, int skipAhead) {
// If skipAhead is below the minimum required, we skip ahead to the first valid slot.
if (skipAhead < sided.getStartInventorySide(orientation)) {
skipAhead = sided.getStartInventorySide(orientation);
}
if (skipAhead > sided.getStartInventorySide(orientation) + sided.getSizeInventorySide(orientation))
return -1;
return getPartialSlot(stack, skipAhead, sided.getStartInventorySide(orientation) + sided.getSizeInventorySide(orientation));
}
@Override
protected int getEmptySlot(ItemStack stack, ForgeDirection orientation, int slotIndex) {
return getEmptySlot(stack, sided.getStartInventorySide(orientation), sided.getStartInventorySide(orientation) + sided.getSizeInventorySide(orientation));
}
}

View file

@ -1,38 +0,0 @@
package buildcraft.core.inventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
public class TransactorVanillaSided extends TransactorSimple {
private ISidedInventory sided;
public TransactorVanillaSided(ISidedInventory inventory)
{
super(inventory);
this.sided = inventory;
}
@Override
protected int getPartialSlot(ItemStack stack, ForgeDirection orientation, int slotIndex)
{
return getSlotOnSideForStack(stack, orientation, slotIndex);
}
@Override
protected int getEmptySlot(ItemStack stack, ForgeDirection orientation, int slotIndex)
{
return getSlotOnSideForStack(stack, orientation, slotIndex);
}
private int getSlotOnSideForStack(ItemStack stack, ForgeDirection orientation, int slotIndex)
{
int[] sideSlots = sided.getAccessibleSlotsFromSide(orientation.ordinal());
if (slotIndex >= sideSlots.length)
return -1;
int targetSlot = sideSlots[slotIndex];
return sided.isStackValidForSlot(targetSlot, stack) && sided.canInsertItem(targetSlot, stack, orientation.ordinal()) ? targetSlot : -1;
}
}