Skip to content

Commit dfd6f5a

Browse files
committed
Add weak signal forwarding + test
1 parent f656e66 commit dfd6f5a

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

src/main/java/dev/technici4n/moderndynamics/extender/MachineExtenderBlock.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import dev.technici4n.moderndynamics.MdBlock;
2222
import net.minecraft.core.BlockPos;
2323
import net.minecraft.core.Direction;
24+
import net.minecraft.server.level.ServerLevel;
2425
import net.minecraft.util.RandomSource;
26+
import net.minecraft.world.Containers;
2527
import net.minecraft.world.item.context.BlockPlaceContext;
2628
import net.minecraft.world.level.Level;
2729
import net.minecraft.world.level.LevelReader;
@@ -35,6 +37,8 @@
3537
import org.jetbrains.annotations.Nullable;
3638

3739
public class MachineExtenderBlock extends MdBlock {
40+
private static final ScopedValue<?> SIGNAL_FORWARDING = ScopedValue.newInstance();
41+
3842
public static final BooleanProperty TOP = BooleanProperty.create("top");
3943

4044
public MachineExtenderBlock(Properties props) {
@@ -90,10 +94,40 @@ protected void neighborChanged(BlockState state, Level level, BlockPos pos, Bloc
9094
super.neighborChanged(state, level, pos, block, orientation, movedByPiston);
9195
}
9296

93-
// TODO 26.1: This previously used the vanilla callback, but I have my doubt it was ever called
9497
@Override
9598
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighbor) {
99+
if (pos.below().equals(neighbor) && level instanceof Level actualLevel) {
100+
actualLevel.updateNeighborsAtExceptFromFacing(pos, this, Direction.DOWN, null);
101+
}
102+
}
103+
104+
@Override
105+
protected boolean hasAnalogOutputSignal(BlockState state) {
106+
return true;
107+
}
108+
109+
@Override
110+
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos, Direction direction) {
111+
if (direction == Direction.DOWN || SIGNAL_FORWARDING.isBound()) {
112+
return 0;
113+
}
114+
115+
// Seek to the first non-extender below
116+
var below = pos.below();
117+
while (below.getY() > 0 && level.getBlockState(below).is(this)) {
118+
below = below.below();
119+
}
96120

97-
super.onNeighborChange(state, level, pos, neighbor);
121+
// Ensure we don't get recursively called
122+
var source = below;
123+
return ScopedValue.where(SIGNAL_FORWARDING, null).call(() -> {
124+
return level.getBlockState(source).getAnalogOutputSignal(level, source, Direction.UP);
125+
});
126+
}
127+
128+
@Override
129+
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
130+
// This mirrors what ChestBlock does
131+
Containers.updateNeighboursAfterDestroy(state, level, pos);
98132
}
99133
}

src/main/java/dev/technici4n/moderndynamics/pipe/PipeItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class PipeItem extends BlockItem {
3030
public PipeItem(PipeBlock block, Item.Properties props) {
31-
super(block, props);
31+
super(block, props.useBlockDescriptionPrefix());
3232
block.setItem(this);
3333
}
3434

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dev.technici4n.moderndynamics.test;
2+
3+
import dev.technici4n.moderndynamics.init.MdBlocks;
4+
import dev.technici4n.moderndynamics.test.framework.MdGameTestHelper;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.core.Direction;
7+
import net.minecraft.world.item.Items;
8+
import net.minecraft.world.level.block.Blocks;
9+
import net.minecraft.world.level.block.RedstoneLampBlock;
10+
import net.neoforged.neoforge.capabilities.Capabilities;
11+
import net.neoforged.neoforge.transfer.item.ItemResource;
12+
import net.neoforged.neoforge.transfer.transaction.Transaction;
13+
14+
import java.util.List;
15+
16+
public class MachineExtenderTest {
17+
@MdGameTest
18+
public void testForwardWeakSignal(MdGameTestHelper helper) {
19+
var l0 = BlockPos.ZERO.above();
20+
var l1 = l0.above();
21+
var l2 = l1.above();
22+
23+
helper.setBlock(l0, Blocks.HOPPER);
24+
helper.setBlock(l0.north().below(), Blocks.STONE, Direction.SOUTH);
25+
helper.setBlock(l0.north(), Blocks.COMPARATOR, Direction.SOUTH);
26+
helper.setBlock(l0.north().north(), Blocks.REDSTONE_LAMP);
27+
28+
helper.setBlock(l1, MdBlocks.MACHINE_EXTENDER.get());
29+
helper.setBlock(l1.east().below(), Blocks.STONE);
30+
helper.setBlock(l1.east(), Blocks.COMPARATOR, Direction.WEST);
31+
helper.setBlock(l1.east().east(), Blocks.REDSTONE_LAMP);
32+
33+
helper.setBlock(l2, MdBlocks.MACHINE_EXTENDER.get());
34+
helper.setBlock(l2.west().below(), Blocks.STONE);
35+
helper.setBlock(l2.west(), Blocks.COMPARATOR, Direction.EAST);
36+
helper.setBlock(l2.west().west(), Blocks.REDSTONE_LAMP);
37+
38+
var lamps = List.of(l0.north().north(), l1.east().east(), l2.west().west());
39+
var lampOff = Blocks.REDSTONE_LAMP.defaultBlockState();
40+
var lampOn = lampOff.setValue(RedstoneLampBlock.LIT, true);
41+
42+
helper.startSequence()
43+
.thenExecute(() -> {
44+
for (var lamp : lamps) {
45+
helper.assertBlockState(lamp, lampOff);
46+
}
47+
})
48+
.thenExecuteAfter(1, () -> {
49+
// Fill the hopper
50+
try (var tx = Transaction.openRoot()) {
51+
var items = helper.getLevel().getCapability(Capabilities.Item.BLOCK, helper.absolutePos(l0), Direction.UP);
52+
for (int i = 0; i < items.size(); i++) {
53+
items.insert(ItemResource.of(Items.STICK), 64, tx);
54+
}
55+
tx.commit();
56+
}
57+
})
58+
.thenExecuteAfter(5, () -> {
59+
for (var lamp : lamps) {
60+
helper.assertBlockState(lamp, lampOn);
61+
}
62+
})
63+
.thenSucceed();
64+
}
65+
}

src/main/java/dev/technici4n/moderndynamics/test/MdGameTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public final class MdGameTests {
4646
private static final List<Class<?>> testClasses = List.of(
4747
FluidTransferTest.class,
4848
ItemDistributionTest.class,
49-
ItemTransferTest.class);
49+
ItemTransferTest.class,
50+
MachineExtenderTest.class);
5051

5152
private static final Map<Identifier, TestData<ResourceKey<TestEnvironmentDefinition<?>>>> tests = new HashMap<>();
5253

0 commit comments

Comments
 (0)