/
ContainerPlayerTFC.java
389 lines (350 loc) · 12.9 KB
/
ContainerPlayerTFC.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.bioxx.tfc.Containers;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import com.bioxx.tfc.Containers.Slots.SlotArmorTFC;
import com.bioxx.tfc.Core.Player.PlayerInventory;
import com.bioxx.tfc.Food.ItemFoodTFC;
import com.bioxx.tfc.Food.ItemMeal;
import com.bioxx.tfc.Handlers.CraftingHandler;
import com.bioxx.tfc.Handlers.FoodCraftingHandler;
import com.bioxx.tfc.Items.ItemTFCArmor;
import com.bioxx.tfc.api.TFCItems;
import com.bioxx.tfc.api.Interfaces.IEquipable;
import com.bioxx.tfc.api.Interfaces.IEquipable.EquipType;
import com.bioxx.tfc.api.Interfaces.IFood;
public class ContainerPlayerTFC extends ContainerPlayer
{
private final EntityPlayer thePlayer;
public ContainerPlayerTFC(InventoryPlayer playerInv, boolean par2, EntityPlayer player)
{
super(playerInv, par2, player);
this.craftMatrix = new InventoryCrafting(this, 3, 3);
this.inventorySlots.clear();
this.inventoryItemStacks.clear();
this.thePlayer = player;
this.addSlotToContainer(new SlotCrafting(player, craftMatrix, craftResult, 0, 152, 36));
int x;
int y;
for (x = 0; x < 2; ++x)
{
for (y = 0; y < 2; ++y)
this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
}
for (x = 0; x < playerInv.armorInventory.length; ++x)
{
int index = playerInv.getSizeInventory() - 1 - x;
this.addSlotToContainer(new SlotArmorTFC(this, playerInv, index, 8, 8 + x * 18, x));
}
PlayerInventory.buildInventoryLayout(this, playerInv, 8, 90, false, true);
//Manually built the remaining crafting slots because of an order issue. These have to be created after the default slots
if(player.getEntityData().hasKey("craftingTable") || !player.worldObj.isRemote)
{
x = 2; y = 0; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
x = 2; y = 1; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
x = 0; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
x = 1; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
x = 2; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18, 18 + x * 18));
}
else
{
//Have to create some dummy slots
x = 2; y = 0; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18-50000, 18 + x * 18));
x = 2; y = 1; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18-50000, 18 + x * 18));
x = 0; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18-50000, 18 + x * 18));
x = 1; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18-50000, 18 + x * 18));
x = 2; y = 2; this.addSlotToContainer(new Slot(craftMatrix, y + x * 3, 82 + y * 18-50000, 18 + x * 18));
}
PlayerInventory.addExtraEquipables(this, playerInv, 8, 90, false);
this.onCraftMatrixChanged(this.craftMatrix);
}
/**
* Callback for when the crafting matrix is changed.
*/
@Override
public void onCraftMatrixChanged(IInventory iinventory)
{
super.onCraftMatrixChanged(iinventory);
Slot craftOut = (Slot) this.inventorySlots.get(0);
if (craftOut != null && craftOut.getHasStack())
{
ItemStack craftResult = craftOut.getStack();
if (craftResult != null)
{
if (craftResult.getItem() instanceof ItemFoodTFC)
FoodCraftingHandler.updateOutput(thePlayer, craftResult, craftMatrix);
else
CraftingHandler.transferNBT(false, thePlayer, craftResult, craftMatrix);
}
}
}
@Override
public void onContainerClosed(EntityPlayer player)
{
if(!player.worldObj.isRemote)
{
super.onContainerClosed(player);
for (int i = 0; i < 9; ++i)
{
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
if (itemstack != null)
player.dropPlayerItemWithRandomChoice(itemstack, false);
}
this.craftResult.setInventorySlotContents(0, (ItemStack)null);
}
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slotNum)
{
ItemStack origStack = null;
Slot slot = (Slot) this.inventorySlots.get(slotNum);
Slot equipmentSlot = (Slot) this.inventorySlots.get(50);
if (slot != null && slot.getHasStack())
{
ItemStack slotStack = slot.getStack();
origStack = slotStack.copy();
// Crafting Grid Output
if (slotNum == 0)
{
FoodCraftingHandler.preCraft(player, slotStack, craftMatrix);
CraftingHandler.preCraft(player, slotStack, craftMatrix);
if (!this.mergeItemStack(slotStack, 9, 45, true, true))
return null;
slot.onSlotChange(slotStack, origStack);
}
// From crafting grid input to inventory
else if (slotNum >= 1 && slotNum < 5 || player.getEntityData().hasKey("craftingTable") && slotNum >= 45 && slotNum < 50)
{
if (!this.mergeItemStack(slotStack, 9, 45, true, false))
return null;
}
// From armor or equipment slot to inventory
else if (slotNum >= 5 && slotNum < 9 || slotNum == 50)
{
if (!this.mergeItemStack(slotStack, 9, 45, true, false))
return null;
}
// From inventory to armor slots
else if (origStack.getItem() instanceof ItemArmor)
{
int armorSlotNum = 5 + ((ItemArmor) origStack.getItem()).armorType;
if (origStack.getItem() instanceof ItemTFCArmor)
{
armorSlotNum = 5 + ((ItemTFCArmor) origStack.getItem()).getUnadjustedArmorType();
if (!((Slot) this.inventorySlots.get(armorSlotNum)).getHasStack())
{
if (!this.mergeItemStack(slotStack, armorSlotNum, armorSlotNum + 1, false, false))
return null;
}
}
else if (!((Slot) this.inventorySlots.get(armorSlotNum)).getHasStack())
{
if (!this.mergeItemStack(slotStack, armorSlotNum, armorSlotNum + 1, false, false))
return null;
}
}
// From inventory to back slot
else if (!equipmentSlot.getHasStack() && origStack.getItem() instanceof IEquipable)
{
IEquipable equipment = (IEquipable) origStack.getItem();
if (equipment.getEquipType(origStack) == EquipType.BACK && (equipment == TFCItems.quiver || equipment.getTooHeavyToCarry(origStack)))
{
ItemStack backStack = slotStack.copy();
backStack.stackSize = 1;
equipmentSlot.putStack(backStack);
slotStack.stackSize--;
}
}
// Food from inventory/hotbar to crafting grid
else if (slotNum >= 9 && slotNum < 45 && origStack.getItem() instanceof IFood && !(origStack.getItem() instanceof ItemMeal) && !isCraftingGridFull())
{
if (!this.mergeItemStack(slotStack, 1, 5, false, false) && slotStack.stackSize == 0)
return null;
else if (slotStack.stackSize > 0 && player.getEntityData().hasKey("craftingTable") && !this.mergeItemStack(slotStack, 45, 50, false))
return null;
else if (slotStack.stackSize > 0 && slotNum >= 9 && slotNum < 36)
{
if (!this.mergeItemStack(slotStack, 36, 45, false, false))
return null;
}
else if (slotStack.stackSize > 0 && slotNum >= 36 && slotNum < 45)
{
if (!this.mergeItemStack(slotStack, 9, 36, false, false))
return null;
}
}
// From inventory to hotbar
else if (slotNum >= 9 && slotNum < 36)
{
if (!this.mergeItemStack(slotStack, 36, 45, false, false))
return null;
}
// From hotbar to inventory
else if (slotNum >= 36 && slotNum < 45)
{
if (!this.mergeItemStack(slotStack, 9, 36, false, false))
return null;
}
if (slotStack.stackSize <= 0)
slot.putStack(null);
else
slot.onSlotChanged();
if (slotStack.stackSize == origStack.stackSize)
return null;
slot.onPickupFromSlot(player, slotStack);
}
return origStack;
}
@Override
public ItemStack slotClick(int sourceSlotID, int destSlotID, int clickType, EntityPlayer p)
{
if (sourceSlotID >= 0 && sourceSlotID < this.inventorySlots.size())
{
Slot sourceSlot = (Slot) this.inventorySlots.get(sourceSlotID);
ItemStack slotStack = sourceSlot.getStack();
// Hotbar press to remove from crafting output
if (clickType == 2 && sourceSlotID == 0 && slotStack != null)
{
CraftingHandler.preCraft(p, slotStack, craftMatrix);
}
// S and D hotkeys for trimming/combining food
else if (clickType == 7 && sourceSlotID >= 9 && sourceSlotID < 45)
{
if (sourceSlot.canTakeStack(p))
{
Slot destSlot = (Slot) this.inventorySlots.get(destSlotID);
destSlot.putStack(slotStack);
sourceSlot.putStack(null);
return null;
}
}
// Couldn't figure out what was causing the food dupe with a full inventory, so we're just going to block shift clicking for that case.
else if (clickType == 1 && sourceSlotID == 0 && isInventoryFull() && slotStack != null && slotStack.getItem() instanceof IFood)
return null;
}
return super.slotClick(sourceSlotID, destSlotID, clickType, p);
}
protected boolean isCraftingGridFull()
{
for(int i = 0; i < this.craftMatrix.getSizeInventory(); i++)
{
if(this.craftMatrix.getStackInSlot(i) == null)
return false;
}
return true;
}
protected boolean isInventoryFull()
{
// Slots 9 through 44 are the standard inventory and hotbar.
for (int i = 9; i < 45; i++)
{
if (((Slot) inventorySlots.get(i)).getStack() == null)
return false;
}
return true;
}
public EntityPlayer getPlayer()
{
return this.thePlayer;
}
protected boolean mergeItemStack(ItemStack is, int slotStart, int slotFinish, boolean reverseOrder, boolean craftingOutput)
{
boolean merged = false;
int slotIndex = slotStart;
if (reverseOrder)
slotIndex = slotFinish - 1;
Slot slot;
ItemStack slotstack;
if (is.isStackable())
{
while (is.stackSize > 0 && (!reverseOrder && slotIndex < slotFinish || reverseOrder && slotIndex >= slotStart))
{
slot = (Slot) this.inventorySlots.get(slotIndex);
slotstack = slot.getStack();
if (slotstack != null && slotstack.getItem() == is.getItem()
//&& !is.getHasSubtypes()
&& is.getItemDamage() == slotstack.getItemDamage() && ItemStack.areItemStackTagsEqual(is, slotstack) && slotstack.stackSize < slot.getSlotStackLimit())
{
int mergedStackSize = is.stackSize + getSmaller(slotstack.stackSize, slot.getSlotStackLimit());
//First we check if we can add the two stacks together and the resulting stack is smaller than the maximum size for the slot or the stack
if (mergedStackSize <= is.getMaxStackSize() && mergedStackSize <= slot.getSlotStackLimit())
{
is.stackSize = 0;
slotstack.stackSize = mergedStackSize;
slot.onSlotChanged();
merged = true;
}
// Do not attempt merge stacks resulting in greater than the max size for slot/stack if shift-clicking the crafting grid output
else if (!craftingOutput && slotstack.stackSize < is.getMaxStackSize() && slotstack.stackSize < slot.getSlotStackLimit())
{
// Slot stack size is greater than or equal to the item's max stack size. Most containers are this case.
if (slot.getSlotStackLimit() >= is.getMaxStackSize())
{
is.stackSize -= is.getMaxStackSize() - slotstack.stackSize;
slotstack.stackSize = is.getMaxStackSize();
slot.onSlotChanged();
merged = true;
}
// Slot stack size is smaller than the item's normal max stack size. Example: Log Piles
else if (slot.getSlotStackLimit() < is.getMaxStackSize())
{
is.stackSize -= slot.getSlotStackLimit() - slotstack.stackSize;
slotstack.stackSize = slot.getSlotStackLimit();
slot.onSlotChanged();
merged = true;
}
}
}
if (reverseOrder)
--slotIndex;
else
++slotIndex;
}
}
if (is.stackSize > 0)
{
if (reverseOrder)
slotIndex = slotFinish - 1;
else
slotIndex = slotStart;
while (!reverseOrder && slotIndex < slotFinish || reverseOrder && slotIndex >= slotStart)
{
slot = (Slot) this.inventorySlots.get(slotIndex);
slotstack = slot.getStack();
if (slotstack == null && slot.isItemValid(is) && slot.getSlotStackLimit() < is.stackSize)
{
ItemStack copy = is.copy();
copy.stackSize = slot.getSlotStackLimit();
is.stackSize -= slot.getSlotStackLimit();
slot.putStack(copy);
slot.onSlotChanged();
merged = true;
//this.bagsSlotNum = slotIndex;
break;
}
else if (slotstack == null && slot.isItemValid(is))
{
slot.putStack(is.copy());
slot.onSlotChanged();
is.stackSize = 0;
merged = true;
break;
}
if (reverseOrder)
--slotIndex;
else
++slotIndex;
}
}
return merged;
}
protected int getSmaller(int i, int j)
{
if (i < j)
return i;
else
return j;
}
}