Skip to content

Commit e33bf94

Browse files
committed
Absorb #2368 pt1
1 parent 9a81375 commit e33bf94

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

src/main/java/gregtech/common/items/behaviors/spray/AbstractSprayBehavior.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package gregtech.common.items.behaviors.spray;
22

3+
import gregtech.api.color.ColoredBlockContainer;
34
import gregtech.api.items.metaitem.MetaItem;
45
import gregtech.api.items.metaitem.stats.IItemBehaviour;
5-
import gregtech.api.util.color.ColoredBlockContainer;
6-
import gregtech.api.color.ColoredBlockContainer;
6+
import gregtech.api.pipenet.tile.IPipeTile;
7+
import gregtech.common.pipelike.PipeCollectorWalker;
78
import gregtech.core.sound.GTSoundEvents;
89

910
import net.minecraft.entity.player.EntityPlayer;
1011
import net.minecraft.item.EnumDyeColor;
1112
import net.minecraft.item.ItemStack;
13+
import net.minecraft.tileentity.TileEntity;
1214
import net.minecraft.util.ActionResult;
1315
import net.minecraft.util.EnumActionResult;
1416
import net.minecraft.util.EnumFacing;
@@ -86,20 +88,64 @@ public ActionResult<ItemStack> onItemUse(EntityPlayer player, World world, Block
8688
return EnumActionResult.PASS;
8789
} else if (!player.canPlayerEdit(pos, facing, sprayCan)) {
8890
return EnumActionResult.FAIL;
89-
} else if (!tryPaintBlock(player, world, pos, facing, sprayCan)) {
91+
}
92+
93+
int returnCode = tryPaintBlock(world, pos, player, hand, facing, sprayCan);
94+
if (returnCode == 0) {
9095
return EnumActionResult.PASS;
96+
} else if (returnCode == 1) {
97+
onSpray(player, hand, sprayCan);
9198
}
9299

93-
onSpray(player, hand, sprayCan);
94100
world.playSound(null, player.posX, player.posY, player.posZ, GTSoundEvents.SPRAY_CAN_TOOL,
95101
SoundCategory.PLAYERS, 1.0f, 1.0f);
96102
return EnumActionResult.SUCCESS;
97103
}
98104

99-
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
100-
protected boolean tryPaintBlock(@NotNull EntityPlayer player, @NotNull World world, @NotNull BlockPos pos,
101-
@NotNull EnumFacing side, @NotNull ItemStack sprayCan) {
105+
/**
106+
* Return codes:<br/>
107+
* {@code -1}: didn't paint any block(s)<br/>
108+
* {@code 0}: colored 1 block</br>
109+
* {@code 1+}: colored multiple blocks and {@link #onSpray(EntityPlayer, EnumHand, ItemStack)} was handled already
110+
*/
111+
protected int tryPaintBlock(@NotNull World world, @NotNull BlockPos pos, @NotNull EntityPlayer player,
112+
@NotNull EnumHand hand, @NotNull EnumFacing side, @NotNull ItemStack sprayCan) {
113+
if (player.isSneaking()) {
114+
TileEntity te = world.getTileEntity(pos);
115+
if (te instanceof IPipeTile<?, ?>pipeTile) {
116+
return traversePipes(world, player, hand, pos, pipeTile, sprayCan);
117+
}
118+
}
119+
102120
ColoredBlockContainer blockContainer = ColoredBlockContainer.getInstance(world, pos, side, player);
103-
return blockContainer.isValid() && blockContainer.setColor(getColor(sprayCan));
121+
if (blockContainer.isValid() && blockContainer.setColor(getColor(sprayCan))) {
122+
return 0;
123+
}
124+
125+
return -1;
126+
}
127+
128+
protected int traversePipes(@NotNull World world, @NotNull EntityPlayer player, @NotNull EnumHand hand,
129+
@NotNull BlockPos startPos, @NotNull IPipeTile<?, ?> startingPipe,
130+
@NotNull ItemStack sprayCan) {
131+
EnumDyeColor dyeColor = getColor(sprayCan);
132+
int color = dyeColor == null ? -1 : dyeColor.colorValue;
133+
int[] paintedCountHolder = { 0 };
134+
PipeCollectorWalker.collectPipeNet(world, startPos, startingPipe, pipe -> {
135+
if (!canSpray(sprayCan)) {
136+
return false;
137+
}
138+
139+
if (pipe.getPaintingColor() != color) {
140+
pipe.setPaintingColor(color);
141+
pipe.scheduleRenderUpdate();
142+
onSpray(player, hand, sprayCan);
143+
paintedCountHolder[0]++;
144+
}
145+
146+
return true;
147+
});
148+
149+
return paintedCountHolder[0];
104150
}
105151
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package gregtech.common.pipelike;
2+
3+
import gregtech.api.pipenet.PipeNetWalker;
4+
import gregtech.api.pipenet.tile.IPipeTile;
5+
import gregtech.api.util.function.BooleanFunction;
6+
7+
import net.minecraft.tileentity.TileEntity;
8+
import net.minecraft.util.EnumFacing;
9+
import net.minecraft.util.math.BlockPos;
10+
import net.minecraft.world.World;
11+
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
public class PipeCollectorWalker<T extends IPipeTile<?, ?>> extends PipeNetWalker<T> {
16+
17+
public static void collectPipeNet(@NotNull World world, @NotNull BlockPos sourcePipe, @NotNull IPipeTile<?, ?> pipe,
18+
@NotNull BooleanFunction<IPipeTile<?, ?>> pipeFunction) {
19+
PipeCollectorWalker<? extends IPipeTile<?, ?>> walker = (PipeCollectorWalker<? extends IPipeTile<?, ?>>) new PipeCollectorWalker<>(
20+
world, sourcePipe, 0, pipe.getClass());
21+
walker.pipeFunction = pipeFunction;
22+
walker.traversePipeNet();
23+
}
24+
25+
// I love type erasure - htmlcsjs
26+
private final Class<T> basePipeClass;
27+
/**
28+
* Function to run on every pipe
29+
* If false is returned then halt the walker
30+
*/
31+
private BooleanFunction<IPipeTile<?, ?>> pipeFunction;
32+
33+
private BlockPos sourcePipe;
34+
35+
protected PipeCollectorWalker(World world, BlockPos sourcePipe, int walkedBlocks, Class<T> basePipeClass) {
36+
super(world, sourcePipe, walkedBlocks);
37+
this.sourcePipe = sourcePipe;
38+
this.basePipeClass = basePipeClass;
39+
}
40+
41+
@Override
42+
protected PipeNetWalker<T> createSubWalker(World world, EnumFacing facingToNextPos, BlockPos nextPos,
43+
int walkedBlocks) {
44+
PipeCollectorWalker<T> walker = new PipeCollectorWalker<>(world, nextPos, walkedBlocks, this.basePipeClass);
45+
walker.pipeFunction = pipeFunction;
46+
walker.sourcePipe = sourcePipe;
47+
return walker;
48+
}
49+
50+
@Override
51+
protected void checkPipe(T pipeTile, BlockPos pos) {
52+
if (this.pipeFunction != null && !this.pipeFunction.apply(pipeTile)) {
53+
this.root.stop();
54+
}
55+
}
56+
57+
@Override
58+
protected void checkNeighbour(T pipeTile, BlockPos pipePos, EnumFacing faceToNeighbour,
59+
@Nullable TileEntity neighbourTile) {}
60+
61+
@Override
62+
protected Class<T> getBasePipeClass() {
63+
return basePipeClass;
64+
}
65+
}

0 commit comments

Comments
 (0)