-
Notifications
You must be signed in to change notification settings - Fork 88
/
BuilderTileEntity.java
2211 lines (1971 loc) · 86 KB
/
BuilderTileEntity.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mcjty.rftools.blocks.builder;
import com.mojang.authlib.GameProfile;
import mcjty.lib.container.DefaultSidedInventory;
import mcjty.lib.container.InventoryHelper;
import mcjty.lib.entity.GenericEnergyReceiverTileEntity;
import mcjty.lib.network.Argument;
import mcjty.lib.network.PacketRequestIntegerFromServer;
import mcjty.lib.tools.InventoryTools;
import mcjty.lib.tools.ItemStackTools;
import mcjty.lib.varia.*;
import mcjty.rftools.RFTools;
import mcjty.rftools.blocks.teleporter.TeleportationTools;
import mcjty.rftools.hud.IHudSupport;
import mcjty.rftools.items.builder.ShapeCardItem;
import mcjty.rftools.items.storage.StorageFilterCache;
import mcjty.rftools.items.storage.StorageFilterItem;
import mcjty.rftools.network.PacketGetHudLog;
import mcjty.rftools.network.RFToolsMessages;
import mcjty.rftools.proxy.CommonProxy;
import mcjty.rftools.varia.RFToolsTools;
import mcjty.typed.Type;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fluids.BlockFluidBase;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class BuilderTileEntity extends GenericEnergyReceiverTileEntity implements DefaultSidedInventory, ITickable,
IHudSupport {
public static final String COMPONENT_NAME = "builder";
public static final String CMD_SETMODE = "setMode";
public static final String CMD_SETANCHOR = "setAnchor";
public static final String CMD_SETROTATE = "setRotate";
public static final String CMD_SETSILENT = "setSilent";
public static final String CMD_SETSUPPORT = "setSupport";
public static final String CMD_SETENTITIES = "setEntities";
public static final String CMD_SETLOOP = "setLoop";
public static final String CMD_GETLEVEL = "getLevel";
public static final String CLIENTCMD_GETLEVEL = "getLevel";
private InventoryHelper inventoryHelper = new InventoryHelper(this, BuilderContainer.factory, 2);
public static final int MODE_COPY = 0;
public static final int MODE_MOVE = 1;
public static final int MODE_SWAP = 2;
public static final int MODE_BACK = 3;
public static final int MODE_COLLECT = 4;
public static final String[] MODES = new String[] { "Copy", "Move", "Swap", "Back", "Collect" };
public static final String ROTATE_0 = "0";
public static final String ROTATE_90 = "90";
public static final String ROTATE_180 = "180";
public static final String ROTATE_270 = "270";
public static final int ANCHOR_SW = 0;
public static final int ANCHOR_SE = 1;
public static final int ANCHOR_NW = 2;
public static final int ANCHOR_NE = 3;
private int mode = MODE_COPY;
private int rotate = 0;
private int anchor = ANCHOR_SW;
private boolean silent = false;
private boolean supportMode = false;
private boolean entityMode = false;
private boolean loopMode = false;
// For usage in the gui
private static int currentLevel = 0;
private int collectCounter = BuilderConfiguration.collectTimer;
private int collectXP = 0;
private boolean boxValid = false;
private BlockPos minBox = null;
private BlockPos maxBox = null;
private BlockPos scan = null;
private int projDx;
private int projDy;
private int projDz;
private long lastHudTime = 0;
private List<String> clientHudLog = new ArrayList<>();
private int cardType = ShapeCardItem.CARD_UNKNOWN; // One of the card types out of ShapeCardItem.CARD_...
private StorageFilterCache filterCache = null;
// For chunkloading with the quarry.
private ForgeChunkManager.Ticket ticket = null;
// The currently forced chunk.
private ChunkPos forcedChunk = null;
// Cached set of blocks that we need to build in shaped mode
private Set<BlockPos> cachedBlocks = null;
private ChunkPos cachedChunk = null; // For which chunk are the cachedBlocks valid
// Cached set of blocks that we want to void with the quarry.
private Set<Block> cachedVoidableBlocks = null;
private static FakePlayer harvester = null;
public BuilderTileEntity() {
super(BuilderConfiguration.BUILDER_MAXENERGY, BuilderConfiguration.BUILDER_RECEIVEPERTICK);
}
@Override
protected boolean needsCustomInvWrapper() {
return true;
}
private static FakePlayer getHarvester() {
if (harvester == null) {
harvester = FakePlayerFactory.get(DimensionManager.getWorld(0), new GameProfile(new UUID(111, 333), "rftools_builder"));
}
return harvester;
}
@Override
public EnumFacing getBlockOrientation() {
return BlockTools.getOrientationHoriz(getBlockMetadata());
}
@Override
public boolean isBlockAboveAir() {
return getWorld().isAirBlock(pos.up());
}
@Override
public List<String> getClientLog() {
return clientHudLog;
}
public List<String> getHudLog() {
List<String> list = new ArrayList<>();
list.add(TextFormatting.BLUE + "Mode:");
if (isShapeCard()) {
switch (getCardType()) {
case ShapeCardItem.CARD_VOID:
list.add(" Void mode");
break;
case ShapeCardItem.CARD_PUMP:
list.add(" Pump");
break;
case ShapeCardItem.CARD_PUMP_CLEAR:
list.add(" Pump");
list.add(" (clearing)");
break;
case ShapeCardItem.CARD_QUARRY_FORTUNE:
list.add(" Fortune quarry");
break;
case ShapeCardItem.CARD_QUARRY_CLEAR_FORTUNE:
list.add(" Fortune quarry");
list.add(" (clearing)");
break;
case ShapeCardItem.CARD_QUARRY_SILK:
list.add(" Silktouch quarry");
break;
case ShapeCardItem.CARD_QUARRY_CLEAR_SILK:
list.add(" Silktouch quarry");
list.add(" (clearing)");
break;
case ShapeCardItem.CARD_QUARRY:
list.add(" Normal quarry");
break;
case ShapeCardItem.CARD_QUARRY_CLEAR:
list.add(" Normal quarry");
list.add(" (clearing)");
break;
case ShapeCardItem.CARD_SHAPE:
list.add(" Shape card");
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
if (ItemStackTools.isValid(shapeCard)) {
ShapeCardItem.Shape shape = ShapeCardItem.getShape(shapeCard);
if (shape != null) {
list.add(" " + shape.getDescription());
}
}
break;
}
} else {
list.add(" Space card: " + new String[]{"copy", "move", "swap", "back", "collect"}[mode]);
}
if (scan != null) {
list.add(TextFormatting.BLUE + "Progress:");
list.add(" Y level: " + scan.getY());
int minChunkX = minBox.getX() >> 4;
int minChunkZ = minBox.getZ() >> 4;
int maxChunkX = maxBox.getX() >> 4;
int maxChunkZ = maxBox.getZ() >> 4;
int curX = scan.getX() >> 4;
int curZ = scan.getZ() >> 4;
int totChunks = (maxChunkX-minChunkX+1) * (maxChunkZ-minChunkZ+1);
int curChunk = (curZ-minChunkZ) * (maxChunkX-minChunkX) + curX-minChunkX;
list.add(" Chunk: " + curChunk + " of " + totChunks);
}
return list;
}
@Override
public BlockPos getBlockPos() {
return getPos();
}
@Override
public long getLastUpdateTime() {
return lastHudTime;
}
@Override
public void setLastUpdateTime(long t) {
lastHudTime = t;
}
private boolean isShapeCard() {
ItemStack itemStack = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
if (ItemStackTools.isEmpty(itemStack)) {
return false;
}
return itemStack.getItem() == BuilderSetup.shapeCardItem;
}
private NBTTagCompound hasCard() {
ItemStack itemStack = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
if (ItemStackTools.isEmpty(itemStack)) {
return null;
}
return itemStack.getTagCompound();
}
private void makeSupportBlocksShaped() {
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
BlockPos dimension = ShapeCardItem.getClampedDimension(shapeCard, BuilderConfiguration.maxBuilderDimension);
BlockPos offset = ShapeCardItem.getClampedOffset(shapeCard, BuilderConfiguration.maxBuilderOffset);
ShapeCardItem.Shape shape = ShapeCardItem.getShape(shapeCard);
List<BlockPos> blocks = new ArrayList<>();
ShapeCardItem.composeShape(shape.makeHollow(), getWorld(), getPos(), dimension, offset, blocks,
BuilderConfiguration.maxBuilderDimension*256* BuilderConfiguration.maxBuilderDimension, false, null);
for (BlockPos p : blocks) {
if (getWorld().isAirBlock(p)) {
getWorld().setBlockState(p, BuilderSetup.supportBlock.getDefaultState().withProperty(SupportBlock.STATUS, SupportBlock.STATUS_OK), 3);
}
}
}
private void makeSupportBlocks() {
if (isShapeCard()) {
makeSupportBlocksShaped();
return;
}
SpaceChamberRepository.SpaceChamberChannel chamberChannel = calculateBox();
if (chamberChannel != null) {
int dimension = chamberChannel.getDimension();
World world = DimensionManager.getWorld(dimension);
if (world == null) {
return;
}
BlockPos.MutableBlockPos src = new BlockPos.MutableBlockPos();
BlockPos.MutableBlockPos dest = new BlockPos.MutableBlockPos();
for (int x = minBox.getX() ; x <= maxBox.getX() ; x++) {
for (int y = minBox.getY() ; y <= maxBox.getY() ; y++) {
for (int z = minBox.getZ() ; z <= maxBox.getZ() ; z++) {
src.setPos(x, y, z);
sourceToDest(src, dest);
IBlockState srcState = world.getBlockState(src);
Block srcBlock = srcState.getBlock();
IBlockState dstState = world.getBlockState(dest);
Block dstBlock = dstState.getBlock();
int error = SupportBlock.STATUS_OK;
if (mode != MODE_COPY) {
TileEntity srcTileEntity = world.getTileEntity(src);
TileEntity dstTileEntity = getWorld().getTileEntity(dest);
int error1 = isMovable(world, src, srcBlock, srcTileEntity);
int error2 = isMovable(getWorld(), dest, dstBlock, dstTileEntity);
error = Math.max(error1, error2);
}
if (isEmpty(srcState, srcBlock) && !isEmpty(dstState, dstBlock)) {
getWorld().setBlockState(src, BuilderSetup.supportBlock.getDefaultState().withProperty(SupportBlock.STATUS, error), 3);
}
if (isEmpty(dstState, dstBlock) && !isEmpty(srcState, srcBlock)) {
getWorld().setBlockState(dest, BuilderSetup.supportBlock.getDefaultState().withProperty(SupportBlock.STATUS, error), 3);
}
}
}
}
}
}
private void clearSupportBlocksShaped() {
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
BlockPos dimension = ShapeCardItem.getClampedDimension(shapeCard, BuilderConfiguration.maxBuilderDimension);
BlockPos offset = ShapeCardItem.getClampedOffset(shapeCard, BuilderConfiguration.maxBuilderOffset);
ShapeCardItem.Shape shape = ShapeCardItem.getShape(shapeCard);
List<BlockPos> blocks = new ArrayList<BlockPos>();
ShapeCardItem.composeShape(shape.makeHollow(), getWorld(), getPos(), dimension, offset, blocks, BuilderConfiguration.maxSpaceChamberDimension* BuilderConfiguration.maxSpaceChamberDimension* BuilderConfiguration.maxSpaceChamberDimension, false, null);
for (BlockPos block : blocks) {
if (getWorld().getBlockState(block).getBlock() == BuilderSetup.supportBlock) {
getWorld().setBlockToAir(block);
}
}
}
public void clearSupportBlocks() {
if (getWorld().isRemote) {
// Don't do anything on the client.
return;
}
if (isShapeCard()) {
clearSupportBlocksShaped();
return;
}
SpaceChamberRepository.SpaceChamberChannel chamberChannel = calculateBox();
if (chamberChannel != null) {
int dimension = chamberChannel.getDimension();
World world = DimensionManager.getWorld(dimension);
BlockPos.MutableBlockPos src = new BlockPos.MutableBlockPos();
BlockPos.MutableBlockPos dest = new BlockPos.MutableBlockPos();
for (int x = minBox.getX() ; x <= maxBox.getX() ; x++) {
for (int y = minBox.getY() ; y <= maxBox.getY() ; y++) {
for (int z = minBox.getZ() ; z <= maxBox.getZ() ; z++) {
src.setPos(x, y, z);
if (world != null) {
Block srcBlock = world.getBlockState(src).getBlock();
if (srcBlock == BuilderSetup.supportBlock) {
world.setBlockToAir(src);
}
}
sourceToDest(src, dest);
Block dstBlock = getWorld().getBlockState(dest).getBlock();
if (dstBlock == BuilderSetup.supportBlock) {
getWorld().setBlockToAir(dest);
}
}
}
}
}
}
public boolean hasLoopMode() {
return loopMode;
}
public void setLoopMode(boolean loopMode) {
this.loopMode = loopMode;
markDirtyClient();
}
public boolean hasEntityMode() {
return entityMode;
}
public void setEntityMode(boolean entityMode) {
this.entityMode = entityMode;
markDirtyClient();
}
public boolean hasSupportMode() {
return supportMode;
}
public void setSupportMode(boolean supportMode) {
this.supportMode = supportMode;
if (supportMode) {
makeSupportBlocks();
} else {
clearSupportBlocks();
}
markDirtyClient();
}
public boolean isSilent() {
return silent;
}
public void setSilent(boolean silent) {
this.silent = silent;
markDirtyClient();
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
if (mode != this.mode) {
this.mode = mode;
restartScan();
markDirtyClient();
}
}
public void resetBox() {
boxValid = false;
}
public int getAnchor() {
return anchor;
}
public void setAnchor(int anchor) {
if (supportMode) {
clearSupportBlocks();
}
boxValid = false;
this.anchor = anchor;
if (isShapeCard()) {
// If there is a shape card we modify it for the new settings.
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
BlockPos dimension = ShapeCardItem.getDimension(shapeCard);
BlockPos minBox = positionBox(dimension);
int dx = dimension.getX();
int dy = dimension.getY();
int dz = dimension.getZ();
BlockPos offset = new BlockPos(minBox.getX() + (int)Math.ceil(dx / 2), minBox.getY() + (int)Math.ceil(dy / 2), minBox.getZ() + (int)Math.ceil(dz / 2));
NBTTagCompound tagCompound = shapeCard.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
shapeCard.setTagCompound(tagCompound);
}
tagCompound.setInteger("offsetX", offset.getX());
tagCompound.setInteger("offsetY", offset.getY());
tagCompound.setInteger("offsetZ", offset.getZ());
}
if (supportMode) {
makeSupportBlocks();
}
markDirtyClient();
}
// Give a dimension, return a min coordinate of the box right in front of the builder
private BlockPos positionBox(BlockPos dimension) {
IBlockState state = getWorld().getBlockState(getPos());
EnumFacing direction = state.getValue(BuilderSetup.builderBlock.FACING_HORIZ);
int spanX = dimension.getX();
int spanY = dimension.getY();
int spanZ = dimension.getZ();
int x = 0;
int y;
int z = 0;
y = - ((anchor == ANCHOR_NE || anchor == ANCHOR_NW) ? spanY - 1 : 0);
switch (direction) {
case SOUTH:
x = - ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanX - 1 : 0);
z = - spanZ;
break;
case NORTH:
x = 1 - spanX + ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanX - 1 : 0);
z = 1;
break;
case WEST:
x = 1;
z = - ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanZ - 1 : 0);
break;
case EAST:
x = - spanX;
z = - ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? 0 : spanZ - 1);
break;
case DOWN:
case UP:
default:
break;
}
return new BlockPos(x, y, z);
}
public int getRotate() {
return rotate;
}
public void setRotate(int rotate) {
if (supportMode) {
clearSupportBlocks();
}
boxValid = false;
this.rotate = rotate;
if (supportMode) {
makeSupportBlocks();
}
markDirtyClient();
}
@Override
public void setPowerInput(int powered) {
boolean changed = powerLevel != powered;
super.setPowerInput(powered);
if (changed) {
if (loopMode || (powered > 0 && scan == null)) {
restartScan();
}
}
}
private void createProjection(SpaceChamberRepository.SpaceChamberChannel chamberChannel) {
BlockPos minC = rotate(chamberChannel.getMinCorner());
BlockPos maxC = rotate(chamberChannel.getMaxCorner());
BlockPos minCorner = new BlockPos(Math.min(minC.getX(), maxC.getX()), Math.min(minC.getY(), maxC.getY()), Math.min(minC.getZ(), maxC.getZ()));
BlockPos maxCorner = new BlockPos(Math.max(minC.getX(), maxC.getX()), Math.max(minC.getY(), maxC.getY()), Math.max(minC.getZ(), maxC.getZ()));
IBlockState state = getWorld().getBlockState(getPos());
EnumFacing direction = state.getValue(BuilderSetup.builderBlock.FACING_HORIZ);
int xCoord = getPos().getX();
int yCoord = getPos().getY();
int zCoord = getPos().getZ();
int spanX = maxCorner.getX() - minCorner.getX();
int spanY = maxCorner.getY() - minCorner.getY();
int spanZ = maxCorner.getZ() - minCorner.getZ();
switch (direction) {
case SOUTH:
projDx = xCoord + EnumFacing.NORTH.getDirectionVec().getX() - minCorner.getX() - ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanX : 0);
projDz = zCoord + EnumFacing.NORTH.getDirectionVec().getZ() - minCorner.getZ() - spanZ;
break;
case NORTH:
projDx = xCoord + EnumFacing.SOUTH.getDirectionVec().getX() - minCorner.getX() - spanX + ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanX : 0);
projDz = zCoord + EnumFacing.SOUTH.getDirectionVec().getZ() - minCorner.getZ();
break;
case WEST:
projDx = xCoord + EnumFacing.EAST.getDirectionVec().getX() - minCorner.getX();
projDz = zCoord + EnumFacing.EAST.getDirectionVec().getZ() - minCorner.getZ() - ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanZ : 0);
break;
case EAST:
projDx = xCoord + EnumFacing.WEST.getDirectionVec().getX() - minCorner.getX() - spanX;
projDz = zCoord + EnumFacing.WEST.getDirectionVec().getZ() - minCorner.getZ() - spanZ + ((anchor == ANCHOR_NE || anchor == ANCHOR_SE) ? spanZ : 0);
break;
case DOWN:
case UP:
default:
break;
}
projDy = yCoord - minCorner.getY() - ((anchor == ANCHOR_NE || anchor == ANCHOR_NW) ? spanY : 0);
}
private void calculateBox(NBTTagCompound cardCompound) {
int channel = cardCompound.getInteger("channel");
SpaceChamberRepository repository = SpaceChamberRepository.getChannels(getWorld());
SpaceChamberRepository.SpaceChamberChannel chamberChannel = repository.getChannel(channel);
BlockPos minCorner = chamberChannel.getMinCorner();
BlockPos maxCorner = chamberChannel.getMaxCorner();
if (minCorner == null || maxCorner == null) {
return;
}
if (boxValid) {
// Double check if the box is indeed still valid.
if (minCorner.equals(minBox) && maxCorner.equals(maxBox)) {
return;
}
}
boxValid = true;
cardType = ShapeCardItem.CARD_SPACE;
createProjection(chamberChannel);
minBox = minCorner;
maxBox = maxCorner;
restartScan();
}
private void checkStateServerShaped() {
float factor = getInfusedFactor();
for (int i = 0 ; i < BuilderConfiguration.quarryBaseSpeed + (factor * BuilderConfiguration.quarryInfusionSpeedFactor) ; i++) {
if (scan != null) {
handleBlockShaped();
}
}
}
@Override
public InventoryHelper getInventoryHelper() {
return inventoryHelper;
}
@Override
public void update() {
if (!getWorld().isRemote) {
checkStateServer();
}
}
private void checkStateServer() {
if (powerLevel == 0 && loopMode) {
return;
}
if (scan == null) {
return;
}
if (isShapeCard()) {
if (powerLevel == 0) {
chunkUnload();
return;
}
checkStateServerShaped();
return;
}
SpaceChamberRepository.SpaceChamberChannel chamberChannel = calculateBox();
if (chamberChannel == null) {
scan = null;
markDirty();
return;
}
int dimension = chamberChannel.getDimension();
World world = DimensionManager.getWorld(dimension);
if (world == null) {
// The other location must be loaded.
return;
}
if (mode == MODE_COLLECT) {
collectItems(world);
} else {
float factor = getInfusedFactor();
for (int i = 0; i < 2 + (factor * 40); i++) {
if (scan != null) {
handleBlock(world);
}
}
}
}
private void collectItems(World world) {
// Collect item mode
collectCounter--;
if (collectCounter > 0) {
return;
}
collectCounter = BuilderConfiguration.collectTimer;
if (!loopMode) {
scan = null;
}
int rf = getEnergyStored(EnumFacing.DOWN);
float area = (maxBox.getX() - minBox.getX() + 1) * (maxBox.getY() - minBox.getY() + 1) * (maxBox.getZ() - minBox.getZ() + 1);
float infusedFactor = (4.0f - getInfusedFactor()) / 4.0f;
int rfNeeded = (int) (BuilderConfiguration.collectRFPerTickPerArea * area * infusedFactor) * BuilderConfiguration.collectTimer;
if (rfNeeded > rf) {
// Not enough energy.
return;
}
consumeEnergy(rfNeeded);
AxisAlignedBB bb = new AxisAlignedBB(minBox.getX() - .8, minBox.getY() - .8, minBox.getZ() - .8, maxBox.getX() + .8, maxBox.getY() + .8, maxBox.getZ() + .8);
List<Entity> items = world.getEntitiesWithinAABB(Entity.class, bb);
for (Entity entity : items) {
if (entity instanceof EntityItem) {
if (collectItem(world, infusedFactor, (EntityItem) entity)) {
return;
}
} else if (entity instanceof EntityXPOrb) {
if (collectXP(world, infusedFactor, (EntityXPOrb) entity)) {
return;
}
}
}
}
private boolean collectXP(World world, float infusedFactor, EntityXPOrb orb) {
int rf;
int rfNeeded;
int xp = orb.getXpValue();
rf = getEnergyStored(EnumFacing.DOWN);
rfNeeded = (int) (BuilderConfiguration.collectRFPerXP * infusedFactor * xp);
if (rfNeeded > rf) {
// Not enough energy.
return true;
}
collectXP += xp;
int bottles = collectXP / 7;
if (bottles > 0) {
if (ItemStackTools.isEmpty(insertItem(new ItemStack(Items.EXPERIENCE_BOTTLE, bottles)))) {
collectXP = collectXP % 7;
world.removeEntity(orb);
consumeEnergy(rfNeeded);
} else {
collectXP = 0;
}
}
return false;
}
private boolean collectItem(World world, float infusedFactor, EntityItem item) {
int rf;
int rfNeeded;
ItemStack stack = item.getEntityItem();
rf = getEnergyStored(EnumFacing.DOWN);
rfNeeded = (int) (BuilderConfiguration.collectRFPerItem * infusedFactor) * ItemStackTools.getStackSize(stack);
if (rfNeeded > rf) {
// Not enough energy.
return true;
}
consumeEnergy(rfNeeded);
world.removeEntity(item);
stack = insertItem(stack);
if (ItemStackTools.isValid(stack)) {
BlockPos position = item.getPosition();
EntityItem entityItem = new EntityItem(getWorld(), position.getX(), position.getY(), position.getZ(), stack);
mcjty.lib.tools.WorldTools.spawnEntity(getWorld(), entityItem);
}
return false;
}
private void calculateBoxShaped() {
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
if (ItemStackTools.isEmpty(shapeCard)) {
return;
}
BlockPos dimension = ShapeCardItem.getClampedDimension(shapeCard, BuilderConfiguration.maxBuilderDimension);
BlockPos offset = ShapeCardItem.getClampedOffset(shapeCard, BuilderConfiguration.maxBuilderOffset);
BlockPos minCorner = ShapeCardItem.getMinCorner(getPos(), dimension, offset);
BlockPos maxCorner = ShapeCardItem.getMaxCorner(getPos(), dimension, offset);
if (minCorner.getY() < 0) {
minCorner = new BlockPos(minCorner.getX(), 0, minCorner.getZ());
} else if (minCorner.getY() > 255) {
minCorner = new BlockPos(minCorner.getX(), 255, minCorner.getZ());
}
if (maxCorner.getY() < 0) {
maxCorner = new BlockPos(maxCorner.getX(), 0, maxCorner.getZ());
} else if (maxCorner.getY() > 255) {
maxCorner = new BlockPos(maxCorner.getX(), 255, maxCorner.getZ());
}
if (boxValid) {
// Double check if the box is indeed still valid.
if (minCorner.equals(minBox) && maxCorner.equals(maxBox)) {
return;
}
}
boxValid = true;
cardType = shapeCard.getItemDamage();
cachedBlocks = null;
cachedChunk = null;
cachedVoidableBlocks = null;
minBox = minCorner;
maxBox = maxCorner;
restartScan();
}
private SpaceChamberRepository.SpaceChamberChannel calculateBox() {
NBTTagCompound tc = hasCard();
if (tc == null) {
return null;
}
int channel = tc.getInteger("channel");
if (channel == -1) {
return null;
}
SpaceChamberRepository repository = SpaceChamberRepository.getChannels(getWorld());
SpaceChamberRepository.SpaceChamberChannel chamberChannel = repository.getChannel(channel);
if (chamberChannel == null) {
return null;
}
calculateBox(tc);
if (!boxValid) {
return null;
}
return chamberChannel;
}
private Set<BlockPos> getCachedBlocks(ChunkPos chunk) {
if ((chunk != null && !chunk.equals(cachedChunk)) || (chunk == null && cachedChunk != null)) {
cachedBlocks = null;
}
if (cachedBlocks == null) {
cachedBlocks = new HashSet<>();
ItemStack shapeCard = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
ShapeCardItem.Shape shape = ShapeCardItem.getShape(shapeCard);
BlockPos dimension = ShapeCardItem.getClampedDimension(shapeCard, BuilderConfiguration.maxBuilderDimension);
BlockPos offset = ShapeCardItem.getClampedOffset(shapeCard, BuilderConfiguration.maxBuilderOffset);
ShapeCardItem.composeShape(shape, getWorld(), getPos(), dimension, offset, cachedBlocks,
BuilderConfiguration.maxSpaceChamberDimension * BuilderConfiguration.maxSpaceChamberDimension * BuilderConfiguration.maxSpaceChamberDimension,
!ShapeCardItem.isNormalShapeCard(shapeCard), chunk);
cachedChunk = chunk;
}
return cachedBlocks;
}
private void handleBlockShaped() {
for (int i = 0 ; i < 100 ; i++) {
if (scan == null) {
return;
}
if (getCachedBlocks(new ChunkPos(scan.getX() >> 4, scan.getZ() >> 4)).contains(scan)) {
if (!handleSingleBlock()) {
nextLocation();
}
return;
} else {
nextLocation();
}
}
}
private int getCardType() {
if (cardType == ShapeCardItem.CARD_UNKNOWN) {
ItemStack card = inventoryHelper.getStackInSlot(BuilderContainer.SLOT_TAB);
if (ItemStackTools.isValid(card)) {
cardType = card.getItemDamage();
}
}
return cardType;
}
// Return true if we have to wait at this spot.
private boolean handleSingleBlock() {
BlockPos srcPos = scan;
int sx = scan.getX();
int sy = scan.getY();
int sz = scan.getZ();
if (!chunkLoad(sx, sz)) {
// The chunk is not available and we could not chunkload it. We have to wait.
return true;
}
int rfNeeded;
switch (getCardType()) {
case ShapeCardItem.CARD_PUMP:
case ShapeCardItem.CARD_PUMP_CLEAR:
rfNeeded = BuilderConfiguration.builderRfPerLiquid;
break;
case ShapeCardItem.CARD_VOID:
rfNeeded = (int) (BuilderConfiguration.builderRfPerQuarry * BuilderConfiguration.voidShapeCardFactor);
break;
case ShapeCardItem.CARD_QUARRY_FORTUNE:
case ShapeCardItem.CARD_QUARRY_CLEAR_FORTUNE:
rfNeeded = (int) (BuilderConfiguration.builderRfPerQuarry * BuilderConfiguration.fortunequarryShapeCardFactor);
break;
case ShapeCardItem.CARD_QUARRY_SILK:
case ShapeCardItem.CARD_QUARRY_CLEAR_SILK:
rfNeeded = (int) (BuilderConfiguration.builderRfPerQuarry * BuilderConfiguration.silkquarryShapeCardFactor);
break;
case ShapeCardItem.CARD_QUARRY:
case ShapeCardItem.CARD_QUARRY_CLEAR:
rfNeeded = BuilderConfiguration.builderRfPerQuarry;
break;
case ShapeCardItem.CARD_SHAPE:
rfNeeded = BuilderConfiguration.builderRfPerOperation;
break;
default:
rfNeeded = 0;
break;
}
Block block = null;
if (getCardType() != ShapeCardItem.CARD_SHAPE) {
IBlockState state = getWorld().getBlockState(srcPos);
block = state.getBlock();
if (!isEmpty(state, block)) {
float hardness;
if (isFluidBlock(block)) {
hardness = 1.0f;
} else {
if (getCachedVoidableBlocks().contains(block)) {
rfNeeded = (int) (BuilderConfiguration.builderRfPerQuarry * BuilderConfiguration.voidShapeCardFactor);
}
hardness = block.getBlockHardness(state, getWorld(), srcPos);
}
rfNeeded *= (int) ((hardness + 1) * 2);
}
}
rfNeeded = (int) (rfNeeded * (3.0f - getInfusedFactor()) / 3.0f);
if (rfNeeded > getEnergyStored(EnumFacing.DOWN)) {
// Not enough energy.
return true;
}
switch (getCardType()) {
case ShapeCardItem.CARD_PUMP:
case ShapeCardItem.CARD_PUMP_CLEAR:
return pumpBlock(rfNeeded, srcPos, block);
case ShapeCardItem.CARD_VOID:
return voidBlock(rfNeeded, srcPos, block);
case ShapeCardItem.CARD_QUARRY:
case ShapeCardItem.CARD_QUARRY_CLEAR:
return quarryBlock(rfNeeded, srcPos, block);
case ShapeCardItem.CARD_QUARRY_FORTUNE:
case ShapeCardItem.CARD_QUARRY_CLEAR_FORTUNE:
return quarryBlock(rfNeeded, srcPos, block);
case ShapeCardItem.CARD_QUARRY_SILK:
case ShapeCardItem.CARD_QUARRY_CLEAR_SILK:
return silkQuarryBlock(rfNeeded, srcPos, block);
case ShapeCardItem.CARD_SHAPE:
return buildBlock(rfNeeded, srcPos);
}
return true;
}
private boolean buildBlock(int rfNeeded, BlockPos srcPos) {
if (isEmptyOrReplacable(getWorld(), srcPos)) {
ItemStack stack = consumeBlock(getWorld(), srcPos, null);
if (ItemStackTools.isEmpty(stack)) {
return true; // We could not find a block. Wait
}
FakePlayer fakePlayer = getHarvester();
IBlockState newState = placeBlockAt(getWorld(), srcPos, stack, null, fakePlayer);
if (!silent) {
SoundTools.playSound(getWorld(), newState.getBlock().getSoundType().getBreakSound(), srcPos.getX(), srcPos.getY(), srcPos.getZ(), 1.0f, 1.0f);
}
consumeEnergy(rfNeeded);
}
return false;
}
private IBlockState placeBlockAt(World world, BlockPos pos, ItemStack stack, @Nullable Integer origMeta, FakePlayer fakePlayer) {
Item item = stack.getItem();
if (item instanceof ItemBlock) {
ItemBlock itemBlock = (ItemBlock) item;
if (origMeta == null) {
origMeta = itemBlock.getDamage(stack);
}
IBlockState newState = itemBlock.block.getStateFromMeta(origMeta);
itemBlock.placeBlockAt(stack, fakePlayer, world, pos, EnumFacing.UP, 0, 0, 0, newState);
return newState;
} else {
InventoryTools.onItemUseWithStack(item, stack, fakePlayer, world, pos.down(), EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0);
return world.getBlockState(pos);
}
}