package com.yolp900.itsjustacharm.common.inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public abstract class ModContainer extends Container {
@Override
public abstract ItemStack transferStackInSlot(EntityPlayer player, int clickedSlotNumber);
@Override
protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
boolean ret = mergeItemStackRefill(stack, startIndex, endIndex, useEndIndex);
if (stack != null && stack.stackSize > 0) {
ret |= mergeItemStackMove(stack, startIndex, endIndex, useEndIndex);
}
return ret;
}
private boolean mergeItemStackRefill(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
if (stack.stackSize <= 0) {
return false;
}
boolean flag1 = false;
int k = startIndex;
if (useEndIndex) {
k = endIndex - 1;
}
Slot slot;
ItemStack itemstack1;
if (stack.isStackable()) {
while (stack.stackSize > 0 && (!useEndIndex && k < endIndex || useEndIndex && k >= startIndex)) {
slot = this.inventorySlots.get(k);
itemstack1 = slot.getStack();
if (itemstack1 != null
&& itemstack1.getItem() == stack.getItem()
&& (!stack.getHasSubtypes() || stack.getMetadata() == itemstack1.getMetadata())
&& ItemStack.areItemStackTagsEqual(stack, itemstack1)
&& this.canMergeSlot(stack, slot)) {
int l = itemstack1.stackSize + stack.stackSize;
int limit = Math.min(stack.getMaxStackSize(), slot.getItemStackLimit(stack));
if (l <= limit) {
stack.stackSize = 0;
itemstack1.stackSize = l;
slot.onSlotChanged();
flag1 = true;
} else if (itemstack1.stackSize < limit) {
stack.stackSize -= limit - itemstack1.stackSize;
itemstack1.stackSize = limit;
slot.onSlotChanged();
flag1 = true;
}
}
if (useEndIndex) {
--k;
} else {
++k;
}
}
}
return flag1;
}
private boolean mergeItemStackMove(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
if (stack.stackSize <= 0) {
return false;
}
boolean flag1 = false;
int k;
if (useEndIndex) {
k = endIndex - 1;
} else {
k = startIndex;
}
while (!useEndIndex && k < endIndex || useEndIndex && k >= startIndex) {
Slot slot = this.inventorySlots.get(k);
ItemStack itemstack1 = slot.getStack();
if (itemstack1 == null && slot.isItemValid(stack) && this.canMergeSlot(stack, slot)) // Forge: Make sure to respect isItemValid in the slot.
{
int limit = slot.getItemStackLimit(stack);
ItemStack stack2 = stack.copy();
if (stack2.stackSize > limit) {
stack2.stackSize = limit;
stack.stackSize -= limit;
} else {
stack.stackSize = 0;
}
slot.putStack(stack2);
slot.onSlotChanged();
flag1 = true;
if (stack.stackSize == 0) {
break;
}
}
if (useEndIndex) {
--k;
} else {
++k;
}
}
return flag1;
}
@Override
public abstract boolean canInteractWith(EntityPlayer player);
}