Very unstable improved machine searching part5

This commit is contained in:
Raoul Van den Berge
2016-05-14 10:25:10 +02:00
parent d9be632072
commit f50cadffb1
3 changed files with 27 additions and 17 deletions

View File

@@ -54,11 +54,7 @@ public abstract class BlockMachine extends BlockBase {
super.onNeighborBlockChange(world, pos, state, neighborBlock); super.onNeighborBlockChange(world, pos, state, neighborBlock);
if (!world.isRemote) { if (!world.isRemote) {
TileMachine machine = (TileMachine) world.getTileEntity(pos); ((TileMachine) world.getTileEntity(pos)).searchController();
if (!machine.isConnected()) {
machine.searchController();
}
} }
} }
} }

View File

@@ -26,6 +26,11 @@ public class ControllerSearcher {
Block block = world.getBlockState(current).getBlock(); Block block = world.getBlockState(current).getBlock();
if (tile instanceof TileMachine || block == RefinedStorageBlocks.CABLE) { 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) { for (EnumFacing dir : EnumFacing.VALUES) {
TileController controller = search(world, current.offset(dir), visited); TileController controller = search(world, current.offset(dir), visited);

View File

@@ -28,14 +28,14 @@ public abstract class TileMachine extends TileBase implements INetworkTile, IRed
TileController newController = ControllerSearcher.search(worldObj, pos, visited); TileController newController = ControllerSearcher.search(worldObj, pos, visited);
if (newController != null) { if (controller == null) {
this.controller = newController; if (newController != null) {
onConnected(newController);
onConnected(); }
} else if (this.controller != null) { } else {
this.controller = null; if (newController == null) {
onDisconnected();
onDisconnected(); }
} }
} }
@@ -50,24 +50,33 @@ public abstract class TileMachine extends TileBase implements INetworkTile, IRed
super.update(); super.update();
} }
public void onConnected() { public void onConnected(TileController controller) {
connected = true; this.controller = controller;
this.connected = true;
if (worldObj.getBlockState(pos).getBlock() == block) { if (worldObj.getBlockState(pos).getBlock() == block) {
worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, true)); worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, true));
} }
worldObj.notifyNeighborsOfStateChange(pos, block);
controller.addMachine(this); controller.addMachine(this);
} }
public void onDisconnected() { public void onDisconnected() {
connected = false; this.connected = false;
if (worldObj.getBlockState(pos).getBlock() == block) { if (worldObj.getBlockState(pos).getBlock() == block) {
worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, false)); 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() { public boolean isConnected() {