-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAutoWorkbench.java
382 lines (293 loc) · 10.1 KB
/
TileAutoWorkbench.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
/**
* Copyright (c) SpaceToad, 2011
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.factory;
import java.util.LinkedList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import buildcraft.api.core.Position;
import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.core.inventory.TransactorRoundRobin;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
private ItemStack stackList[] = new ItemStack[9];
class LocalInventoryCrafting extends InventoryCrafting {
public LocalInventoryCrafting() {
super(new Container() {
@SuppressWarnings("all")
public boolean isUsableByPlayer(EntityPlayer entityplayer) {
return false;
}
@SuppressWarnings("all")
public boolean canInteractWith(EntityPlayer entityplayer) {
// TODO Auto-generated method stub
return false;
}
}, 3, 3);
// TODO Auto-generated constructor stub
}
}
@Override
public int getSizeInventory() {
return stackList.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return stackList[i];
}
@Override
public ItemStack decrStackSize(int slotId, int count) {
if (stackList[slotId] == null)
return null;
if (stackList[slotId].stackSize > count)
return stackList[slotId].splitStack(count);
ItemStack stack = stackList[slotId];
stackList[slotId] = null;
return stack;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
stackList[i] = itemstack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (this.stackList[slot] == null)
return null;
ItemStack stackToTake = this.stackList[slot];
this.stackList[slot] = null;
return stackToTake;
}
@Override
public String getInvName() {
return "";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this;
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
Utils.readStacksFromNBT(nbttagcompound, "stackList", stackList);
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
Utils.writeStacksToNBT(nbttagcompound, "stackList", stackList);
}
class StackPointer {
IInventory inventory;
int index;
ItemStack item;
}
public ItemStack findRecipe() {
InventoryCrafting craftMatrix = new LocalInventoryCrafting();
for (int i = 0; i < getSizeInventory(); ++i) {
ItemStack stack = getStackInSlot(i);
craftMatrix.setInventorySlotContents(i, stack);
}
ItemStack recipe = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj);
return recipe;
}
public ItemStack extractItem(boolean doRemove, boolean removeRecipe) {
InventoryCrafting craftMatrix = new LocalInventoryCrafting();
LinkedList<StackPointer> pointerList = new LinkedList<StackPointer>();
int itemsToLeave = (removeRecipe ? 0 : 1);
for (int i = 0; i < getSizeInventory(); ++i) {
ItemStack stack = getStackInSlot(i);
if (stack != null) {
if (stack.stackSize <= itemsToLeave) {
StackPointer pointer = getNearbyItem(stack);
if (pointer == null) {
resetPointers(pointerList);
return null;
} else {
pointerList.add(pointer);
}
} else {
StackPointer pointer = new StackPointer();
pointer.inventory = this;
pointer.item = this.decrStackSize(i, 1);
pointer.index = i;
stack = pointer.item;
pointerList.add(pointer);
}
}
craftMatrix.setInventorySlotContents(i, stack);
}
ItemStack resultStack = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj);
if (resultStack == null || !doRemove) {
resetPointers(pointerList);
} else {
for (StackPointer p : pointerList) {
// replace with the container where appropriate
if (p.item.getItem().getContainerItem() != null) {
ItemStack newStack = p.item.getItem().getContainerItemStack(p.item);
if (p.item.isItemStackDamageable()) {
if (newStack.getItemDamage() >= p.item.getMaxDamage()) {
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(CoreProxy.proxy.getBuildCraftPlayer(worldObj, xCoord, yCoord, zCoord),
newStack));
this.worldObj.playSoundAtEntity(CoreProxy.proxy.getBuildCraftPlayer(worldObj, xCoord, yCoord, zCoord), "random.break", 0.8F,
0.8F + this.worldObj.rand.nextFloat() * 0.4F);
newStack = null;
}
}
p.inventory.setInventorySlotContents(p.index, newStack);
}
}
}
return resultStack;
}
public void resetPointers(LinkedList<StackPointer> pointers) {
for (StackPointer p : pointers) {
ItemStack item = p.inventory.getStackInSlot(p.index);
if (item == null) {
p.inventory.setInventorySlotContents(p.index, p.item);
} else {
p.inventory.getStackInSlot(p.index).stackSize++;
}
}
}
public StackPointer getNearbyItem(ItemStack stack) {
StackPointer pointer = null;
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.WEST);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.EAST);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.DOWN);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.UP);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.NORTH);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.SOUTH);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(stack, ForgeDirection.UNKNOWN);
}
return pointer;
}
public StackPointer getNearbyItemFromOrientation(ItemStack itemStack, ForgeDirection direction) {
TileEntity tile = worldObj.getBlockTileEntity(xCoord + direction.offsetX, yCoord + direction.offsetY, zCoord + direction.offsetZ);
if (tile instanceof ISpecialInventory) {
// Don't get stuff out of ISpecialInventory for now / we wouldn't
// know how to put it back... And it's not clear if we want to
// have workbenches automatically getting things from one another.
} else if (tile instanceof IInventory) {
IInventory inventory = Utils.getInventory((IInventory) tile);
for (int j = 0; j < inventory.getSizeInventory(); ++j) {
ItemStack stack = inventory.getStackInSlot(j);
if (stack != null) {
if (stack.stackSize > 0) {
if (stack.itemID == itemStack.itemID) {
if (!stack.isItemStackDamageable()) {
if (stack.itemID == itemStack.itemID && stack.getItemDamage() == itemStack.getItemDamage()) {
inventory.decrStackSize(j, 1);
StackPointer result = new StackPointer();
result.inventory = inventory;
result.index = j;
result.item = stack;
return result;
}
} else {
if (stack.itemID == itemStack.itemID) {
inventory.decrStackSize(j, 1);
StackPointer result = new StackPointer();
result.inventory = inventory;
result.index = j;
result.item = stack;
return result;
}
}
}
}
}
}
}
return null;
}
public StackPointer getNearbyItem(int itemId, int damage) {
StackPointer pointer = null;
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.WEST);
if (pointer == null) {
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.EAST);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.DOWN);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.UP);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.NORTH);
}
if (pointer == null) {
pointer = getNearbyItemFromOrientation(itemId, damage, ForgeDirection.SOUTH);
}
return pointer;
}
public StackPointer getNearbyItemFromOrientation(int itemId, int damage, ForgeDirection orientation) {
Position p = new Position(xCoord, yCoord, zCoord, orientation);
p.moveForwards(1.0);
TileEntity tile = worldObj.getBlockTileEntity((int) p.x, (int) p.y, (int) p.z);
if (tile instanceof ISpecialInventory) {
// Don't get stuff out of ISpecialInventory for now / we wouldn't
// know how to put it back... And it's not clear if we want to
// have workbenches automatically getting things from one another.
} else if (tile instanceof IInventory) {
IInventory inventory = Utils.getInventory((IInventory) tile);
for (int j = 0; j < inventory.getSizeInventory(); ++j) {
ItemStack stack = inventory.getStackInSlot(j);
if (stack != null && stack.stackSize > 0 && stack.itemID == itemId && stack.getItemDamage() == damage) {
inventory.decrStackSize(j, 1);
StackPointer result = new StackPointer();
result.inventory = inventory;
result.index = j;
result.item = stack;
return result;
}
}
}
return null;
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
/* ISPECIALINVENTORY */
@Override
public int addItem(ItemStack stack, boolean doAdd, ForgeDirection from) {
return new TransactorRoundRobin(this).add(stack, from, doAdd).stackSize;
}
@Override
public ItemStack[] extractItem(boolean doRemove, ForgeDirection from, int maxItemCount) {
return new ItemStack[] { extractItem(doRemove, false) };
}
}