From b48c0fce5fcc88458022114d9616d67ca01ac010 Mon Sep 17 00:00:00 2001 From: logan Date: Sun, 26 Jan 2025 01:55:53 -0800 Subject: [PATCH] Make Redstone Interface block support new event system --- .../common/block/RedstoneInterfaceBlock.java | 7 +++ .../RedstoneInterfaceBlockEntity.java | 51 ++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/block/RedstoneInterfaceBlock.java b/src/main/java/li/cil/oc2/common/block/RedstoneInterfaceBlock.java index 41b0fd1f..1d1ef02c 100644 --- a/src/main/java/li/cil/oc2/common/block/RedstoneInterfaceBlock.java +++ b/src/main/java/li/cil/oc2/common/block/RedstoneInterfaceBlock.java @@ -2,6 +2,7 @@ package li.cil.oc2.common.block; +import net.minecraft.world.level.Level; import li.cil.oc2.common.blockentity.BlockEntities; import li.cil.oc2.common.blockentity.RedstoneInterfaceBlockEntity; import net.minecraft.core.BlockPos; @@ -61,6 +62,12 @@ public final class RedstoneInterfaceBlock extends HorizontalDirectionalBlock imp return getSignal(state, level, pos, side); } + @Override + public void neighborChanged(BlockState state, Level worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) { + RedstoneInterfaceBlockEntity ribe = (RedstoneInterfaceBlockEntity) worldIn.getBlockEntity(pos); + ribe.neighborChanged(fromPos); + } + /////////////////////////////////////////////////////////////////// // EntityBlock diff --git a/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java b/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java index 45656392..31c71ced 100644 --- a/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java +++ b/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: MIT */ package li.cil.oc2.common.blockentity; - +import li.cil.oc2.api.util.*; import li.cil.oc2.api.bus.device.object.Callback; import li.cil.oc2.api.bus.device.object.DocumentedDevice; import li.cil.oc2.api.bus.device.object.NamedDevice; @@ -16,14 +16,16 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.util.Mth; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.fml.ModList; - -import javax.annotation.Nullable; import java.util.Collection; - +import net.minecraftforge.fml.ModList; +import java.util.*; +import javax.annotation.Nullable; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import li.cil.oc2.api.bus.device.rpc.*; import static java.util.Collections.singletonList; -public final class RedstoneInterfaceBlockEntity extends ModBlockEntity implements NamedDevice, DocumentedDevice { +public final class RedstoneInterfaceBlockEntity extends ModBlockEntity implements NamedDevice, DocumentedDevice, RPCEventSource { private static final String OUTPUT_TAG_NAME = "output"; private static final String BUNDLED_TAG_NAME = "bundled"; @@ -39,6 +41,8 @@ public final class RedstoneInterfaceBlockEntity extends ModBlockEntity implement private static final String VALUES = "values"; private static final String COLOUR = "colour"; + private final HashMap subscribers = new HashMap(); + /////////////////////////////////////////////////////////////////// private final byte[] output = new byte[Constants.BLOCK_FACE_COUNT]; @@ -283,5 +287,40 @@ public final class RedstoneInterfaceBlockEntity extends ModBlockEntity implement public byte[] getBundledSignal(Direction direction) { final int index = direction.get3DDataValue(); return this.bundled_output[index]; + + @Override + public void subscribe(IEventSink sink, UUID myid) { + subscribers.put(sink, myid); + } + @Override + public void unsubscribe(IEventSink sink) { + subscribers.remove(sink); + } + + public void neighborChanged(BlockPos fromPos) { + int sl = 0; + if (level == null) { + return; + } + + final BlockPos pos = getBlockPos(); + final Direction direction = Side.relativeDirection(fromPos, pos); + assert direction != null; + + final ChunkPos chunkPos = new ChunkPos(fromPos); + if (!level.hasChunk(chunkPos.x, chunkPos.z)) { + sl = 0; + } + + sl = level.getSignal(fromPos, direction); + JsonObject msg = new JsonObject(); + msg.addProperty("event", "redstone"); + msg.addProperty("side", ""+direction); + msg.addProperty("level", sl); + + System.out.println("updating redstone interface block with level: "+sl); + for (var subscriber : subscribers.entrySet()) { + subscriber.getKey().postEvent(subscriber.getValue(), msg); + } } }