package WayofTime.bloodmagic.routing; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraft.world.World; public class TileMasterRoutingNode extends TileEntity implements IMasterRoutingNode { // A list of connections private HashMap> connectionMap = new HashMap>(); private List generalNodeList = new LinkedList(); @Override public boolean isConnected(List path, BlockPos nodePos) { if (!connectionMap.containsKey(nodePos)) { return false; } TileEntity tile = worldObj.getTileEntity(nodePos); if (!(tile instanceof IRoutingNode)) { connectionMap.remove(nodePos); return false; } IRoutingNode node = (IRoutingNode) tile; List connectionList = node.getConnected(); List testPath = path.subList(0, path.size()); testPath.add(nodePos); for (BlockPos testPos : connectionList) { if (testPath.contains(testPos)) { continue; } if (testPos.equals(this.getPos()) && node.isConnectionEnabled(testPos)) { path.clear(); path.addAll(testPath); return true; } else if (NodeHelper.isNodeConnectionEnabled(worldObj, node, testPos)) { if (isConnected(testPath, testPos)) { path.clear(); path.addAll(testPath); return true; } } } return false; } @Override public boolean isConnectionEnabled(BlockPos testPos) { return true; } @Override public void addNodeToList(IRoutingNode node) { BlockPos newPos = node.getBlockPos(); if (!generalNodeList.contains(newPos)) { generalNodeList.add(newPos); } //TODO: make more node lists so that you can more easily differentiate the different types of nodes } @Override public void addConnections(BlockPos pos, List connectionList) { for (BlockPos testPos : connectionList) { addConnection(pos, testPos); } } @Override public void addConnection(BlockPos pos1, BlockPos pos2) { if (connectionMap.containsKey(pos1) && !connectionMap.get(pos1).contains(pos2)) { connectionMap.get(pos1).add(pos2); } else { List list = new LinkedList(); list.add(pos2); connectionMap.put(pos1, list); } if (connectionMap.containsKey(pos2) && !connectionMap.get(pos2).contains(pos1)) { connectionMap.get(pos2).add(pos1); } else { List list = new LinkedList(); list.add(pos1); connectionMap.put(pos2, list); } } @Override public void removeConnection(BlockPos pos1, BlockPos pos2) { if (connectionMap.containsKey(pos1)) { List posList = connectionMap.get(pos1); posList.remove(pos2); if (posList.isEmpty()) { connectionMap.remove(pos1); } } if (connectionMap.containsKey(pos2)) { List posList = connectionMap.get(pos2); posList.remove(pos1); if (posList.isEmpty()) { connectionMap.remove(pos2); } } } @Override public void connectMasterToRemainingNode(World world, List alreadyChecked, IMasterRoutingNode master) { return; } @Override public BlockPos getBlockPos() { return this.getPos(); } @Override public List getConnected() { return new LinkedList(); } @Override public BlockPos getMasterPos() { return this.getPos(); } }