-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAutoWorkbench.java
354 lines (309 loc) · 8.79 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
/**
* 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 buildcraft.core.TileBuildCraft;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.InventoryIterator.IInvSlot;
import buildcraft.core.inventory.InventoryMapper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.CraftingHelper;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.inventory.TransactorRoundRobin;
import buildcraft.core.utils.Utils;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.util.ChunkCoordinates;
import net.minecraftforge.common.ForgeDirection;
public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory {
public static final int SLOT_RESULT = 0;
public static final int CRAFT_TIME = 256;
public static final int UPDATE_TIME = 16;
private static final int[] SLOTS = Utils.createSlotArray(0, 5);
private SimpleInventory inv = new SimpleInventory(5, "Auto Workbench", 64);
private IInventory invBuffer = new InventoryMapper(inv, 1, 4);
public InventoryCrafting craftMatrix = new LocalInventoryCrafting();
public boolean useLast;
private final TransactorRoundRobin transactor = new TransactorRoundRobin(craftMatrix);
private EntityPlayer internalPlayer;
private SlotCrafting craftSlot;
private InventoryCraftResult craftResult = new InventoryCraftResult();
public int progress;
private int update = Utils.RANDOM.nextInt();
private class LocalInventoryCrafting extends InventoryCrafting {
public LocalInventoryCrafting() {
super(new Container() {
@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
return false;
}
}, 3, 3);
}
}
private final class InternalPlayer extends EntityPlayer {
public InternalPlayer() {
super(TileAutoWorkbench.this.worldObj);
posX = TileAutoWorkbench.this.xCoord;
posY = TileAutoWorkbench.this.yCoord + 1;
posZ = TileAutoWorkbench.this.zCoord;
username = "[Buildcraft]";
}
@Override
public void sendChatToPlayer(String var1) {
}
@Override
public boolean canCommandSenderUseCommand(int var1, String var2) {
return false;
}
@Override
public ChunkCoordinates getPlayerCoordinates() {
return null;
}
}
@Override
public void destroy() {
super.destroy();
Utils.dropItems(worldObj, craftMatrix, xCoord, yCoord, zCoord);
}
@Override
public int getSizeInventory() {
return inv.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return inv.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int count) {
return inv.decrStackSize(slot, count);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inv.setInventorySlotContents(slot, stack);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inv.getStackInSlotOnClosing(slot);
}
@Override
public String getInvName() {
return "";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64.0D;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
inv.readFromNBT(data);
Utils.readInvFromNBT(craftMatrix, "matrix", data);
// Legacy Code
if (data.hasKey("stackList")) {
ItemStack[] stacks = new ItemStack[9];
Utils.readStacksFromNBT(data, "stackList", stacks);
for (int i = 0; i < 9; i++) {
craftMatrix.setInventorySlotContents(i, stacks[i]);
}
}
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
inv.writeToNBT(data);
Utils.writeInvToNBT(craftMatrix, "matrix", data);
}
public ItemStack findRecipeOutput() {
IRecipe recipe = findRecipe();
if (recipe == null) {
return null;
}
ItemStack result = recipe.getCraftingResult(craftMatrix);
if (result != null) {
result = result.copy();
}
return result;
}
public IRecipe findRecipe() {
for (IInvSlot slot : InventoryIterator.getIterable(craftMatrix, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (stack == null) {
continue;
}
if (!stack.isStackable()) {
return null;
}
if (stack.getItem().hasContainerItem()) {
return null;
}
}
return CraftingHelper.findMatchingRecipe(craftMatrix, worldObj);
}
@Override
public boolean canUpdate() {
return true;
}
@Override
public void updateEntity() {
super.updateEntity();
if (CoreProxy.proxy.isRenderWorld(worldObj)) {
return;
}
processHiddenBuffer();
if (craftSlot == null) {
internalPlayer = new InternalPlayer();
craftSlot = new SlotCrafting(internalPlayer, craftMatrix, craftResult, 0, 0, 0);
}
if (inv.getStackInSlot(SLOT_RESULT) != null) {
return;
}
update++;
if (update % UPDATE_TIME == 0) {
updateCrafting();
}
}
public int getProgressScaled(int i) {
return (progress * i) / CRAFT_TIME;
}
/**
* Moves items out of the hidden input buffer into the craft grid.
*/
private void processHiddenBuffer() {
for (IInvSlot slot : InventoryIterator.getIterable(invBuffer, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (stack == null) {
continue;
}
if (!gridHasRoomFor(stack)) {
slot.setStackInSlot(null);
Utils.dropItems(worldObj, stack, xCoord, yCoord + 1, zCoord);
continue;
}
stack.stackSize -= transactor.add(stack, ForgeDirection.DOWN, true).stackSize;
if (stack.stackSize <= 0) {
slot.setStackInSlot(null);
}
}
}
/**
* Increment craft job, find recipes, produce output
*/
private void updateCrafting() {
IRecipe recipe = findRecipe();
if (recipe == null) {
progress = 0;
return;
}
if (!useLast && isLast()) {
progress = 0;
return;
}
progress += UPDATE_TIME;
if (progress < CRAFT_TIME) {
return;
}
progress = 0;
useLast = false;
ItemStack result = recipe.getCraftingResult(craftMatrix);
if (result == null) {
return;
}
result = result.copy();
craftSlot.onPickupFromSlot(internalPlayer, result);
inv.setInventorySlotContents(SLOT_RESULT, result);
// clean fake player inventory (crafting handler support)
for (IInvSlot slot : InventoryIterator.getIterable(internalPlayer.inventory, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (stack != null) {
slot.setStackInSlot(null);
Utils.dropItems(worldObj, stack, xCoord, yCoord + 1, zCoord);
}
}
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
@Override
public boolean isInvNameLocalized() {
return false;
}
@Override
public boolean isStackValidForSlot(int slot, ItemStack stack) {
if (slot == SLOT_RESULT) {
return false;
}
if (stack == null) {
return false;
}
if (!stack.isStackable()) {
return false;
}
if (stack.getItem().hasContainerItem()) {
return false;
}
return gridHasRoomFor(stack);
}
@Override
public int[] getAccessibleSlotsFromSide(int var1) {
return SLOTS;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
return isStackValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
return slot == SLOT_RESULT;
}
/**
* Check if there is room for the stack in the crafting grid.
*
* @param input
* @return true if in grid
*/
private boolean gridHasRoomFor(ItemStack input) {
int space = 0;
for (IInvSlot slot : InventoryIterator.getIterable(craftMatrix, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (StackHelper.instance().canStacksMerge(stack, input)) {
space += stack.getMaxStackSize() - stack.stackSize;
}
}
return space >= input.stackSize;
}
/**
* Returns true if there are only enough inputs for a single craft job.
*
* @return true or false
*/
public boolean isLast() {
int minStackSize = 64;
for (IInvSlot slot : InventoryIterator.getIterable(craftMatrix, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (stack != null && stack.stackSize < minStackSize) {
minStackSize = stack.stackSize;
}
}
return minStackSize <= 1;
}
}