diff --git a/src/main/java/refinedstorage/block/BlockCable.java b/src/main/java/refinedstorage/block/BlockCable.java index a470d8704..2aa216733 100755 --- a/src/main/java/refinedstorage/block/BlockCable.java +++ b/src/main/java/refinedstorage/block/BlockCable.java @@ -1,5 +1,6 @@ package refinedstorage.block; +import net.minecraft.block.Block; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.BlockStateContainer; @@ -9,7 +10,8 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -import refinedstorage.tile.TileCable; +import refinedstorage.tile.TileController; +import refinedstorage.tile.TileMachine; public class BlockCable extends BlockBase { public static final PropertyBool NORTH = PropertyBool.create("north"); @@ -40,12 +42,24 @@ public class BlockCable extends BlockBase { @Override public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) { return super.getActualState(state, world, pos) - .withProperty(NORTH, TileCable.hasConnectionWith(world, pos.north())) - .withProperty(EAST, TileCable.hasConnectionWith(world, pos.east())) - .withProperty(SOUTH, TileCable.hasConnectionWith(world, pos.south())) - .withProperty(WEST, TileCable.hasConnectionWith(world, pos.west())) - .withProperty(UP, TileCable.hasConnectionWith(world, pos.up())) - .withProperty(DOWN, TileCable.hasConnectionWith(world, pos.down())); + .withProperty(NORTH, hasConnectionWith(world, pos.north())) + .withProperty(EAST, hasConnectionWith(world, pos.east())) + .withProperty(SOUTH, hasConnectionWith(world, pos.south())) + .withProperty(WEST, hasConnectionWith(world, pos.west())) + .withProperty(UP, hasConnectionWith(world, pos.up())) + .withProperty(DOWN, hasConnectionWith(world, pos.down())); + } + + public static boolean hasConnectionWith(IBlockAccess world, BlockPos pos) { + Block block = world.getBlockState(pos).getBlock(); + + if (block instanceof BlockCable) { + return true; + } + + TileEntity tile = world.getTileEntity(pos); + + return tile instanceof TileMachine || tile instanceof TileController; } @Override @@ -60,16 +74,6 @@ public class BlockCable extends BlockBase { return getBoundingBox(state, world, pos); } - @Override - public boolean hasTileEntity(IBlockState state) { - return true; - } - - @Override - public TileEntity createTileEntity(World world, IBlockState state) { - return new TileCable(); - } - @Override public boolean isOpaqueCube(IBlockState state) { return false; diff --git a/src/main/java/refinedstorage/proxy/CommonProxy.java b/src/main/java/refinedstorage/proxy/CommonProxy.java index c83884244..a39b8e253 100755 --- a/src/main/java/refinedstorage/proxy/CommonProxy.java +++ b/src/main/java/refinedstorage/proxy/CommonProxy.java @@ -42,7 +42,6 @@ public class CommonProxy { NetworkRegistry.INSTANCE.registerGuiHandler(RefinedStorage.INSTANCE, new GuiHandler()); GameRegistry.registerTileEntity(TileController.class, ID + ":controller"); - GameRegistry.registerTileEntity(TileCable.class, ID + ":cable"); GameRegistry.registerTileEntity(TileGrid.class, ID + ":grid"); GameRegistry.registerTileEntity(TileDiskDrive.class, ID + ":disk_drive"); GameRegistry.registerTileEntity(TileExternalStorage.class, ID + ":external_storage"); diff --git a/src/main/java/refinedstorage/tile/MachineSearcher.java b/src/main/java/refinedstorage/tile/MachineSearcher.java new file mode 100755 index 000000000..721c45357 --- /dev/null +++ b/src/main/java/refinedstorage/tile/MachineSearcher.java @@ -0,0 +1,47 @@ +package refinedstorage.tile; + +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import refinedstorage.RefinedStorageBlocks; + +import java.util.List; + +public class MachineSearcher { + public static void search(TileController controller, BlockPos current, List visited, List machines) { + for (BlockPos visit : visited) { + if (visit.equals(current) || controller.getPos().equals(current)) { + return; + } + } + + visited.add(current); + + Block block = controller.getWorld().getBlockState(current).getBlock(); + TileEntity tile = controller.getWorld().getTileEntity(current); + + if (tile instanceof TileController) { + if (!tile.getPos().equals(controller.getPos())) { + controller.getWorld().createExplosion(null, tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), 4.5f, true); + } + } + + if (tile instanceof TileMachine) { + TileMachine machine = (TileMachine) tile; + + if (machine.getRedstoneMode().isEnabled(controller.getWorld(), tile.getPos())) { + machines.add(machine); + } else if (machine instanceof TileRelay) { + // If the relay is disabled we can't search any further + return; + } + } + + if (tile instanceof TileMachine || block == RefinedStorageBlocks.CABLE) { + for (EnumFacing dir : EnumFacing.VALUES) { + search(controller, current.offset(dir), visited, machines); + } + } + } +} diff --git a/src/main/java/refinedstorage/tile/TileCable.java b/src/main/java/refinedstorage/tile/TileCable.java deleted file mode 100755 index 6bdbd3e9f..000000000 --- a/src/main/java/refinedstorage/tile/TileCable.java +++ /dev/null @@ -1,64 +0,0 @@ -package refinedstorage.tile; - -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.IBlockAccess; - -import java.util.List; - -public class TileCable extends TileBase { - public static boolean hasConnectionWith(IBlockAccess world, BlockPos pos) { - TileEntity tile = world.getTileEntity(pos); - - return tile instanceof TileCable || tile instanceof TileMachine || tile instanceof TileController; - } - - public void addMachines(List visited, List machines, TileController controller) { - for (BlockPos visitedBlock : visited) { - if (visitedBlock.equals(pos)) { - return; - } - } - - visited.add(pos); - - for (EnumFacing dir : EnumFacing.VALUES) { - BlockPos newPos = pos.offset(dir); - - boolean alreadyVisited = false; - - for (BlockPos visitedBlock : visited) { - if (visitedBlock.equals(newPos)) { - alreadyVisited = true; - } - } - - if (alreadyVisited) { - continue; - } - - TileEntity tile = worldObj.getTileEntity(newPos); - - if (tile instanceof TileMachine && ((TileMachine) tile).getRedstoneMode().isEnabled(worldObj, newPos)) { - machines.add((TileMachine) tile); - - visited.add(newPos); - - if (tile instanceof TileRelay) { - for (EnumFacing relayDir : EnumFacing.VALUES) { - TileEntity nextToRelay = worldObj.getTileEntity(newPos.offset(relayDir)); - - if (nextToRelay instanceof TileCable) { - ((TileCable) nextToRelay).addMachines(visited, machines, controller); - } - } - } - } else if (tile instanceof TileCable) { - ((TileCable) tile).addMachines(visited, machines, controller); - } else if (tile instanceof TileController && !controller.getPos().equals(newPos)) { - worldObj.createExplosion(null, pos.getX(), pos.getY(), pos.getZ(), 4.5f, true); - } - } - } -} diff --git a/src/main/java/refinedstorage/tile/TileController.java b/src/main/java/refinedstorage/tile/TileController.java index 051f98f25..f00bf0f06 100755 --- a/src/main/java/refinedstorage/tile/TileController.java +++ b/src/main/java/refinedstorage/tile/TileController.java @@ -30,7 +30,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor private List machines = new ArrayList(); - private List visitedCables = new ArrayList(); + private List visited = new ArrayList(); private EnergyStorage energy = new EnergyStorage(32000); private int energyUsage; @@ -52,16 +52,12 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor if (!isActive()) { disconnectAll(); } else { - visitedCables.clear(); + visited.clear(); List newMachines = new ArrayList(); for (EnumFacing dir : EnumFacing.VALUES) { - TileEntity tile = worldObj.getTileEntity(pos.offset(dir)); - - if (tile instanceof TileCable) { - ((TileCable) tile).addMachines(visitedCables, newMachines, this); - } + MachineSearcher.search(this, pos.offset(dir), visited, newMachines); } for (TileMachine machine : machines) {