-
Notifications
You must be signed in to change notification settings - Fork 497
/
BptBuilderBlueprint.java
657 lines (536 loc) · 16.8 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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
* 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 java.util.TreeSet;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.SchematicRegistry;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.core.StackKey;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.BlockIndex;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
import buildcraft.core.inventory.InventoryCopy;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.StackHelper;
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>();
protected TreeSet <Integer> builtEntities = new TreeSet <Integer> ();
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);
}
@Override
protected void initialize () {
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 (yCoord < 0 || yCoord >= context.world.getHeight()) {
continue;
}
if (!clearedLocations.contains(new BlockIndex(
xCoord, yCoord, zCoord))) {
SchematicBlock slot = (SchematicBlock) blueprint.contents[i][j][k];
if (slot == null && !blueprint.excavate) {
continue;
}
if (slot == null) {
slot = new SchematicBlock();
slot.meta = 0;
slot.block = Blocks.air;
}
if (!SchematicRegistry.isAllowedForBuilding(slot.block)) {
continue;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.ClearIfInvalid;
b.buildStage = 0;
buildList.add(b);
}
}
}
}
LinkedList<BuildingSlotBlock> tmpStandalone = new LinkedList<BuildingSlotBlock>();
LinkedList<BuildingSlotBlock> tmpSupported = new LinkedList<BuildingSlotBlock>();
LinkedList<BuildingSlotBlock> tmpExpanding = 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;
SchematicBlock slot = (SchematicBlock) blueprint.contents[i][j][k];
if (slot == null || yCoord < 0 || yCoord >= context.world.getHeight()) {
continue;
}
if (!SchematicRegistry.isAllowedForBuilding(slot.block)) {
continue;
}
BuildingSlotBlock b = new BuildingSlotBlock();
b.schematic = slot;
b.x = xCoord;
b.y = yCoord;
b.z = zCoord;
b.mode = Mode.Build;
if (!builtLocations.contains(new BlockIndex(xCoord, yCoord,
zCoord))) {
switch (slot.getBuildStage()) {
case STANDALONE:
tmpStandalone.add(b);
b.buildStage = 1;
break;
case SUPPORTED:
tmpSupported.add(b);
b.buildStage = 2;
break;
case EXPANDING:
tmpExpanding.add(b);
b.buildStage = 3;
break;
}
} else {
postProcessing.add(b);
}
}
}
}
buildList.addAll(tmpStandalone);
buildList.addAll(tmpSupported);
buildList.addAll(tmpExpanding);
iterator = new BuildingSlotIterator(buildList);
int seqId = 0;
for (SchematicEntity e : ((Blueprint) blueprint).entities) {
BuildingSlotEntity b = new BuildingSlotEntity();
b.schematic = e;
b.sequenceNumber = seqId;
if (!builtEntities.contains(seqId)) {
entityList.add(b);
} else {
postProcessing.add(b);
}
seqId++;
}
recomputeNeededItems();
}
public void deploy () {
for (BuildingSlotBlock b : buildList) {
if (b.mode == Mode.ClearIfInvalid) {
context.world.setBlockToAir(b.x, b.y, b.z);
} else {
b.stackConsumed = new LinkedList<ItemStack>();
try {
for (ItemStack stk : b.getRequirements(context)) {
if (stk != null) {
b.stackConsumed.add(stk.copy());
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
b.writeToWorld(context);
}
}
for (BuildingSlotEntity e : entityList) {
e.stackConsumed = new LinkedList<ItemStack>();
try {
for (ItemStack stk : e.getRequirements(context)) {
if (stk != null) {
e.stackConsumed.add(stk.copy());
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
e.writeToWorld(context);
}
for (BuildingSlotBlock b : buildList) {
if (b.mode != Mode.ClearIfInvalid) {
b.postProcessing(context);
}
}
for (BuildingSlotEntity e : entityList) {
e.postProcessing(context);
}
}
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();
if (slot.buildStage > buildList.getFirst().buildStage) {
iterator.reset ();
return null;
}
try {
if (!slot.isAlreadyBuilt(context)) {
if (slot.mode == Mode.ClearIfInvalid) {
if (BuildCraftAPI.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();
clearedLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
return slot;
}
}
} else if (!slot.schematic.doNotBuild()) {
if (checkRequirements(builder, slot.schematic)) {
// At this stage, regardless of the fact that the
// block can actually be built or not, we'll try.
// When the item reaches the actual block, we'll
// verify that the location is indeed clear, and
// avoid building otherwise.
useRequirements(builder, slot);
iterator.remove();
postProcessing.add(slot);
builtLocations.add(new BlockIndex(slot.x,
slot.y, slot.z));
return slot;
}
} else {
// Even slots that don't need to be build may need
// post processing, see below for the argument.
postProcessing.add(slot);
iterator.remove();
}
} else {
if (slot.mode == Mode.Build) {
// Even slots that considered already built may need
// post processing calls. For example, flowing water
// may need to be adjusted, engines may need to be
// turned to the right direction, etc.
postProcessing.add(slot);
}
iterator.remove();
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint",
"internalGetBlock", t);
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);
builtEntities.add(slot.sequenceNumber);
return slot;
}
}
}
return null;
}
public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
try {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
LinkedList <ItemStack> stacksUsed = new LinkedList<ItemStack>();
if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
for (ItemStack s : tmpReq) {
stacksUsed.add(s);
}
if (builder.energyAvailable() < slot.getEnergyRequirement (stacksUsed)) {
return false;
} else {
return true;
}
}
for (ItemStack reqStk : tmpReq) {
for (IInvSlot slotInv : InventoryIterator.getIterable(new InventoryCopy(builder), ForgeDirection.UNKNOWN)) {
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
continue;
}
ItemStack invStk = slotInv.getStackInSlot();
if (invStk != null && invStk.stackSize > 0 && StackHelper.isCraftingEquivalent(reqStk, invStk, true)) {
try {
stacksUsed.add(slot.useItem(context, reqStk, slotInv));
} 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;
}
}
if (builder.energyAvailable() < slot.getEnergyRequirement (stacksUsed)) {
return false;
} else {
return true;
}
}
public void useRequirements(TileAbstractBuilder builder, BuildingSlot slot) {
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
try {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
}
}
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "useRequirements", t);
}
if (context.world ().getWorldInfo().getGameType() == GameType.CREATIVE) {
for (ItemStack s : tmpReq) {
slot.addStackConsumed(s);
}
builder.consumeEnergy(slot.getEnergyRequirement());
return;
}
ListIterator<ItemStack> itr = tmpReq.listIterator();
while (itr.hasNext()) {
ItemStack reqStk = itr.next();
boolean smallStack = reqStk.stackSize == 1;
ItemStack usedStack = reqStk;
for (IInvSlot slotInv : InventoryIterator.getIterable(builder, ForgeDirection.UNKNOWN)) {
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
continue;
}
ItemStack invStk = slotInv.getStackInSlot();
if (invStk != null && invStk.stackSize > 0 && StackHelper.isCraftingEquivalent(reqStk, invStk, true)) {
try {
usedStack = slot.getSchematic().useItem(context, reqStk, slotInv);
slot.addStackConsumed (usedStack);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBuilderBlueprint", "useRequirements", t);
}
if (reqStk.stackSize == 0) {
break;
}
}
}
if (reqStk.stackSize != 0) {
return;
}
if (smallStack) {
itr.set(usedStack); // set to the actual item used.
}
}
builder.consumeEnergy(slot.getEnergyRequirement ());
return;
}
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);
}
}
}
@Override
public void saveBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) {
super.saveBuildStateToNBT(nbt, builder);
int [] entitiesBuiltArr = new int [builtEntities.size()];
int id = 0;
for (Integer i : builtEntities) {
entitiesBuiltArr [id] = i;
id++;
}
nbt.setIntArray("builtEntities", entitiesBuiltArr);
}
@Override
public void loadBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) {
super.loadBuildStateToNBT(nbt, builder);
int [] entitiesBuiltArr = nbt.getIntArray("builtEntities");
for (int i = 0; i < entitiesBuiltArr.length; ++i) {
builtEntities.add(i);
}
}
}