-
Notifications
You must be signed in to change notification settings - Fork 755
/
CraftingStationContainer.java
206 lines (181 loc) · 6.54 KB
/
CraftingStationContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.world.World;
import tconstruct.blocks.logic.CraftingStationLogic;
import tconstruct.library.crafting.ToolBuilder;
import tconstruct.library.tools.ToolCore;
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;
private World worldObj;
private int posX;
private int posY;
private int posZ;
public CraftingStationContainer(InventoryPlayer inventorplayer, CraftingStationLogic logic, int x, int y, int z)
{
this.worldObj = logic.worldObj;
this.posX = x;
this.posY = y;
this.posZ = z;
this.logic = logic;
craftMatrix = new InventoryCraftingStation(this, 3, 3, logic);
craftResult = new InventoryCraftingStationResult(logic);
this.addSlotToContainer(new SlotCrafting(inventorplayer.player, this.craftMatrix, this.craftResult, 0, 124, 35));
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));
}
//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;
}
}
this.onCraftMatrixChanged(this.craftMatrix);
}
public void onCraftMatrixChanged (IInventory par1IInventory)
{
ItemStack tool = modifyTool();
if (tool != null)
this.craftResult.setInventorySlotContents(0, tool);
else
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
}
public ItemStack modifyTool ()
{
ItemStack input = craftMatrix.getStackInSlot(4);
if (input != null)
{
Item item = input.getItem();
if (item instanceof ToolCore)
{
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.modifyTool(input, slots, "");
if (output != null)
return output;
}
}
return null;
}
public void onContainerClosed (EntityPlayer par1EntityPlayer)
{
super.onContainerClosed(par1EntityPlayer);
if (!this.worldObj.isRemote)
{
for (int i = 0; i < 9; ++i)
{
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
if (itemstack != null)
{
par1EntityPlayer.dropPlayerItem(itemstack);
}
}
}
}
public boolean canInteractWith (EntityPlayer par1EntityPlayer)
{
return par1EntityPlayer.getDistanceSq((double) this.posX + 0.5D, (double) this.posY + 0.5D, (double) this.posZ + 0.5D) <= 64.0D;
}
public ItemStack transferStackInSlot (EntityPlayer par1EntityPlayer, int par2)
{
ItemStack itemstack = null;
Slot slot = (Slot) this.inventorySlots.get(par2);
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)
{
slot.putStack((ItemStack) null);
}
else
{
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize)
{
return null;
}
slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
}
return itemstack;
}
public boolean func_94530_a (ItemStack par1ItemStack, Slot par2Slot)
{
return par2Slot.inventory != this.craftResult && super.func_94530_a(par1ItemStack, par2Slot);
}
}