Files
TinkersConstruct/src/main/java/tconstruct/inventory/CraftingStationContainer.java
T

219 lines
7.1 KiB
Java
Raw Normal View History

2013-08-07 19:06:26 -07:00
package tconstruct.inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
2013-12-12 03:08:05 -08:00
import net.minecraft.item.Item;
2013-08-07 19:06:26 -07:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.world.World;
import tconstruct.blocks.logic.CraftingStationLogic;
2013-12-12 21:05:26 -08:00
import tconstruct.library.armor.ArmorCore;
import tconstruct.library.crafting.ToolBuilder;
import tconstruct.library.tools.ToolCore;
2013-08-07 19:06:26 -07:00
public class CraftingStationContainer extends Container
{
/** The crafting matrix inventory (3x3). */
public InventoryCrafting craftMatrix;// = new InventoryCrafting(this, 3, 3);
public IInventory craftResult;// = new InventoryCraftResult();
private CraftingStationLogic logic;
2014-02-05 08:28:53 -05:00
private World worldObj;
2013-08-07 19:06:26 -07:00
private int posX;
private int posY;
private int posZ;
public CraftingStationContainer(InventoryPlayer inventorplayer, CraftingStationLogic logic, int x, int y, int z)
{
2014-02-05 08:28:53 -05:00
this.worldObj = logic.getWorld();
2013-08-07 19:06:26 -07:00
this.posX = x;
this.posY = y;
this.posZ = z;
this.logic = logic;
2013-08-07 19:06:26 -07:00
craftMatrix = new InventoryCraftingStation(this, 3, 3, logic);
craftResult = new InventoryCraftingStationResult(logic);
2013-08-31 05:13:31 -07:00
2013-08-07 19:06:26 -07:00
this.addSlotToContainer(new SlotCrafting(inventorplayer.player, this.craftMatrix, this.craftResult, 0, 124, 35));
2013-08-31 05:13:31 -07:00
2013-08-07 19:06:26 -07:00
int row;
int column;
for (row = 0; row < 3; ++row)
{
for (column = 0; column < 3; ++column)
{
this.addSlotToContainer(new Slot(this.craftMatrix, column + row * 3, 30 + column * 18, 17 + row * 18));
}
}
//Player Inventory
for (row = 0; row < 3; ++row)
{
for (column = 0; column < 9; ++column)
{
this.addSlotToContainer(new Slot(inventorplayer, column + row * 9 + 9, 8 + column * 18, 84 + row * 18));
}
}
for (column = 0; column < 9; ++column)
{
this.addSlotToContainer(new Slot(inventorplayer, column, 8 + column * 18, 142));
}
2013-12-06 11:37:06 -08:00
//Side inventory
if (logic.chest != null)
{
IInventory chest = logic.chest.get();
IInventory doubleChest = logic.doubleChest == null ? null : logic.doubleChest.get();
int count = 0;
for (column = 0; column < 9; column++)
{
for (row = 0; row < 6; row++)
{
int value = count < 27 ? count : count - 27;
this.addSlotToContainer(new Slot(count < 27 ? chest : doubleChest, value, -108 + row * 18, 19 + column * 18));
count++;
if (count >= 27 && doubleChest == null)
break;
}
if (count >= 27 && doubleChest == null)
break;
}
}
2013-08-07 19:06:26 -07:00
this.onCraftMatrixChanged(this.craftMatrix);
}
2013-08-31 05:13:31 -07:00
public void onCraftMatrixChanged (IInventory par1IInventory)
2013-08-07 19:06:26 -07:00
{
ItemStack tool = modifyTool();
if (tool != null)
this.craftResult.setInventorySlotContents(0, tool);
else
2014-02-05 08:28:53 -05:00
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
}
public ItemStack modifyTool ()
{
2013-12-12 03:08:05 -08:00
ItemStack input = craftMatrix.getStackInSlot(4);
if (input != null)
{
2013-12-12 03:08:05 -08:00
Item item = input.getItem();
if (item instanceof ToolCore)
{
ItemStack[] slots = new ItemStack[8];
2013-12-12 03:08:05 -08:00
for (int i = 0; i < 4; i++)
{
2013-12-12 03:08:05 -08:00
slots[i] = craftMatrix.getStackInSlot(i);
slots[i + 4] = craftMatrix.getStackInSlot(i + 5);
}
ItemStack output = ToolBuilder.instance.modifyTool(input, slots, "");
if (output != null)
return output;
}
2013-12-12 21:05:26 -08:00
else if (item instanceof ArmorCore)
{
ItemStack[] slots = new ItemStack[8];
for (int i = 0; i < 4; i++)
{
slots[i] = craftMatrix.getStackInSlot(i);
slots[i + 4] = craftMatrix.getStackInSlot(i + 5);
}
ItemStack output = ToolBuilder.instance.modifyArmor(input, slots, "");
if (output != null)
return output;
}
}
return null;
2013-08-07 19:06:26 -07:00
}
2013-08-31 05:13:31 -07:00
public void onContainerClosed (EntityPlayer par1EntityPlayer)
2013-08-07 19:06:26 -07:00
{
super.onContainerClosed(par1EntityPlayer);
2014-02-05 08:28:53 -05:00
if (!this.worldObj.isRemote)
2013-08-07 19:06:26 -07:00
{
for (int i = 0; i < 9; ++i)
{
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
if (itemstack != null)
{
2014-02-02 12:06:48 -05:00
par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack,false);
2013-08-07 19:06:26 -07:00
}
}
}
}
2013-08-31 05:13:31 -07:00
public boolean canInteractWith (EntityPlayer par1EntityPlayer)
2013-08-07 19:06:26 -07:00
{
2013-08-31 05:13:31 -07:00
return par1EntityPlayer.getDistanceSq((double) this.posX + 0.5D, (double) this.posY + 0.5D, (double) this.posZ + 0.5D) <= 64.0D;
2013-08-07 19:06:26 -07:00
}
2013-08-31 05:13:31 -07:00
public ItemStack transferStackInSlot (EntityPlayer par1EntityPlayer, int par2)
2013-08-07 19:06:26 -07:00
{
ItemStack itemstack = null;
2013-08-31 05:13:31 -07:00
Slot slot = (Slot) this.inventorySlots.get(par2);
2013-08-07 19:06:26 -07:00
if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (par2 == 0)
{
if (!this.mergeItemStack(itemstack1, 10, 46, true))
{
return null;
}
slot.onSlotChange(itemstack1, itemstack);
}
else if (par2 >= 10 && par2 < 37)
{
if (!this.mergeItemStack(itemstack1, 37, 46, false))
{
return null;
}
}
else if (par2 >= 37 && par2 < 46)
{
if (!this.mergeItemStack(itemstack1, 10, 37, false))
{
return null;
}
}
else if (!this.mergeItemStack(itemstack1, 10, 46, false))
{
return null;
}
if (itemstack1.stackSize == 0)
{
2013-08-31 05:13:31 -07:00
slot.putStack((ItemStack) null);
2013-08-07 19:06:26 -07:00
}
else
{
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize)
{
return null;
}
slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
}
return itemstack;
}
2013-08-31 05:13:31 -07:00
public boolean func_94530_a (ItemStack par1ItemStack, Slot par2Slot)
2013-08-07 19:06:26 -07:00
{
return par2Slot.inventory != this.craftResult && super.func_94530_a(par1ItemStack, par2Slot);
}
}