From abe917cd545d4e49894c02f5ad4ae88cccf22d4a Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 7 Apr 2018 10:23:11 +0100 Subject: [PATCH] Fix peripherals showing up when they shouldn't on world load When initially attaching a modem, the adjacent computer would not show up on its own peripheral list (like in vanilla CC). However, it would show up when the chunk was reloaded as peripherals were added through a different method. This prevents such behaviour, always hiding the remote peripheral from the object which provides it. Closes #20 --- .../shared/peripheral/modem/TileCable.java | 13 ++++++++----- .../shared/peripheral/modem/TileWiredModemFull.java | 11 +++++++---- .../peripheral/modem/WiredModemPeripheral.java | 12 +++--------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java index f45707784..64f0dcd37 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java @@ -76,7 +76,7 @@ public class TileCable extends TileModemBase public Vec3d getPosition() { BlockPos pos = m_entity.getPos(); - return new Vec3d( (double) pos.getX() + 0.5, (double) pos.getY() + 0.5, (double) pos.getZ() + 0.5 ); + return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ); } @Nonnull @@ -92,10 +92,7 @@ public class TileCable extends TileModemBase @Override protected void attachPeripheral( String name, IPeripheral peripheral ) { - if( !name.equals( m_entity.getConnectedPeripheralName() ) ) - { - ((WiredModemPeripheral) m_entity.m_modem).attachPeripheral( name, peripheral ); - } + ((WiredModemPeripheral) m_entity.m_modem).attachPeripheral( name, peripheral ); } @Override @@ -133,6 +130,12 @@ public class TileCable extends TileModemBase m_node = m_cable.getNode(); return new WiredModemPeripheral( m_cable ) { + @Override + protected boolean canSeePeripheral( @Nonnull String peripheralName ) + { + return !peripheralName.equals( getConnectedPeripheralName() ); + } + @Nonnull @Override public Vec3d getPosition() diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java index f7afde08b..3acb04fbd 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java @@ -48,10 +48,7 @@ public class TileWiredModemFull extends TilePeripheralBase for( int i = 0; i < 6; i++ ) { WiredModemPeripheral modem = m_entity.m_modems[i]; - if( modem != null && !name.equals( m_entity.getCachedPeripheralName( EnumFacing.VALUES[i] ) ) ) - { - modem.attachPeripheral( name, peripheral ); - } + if( modem != null ) modem.attachPeripheral( name, peripheral ); } } @@ -416,6 +413,12 @@ public class TileWiredModemFull extends TilePeripheralBase { peripheral = m_modems[side.ordinal()] = new WiredModemPeripheral( m_element ) { + @Override + protected boolean canSeePeripheral( @Nonnull String peripheralName ) + { + return !peripheralName.equals( getCachedPeripheralName( side ) ); + } + @Nonnull @Override public Vec3d getPosition() diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/WiredModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/WiredModemPeripheral.java index f139dfc4f..f92dc2a6c 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/WiredModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/WiredModemPeripheral.java @@ -10,7 +10,6 @@ import dan200.computercraft.api.network.wired.IWiredNode; import dan200.computercraft.api.network.wired.IWiredSender; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; -import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import javax.annotation.Nonnull; @@ -20,7 +19,7 @@ import java.util.Map; import static dan200.computercraft.core.apis.ArgumentHelper.getString; -public class WiredModemPeripheral extends ModemPeripheral implements IWiredSender +public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender { private final WiredModemElement modem; @@ -57,12 +56,7 @@ public class WiredModemPeripheral extends ModemPeripheral implements IWiredSende return modem.getWorld(); } - @Nonnull - @Override - public Vec3d getPosition() - { - return modem.getPosition(); - } + protected abstract boolean canSeePeripheral( @Nonnull String peripheralName ); //endregion //region IPeripheral @@ -223,7 +217,7 @@ public class WiredModemPeripheral extends ModemPeripheral implements IWiredSende private void attachPeripheralImpl( String periphName, IPeripheral peripheral ) { - if( !peripheralWrappers.containsKey( periphName ) ) + if( !peripheralWrappers.containsKey( periphName ) && canSeePeripheral( periphName ) ) { RemotePeripheralWrapper wrapper = new RemotePeripheralWrapper( modem, peripheral, getComputer(), periphName ); peripheralWrappers.put( periphName, wrapper );