machines are now connected to the controller
This commit is contained in:
@@ -2,8 +2,10 @@ package storagecraft;
|
||||
|
||||
import storagecraft.block.BlockCable;
|
||||
import storagecraft.block.BlockController;
|
||||
import storagecraft.block.BlockGrid;
|
||||
|
||||
public class SCBlocks {
|
||||
public static final BlockController CONTROLLER = new BlockController();
|
||||
public static final BlockCable CABLE = new BlockCable();
|
||||
public static final BlockGrid GRID = new BlockGrid();
|
||||
}
|
||||
|
@@ -14,4 +14,11 @@ public class BlockController extends BlockSC implements ITileEntityProvider {
|
||||
public TileEntity createNewTileEntity(World world, int meta) {
|
||||
return new TileController();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPreDestroy(World world, int x, int y, int z, int meta) {
|
||||
((TileController) world.getTileEntity(x, y, z)).onDestroyed();
|
||||
|
||||
super.onBlockPreDestroy(world, x, y, z, meta);
|
||||
}
|
||||
}
|
||||
|
17
src/main/java/storagecraft/block/BlockGrid.java
Normal file
17
src/main/java/storagecraft/block/BlockGrid.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package storagecraft.block;
|
||||
|
||||
import net.minecraft.block.ITileEntityProvider;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
public class BlockGrid extends BlockSC implements ITileEntityProvider {
|
||||
public BlockGrid() {
|
||||
super("grid");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity createNewTileEntity(World world, int meta) {
|
||||
return new TileGrid();
|
||||
}
|
||||
}
|
@@ -5,21 +5,24 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import static storagecraft.SC.NETWORK;
|
||||
import storagecraft.SC;
|
||||
import storagecraft.SCBlocks;
|
||||
import storagecraft.network.MessageTileUpdate;
|
||||
import storagecraft.tile.TileCable;
|
||||
import storagecraft.tile.TileController;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
public class CommonProxy {
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT);
|
||||
SC.NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT);
|
||||
|
||||
GameRegistry.registerTileEntity(TileController.class, "controller");
|
||||
GameRegistry.registerTileEntity(TileCable.class, "cable");
|
||||
GameRegistry.registerTileEntity(TileGrid.class, "grid");
|
||||
|
||||
GameRegistry.registerBlock(SCBlocks.CONTROLLER, "controller");
|
||||
GameRegistry.registerBlock(SCBlocks.CABLE, "cable");
|
||||
GameRegistry.registerBlock(SCBlocks.GRID, "grid");
|
||||
}
|
||||
|
||||
public void init(FMLInitializationEvent e) {
|
||||
|
@@ -1,4 +1,7 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
public interface IMachine {
|
||||
public void onConnected(TileController controller);
|
||||
|
||||
public void onDisconnected();
|
||||
}
|
||||
|
@@ -1,20 +1,69 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import storagecraft.block.BlockCable;
|
||||
|
||||
public class TileCable extends TileSC {
|
||||
public boolean hasConnection(ForgeDirection dir) {
|
||||
Block block = worldObj.getBlock(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
class VisitedCable {
|
||||
public int x;
|
||||
public int y;
|
||||
public int z;
|
||||
|
||||
if (!(block instanceof BlockCable)) {
|
||||
public VisitedCable(int x, int y, int z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isCable(World world, int x, int y, int z, ForgeDirection dir) {
|
||||
Block block = world.getBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ);
|
||||
|
||||
return block instanceof BlockCable;
|
||||
}
|
||||
|
||||
public boolean hasConnection(ForgeDirection dir) {
|
||||
if (!isCable(worldObj, xCoord, yCoord, zCoord, dir)) {
|
||||
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
return tile instanceof IMachine;
|
||||
return tile instanceof IMachine || tile instanceof TileController;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<IMachine> findMachines() {
|
||||
return findMachines(new ArrayList(), true);
|
||||
}
|
||||
|
||||
private List<IMachine> findMachines(List<VisitedCable> visited, boolean ignoreController) {
|
||||
List<IMachine> machines = new ArrayList<IMachine>();
|
||||
|
||||
for (VisitedCable visitedCable : visited) {
|
||||
if (visitedCable.x == xCoord && visitedCable.y == yCoord && visitedCable.z == zCoord) {
|
||||
return machines;
|
||||
}
|
||||
}
|
||||
|
||||
visited.add(new VisitedCable(xCoord, yCoord, zCoord));
|
||||
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
if (tile instanceof IMachine) {
|
||||
machines.add((IMachine) tile);
|
||||
} else if (tile instanceof TileCable) {
|
||||
machines.addAll(((TileCable) tile).findMachines(visited, false));
|
||||
} else if (tile instanceof TileController && !ignoreController) {
|
||||
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 4.5f, true);
|
||||
}
|
||||
}
|
||||
|
||||
return machines;
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,55 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
public class TileController extends TileSC implements IMachine {
|
||||
public List<TileSC> getMachines() {
|
||||
return null;
|
||||
public class TileController extends TileSC {
|
||||
private List<IMachine> connectedMachines = new ArrayList<IMachine>();
|
||||
|
||||
private int ticks = 0;
|
||||
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
super.updateEntity();
|
||||
|
||||
if (!worldObj.isRemote) {
|
||||
ticks++;
|
||||
|
||||
if (ticks % 40 == 0) {
|
||||
List<IMachine> machines = new ArrayList<IMachine>();
|
||||
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
if (tile instanceof TileCable) {
|
||||
machines.addAll(((TileCable) tile).findMachines());
|
||||
}
|
||||
}
|
||||
|
||||
for (IMachine machine : connectedMachines) {
|
||||
if (!machines.contains(machine)) {
|
||||
machine.onDisconnected();
|
||||
}
|
||||
}
|
||||
|
||||
for (IMachine machine : machines) {
|
||||
if (!connectedMachines.contains(machine)) {
|
||||
machine.onConnected(this);
|
||||
}
|
||||
}
|
||||
|
||||
connectedMachines = machines;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onDestroyed() {
|
||||
for (IMachine machine : connectedMachines) {
|
||||
machine.onDisconnected();
|
||||
}
|
||||
|
||||
connectedMachines.clear();
|
||||
}
|
||||
}
|
||||
|
11
src/main/java/storagecraft/tile/TileGrid.java
Normal file
11
src/main/java/storagecraft/tile/TileGrid.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
public class TileGrid extends TileSC implements IMachine {
|
||||
@Override
|
||||
public void onConnected(TileController controller) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user