Skip to content

Commit 7e605b8

Browse files
committed
pass boolean through the chain rather than an atomic variable
1 parent ade6faa commit 7e605b8

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

src/generators/java/mekanism/generators/common/registries/GeneratorsBlockTypes.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package mekanism.generators.common.registries;
22

3-
import mekanism.api.functions.TriConsumer;
43
import mekanism.common.block.attribute.AttributeCustomSelectionBox;
54
import mekanism.common.block.attribute.AttributeHasBounding.HandleBoundingBlock;
5+
import mekanism.common.block.attribute.AttributeHasBounding.TriBooleanFunction;
66
import mekanism.common.block.attribute.AttributeMultiblock;
77
import mekanism.common.block.attribute.AttributeParticleFX;
88
import mekanism.common.block.attribute.AttributeUpgradeSupport;
@@ -102,12 +102,15 @@ private GeneratorsBlockTypes() {
102102
.with(AttributeUpgradeSupport.MUFFLING_ONLY)
103103
.withBounding(new HandleBoundingBlock() {
104104
@Override
105-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
105+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> consumer) {
106106
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
107107
for (int i = 0; i < 4; i++) {
108108
mutable.setWithOffset(pos, 0, i + 1, 0);
109-
consumer.accept(level, mutable, data);
109+
if (!consumer.accept(level, mutable, data)) {
110+
return false;
111+
}
110112
}
113+
return true;
111114
}
112115
})
113116
.withComputerSupport("windGenerator")
@@ -134,15 +137,20 @@ public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data
134137
.with(AttributeUpgradeSupport.MUFFLING_ONLY)
135138
.withBounding(new HandleBoundingBlock() {
136139
@Override
137-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
140+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> consumer) {
138141
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(pos.getX(), pos.getY() + 1, pos.getZ());
139-
consumer.accept(level, mutable, data);
142+
if (!consumer.accept(level, mutable, data)) {
143+
return false;
144+
}
140145
for (int x = -1; x <= 1; x++) {
141146
for (int z = -1; z <= 1; z++) {
142147
mutable.setWithOffset(pos, x, 2, z);
143-
consumer.accept(level, mutable, data);
148+
if (!consumer.accept(level, mutable, data)) {
149+
return false;
150+
}
144151
}
145152
}
153+
return true;
146154
}
147155
})
148156
.withComputerSupport("advancedSolarGenerator")

src/main/java/mekanism/common/block/attribute/AttributeHasBounding.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package mekanism.common.block.attribute;
22

3-
import mekanism.api.functions.TriConsumer;
43
import mekanism.common.Mekanism;
54
import mekanism.common.block.BlockBounding;
65
import mekanism.common.block.states.BlockStateHelper;
@@ -20,8 +19,8 @@ public class AttributeHasBounding implements Attribute {
2019

2120
public static final AttributeHasBounding ABOVE_ONLY = new AttributeHasBounding(new HandleBoundingBlock() {
2221
@Override
23-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
24-
consumer.accept(level, pos.above(), data);
22+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> consumer) {
23+
return consumer.accept(level, pos.above(), data);
2524
}
2625
});
2726

@@ -43,6 +42,7 @@ public void removeBoundingBlocks(Level world, BlockPos pos, BlockState state) {
4342
RegistryUtils.getName(boundingState.getBlock()));
4443
}
4544
}
45+
return true;
4646
});
4747
}
4848

@@ -59,15 +59,22 @@ public void placeBoundingBlocks(Level world, BlockPos orig, BlockState state) {
5959
Mekanism.logger.warn("Unable to find Bounding Block Tile at: {}", boundingLocation);
6060
}
6161
}
62+
return true;//todo decide if this should bail on failure with partially place blocks
6263
});
6364
}
6465

65-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
66-
boundingPosHandlers.handle(level, pos, state, data, consumer);
66+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> predicate) {
67+
return boundingPosHandlers.handle(level, pos, state, data, predicate);
6768
}
6869

6970
public interface HandleBoundingBlock {
7071

71-
<DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer);
72+
<DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> predicate);
73+
}
74+
75+
@FunctionalInterface
76+
public interface TriBooleanFunction<PARAM1, PARAM2, PARAM3> {
77+
78+
boolean accept(PARAM1 param1, PARAM2 param2, PARAM3 param3);
7279
}
7380
}

src/main/java/mekanism/common/item/block/ItemBlockTooltip.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ public boolean placeBlock(@NotNull BlockPlaceContext context, @NotNull BlockStat
8383
if (hasBounding == null) {
8484
return super.placeBlock(context, state);
8585
}
86-
AtomicBoolean isValid = new AtomicBoolean(true);
87-
hasBounding.handle(context.getLevel(), context.getClickedPos(), state, context, (level, pos, ctx) -> {
88-
if (isValid.get() && !WorldUtils.isValidReplaceableBlock(level, ctx, pos)) {
89-
isValid.set(false);
90-
}
91-
});
92-
return isValid.get() && super.placeBlock(context, state);
86+
87+
return hasBounding.handle(context.getLevel(), context.getClickedPos(), state, context, (level, pos, ctx) -> WorldUtils.isValidReplaceableBlock(level, ctx, pos)) && super.placeBlock(context, state);
9388
}
9489

9590
@Override

src/main/java/mekanism/common/registries/MekanismBlockTypes.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.common.collect.Table;
55
import java.util.function.Supplier;
66
import mekanism.api.Upgrade;
7-
import mekanism.api.functions.TriConsumer;
87
import mekanism.api.math.FloatingLong;
98
import mekanism.api.text.ILangEntry;
109
import mekanism.api.tier.ITier;
@@ -14,6 +13,7 @@
1413
import mekanism.common.block.attribute.AttributeCustomSelectionBox;
1514
import mekanism.common.block.attribute.AttributeHasBounding;
1615
import mekanism.common.block.attribute.AttributeHasBounding.HandleBoundingBlock;
16+
import mekanism.common.block.attribute.AttributeHasBounding.TriBooleanFunction;
1717
import mekanism.common.block.attribute.AttributeMultiblock;
1818
import mekanism.common.block.attribute.AttributeParticleFX;
1919
import mekanism.common.block.attribute.AttributeSideConfig;
@@ -334,18 +334,21 @@ private MekanismBlockTypes() {
334334
.with(AttributeCustomSelectionBox.JSON)
335335
.withBounding(new HandleBoundingBlock() {
336336
@Override
337-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
337+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> consumer) {
338338
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
339339
for (int x = -1; x <= 1; x++) {
340340
for (int y = 0; y <= 1; y++) {
341341
for (int z = -1; z <= 1; z++) {
342342
if (x != 0 || y != 0 || z != 0) {
343343
mutable.setWithOffset(pos, x, y, z);
344-
consumer.accept(level, mutable, data);
344+
if (!consumer.accept(level, mutable, data)) {
345+
return false;
346+
}
345347
}
346348
}
347349
}
348350
}
351+
return true;
349352
}
350353
})
351354
.withComputerSupport("digitalMiner")
@@ -535,12 +538,16 @@ public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data
535538
.with(AttributeCustomSelectionBox.JSON)
536539
.withBounding(new HandleBoundingBlock() {
537540
@Override
538-
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
541+
public <DATA> boolean handle(Level level, BlockPos pos, BlockState state, DATA data, TriBooleanFunction<Level, BlockPos, DATA> consumer) {
539542
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
540-
consumer.accept(level, mutable.setWithOffset(pos, Direction.UP), data);
543+
if (!consumer.accept(level, mutable.setWithOffset(pos, Direction.UP), data)) {
544+
return false;
545+
}
541546
mutable.setWithOffset(pos, MekanismUtils.getRight(Attribute.getFacing(state)));
542-
consumer.accept(level, mutable, data);
543-
consumer.accept(level, mutable.move(Direction.UP), data);
547+
if (!consumer.accept(level, mutable, data)) {
548+
return false;
549+
}
550+
return consumer.accept(level, mutable.move(Direction.UP), data);
544551
}
545552
})
546553
.withComputerSupport("modificationStation")

0 commit comments

Comments
 (0)