-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAssemblyTable.java
493 lines (400 loc) · 12.4 KB
/
TileAssemblyTable.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package buildcraft.silicon;
import buildcraft.api.gates.IAction;
import buildcraft.api.recipes.AssemblyRecipe;
import buildcraft.core.DefaultProps;
import buildcraft.core.IMachine;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketNBT;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import cpw.mods.fml.common.FMLCommonHandler;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
public class TileAssemblyTable extends TileEntity implements IMachine, IInventory, ILaserTarget {
ItemStack[] items = new ItemStack[12];
LinkedHashSet<AssemblyRecipe> plannedOutput = new LinkedHashSet<AssemblyRecipe>();
public AssemblyRecipe currentRecipe;
private float currentRequiredEnergy = 0;
private float energyStored = 0;
private float[] recentEnergy = new float[20];
private int tick = 0;
private int recentEnergyAverage;
public static class SelectionMessage {
public boolean select;
public ItemStack stack;
public NBTTagCompound getNBT() {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setBoolean("s", select);
NBTTagCompound itemNBT = new NBTTagCompound();
stack.writeToNBT(itemNBT);
nbt.setCompoundTag("i", itemNBT);
return nbt;
}
public void fromNBT(NBTTagCompound nbt) {
select = nbt.getBoolean("s");
NBTTagCompound itemNBT = nbt.getCompoundTag("i");
stack = ItemStack.loadItemStackFromNBT(itemNBT);
}
}
public LinkedList<AssemblyRecipe> getPotentialOutputs() {
LinkedList<AssemblyRecipe> result = new LinkedList<AssemblyRecipe>();
for (AssemblyRecipe recipe : AssemblyRecipe.assemblyRecipes) {
if (recipe.canBeDone(items)) {
result.add(recipe);
}
}
return result;
}
@Override
public void receiveLaserEnergy(float energy) {
energyStored += energy;
recentEnergy[tick] += energy;
}
@Override
public boolean canUpdate() {
return !FMLCommonHandler.instance().getEffectiveSide().isClient();
}
@Override
public void updateEntity() { // WARNING: run only server-side, see canUpdate()
tick++;
tick = tick % recentEnergy.length;
recentEnergy[tick] = 0.0f;
if (currentRecipe == null)
return;
if (!currentRecipe.canBeDone(items)) {
setNextCurrentRecipe();
if (currentRecipe == null)
return;
}
if (energyStored >= currentRecipe.energy) {
energyStored = 0;
if (currentRecipe.canBeDone(items)) {
for (ItemStack in : currentRecipe.input) {
if (in == null) {
continue; // Optimisation, reduces calculation for a null ingredient
}
int found = 0; // Amount of ingredient found in inventory
for (int i = 0; i < items.length; ++i) {
if (items[i] == null) {
continue; // Broken out of large if statement, increases clarity
}
if (StackHelper.instance().isCraftingEquivalent(in, items[i], true)) {
int supply = items[i].stackSize;
int toBeFound = in.stackSize - found;
if (supply >= toBeFound) {
found += decrStackSize(i, toBeFound).stackSize; // Adds the amount of ingredient taken (in this case the total still needed)
} else {
found += decrStackSize(i, supply).stackSize; // Adds the amount of ingredient taken (in this case the total in that slot)
}
if (found >= in.stackSize) {
break; // Breaks out of the for loop when the required amount of ingredient has been taken
}
}
}
}
ItemStack remaining = currentRecipe.output.copy();
remaining.stackSize -= Utils.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, remaining);
if (remaining.stackSize > 0) {
remaining.stackSize -= Utils.addToRandomPipeAround(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UNKNOWN, remaining);
}
if (remaining.stackSize > 0) {
EntityItem entityitem = new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5, currentRecipe.output.copy());
worldObj.spawnEntityInWorld(entityitem);
}
setNextCurrentRecipe();
}
}
}
public float getCompletionRatio(float ratio) {
if (currentRecipe == null)
return 0;
else if (energyStored >= currentRequiredEnergy)
return ratio;
else
return energyStored / currentRequiredEnergy * ratio;
}
/* IINVENTORY */
@Override
public int getSizeInventory() {
return items.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return items[i];
}
@Override
public ItemStack decrStackSize(int i, int j) {
ItemStack stack = items[i].splitStack(j);
if (items[i].stackSize == 0) {
items[i] = null;
}
return stack;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
items[i] = itemstack;
if (currentRecipe == null) {
setNextCurrentRecipe();
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (this.items[slot] == null)
return null;
ItemStack stackToTake = this.items[slot];
this.items[slot] = null;
return stackToTake;
}
@Override
public String getInvName() {
return "Assembly Table";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this;
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
Utils.readStacksFromNBT(nbttagcompound, "items", items);
energyStored = nbttagcompound.getFloat("energyStored");
NBTTagList list = nbttagcompound.getTagList("planned");
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound cpt = (NBTTagCompound) list.tagAt(i);
ItemStack stack = ItemStack.loadItemStackFromNBT(cpt);
for (AssemblyRecipe r : AssemblyRecipe.assemblyRecipes) {
if (r.output.itemID == stack.itemID && r.output.getItemDamage() == stack.getItemDamage()) {
plannedOutput.add(r);
break;
}
}
}
if (nbttagcompound.hasKey("recipe")) {
ItemStack stack = ItemStack.loadItemStackFromNBT(nbttagcompound.getCompoundTag("recipe"));
for (AssemblyRecipe r : plannedOutput) {
if (r.output.itemID == stack.itemID && r.output.getItemDamage() == stack.getItemDamage()) {
setCurrentRecipe(r);
break;
}
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
Utils.writeStacksToNBT(nbttagcompound, "items", items);
nbttagcompound.setFloat("energyStored", energyStored);
NBTTagList list = new NBTTagList();
for (AssemblyRecipe recipe : plannedOutput) {
NBTTagCompound cpt = new NBTTagCompound();
recipe.output.writeToNBT(cpt);
list.appendTag(cpt);
}
nbttagcompound.setTag("planned", list);
if (currentRecipe != null) {
NBTTagCompound recipe = new NBTTagCompound();
currentRecipe.output.writeToNBT(recipe);
nbttagcompound.setTag("recipe", recipe);
}
}
public boolean isPlanned(AssemblyRecipe recipe) {
if (recipe == null)
return false;
return plannedOutput.contains(recipe);
}
public boolean isAssembling(AssemblyRecipe recipe) {
return recipe != null && recipe == currentRecipe;
}
private void setCurrentRecipe(AssemblyRecipe recipe) {
this.currentRecipe = recipe;
if (recipe != null) {
this.currentRequiredEnergy = recipe.energy;
} else {
this.currentRequiredEnergy = 0;
}
}
public void planOutput(AssemblyRecipe recipe) {
if (recipe != null && !isPlanned(recipe)) {
plannedOutput.add(recipe);
if (!isAssembling(currentRecipe) || !isPlanned(currentRecipe)) {
setCurrentRecipe(recipe);
}
}
}
public void cancelPlanOutput(AssemblyRecipe recipe) {
if (isAssembling(recipe)) {
setCurrentRecipe(null);
}
plannedOutput.remove(recipe);
if (!plannedOutput.isEmpty()) {
setCurrentRecipe(plannedOutput.iterator().next());
}
}
public void setNextCurrentRecipe() {
boolean takeNext = false;
for (AssemblyRecipe recipe : plannedOutput) {
if (recipe == currentRecipe) {
takeNext = true;
} else if (takeNext && recipe.canBeDone(items)) {
setCurrentRecipe(recipe);
return;
}
}
for (AssemblyRecipe recipe : plannedOutput) {
if (recipe.canBeDone(items)) {
setCurrentRecipe(recipe);
return;
}
}
setCurrentRecipe(null);
}
public void handleSelectionMessage(SelectionMessage message) {
for (AssemblyRecipe recipe : AssemblyRecipe.assemblyRecipes) {
if (recipe.output.isItemEqual(message.stack) && ItemStack.areItemStackTagsEqual(recipe.output, message.stack)) {
if (message.select) {
planOutput(recipe);
} else {
cancelPlanOutput(recipe);
}
break;
}
}
}
public void sendSelectionTo(EntityPlayer player) {
for (AssemblyRecipe r : AssemblyRecipe.assemblyRecipes) {
SelectionMessage message = new SelectionMessage();
message.stack = r.output;
if (isPlanned(r)) {
message.select = true;
} else {
message.select = false;
}
PacketNBT packet = new PacketNBT(PacketIds.SELECTION_ASSEMBLY_SEND, message.getNBT(), xCoord, yCoord, zCoord);
packet.posX = xCoord;
packet.posY = yCoord;
packet.posZ = zCoord;
// FIXME: This needs to be switched over to new synch system.
CoreProxy.proxy.sendToPlayers(packet.getPacket(), worldObj, (int) player.posX, (int) player.posY, (int) player.posZ,
DefaultProps.NETWORK_UPDATE_RANGE);
}
}
/* SMP GUI */
public void getGUINetworkData(int i, int j) {
int currentStored = (int) (energyStored * 100.0);
int requiredEnergy = (int) (currentRequiredEnergy * 100.0);
switch (i) {
case 0:
requiredEnergy = (requiredEnergy & 0xFFFF0000) | (j & 0xFFFF);
currentRequiredEnergy = (requiredEnergy / 100.0f);
break;
case 1:
currentStored = (currentStored & 0xFFFF0000) | (j & 0xFFFF);
energyStored = (currentStored / 100.0f);
break;
case 2:
requiredEnergy = (requiredEnergy & 0xFFFF) | ((j & 0xFFFF) << 16);
currentRequiredEnergy = (requiredEnergy / 100.0f);
break;
case 3:
currentStored = (currentStored & 0xFFFF) | ((j & 0xFFFF) << 16);
energyStored = (currentStored / 100.0f);
break;
case 4:
recentEnergyAverage = recentEnergyAverage & 0xFFFF0000 | (j & 0xFFFF);
break;
case 5:
recentEnergyAverage = (recentEnergyAverage & 0xFFFF) | ((j & 0xFFFF) << 16);
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int requiredEnergy = (int) (currentRequiredEnergy * 100.0);
int currentStored = (int) (energyStored * 100.0);
int lRecentEnergy = 0;
for (int i = 0; i < recentEnergy.length; i++) {
lRecentEnergy += (int) (recentEnergy[i] * 100.0 / (recentEnergy.length - 1));
}
iCrafting.sendProgressBarUpdate(container, 0, requiredEnergy & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 1, currentStored & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 2, (requiredEnergy >>> 16) & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 3, (currentStored >>> 16) & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 4, lRecentEnergy & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 5, (lRecentEnergy >>> 16) & 0xFFFF);
}
@Override
public boolean isActive() {
return currentRecipe != null;
}
@Override
public boolean manageFluids() {
return false;
}
@Override
public boolean manageSolids() {
return false;
}
@Override
public boolean allowAction(IAction action) {
return false;
}
public int getRecentEnergyAverage() {
return recentEnergyAverage;
}
public float getStoredEnergy() {
return energyStored;
}
public float getRequiredEnergy() {
return currentRequiredEnergy;
}
@Override
public boolean hasCurrentWork() {
return currentRecipe != null;
}
@Override
public int getXCoord() {
return xCoord;
}
@Override
public int getYCoord() {
return yCoord;
}
@Override
public int getZCoord() {
return zCoord;
}
@Override
public boolean isInvNameLocalized() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isInvalidTarget() {
return isInvalid();
}
}