-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathBlockPipe.java
More file actions
831 lines (731 loc) · 36.7 KB
/
BlockPipe.java
File metadata and controls
831 lines (731 loc) · 36.7 KB
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
package gregtech.api.pipenet.block;
import gregtech.api.block.BuiltInRenderBlock;
import gregtech.api.cover.Cover;
import gregtech.api.cover.CoverHolder;
import gregtech.api.cover.CoverRayTracer;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.pipenet.IBlockAppearance;
import gregtech.api.pipenet.PipeNet;
import gregtech.api.pipenet.WorldPipeNet;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.pipenet.tile.PipeCoverableImplementation;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.pipe.PipeRenderProperties;
import gregtech.client.renderer.pipe.cover.CoverRendererPackage;
import gregtech.common.ConfigHolder;
import gregtech.common.blocks.BlockFrame;
import gregtech.common.blocks.MetaBlocks;
import gregtech.common.items.MetaItems;
import gregtech.integration.ctm.IFacadeWrapper;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.raytracer.IndexedCuboid6;
import codechicken.lib.raytracer.RayTracer;
import codechicken.lib.vec.Cuboid6;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Random;
import static gregtech.api.metatileentity.MetaTileEntity.FULL_CUBE_COLLISION;
@SuppressWarnings("deprecation")
public abstract class BlockPipe<PipeType extends Enum<PipeType> & IPipeType<NodeDataType>, NodeDataType,
WorldPipeNetType extends WorldPipeNet<NodeDataType, ? extends PipeNet<NodeDataType>>> extends BuiltInRenderBlock
implements ITileEntityProvider, IFacadeWrapper, IBlockAppearance {
public static final PropertyBool NORTH = PropertyBool.create("north");
public static final PropertyBool EAST = PropertyBool.create("east");
public static final PropertyBool SOUTH = PropertyBool.create("south");
public static final PropertyBool WEST = PropertyBool.create("west");
public static final PropertyBool UP = PropertyBool.create("up");
public static final PropertyBool DOWN = PropertyBool.create("down");
public static final EnumMap<EnumFacing, PropertyBool> FACINGS = buildFacings();
private static @NotNull EnumMap<EnumFacing, PropertyBool> buildFacings() {
EnumMap<EnumFacing, PropertyBool> map = new EnumMap<>(EnumFacing.class);
map.put(EnumFacing.NORTH, NORTH);
map.put(EnumFacing.EAST, EAST);
map.put(EnumFacing.SOUTH, SOUTH);
map.put(EnumFacing.WEST, WEST);
map.put(EnumFacing.UP, UP);
map.put(EnumFacing.DOWN, DOWN);
return map;
}
public static final PropertyBool FRAMED = PropertyBool.create("framed");
protected final ThreadLocal<IPipeTile<PipeType, NodeDataType>> tileEntities = new ThreadLocal<>();
public BlockPipe() {
super(net.minecraft.block.material.Material.IRON);
setTranslationKey("pipe");
setSoundType(SoundType.METAL);
setHardness(2.0f);
setResistance(3.0f);
setLightOpacity(0);
disableStats();
}
public static Cuboid6 getSideBox(EnumFacing side, float thickness) {
float min = (1.0f - thickness) / 2.0f, max = min + thickness;
float faceMin = 0f, faceMax = 1f;
if (side == null)
return new Cuboid6(min, min, min, max, max, max);
Cuboid6 cuboid;
switch (side) {
case WEST:
cuboid = new Cuboid6(faceMin, min, min, min, max, max);
break;
case EAST:
cuboid = new Cuboid6(max, min, min, faceMax, max, max);
break;
case NORTH:
cuboid = new Cuboid6(min, min, faceMin, max, max, min);
break;
case SOUTH:
cuboid = new Cuboid6(min, min, max, max, max, faceMax);
break;
case UP:
cuboid = new Cuboid6(min, max, min, max, faceMax, max);
break;
case DOWN:
cuboid = new Cuboid6(min, faceMin, min, max, min, max);
break;
default:
cuboid = new Cuboid6(min, min, min, max, max, max);
}
return cuboid;
}
/**
* @return the pipe cuboid for that side but with a offset one the facing with the cover to prevent z fighting.
*/
public static Cuboid6 getCoverSideBox(EnumFacing side, float thickness) {
Cuboid6 cuboid = getSideBox(side, thickness);
if (side != null)
cuboid.setSide(side, side.getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE ? 0.001 : 0.999);
return cuboid;
}
public abstract Class<PipeType> getPipeTypeClass();
public abstract WorldPipeNetType getWorldPipeNet(World world);
public abstract TileEntityPipeBase<PipeType, NodeDataType> createNewTileEntity(boolean supportsTicking);
public abstract NodeDataType createProperties(IPipeTile<PipeType, NodeDataType> pipeTile);
public abstract NodeDataType createItemProperties(ItemStack itemStack);
public abstract ItemStack getDropItem(IPipeTile<PipeType, NodeDataType> pipeTile);
@NotNull
@Override
@SuppressWarnings({ "deprecation", "unchecked" })
public ItemStack getItem(@NotNull World world, @NotNull BlockPos pos, @NotNull IBlockState state) {
var te = world.getTileEntity(pos);
if (!(te instanceof IPipeTile<?, ?>pipeTile)) return ItemStack.EMPTY;
return getDropItem((IPipeTile<PipeType, NodeDataType>) pipeTile);
}
protected abstract NodeDataType getFallbackType();
// TODO this has no reason to need an ItemStack parameter
public abstract PipeType getPipeType();
public abstract void setTileEntityData(TileEntityPipeBase<PipeType, NodeDataType> pipeTile, ItemStack itemStack);
@Override
public abstract void getSubBlocks(@NotNull CreativeTabs itemIn, @NotNull NonNullList<ItemStack> items);
@Override
public void breakBlock(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null) {
pipeTile.getCoverableImplementation().dropAllCovers();
tileEntities.set(pipeTile);
}
super.breakBlock(worldIn, pos, state);
getWorldPipeNet(worldIn).removeNode(pos);
}
@Override
public void onBlockAdded(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state) {
worldIn.scheduleUpdate(pos, this, 1);
}
@Override
public void updateTick(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull Random rand) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null) {
int activeConnections = pipeTile.getConnections();
boolean isActiveNode = activeConnections != 0;
getWorldPipeNet(worldIn).addNode(pos, createProperties(pipeTile), 0, activeConnections, isActiveNode);
onActiveModeChange(worldIn, pos, isActiveNode, true);
}
}
@Override
public void onBlockPlacedBy(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull EntityLivingBase placer, @NotNull ItemStack stack) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null) {
setTileEntityData((TileEntityPipeBase<PipeType, NodeDataType>) pipeTile, stack);
// Color pipes/cables on place if holding spray can in off-hand
if (placer instanceof EntityPlayer) {
ItemStack offhand = placer.getHeldItemOffhand();
for (int i = 0; i < EnumDyeColor.values().length; i++) {
if (offhand.isItemEqual(MetaItems.SPRAY_CAN_DYES[i].getStackForm())) {
MetaItems.SPRAY_CAN_DYES[i].getBehaviours().get(0).onItemUse((EntityPlayer) placer, worldIn,
pos, EnumHand.OFF_HAND, EnumFacing.UP, 0, 0, 0);
break;
}
}
}
}
}
@Override
public void neighborChanged(@NotNull IBlockState state, @NotNull World worldIn, @NotNull BlockPos pos,
@NotNull Block blockIn, @NotNull BlockPos fromPos) {
if (worldIn.isRemote) return;
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null) {
pipeTile.getCoverableImplementation().updateInputRedstoneSignals();
EnumFacing facing = GTUtility.getFacingToNeighbor(pos, fromPos);
if (facing == null) return;
pipeTile.onNeighborChanged(facing);
if (!ConfigHolder.machines.gt6StylePipesCables) {
boolean open = pipeTile.isConnected(facing);
boolean canConnect = pipeTile.getCoverableImplementation().getCoverAtSide(facing) != null ||
canConnect(pipeTile, facing);
if (!open && canConnect && state.getBlock() != blockIn)
pipeTile.setConnection(facing, true, false);
if (open && !canConnect)
pipeTile.setConnection(facing, false, false);
updateActiveNodeStatus(worldIn, pos, pipeTile);
}
}
}
@Override
public void onNeighborChange(@NotNull IBlockAccess world, @NotNull BlockPos pos, @NotNull BlockPos neighbor) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(world, pos);
if (pipeTile != null) {
EnumFacing facing = GTUtility.getFacingToNeighbor(pos, neighbor);
if (facing != null) {
pipeTile.onNeighborChanged(facing);
}
}
}
@Override
public void observedNeighborChange(@NotNull IBlockState observerState, @NotNull World world,
@NotNull BlockPos observerPos, @NotNull Block changedBlock,
@NotNull BlockPos changedBlockPos) {
PipeNet<NodeDataType> net = getWorldPipeNet(world).getNetFromPos(observerPos);
if (net != null) {
net.onNeighbourUpdate(changedBlockPos);
}
}
@Override
public boolean canConnectRedstone(@NotNull IBlockState state, @NotNull IBlockAccess world, @NotNull BlockPos pos,
@Nullable EnumFacing side) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(world, pos);
return pipeTile != null && pipeTile.getCoverableImplementation().canConnectRedstone(side);
}
@Override
public boolean shouldCheckWeakPower(@NotNull IBlockState state, @NotNull IBlockAccess world, @NotNull BlockPos pos,
@NotNull EnumFacing side) {
// The check in World::getRedstonePower in the vanilla code base is reversed. Setting this to false will
// actually cause getWeakPower to be called, rather than prevent it.
return false;
}
@Override
public int getWeakPower(@NotNull IBlockState blockState, @NotNull IBlockAccess blockAccess, @NotNull BlockPos pos,
@NotNull EnumFacing side) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(blockAccess, pos);
return pipeTile == null ? 0 : pipeTile.getCoverableImplementation().getOutputRedstoneSignal(side.getOpposite());
}
public void updateActiveNodeStatus(@NotNull World worldIn, BlockPos pos,
IPipeTile<PipeType, NodeDataType> pipeTile) {
if (worldIn.isRemote) return;
PipeNet<NodeDataType> pipeNet = getWorldPipeNet(worldIn).getNetFromPos(pos);
if (pipeNet != null && pipeTile != null) {
int activeConnections = pipeTile.getConnections(); // remove blocked connections
boolean isActiveNodeNow = activeConnections != 0;
boolean modeChanged = pipeNet.markNodeAsActive(pos, isActiveNodeNow);
if (modeChanged) {
onActiveModeChange(worldIn, pos, isActiveNodeNow, false);
}
}
}
@Nullable
@Override
public TileEntity createNewTileEntity(@NotNull World worldIn, int meta) {
return createNewTileEntity(false);
}
/**
* Can be used to update tile entity to tickable when node becomes active
* usable for fluid pipes, as example
*/
protected void onActiveModeChange(World world, BlockPos pos, boolean isActiveNow, boolean isInitialChange) {}
@NotNull
@Override
public ItemStack getPickBlock(@NotNull IBlockState state, @NotNull RayTraceResult target, @NotNull World world,
@NotNull BlockPos pos, @NotNull EntityPlayer player) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(world, pos);
if (pipeTile == null) {
return ItemStack.EMPTY;
}
if (target instanceof CuboidRayTraceResult result) {
if (result.cuboid6.data instanceof CoverRayTracer.CoverSideData coverSideData) {
EnumFacing coverSide = coverSideData.side;
Cover cover = pipeTile.getCoverableImplementation().getCoverAtSide(coverSide);
return cover == null ? ItemStack.EMPTY : cover.getPickItem();
}
}
return getDropItem(pipeTile);
}
@Override
public boolean onBlockActivated(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull EntityPlayer playerIn, @NotNull EnumHand hand, @NotNull EnumFacing facing,
float hitX, float hitY, float hitZ) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
CuboidRayTraceResult rayTraceResult = getServerCollisionRayTrace(playerIn, pos, worldIn);
if (rayTraceResult == null || pipeTile == null) {
return false;
}
return onPipeActivated(worldIn, state, pos, playerIn, hand, facing, rayTraceResult, pipeTile);
}
public boolean onPipeActivated(World world, IBlockState state, BlockPos pos, EntityPlayer entityPlayer,
EnumHand hand, EnumFacing side, CuboidRayTraceResult hit,
IPipeTile<PipeType, NodeDataType> pipeTile) {
ItemStack itemStack = entityPlayer.getHeldItem(hand);
if (pipeTile.getFrameMaterial() == null &&
pipeTile instanceof TileEntityPipeBase &&
pipeTile.getPipeType().getThickness() < 1) {
BlockFrame frameBlock = BlockFrame.getFrameBlockFromItem(itemStack);
if (frameBlock != null) {
((TileEntityPipeBase<PipeType, NodeDataType>) pipeTile)
.setFrameMaterial(frameBlock.getGtMaterial(itemStack));
SoundType type = frameBlock.getSoundType(itemStack);
world.playSound(entityPlayer, pos, type.getPlaceSound(), SoundCategory.BLOCKS,
(type.getVolume() + 1.0F) / 2.0F, type.getPitch() * 0.8F);
if (!entityPlayer.capabilities.isCreativeMode) {
itemStack.shrink(1);
}
return true;
}
}
if (itemStack.getItem() instanceof ItemBlockPipe) {
IBlockState blockStateAtSide = world.getBlockState(pos.offset(side));
if (blockStateAtSide.getBlock() instanceof BlockFrame) {
ItemBlockPipe<?, ?> itemBlockPipe = (ItemBlockPipe<?, ?>) itemStack.getItem();
if (itemBlockPipe.blockPipe.getPipeType() == getPipeType()) {
BlockFrame frameBlock = (BlockFrame) blockStateAtSide.getBlock();
boolean wasPlaced = frameBlock.replaceWithFramedPipe(world, pos.offset(side), blockStateAtSide,
entityPlayer, itemStack, side);
if (wasPlaced) {
pipeTile.setConnection(side, true, false);
}
return wasPlaced;
}
}
}
EnumFacing coverSide = CoverRayTracer.traceCoverSide(hit);
if (coverSide == null) {
return activateFrame(world, state, pos, entityPlayer, hand, hit, pipeTile);
}
if (!(hit.cuboid6.data instanceof CoverRayTracer.CoverSideData)) {
switch (onPipeToolUsed(world, pos, itemStack, coverSide, pipeTile, entityPlayer, hand)) {
case SUCCESS -> {
return true;
}
case FAIL -> {
return false;
}
}
}
Cover cover = pipeTile.getCoverableImplementation().getCoverAtSide(coverSide);
if (cover == null) {
return activateFrame(world, state, pos, entityPlayer, hand, hit, pipeTile);
}
if (itemStack.getItem().getToolClasses(itemStack).contains(ToolClasses.SOFT_MALLET)) {
if (cover.onSoftMalletClick(entityPlayer, hand, hit) == EnumActionResult.SUCCESS) {
ToolHelper.damageItem(itemStack, entityPlayer);
ToolHelper.playToolSound(itemStack, entityPlayer);
return true;
}
}
if ((itemStack.isEmpty() && entityPlayer.isSneaking()) ||
itemStack.getItem().getToolClasses(itemStack).contains(ToolClasses.SCREWDRIVER)) {
if (cover.onScrewdriverClick(entityPlayer, hand, hit) == EnumActionResult.SUCCESS) {
if (!itemStack.isEmpty()) {
ToolHelper.damageItem(itemStack, entityPlayer);
ToolHelper.playToolSound(itemStack, entityPlayer);
}
return true;
}
}
if (itemStack.getItem().getToolClasses(itemStack).contains(ToolClasses.CROWBAR)) {
if (!world.isRemote) {
pipeTile.getCoverableImplementation().removeCover(coverSide);
ToolHelper.damageItem(itemStack, entityPlayer);
ToolHelper.playToolSound(itemStack, entityPlayer);
return true;
}
}
EnumActionResult result = cover.onRightClick(entityPlayer, hand, hit);
if (result == EnumActionResult.PASS) {
if (activateFrame(world, state, pos, entityPlayer, hand, hit, pipeTile)) {
return true;
}
return entityPlayer.isSneaking() && entityPlayer.getHeldItemMainhand().isEmpty() &&
cover.onScrewdriverClick(entityPlayer, hand, hit) != EnumActionResult.PASS;
}
return true;
}
private boolean activateFrame(World world, IBlockState state, BlockPos pos, EntityPlayer entityPlayer,
EnumHand hand, CuboidRayTraceResult hit, IPipeTile<PipeType, NodeDataType> pipeTile) {
if (pipeTile.getFrameMaterial() != null &&
!(entityPlayer.getHeldItem(hand).getItem() instanceof ItemBlockPipe)) {
BlockFrame blockFrame = MetaBlocks.FRAMES.get(pipeTile.getFrameMaterial());
return blockFrame.onBlockActivated(world, pos, state, entityPlayer, hand, hit.sideHit, (float) hit.hitVec.x,
(float) hit.hitVec.y, (float) hit.hitVec.z);
}
return false;
}
/**
* @return 1 if successfully used tool, 0 if failed to use tool,
* -1 if ItemStack failed the capability check (no action done, continue checks).
*/
public EnumActionResult onPipeToolUsed(World world, BlockPos pos, ItemStack stack, EnumFacing coverSide,
IPipeTile<PipeType, NodeDataType> pipeTile, EntityPlayer entityPlayer,
EnumHand hand) {
if (isPipeTool(stack)) {
if (!entityPlayer.world.isRemote) {
if (entityPlayer.isSneaking() && pipeTile.canHaveBlockedFaces()) {
boolean isBlocked = pipeTile.isFaceBlocked(coverSide);
pipeTile.setFaceBlocked(coverSide, !isBlocked);
ToolHelper.playToolSound(stack, entityPlayer);
} else {
boolean isOpen = pipeTile.isConnected(coverSide);
pipeTile.setConnection(coverSide, !isOpen, false);
if (isOpen != pipeTile.isConnected(coverSide)) {
ToolHelper.playToolSound(stack, entityPlayer);
}
}
ToolHelper.damageItem(stack, entityPlayer);
return EnumActionResult.SUCCESS;
}
entityPlayer.swingArm(hand);
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
protected boolean isPipeTool(@NotNull ItemStack stack) {
return ToolHelper.isTool(stack, ToolClasses.WRENCH);
}
@Override
public void onBlockClicked(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull EntityPlayer playerIn) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
if (pipeTile == null || rayTraceResult == null) {
return;
}
EnumFacing coverSide = CoverRayTracer.traceCoverSide(rayTraceResult);
Cover cover = coverSide == null ? null : pipeTile.getCoverableImplementation().getCoverAtSide(coverSide);
if (cover != null) {
cover.onLeftClick(playerIn, rayTraceResult);
}
}
@Override
public void onEntityCollision(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null && pipeTile.getFrameMaterial() != null) {
// make pipe with frame climbable
BlockFrame blockFrame = MetaBlocks.FRAMES.get(pipeTile.getFrameMaterial());
blockFrame.onEntityCollision(worldIn, pos, state, entityIn);
}
}
@SuppressWarnings("unchecked")
@Override
public void harvestBlock(@NotNull World worldIn, @NotNull EntityPlayer player, @NotNull BlockPos pos,
@NotNull IBlockState state, @Nullable TileEntity te, @NotNull ItemStack stack) {
tileEntities.set(te == null ? tileEntities.get() : (IPipeTile<PipeType, NodeDataType>) te);
super.harvestBlock(worldIn, player, pos, state, te, stack);
tileEntities.remove();
}
@Override
public void getDrops(@NotNull NonNullList<ItemStack> drops, @NotNull IBlockAccess world, @NotNull BlockPos pos,
@NotNull IBlockState state, int fortune) {
IPipeTile<PipeType, NodeDataType> pipeTile = tileEntities.get() == null ? getPipeTileEntity(world, pos) :
tileEntities.get();
if (pipeTile == null) return;
if (pipeTile.getFrameMaterial() != null) {
BlockFrame blockFrame = MetaBlocks.FRAMES.get(pipeTile.getFrameMaterial());
drops.add(blockFrame.getItem(pipeTile.getFrameMaterial()));
}
drops.add(getDropItem(pipeTile));
}
@Override
public void addCollisionBoxToList(@NotNull IBlockState state, @NotNull World worldIn, @NotNull BlockPos pos,
@NotNull AxisAlignedBB entityBox, @NotNull List<AxisAlignedBB> collidingBoxes,
@Nullable Entity entityIn, boolean isActualState) {
// This iterator causes some heap memory overhead
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null && pipeTile.getFrameMaterial() != null) {
AxisAlignedBB box = BlockFrame.COLLISION_BOX.offset(pos);
if (box.intersects(entityBox)) {
collidingBoxes.add(box);
}
return;
}
for (Cuboid6 axisAlignedBB : getCollisionBox(worldIn, pos, entityIn)) {
AxisAlignedBB offsetBox = axisAlignedBB.aabb().offset(pos);
if (offsetBox.intersects(entityBox)) collidingBoxes.add(offsetBox);
}
}
@Nullable
@Override
public RayTraceResult collisionRayTrace(@NotNull IBlockState blockState, World worldIn, @NotNull BlockPos pos,
@NotNull Vec3d start, @NotNull Vec3d end) {
if (worldIn.isRemote) {
return getClientCollisionRayTrace(worldIn, pos, start, end);
}
return RayTracer.rayTraceCuboidsClosest(start, end, pos, getCollisionBox(worldIn, pos, null));
}
@SideOnly(Side.CLIENT)
public RayTraceResult getClientCollisionRayTrace(World worldIn, @NotNull BlockPos pos, @NotNull Vec3d start,
@NotNull Vec3d end) {
return RayTracer.rayTraceCuboidsClosest(start, end, pos,
getCollisionBox(worldIn, pos, Minecraft.getMinecraft().player));
}
/**
* This method attempts to properly raytrace the pipe to fix the server not getting the correct raytrace result.
*/
@Nullable
public CuboidRayTraceResult getServerCollisionRayTrace(EntityPlayer playerIn, BlockPos pos, World worldIn) {
return RayTracer.rayTraceCuboidsClosest(
RayTracer.getStartVec(playerIn), RayTracer.getEndVec(playerIn),
pos, getCollisionBox(worldIn, pos, playerIn));
}
@NotNull
@Override
public BlockFaceShape getBlockFaceShape(@NotNull IBlockAccess worldIn, @NotNull IBlockState state,
@NotNull BlockPos pos, @NotNull EnumFacing face) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null && pipeTile.getCoverableImplementation().getCoverAtSide(face) != null) {
return BlockFaceShape.SOLID;
}
return BlockFaceShape.UNDEFINED;
}
@Override
public boolean recolorBlock(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing side,
@NotNull EnumDyeColor color) {
IPipeTile<PipeType, NodeDataType> tileEntityPipe = getPipeTileEntity(world, pos);
if (tileEntityPipe != null && tileEntityPipe.getPipeType() != null &&
tileEntityPipe.getPipeType().isPaintable() &&
tileEntityPipe.getPaintingColor() != color.colorValue) {
tileEntityPipe.setPaintingColor(color.colorValue);
return true;
}
return false;
}
protected boolean isThisPipeBlock(Block block) {
return block != null && block.getClass().isAssignableFrom(getClass());
}
/**
* Just returns proper pipe tile entity
*/
public IPipeTile<PipeType, NodeDataType> getPipeTileEntity(IBlockAccess world, BlockPos selfPos) {
TileEntity tileEntityAtPos = world.getTileEntity(selfPos);
return getPipeTileEntity(tileEntityAtPos);
}
@Nullable
public IPipeTile<PipeType, NodeDataType> getPipeTileEntity(TileEntity tileEntityAtPos) {
if (tileEntityAtPos instanceof IPipeTile &&
isThisPipeBlock(((IPipeTile<PipeType, NodeDataType>) tileEntityAtPos).getPipeBlock())) {
return (IPipeTile<PipeType, NodeDataType>) tileEntityAtPos;
}
return null;
}
public boolean canConnect(IPipeTile<PipeType, NodeDataType> selfTile, EnumFacing facing) {
if (selfTile.getPipeWorld().getBlockState(selfTile.getPipePos().offset(facing)).getBlock() == Blocks.AIR)
return false;
Cover cover = selfTile.getCoverableImplementation().getCoverAtSide(facing);
if (cover != null && !cover.canPipePassThrough()) {
return false;
}
TileEntity other = selfTile.getNeighbor(facing);
if (other instanceof IPipeTile) {
cover = ((IPipeTile<?, ?>) other).getCoverableImplementation().getCoverAtSide(facing.getOpposite());
if (cover != null && !cover.canPipePassThrough())
return false;
return canPipesConnect(selfTile, facing, (IPipeTile<PipeType, NodeDataType>) other);
}
return canPipeConnectToBlock(selfTile, facing, other);
}
public abstract boolean canPipesConnect(IPipeTile<PipeType, NodeDataType> selfTile, EnumFacing side,
IPipeTile<PipeType, NodeDataType> sideTile);
public abstract boolean canPipeConnectToBlock(IPipeTile<PipeType, NodeDataType> selfTile, EnumFacing side,
@Nullable TileEntity tile);
private List<IndexedCuboid6> getCollisionBox(IBlockAccess world, BlockPos pos, @Nullable Entity entityIn) {
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(world, pos);
if (pipeTile == null) {
return Collections.emptyList();
}
if (pipeTile.getFrameMaterial() != null) {
return Collections.singletonList(FULL_CUBE_COLLISION);
}
PipeType pipeType = pipeTile.getPipeType();
if (pipeType == null) {
return Collections.emptyList();
}
int actualConnections = pipeTile.getVisualConnections();
float thickness = pipeType.getThickness();
List<IndexedCuboid6> result = new ArrayList<>();
CoverHolder coverHolder = pipeTile.getCoverableImplementation();
// Check if the machine grid is being rendered
boolean usingGrid = hasPipeCollisionChangingItem(world, pos, entityIn);
if (usingGrid) {
result.add(FULL_CUBE_COLLISION);
}
// Always add normal collision so player doesn't "fall through" the cable/pipe when
// a tool is put in hand, and will still be standing where they were before.
result.add(new IndexedCuboid6(new CoverRayTracer.PrimaryBoxData(usingGrid), getSideBox(null, thickness)));
for (EnumFacing side : EnumFacing.VALUES) {
if ((actualConnections & 1 << side.getIndex()) > 0) {
result.add(new IndexedCuboid6(new PipeConnectionData(side), getSideBox(side, thickness)));
}
}
coverHolder.addCoverCollisionBoundingBox(result);
return result;
}
public boolean hasPipeCollisionChangingItem(IBlockAccess world, BlockPos pos, Entity entity) {
if (entity instanceof EntityPlayer) {
return hasPipeCollisionChangingItem(world, pos, ((EntityPlayer) entity).getHeldItem(EnumHand.MAIN_HAND)) ||
hasPipeCollisionChangingItem(world, pos, ((EntityPlayer) entity).getHeldItem(EnumHand.OFF_HAND)) ||
entity.isSneaking() && isHoldingPipe((EntityPlayer) entity);
}
return false;
}
public abstract boolean isHoldingPipe(EntityPlayer player);
public boolean hasPipeCollisionChangingItem(IBlockAccess world, BlockPos pos, ItemStack stack) {
if (isPipeTool(stack)) return true;
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(world, pos);
if (pipeTile == null) return false;
PipeCoverableImplementation coverable = pipeTile.getCoverableImplementation();
final boolean hasAnyCover = coverable.hasAnyCover();
if (hasAnyCover && ToolHelper.isTool(stack, ToolClasses.SCREWDRIVER)) return true;
final boolean acceptsCovers = coverable.acceptsCovers();
return GTUtility.isCoverBehaviorItem(stack, () -> hasAnyCover, coverDef -> acceptsCovers);
}
@Override
public boolean canRenderInLayer(@NotNull IBlockState state, @NotNull BlockRenderLayer layer) {
return true;
}
@NotNull
@Override
public IBlockState getFacade(@NotNull IBlockAccess world, @NotNull BlockPos pos, @Nullable EnumFacing side,
@NotNull BlockPos otherPos) {
return getFacade(world, pos, side);
}
@NotNull
@Override
public IBlockState getFacade(@NotNull IBlockAccess world, @NotNull BlockPos pos, EnumFacing side) {
IPipeTile<?, ?> pipeTileEntity = getPipeTileEntity(world, pos);
if (pipeTileEntity != null && side != null) {
Cover cover = pipeTileEntity.getCoverableImplementation().getCoverAtSide(side);
if (cover instanceof IFacadeCover) {
return ((IFacadeCover) cover).getVisualState();
}
}
return world.getBlockState(pos);
}
@NotNull
@Override
public IBlockState getVisualState(@NotNull IBlockAccess world, @NotNull BlockPos pos, @NotNull EnumFacing side) {
return getFacade(world, pos, side);
}
@Override
public boolean supportsVisualConnections() {
return true;
}
public static class PipeConnectionData {
public final EnumFacing side;
public PipeConnectionData(EnumFacing side) {
this.side = side;
}
}
@Override
public int getMetaFromState(@NotNull IBlockState state) {
return 0;
}
@Override
protected @NotNull BlockStateContainer createBlockState() {
return constructState(new BlockStateContainer.Builder(this))
.add(NORTH, SOUTH, EAST, WEST, UP, DOWN, FRAMED)
.build();
}
protected @NotNull BlockStateContainer.Builder constructState(BlockStateContainer.@NotNull Builder builder) {
return builder.add(PipeRenderProperties.THICKNESS_PROPERTY).add(PipeRenderProperties.CLOSED_MASK_PROPERTY)
.add(PipeRenderProperties.BLOCKED_MASK_PROPERTY).add(PipeRenderProperties.COLOR_PROPERTY)
.add(PipeRenderProperties.FRAME_MATERIAL_PROPERTY).add(PipeRenderProperties.FRAME_MASK_PROPERTY)
.add(CoverRendererPackage.CRP_PROPERTY);
}
@SuppressWarnings("deprecation")
@Override
public @NotNull IBlockState getActualState(@NotNull IBlockState state, @NotNull IBlockAccess worldIn,
@NotNull BlockPos pos) {
var tile = getPipeTileEntity(worldIn, pos);
if (tile == null) return state;
state = writeConnectionMask(state, tile.getCoverAdjustedConnectionMask());
return state.withProperty(FRAMED, tile.getFrameMaterial() != null);
}
public static IBlockState writeConnectionMask(@NotNull IBlockState state, byte connectionMask) {
for (EnumFacing facing : EnumFacing.VALUES) {
state = state.withProperty(FACINGS.get(facing), GTUtility.evalMask(facing, connectionMask));
}
return state;
}
public static byte readConnectionMask(@NotNull IBlockState state) {
byte mask = 0;
for (EnumFacing facing : EnumFacing.VALUES) {
if (state.getValue(FACINGS.get(facing))) {
mask |= (byte) (1 << facing.ordinal());
}
}
return mask;
}
@Override
public @NotNull IBlockState getExtendedState(@NotNull IBlockState state, @NotNull IBlockAccess world,
@NotNull BlockPos pos) {
var tile = getPipeTileEntity(world, pos);
if (tile == null) return state;
else return tile.getRenderInformation((IExtendedBlockState) state.getActualState(world, pos));
}
@SideOnly(Side.CLIENT)
@Override
protected Pair<TextureAtlasSprite, Integer> getParticleTexture(World world, BlockPos blockPos) {
var tile = getPipeTileEntity(world, blockPos);
if (tile != null) {
return getPipeType().getModel().getParticleTexture(tile.getPaintingColor(), null);
}
return null;
}
}