From 82ed9cd85d42c6b2cd272fd0a65b4aee0e6f4f5a Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Thu, 4 Jun 2020 15:33:56 +0200 Subject: [PATCH] Analog Redstone Links - Redstone link receivers now emit their signal with the level of the strongest transmitter of the same frequency --- .../logistics/RedstoneLinkNetworkHandler.java | 9 ++-- .../extractor/LinkedExtractorTileEntity.java | 4 +- .../block/redstone/RedstoneLinkBlock.java | 52 +++++++++++-------- .../redstone/RedstoneLinkTileEntity.java | 40 +++++++++----- .../LinkedTransposerTileEntity.java | 4 +- .../behaviour/linked/LinkBehaviour.java | 30 ++++++----- 6 files changed, 83 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java b/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java index 0467cf4f6..304ac2f7c 100644 --- a/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java +++ b/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java @@ -86,7 +86,7 @@ public class RedstoneLinkNetworkHandler { public void updateNetworkOf(LinkBehaviour actor) { Set network = getNetworkOf(actor); - boolean powered = false; + int power = 0; for (Iterator iterator = network.iterator(); iterator.hasNext();) { LinkBehaviour other = iterator.next(); @@ -105,10 +105,9 @@ public class RedstoneLinkNetworkHandler { } if (!withinRange(actor, other)) continue; - if (other.isTransmitting()) { - powered = true; + power = Math.max(other.getTransmittedStrength(), power); + if (power == 15) break; - } } for (Iterator iterator = network.iterator(); iterator.hasNext();) { @@ -120,7 +119,7 @@ public class RedstoneLinkNetworkHandler { if (!withinRange(actor, other)) continue; if (other.isListening()) - other.updateReceiver(powered); + other.updateReceiver(power); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/extractor/LinkedExtractorTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/extractor/LinkedExtractorTileEntity.java index ee01fdb0b..096246ca3 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/extractor/LinkedExtractorTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/extractor/LinkedExtractorTileEntity.java @@ -29,8 +29,8 @@ public class LinkedExtractorTileEntity extends ExtractorTileEntity { super.addBehaviours(behaviours); } - public void setSignal(boolean powered) { - receivedSignal = powered; + public void setSignal(int powered) { + receivedSignal = powered > 0; } @Override diff --git a/src/main/java/com/simibubi/create/content/logistics/block/redstone/RedstoneLinkBlock.java b/src/main/java/com/simibubi/create/content/logistics/block/redstone/RedstoneLinkBlock.java index 666455ea8..80ded0153 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/redstone/RedstoneLinkBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/redstone/RedstoneLinkBlock.java @@ -34,12 +34,13 @@ public class RedstoneLinkBlock extends ProperDirectionalBlock implements ITE 0; - } + int power = getPower(worldIn, pos); boolean previouslyPowered = state.get(POWERED); - - if (previouslyPowered != shouldPower) { + if (previouslyPowered != power > 0) worldIn.setBlockState(pos, state.cycle(POWERED), 2); - withTileEntityDo(worldIn, pos, te -> te.transmit(!previouslyPowered)); - } + + int transmit = power; + withTileEntityDo(worldIn, pos, te -> te.transmit(transmit)); + } + + private int getPower(World worldIn, BlockPos pos) { + int power = 0; + for (Direction direction : Iterate.directions) + power = Math.max(worldIn.getRedstonePower(pos.offset(direction), direction), power); + for (Direction direction : Iterate.directions) + power = Math.max(worldIn.getRedstonePower(pos.offset(direction), Direction.UP), power); + return power; } @Override @@ -94,7 +99,12 @@ public class RedstoneLinkBlock extends ProperDirectionalBlock implements ITE 0) != blockState.get(POWERED)) { + receivedSignalChanged = true; world.setBlockState(pos, blockState.cycle(POWERED)); + } + + if (receivedSignalChanged) { Direction attachedFace = blockState.get(RedstoneLinkBlock.FACING).getOpposite(); BlockPos attachedPos = pos.offset(attachedFace); + world.notifyNeighbors(pos, world.getBlockState(pos).getBlock()); world.notifyNeighbors(attachedPos, world.getBlockState(attachedPos).getBlock()); - return; } } @@ -112,4 +122,8 @@ public class RedstoneLinkTileEntity extends SmartTileEntity { return !getBlockState().get(RedstoneLinkBlock.RECEIVER); } + public int getReceivedSignal() { + return receivedSignal; + } + } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/transposer/LinkedTransposerTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/transposer/LinkedTransposerTileEntity.java index ee8fbd031..df24df53a 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/transposer/LinkedTransposerTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/transposer/LinkedTransposerTileEntity.java @@ -30,8 +30,8 @@ public class LinkedTransposerTileEntity extends TransposerTileEntity { super.addBehaviours(behaviours); } - public void setSignal(boolean powered) { - receivedSignal = powered; + public void setSignal(int powered) { + receivedSignal = powered > 0; } @Override diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java index 685d2c66e..9bfb26ab5 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java @@ -36,8 +36,8 @@ public class LinkBehaviour extends TileEntityBehaviour { public boolean newPosition; private Mode mode; - private Supplier transmission; - private Consumer signalCallback; + private Supplier transmission; + private Consumer signalCallback; protected LinkBehaviour(SmartTileEntity te, Pair slots) { super(te); @@ -50,7 +50,7 @@ public class LinkBehaviour extends TileEntityBehaviour { } public static LinkBehaviour receiver(SmartTileEntity te, Pair slots, - Consumer signalCallback) { + Consumer signalCallback) { LinkBehaviour behaviour = new LinkBehaviour(te, slots); behaviour.signalCallback = signalCallback; behaviour.mode = Mode.RECEIVE; @@ -58,7 +58,7 @@ public class LinkBehaviour extends TileEntityBehaviour { } public static LinkBehaviour transmitter(SmartTileEntity te, Pair slots, - Supplier transmission) { + Supplier transmission) { LinkBehaviour behaviour = new LinkBehaviour(te, slots); behaviour.transmission = transmission; behaviour.mode = Mode.TRANSMIT; @@ -81,14 +81,14 @@ public class LinkBehaviour extends TileEntityBehaviour { return mode == Mode.RECEIVE; } - public boolean isTransmitting() { - return mode == Mode.TRANSMIT && transmission.get(); + public int getTransmittedStrength() { + return mode == Mode.TRANSMIT ? transmission.get() : 0; } - public void updateReceiver(boolean networkPowered) { + public void updateReceiver(int networkPower) { if (!newPosition) return; - signalCallback.accept(networkPowered); + signalCallback.accept(networkPower); } public void notifySignalChange() { @@ -119,14 +119,18 @@ public class LinkBehaviour extends TileEntityBehaviour { @Override public void writeNBT(CompoundNBT compound) { super.writeNBT(compound); - compound.put("FrequencyFirst", frequencyFirst.getStack().write(new CompoundNBT())); - compound.put("FrequencyLast", frequencyLast.getStack().write(new CompoundNBT())); - compound.putLong("LastKnownPosition", tileEntity.getPos().toLong()); + compound.put("FrequencyFirst", frequencyFirst.getStack() + .write(new CompoundNBT())); + compound.put("FrequencyLast", frequencyLast.getStack() + .write(new CompoundNBT())); + compound.putLong("LastKnownPosition", tileEntity.getPos() + .toLong()); } @Override public void readNBT(CompoundNBT compound) { - long positionInTag = tileEntity.getPos().toLong(); + long positionInTag = tileEntity.getPos() + .toLong(); long positionKey = compound.getLong("LastKnownPosition"); newPosition = positionInTag != positionKey; @@ -172,7 +176,7 @@ public class LinkBehaviour extends TileEntityBehaviour { float scale; public SlotPositioning(Function> offsetsForState, - Function rotationForState) { + Function rotationForState) { offsets = offsetsForState; rotation = rotationForState; scale = 1;