-
Notifications
You must be signed in to change notification settings - Fork 497
/
BptBuilderBlueprint.java
529 lines (428 loc) · 13.5 KB
/
BptBuilderBlueprint.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* 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.core.blueprints;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map.Entry;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.core.StackKey;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.BlockIndex;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
import buildcraft.core.utils.BCLog;
import buildcraft.core.utils.BlockUtil;
public class BptBuilderBlueprint extends BptBuilderBase {
private LinkedList<BuildingSlotBlock> buildList = new LinkedList<BuildingSlotBlock>();
private LinkedList<BuildingSlotEntity> entityList = new LinkedList<BuildingSlotEntity>();
private LinkedList<BuildingSlot> postProcessing = new LinkedList<BuildingSlot>();
private BuildingSlotIterator iterator;
public LinkedList <ItemStack> neededItems = new LinkedList <ItemStack> ();
public BptBuilderBlueprint(Blueprint bluePrint, World world, int x, int y, int z) {
super(bluePrint, world, x, y, z);
for (int j = bluePrint.sizeY - 1; j >= 0; --j) {
for (int i = 0; i < bluePrint.sizeX; ++i) {
for (int k = 0; k < bluePrint.sizeZ; ++k) {
int xCoord = i + x - blueprint.anchorX;
int yCoord = j + y - blueprint.anchorY;
int zCoord = k + z - blueprint.anchorZ;
if (!clearedLocations.contains(new BlockIndex(xCoord, yCoord, zCoord))) {
SchematicBlock slot = (SchematicBlock) bluePrint.contents[i][j][k];
if (slot == null) {
slot = new SchematicBlock();
slot.meta = 0;
slot.block = Blocks.air;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.ClearIfInvalid;
buildList.add(b);
}
}
}
}
LinkedList<BuildingSlotBlock> tmpBuildList = new LinkedList<BuildingSlotBlock>();
for (int j = 0; j < bluePrint.sizeY; ++j) {
for (int i = 0; i < bluePrint.sizeX; ++i) {
for (int k = 0; k < bluePrint.sizeZ; ++k) {
int xCoord = i + x - blueprint.anchorX;
int yCoord = j + y - blueprint.anchorY;
int zCoord = k + z - blueprint.anchorZ;
if (!builtLocations.contains(new BlockIndex(xCoord, yCoord,
zCoord))) {
SchematicBlock slot = (SchematicBlock) bluePrint.contents[i][j][k];
if (slot == null) {
continue;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.Build;
tmpBuildList.add(b);
}
}
}
}
Collections.sort(tmpBuildList);
buildList.addAll(tmpBuildList);
iterator = new BuildingSlotIterator(buildList);
for (SchematicEntity e : bluePrint.entities) {
// TODO: take into account items already built... How to identify
// them? id in list?
BuildingSlotEntity b = new BuildingSlotEntity();
b.schematic = e;
entityList.add(b);
}
recomputeNeededItems();
}
private void checkDone() {
recomputeNeededItems();
if (buildList.size() == 0 && entityList.size() == 0) {
done = true;
} else {
done = false;
}
}
@Override
public BuildingSlot getNextBlock(World world, TileAbstractBuilder inv) {
if (buildList.size() != 0) {
BuildingSlot slot = internalGetNextBlock(world, inv, buildList);
checkDone();
if (slot != null) {
return slot;
} else {
return null;
}
}
if (entityList.size() != 0) {
BuildingSlot slot = internalGetNextEntity(world, inv, entityList);
checkDone ();
if (slot != null) {
return slot;
} else {
return null;
}
}
checkDone();
return null;
}
private BuildingSlot internalGetNextBlock(World world, TileAbstractBuilder builder, LinkedList<BuildingSlotBlock> list) {
iterator.startIteration();
while (iterator.hasNext()) {
BuildingSlotBlock slot = iterator.next();
boolean getNext = false;
try {
getNext = !slot.schematic.ignoreBuilding()
&& !slot.isAlreadyBuilt(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "internalGetBlock", t);
getNext = false;
}
if (getNext) {
if (slot.mode == Mode.ClearIfInvalid) {
if (BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)
|| BlockUtil.isUnbreakableBlock(world, slot.x, slot.y, slot.z)) {
iterator.remove();
} else {
if (setupForDestroy(builder, context, slot)) {
iterator.remove();
postProcessing.add(slot);
clearedLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
return slot;
}
}
} else {
if (BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)
&& checkRequirements(builder,
slot.schematic)) {
useRequirements(builder, slot);
iterator.remove();
postProcessing.add(slot);
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
return slot;
}
}
} else {
iterator.remove();
}
}
return null;
}
private BuildingSlot internalGetNextEntity(World world,
TileAbstractBuilder builder, LinkedList<BuildingSlotEntity> list) {
Iterator<BuildingSlotEntity> it = list.iterator();
while (it.hasNext()) {
BuildingSlotEntity slot = it.next();
if (slot.isAlreadyBuilt(context)) {
it.remove();
} else {
if (checkRequirements(builder, slot.schematic)) {
useRequirements(builder, slot);
it.remove();
postProcessing.add(slot);
return slot;
}
}
}
return null;
}
public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
double energyRequired = 0;
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
LinkedList<ItemStack> tmpInv = new LinkedList<ItemStack>();
try {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
if (builder.energyAvailable() < energyRequired) {
return false;
}
if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
return true;
}
int size = builder.getSizeInventory();
for (int i = 0; i < size; ++i) {
if (!builder.isBuildingMaterialSlot(i)) {
continue;
}
if (builder.getStackInSlot(i) != null) {
tmpInv.add(builder.getStackInSlot(i).copy());
}
}
for (ItemStack reqStk : tmpReq) {
for (ItemStack invStk : tmpInv) {
if (invStk != null && invStk.stackSize > 0 && isEqual (reqStk, invStk)) {
try {
slot.useItem(context, reqStk, invStk);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
if (reqStk.stackSize == 0) {
break;
}
}
}
if (reqStk.stackSize != 0) {
return false;
}
}
return true;
}
public void useRequirements(TileAbstractBuilder builder, BuildingSlot slot) {
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
double energyRequired = 0;
try {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "useRequirements", t);
}
builder.consumeEnergy(energyRequired);
if (context.world ().getWorldInfo().getGameType() == GameType.CREATIVE) {
for (ItemStack s : slot.getRequirements(context)) {
slot.addStackConsumed(s);
}
return;
}
ListIterator<ItemStack> itr = tmpReq.listIterator();
while (itr.hasNext()) {
ItemStack reqStk = itr.next();
boolean smallStack = reqStk.stackSize == 1;
ItemStack usedStack = reqStk;
int size = builder.getSizeInventory();
for (int i = 0; i < size; ++i) {
if (!builder.isBuildingMaterialSlot(i)) {
}
ItemStack invStk = builder.getStackInSlot(i);
if (invStk != null && invStk.stackSize > 0 && isEqual (reqStk, invStk)) {
try {
usedStack = slot.getSchematic().useItem(context, reqStk, invStk);
slot.addStackConsumed (usedStack);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "useRequirements", t);
}
if (invStk.stackSize == 0) {
builder.setInventorySlotContents(i, null);
} else {
builder.setInventorySlotContents(i, invStk);
}
if (reqStk.stackSize == 0) {
break;
}
}
}
if (reqStk.stackSize != 0) {
return;
}
if (smallStack) {
itr.set(usedStack); // set to the actual item used.
}
}
return;
}
private boolean isEqual(ItemStack reqStk, ItemStack invStk) {
if (reqStk.getItem() != invStk.getItem()) {
return false;
} else if (!invStk.isItemStackDamageable() && (reqStk.getItemDamage() != invStk.getItemDamage())) {
return false;
}
// TODO: stackTagCompound may or may not be relevant. In particular, TMI
// adds some compounds to all objects and kills the comparions. Let's
// avoid considering for now, and later allow mods to provide they
// own item comparator
/*else if (reqStk.stackTagCompound != null && invStk.stackTagCompound == null) {
return false;
} else if (reqStk.stackTagCompound == null && invStk.stackTagCompound != null) {
return false;
} else if (reqStk.stackTagCompound != null && !reqStk.stackTagCompound.equals(invStk.stackTagCompound)) {
return false;
}*/
else {
return true;
}
}
public void recomputeNeededItems() {
neededItems.clear();
HashMap <StackKey, Integer> computeStacks = new HashMap <StackKey, Integer> ();
for (BuildingSlot slot : buildList) {
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
try {
stacks = slot.getRequirements(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "recomputeIfNeeded", t);
}
for (ItemStack stack : stacks) {
if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
continue;
}
StackKey key = new StackKey(stack);
if (!computeStacks.containsKey(key)) {
computeStacks.put(key, stack.stackSize);
} else {
Integer num = computeStacks.get(key);
num += stack.stackSize;
computeStacks.put(key, num);
}
}
}
for (BuildingSlotEntity slot : entityList) {
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
try {
stacks = slot.getRequirements(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "recomputeIfNeeded", t);
}
for (ItemStack stack : stacks) {
if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
continue;
}
StackKey key = new StackKey(stack);
if (!computeStacks.containsKey(key)) {
computeStacks.put(key, stack.stackSize);
} else {
Integer num = computeStacks.get(key);
num += stack.stackSize;
computeStacks.put(key, num);
}
}
}
for (Entry<StackKey, Integer> e : computeStacks.entrySet()) {
ItemStack newStack = e.getKey().stack.copy();
newStack.stackSize = e.getValue();
neededItems.add(newStack);
}
LinkedList <ItemStack> sortedList = new LinkedList <ItemStack> ();
for (ItemStack toInsert : neededItems) {
int index = 0;
boolean didInsert = false;
for (ItemStack inserted : sortedList) {
if (inserted.stackSize < toInsert.stackSize) {
sortedList.add(index, toInsert);
didInsert = true;
break;
}
index++;
}
if (!didInsert) {
sortedList.addLast(toInsert);
}
}
Collections.sort (neededItems, new Comparator<ItemStack>() {
@Override
public int compare(ItemStack o1, ItemStack o2) {
if (o1.stackSize > o2.stackSize) {
return -1;
} else if (o1.stackSize < o2.stackSize) {
return 1;
} else if (Item.getIdFromItem(o1.getItem()) > Item.getIdFromItem(o2.getItem())) {
return -1;
} else if (Item.getIdFromItem(o1.getItem()) < Item.getIdFromItem(o2.getItem())) {
return 1;
} else if (o1.getItemDamage() > o2.getItemDamage()) {
return -1;
} else if (o1.getItemDamage() < o2.getItemDamage()) {
return 1;
} else {
return 0;
}
}
});
}
@Override
public void postProcessing(World world) {
for (BuildingSlot s : postProcessing) {
try {
s.postProcessing(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "postProcessing", t);
}
}
}
}