package com.yolp900.itsjustacharm.common.inventory;
import com.yolp900.itsjustacharm.common.avchievemetns.ICraftAchievement;
import com.yolp900.itsjustacharm.common.crafting.CraftingHandlerConstructionTable;
import com.yolp900.itsjustacharm.common.tileEntities.TileEntityConstructionTable;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import javax.annotation.Nullable;
public class ContainerConstructionTable extends ModContainer {
private TileEntityConstructionTable tile;
public ContainerConstructionTable(InventoryPlayer inventory, TileEntityConstructionTable tile) {
this.tile = tile;
registerSlots(inventory, tile);
}
private void registerSlots(InventoryPlayer inventory, TileEntityConstructionTable tile) {
this.addSlotToContainer(new SlotOutput(tile, 0, 202, 71));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
this.addSlotToContainer(new SlotInput(tile, 1 + j + i * 3, 55 + 18 * j, 53 + 18 * i));
}
}
this.addSlotToContainer(new SlotInput(tile, 10, 73, 19));
this.addSlotToContainer(new SlotInput(tile, 11, 109, 35));
this.addSlotToContainer(new SlotInput(tile, 12, 125, 71));
this.addSlotToContainer(new SlotInput(tile, 13, 109, 107));
this.addSlotToContainer(new SlotInput(tile, 14, 73, 123));
this.addSlotToContainer(new SlotInput(tile, 15, 37, 107));
this.addSlotToContainer(new SlotInput(tile, 16, 21, 71));
this.addSlotToContainer(new SlotInput(tile, 17, 37, 35));
registerInventorySlots(inventory);
}
private void registerInventorySlots(InventoryPlayer inventory) {
for (int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(inventory, j, 44 + j * 18, 231));
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 44 + j * 18, 162 + i * 18));
}
}
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return tile.isUseableByPlayer(player);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int clickedSlotNumber) {
ItemStack itemPlaced = null;
Slot clickedSlot = inventorySlots.get(clickedSlotNumber);
if (clickedSlot != null && clickedSlot.getHasStack()) {
ItemStack itemClicked = clickedSlot.getStack();
if (itemClicked != null) {
itemPlaced = itemClicked.copy();
if (clickedSlotNumber == 0) { // From the output slot
if (!this.mergeItemStackOutput(itemClicked)) { // To the player's inventory, hotbar priority
return null;
}
clickedSlot.onSlotChange(itemClicked, itemPlaced);
} else if (clickedSlotNumber > 0 && clickedSlotNumber < 18) { // From the input slots
if (!this.mergeItemStack(itemClicked, 18, 54, true)) { // To the player's inventory
return null;
}
} else if (clickedSlotNumber > 17 && clickedSlotNumber < 54) { // From the player's inventory
if (clickedSlotNumber >= 18 && clickedSlotNumber < 45) { // From the inventory
if (!this.mergeItemStack(itemClicked, 45, 54, false)) { // To the hotbar
return null;
}
} else if (clickedSlotNumber >= 45 && clickedSlotNumber < 54) { // From the hotbar
if (!this.mergeItemStack(itemClicked, 18, 45, false)) { // To the inventory
return null;
}
}
}
if (itemClicked.stackSize == 0) {
clickedSlot.putStack(null); // In case that while merging we left behind an empty itemstack
} else {
clickedSlot.onSlotChanged(); // In case that while merging we left behind an nonempty itemstack
}
if (itemClicked.stackSize == itemPlaced.stackSize) {
return null; // So if while merging we left behind an itemstack but somehow it did not change in size at all we make sure to return null
}
clickedSlot.onPickupFromSlot(player, itemClicked);
}
}
return itemPlaced;
}
private boolean mergeItemStackOutput(ItemStack stack) {
CraftingHandlerConstructionTable.RecipeType recipeType = CraftingHandlerConstructionTable.getCurrentRecipeType(tile.getGridInputs(), tile.getSecInputs(), tile.getWorld());
if (recipeType != CraftingHandlerConstructionTable.RecipeType.None) {
boolean isThereSpace = false;
int maxAmountOfSpace = 0;
ItemStack copyOfItemStack = ItemStack.copyItemStack(stack);
for (int i = 18; i < 54; i++) {
Slot slot = inventorySlots.get(i);
if (!slot.getHasStack() || slot.getStack() == null) {
isThereSpace = true;
maxAmountOfSpace += 64;
} else if (ItemStack.areItemsEqual(slot.getStack(), stack) && (slot.getStack().stackSize < 64)) {
isThereSpace = true;
int spaceInSlot = 64 - slot.getStack().stackSize;
while (spaceInSlot >= stack.stackSize) {
maxAmountOfSpace += stack.stackSize;
spaceInSlot -= stack.stackSize;
}
}
}
if (isThereSpace) {
int outputSize = maxAmountOfSpace > tile.getMaxStackSizeShiftClickOutput() ? tile.getMaxStackSizeShiftClickOutput() : maxAmountOfSpace;
tile.decrStackSize(0, outputSize);
while (true) {
if (outputSize > 64) {
copyOfItemStack.stackSize = 64;
outputSize -= 64;
super.mergeItemStack(copyOfItemStack.copy(), 18, 54, true);
} else {
copyOfItemStack.stackSize = outputSize;
return super.mergeItemStack(copyOfItemStack.copy(), 18, 54, true);
}
}
}
}
return false;
}
@Override
public ItemStack slotClick(int slot, int dragType, ClickType clickType, EntityPlayer player) {
if (slot == 0) {
ItemStack stack = inventorySlots.get(0).getStack();
if (stack != null) {
Item item = stack.getItem();
if (item instanceof ICraftAchievement) {
player.addStat(((ICraftAchievement) item).getAchievementOnCraft(stack, player, tile));
} else if (item instanceof ItemBlock && Block.getBlockFromItem(item) instanceof ICraftAchievement) {
player.addStat(((ICraftAchievement) Block.getBlockFromItem(item)).getAchievementOnCraft(stack, player, tile));
}
}
if (dragType > 0) dragType = 0;
}
return super.slotClick(slot, dragType, clickType, player);
}
private static class SlotOutput extends Slot {
public SlotOutput(TileEntityConstructionTable inventory, int index, int x, int y) {
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(@Nullable ItemStack stack) {
return false;
}
}
private static class SlotInput extends Slot {
public SlotInput(TileEntityConstructionTable inventory, int index, int x, int y) {
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(@Nullable ItemStack stack) {
return true;
}
}
}