Clean up of code

This commit is contained in:
modmuss50 2015-06-06 22:51:06 +01:00
parent 972efc4481
commit c6dc2e2810

View file

@ -9,95 +9,81 @@ import techreborn.util.ItemUtils;
public abstract class TechRebornContainer extends Container {
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex)
{
ItemStack originalStack = null;
Slot slot = (Slot) inventorySlots.get(slotIndex);
int numSlots = inventorySlots.size();
if (slot != null && slot.getHasStack())
{
ItemStack stackInSlot = slot.getStack();
originalStack = stackInSlot.copy();
if (slotIndex >= numSlots - 9 * 4
&& tryShiftItem(stackInSlot, numSlots))
{
// NOOP
} else if (slotIndex >= numSlots - 9 * 4
&& slotIndex < numSlots - 9)
{
if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots))
return null;
} 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;
slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.stackSize <= 0)
slot.putStack(null);
else
slot.onSlotChanged();
if (stackInSlot.stackSize == originalStack.stackSize)
return null;
slot.onPickupFromSlot(player, stackInSlot);
}
return originalStack;
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
ItemStack originalStack = null;
Slot slot = (Slot) inventorySlots.get(slotIndex);
int numSlots = inventorySlots.size();
if (slot != null && slot.getHasStack()) {
ItemStack stackInSlot = slot.getStack();
originalStack = stackInSlot.copy();
if (slotIndex >= numSlots - 9 * 4 && tryShiftItem(stackInSlot, numSlots)) {
// NOOP
} else if (slotIndex >= numSlots - 9 * 4 && slotIndex < numSlots - 9) {
if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots)) {
return null;
}
} 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;
}
slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.stackSize <= 0) {
slot.putStack(null);
} else {
slot.onSlotChanged();
}
if (stackInSlot.stackSize == originalStack.stackSize) {
return null;
}
slot.onPickupFromSlot(player, stackInSlot);
}
return originalStack;
}
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
&& ItemUtils.isItemEqual(stackInSlot, stackToShift,
true, true))
{
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;
}
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 && 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)
{
@ -115,4 +101,18 @@ public abstract class TechRebornContainer extends Container {
}
return false;
}
public static boolean canStacksMerge(ItemStack stack1, ItemStack stack2) {
if (stack1 == null || stack2 == null) {
return false;
}
if (!stack1.isItemEqual(stack2)) {
return false;
}
if (!ItemStack.areItemStackTagsEqual(stack1, stack2)) {
return false;
}
return true;
}
}