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);
if (!world.isRemote) {
TileMachine machine = (TileMachine) world.getTileEntity(pos);
if (!machine.isConnected()) {
machine.searchController();
}
((TileMachine) world.getTileEntity(pos)).searchController();
}
}
}

View File

@@ -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);

View File

@@ -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() {