diff --git a/src/main/java/refinedstorage/block/BlockMachine.java b/src/main/java/refinedstorage/block/BlockMachine.java index f3439f0dc..fd3119cc8 100755 --- a/src/main/java/refinedstorage/block/BlockMachine.java +++ b/src/main/java/refinedstorage/block/BlockMachine.java @@ -54,11 +54,7 @@ public abstract class BlockMachine extends BlockBase { super.onNeighborBlockChange(world, pos, state, neighborBlock); if (!world.isRemote) { - TileMachine machine = (TileMachine) world.getTileEntity(pos); - - if (!machine.isConnected()) { - machine.searchController(); - } + ((TileMachine) world.getTileEntity(pos)).searchController(); } } } diff --git a/src/main/java/refinedstorage/tile/ControllerSearcher.java b/src/main/java/refinedstorage/tile/ControllerSearcher.java index b68133635..dec8fbf83 100755 --- a/src/main/java/refinedstorage/tile/ControllerSearcher.java +++ b/src/main/java/refinedstorage/tile/ControllerSearcher.java @@ -26,6 +26,11 @@ public class ControllerSearcher { Block block = world.getBlockState(current).getBlock(); if (tile instanceof TileMachine || block == RefinedStorageBlocks.CABLE) { + // We need to have visited more than 1 tile so that the relay can find a controller for itself + if (visited.size() > 1 && tile instanceof TileRelay && !((TileRelay) tile).isConnected()) { + return null; + } + for (EnumFacing dir : EnumFacing.VALUES) { TileController controller = search(world, current.offset(dir), visited); diff --git a/src/main/java/refinedstorage/tile/TileMachine.java b/src/main/java/refinedstorage/tile/TileMachine.java index 096016142..091a1d44f 100755 --- a/src/main/java/refinedstorage/tile/TileMachine.java +++ b/src/main/java/refinedstorage/tile/TileMachine.java @@ -28,14 +28,14 @@ public abstract class TileMachine extends TileBase implements INetworkTile, IRed TileController newController = ControllerSearcher.search(worldObj, pos, visited); - if (newController != null) { - this.controller = newController; - - onConnected(); - } else if (this.controller != null) { - this.controller = null; - - onDisconnected(); + if (controller == null) { + if (newController != null) { + onConnected(newController); + } + } else { + if (newController == null) { + onDisconnected(); + } } } @@ -50,24 +50,33 @@ public abstract class TileMachine extends TileBase implements INetworkTile, IRed super.update(); } - public void onConnected() { - connected = true; + public void onConnected(TileController controller) { + this.controller = controller; + this.connected = true; if (worldObj.getBlockState(pos).getBlock() == block) { worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, true)); } + worldObj.notifyNeighborsOfStateChange(pos, block); + controller.addMachine(this); } public void onDisconnected() { - connected = false; + this.connected = false; if (worldObj.getBlockState(pos).getBlock() == block) { worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, false)); } - controller.removeMachine(this); + // I have no idea why this is needed + if (controller != null) { + this.controller.removeMachine(this); + this.controller = null; + } + + worldObj.notifyNeighborsOfStateChange(pos, block); } public boolean isConnected() {