Skip to content

Commit 3b2c8ca

Browse files
committed
Use a PipeNetWalker instead of manually iterating over a pipenet
1 parent 8725ad3 commit 3b2c8ca

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

src/main/java/gregtech/common/ToolEventHandlers.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ private static boolean shouldRenderGridOverlays(@NotNull IBlockState state, @Nul
305305
ItemStack mainHand, ItemStack offHand, boolean isSneaking) {
306306
if (state.getBlock() instanceof BlockPipe<?, ?, ?>pipe) {
307307
if (isSneaking &&
308-
(mainHand.isEmpty() || mainHand.getItem().getClass() == Item.getItemFromBlock(pipe).getClass() ||
309-
GTUtility.isSprayCan(mainHand) || GTUtility.isSprayCan(offHand))) {
308+
(mainHand.isEmpty() || mainHand.getItem().getClass() == Item.getItemFromBlock(pipe).getClass())) {
310309
return true;
311310
} else {
312311
Set<String> mainToolClasses = mainHand.getItem().getToolClasses(mainHand);

src/main/java/gregtech/common/items/behaviors/ColorSprayBehavior.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import gregtech.api.pipenet.tile.IPipeTile;
88
import gregtech.api.util.GradientUtil;
99
import gregtech.api.util.Mods;
10+
import gregtech.common.pipelike.PipeCollectorWalker;
1011
import gregtech.core.sound.GTSoundEvents;
1112

1213
import net.minecraft.block.Block;
@@ -134,38 +135,27 @@ private boolean traversePipes(EntityPlayer player, World world, EnumHand hand, B
134135
EnumFacing facing) {
135136
startPos = startPos.offset(facing);
136137
TileEntity connectedTe = world.getTileEntity(startPos);
137-
int count = 0;
138138
boolean painted = false;
139139
ItemStack heldItem = player.getHeldItem(hand);
140-
while (connectedTe instanceof IPipeTile<?, ?>connectedPipe && count < MAX_PIPE_TRAVERSAL_LENGTH) {
141-
if (connectedPipe.getPaintingColor() != (this.color == null ? -1 : this.color.colorValue)) {
142-
connectedPipe.setPaintingColor(this.color == null ? -1 : this.color.colorValue);
143-
connectedPipe.scheduleRenderUpdate();
144-
if (getUsesLeft(heldItem) == 1) {
145-
useItemDurability(player, hand, heldItem, empty.copy());
146-
return true;
147-
}
148-
// Off by one durability as the final use is handled by onItemUse, along with the use animation
149-
if (painted) {
150-
useItemDurability(player, hand, heldItem, empty.copy());
151-
}
152-
painted = true;
153-
}
154-
if (connectedPipe.getNumConnections() == 2) {
155-
int connections = connectedPipe.getConnections();
156-
connections &= ~(1 << facing.getOpposite().getIndex());
157-
for (EnumFacing other : EnumFacing.VALUES) {
158-
if ((connections & (1 << other.getIndex())) != 0) {
159-
facing = other;
160-
startPos = startPos.offset(facing);
161-
connectedTe = world.getTileEntity(startPos);
162-
count++;
163-
break;
140+
if (connectedTe instanceof IPipeTile<?, ?>startPipe) {
141+
List<IPipeTile<?, ?>> pipes = PipeCollectorWalker.collectPipeNet(world, startPos, startPipe,
142+
Math.min(MAX_PIPE_TRAVERSAL_LENGTH, getUsesLeft(heldItem)));
143+
for (IPipeTile<?, ?> pipe : pipes) {
144+
if (pipe.getPaintingColor() != (this.color == null ? -1 : this.color.colorValue)) {
145+
pipe.setPaintingColor(this.color == null ? -1 : this.color.colorValue);
146+
pipe.scheduleRenderUpdate();
147+
if (getUsesLeft(heldItem) == 1) {
148+
useItemDurability(player, hand, heldItem, empty.copy());
149+
return true;
150+
}
151+
// Off by one durability as the final use is handled by onItemUse, along with the use animation
152+
if (painted) {
153+
useItemDurability(player, hand, heldItem, empty.copy());
164154
}
155+
painted = true;
165156
}
166-
} else {
167-
break;
168157
}
158+
169159
}
170160
return painted;
171161
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package gregtech.common.pipelike;
2+
3+
import gregtech.api.pipenet.PipeNetWalker;
4+
import gregtech.api.pipenet.tile.IPipeTile;
5+
6+
import net.minecraft.tileentity.TileEntity;
7+
import net.minecraft.util.EnumFacing;
8+
import net.minecraft.util.math.BlockPos;
9+
import net.minecraft.world.World;
10+
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
public class PipeCollectorWalker<T extends IPipeTile<?, ?>> extends PipeNetWalker<T> {
19+
20+
@NotNull
21+
@SuppressWarnings("unchecked")
22+
public static List<IPipeTile<?, ?>> collectPipeNet(World world, BlockPos sourcePipe, IPipeTile<?, ?> pipe,
23+
int limit) {
24+
PipeCollectorWalker<? extends IPipeTile<?, ?>> walker = (PipeCollectorWalker<? extends IPipeTile<?, ?>>) new PipeCollectorWalker<>(
25+
world, sourcePipe, 0, pipe.getClass());
26+
walker.traversePipeNet(limit);
27+
return walker.isFailed() ? Collections.EMPTY_LIST : Collections.unmodifiableList(walker.pipes);
28+
}
29+
30+
public static List<IPipeTile<?, ?>> collectPipeNet(World world, BlockPos sourcePipe, IPipeTile<?, ?> pipe) {
31+
// Default limit for a pipenet walker
32+
return collectPipeNet(world, sourcePipe, pipe, 32768);
33+
}
34+
35+
// I love type erasure
36+
private final Class<T> basePipeClass;
37+
private List<T> pipes = new ArrayList<>();
38+
private BlockPos sourcePipe;
39+
40+
protected PipeCollectorWalker(World world, BlockPos sourcePipe, int walkedBlocks, Class<T> basePipeClass) {
41+
super(world, sourcePipe, walkedBlocks);
42+
this.sourcePipe = sourcePipe;
43+
this.basePipeClass = basePipeClass;
44+
}
45+
46+
@Override
47+
protected PipeNetWalker<T> createSubWalker(World world, EnumFacing facingToNextPos, BlockPos nextPos,
48+
int walkedBlocks) {
49+
PipeCollectorWalker<T> walker = new PipeCollectorWalker<>(world, nextPos, walkedBlocks, this.basePipeClass);
50+
walker.pipes = pipes;
51+
walker.sourcePipe = sourcePipe;
52+
return walker;
53+
}
54+
55+
@Override
56+
protected void checkPipe(T pipeTile, BlockPos pos) {
57+
this.pipes.add(pipeTile);
58+
}
59+
60+
@Override
61+
protected void checkNeighbour(T pipeTile, BlockPos pipePos, EnumFacing faceToNeighbour,
62+
@Nullable TileEntity neighbourTile) {
63+
}
64+
65+
@Override
66+
protected Class<T> getBasePipeClass() {
67+
return basePipeClass;
68+
}
69+
}

src/main/resources/assets/gregtech/lang/en_us.lang

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4701,7 +4701,7 @@ behaviour.paintspray.red.tooltip=Can paint things in Red
47014701
behaviour.paintspray.black.tooltip=Can paint things in Black
47024702
behaviour.paintspray.uses=Remaining Uses: %,d
47034703
behaviour.paintspray.offhand=Hold in offhand to color while placing blocks
4704-
behaviour.paintspray.crouch=Crouch to spray a line of pipes
4704+
behaviour.paintspray.crouch=Crouch to spray a network of pipes
47054705
behaviour.prospecting=Usable for Prospecting
47064706

47074707
# Multiblock machine controllers

0 commit comments

Comments
 (0)