2016-01-12 12:23:26 -05:00
|
|
|
package WayofTime.bloodmagic.tile.routing;
|
|
|
|
|
2016-06-26 12:51:25 -04:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-01-12 17:05:56 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2016-01-12 12:23:26 -05:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-01-12 21:17:26 -05:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-06-26 12:51:25 -04:00
|
|
|
import net.minecraft.util.ITickable;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-01-12 12:23:26 -05:00
|
|
|
import net.minecraft.world.World;
|
2016-01-15 15:06:03 -05:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-06-26 12:51:25 -04:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
|
|
import WayofTime.bloodmagic.routing.IItemRoutingNode;
|
|
|
|
import WayofTime.bloodmagic.routing.IMasterRoutingNode;
|
|
|
|
import WayofTime.bloodmagic.routing.IRoutingNode;
|
|
|
|
import WayofTime.bloodmagic.tile.TileInventory;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
2016-06-26 12:51:25 -04:00
|
|
|
public class TileRoutingNode extends TileInventory implements IRoutingNode, IItemRoutingNode, ITickable
|
2016-01-12 12:23:26 -05:00
|
|
|
{
|
2016-06-26 12:51:25 -04:00
|
|
|
private int currentInput;
|
|
|
|
|
2016-01-14 08:27:09 -05:00
|
|
|
public TileRoutingNode(int size, String name)
|
|
|
|
{
|
|
|
|
super(size, name);
|
|
|
|
}
|
|
|
|
|
2016-06-26 12:51:25 -04:00
|
|
|
@Override
|
|
|
|
public void update()
|
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
if (!getWorld().isRemote)
|
2016-06-26 12:51:25 -04:00
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
currentInput = getWorld().isBlockIndirectlyGettingPowered(pos);
|
|
|
|
// currentInput = getWorld().getStrongPower(pos);
|
2016-06-26 12:51:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 12:23:26 -05:00
|
|
|
private BlockPos masterPos = BlockPos.ORIGIN;
|
|
|
|
private List<BlockPos> connectionList = new LinkedList<BlockPos>();
|
|
|
|
|
2016-01-12 17:05:56 -05:00
|
|
|
@Override
|
2016-09-07 17:46:06 -07:00
|
|
|
public NBTTagCompound serialize(NBTTagCompound tag)
|
2016-01-12 17:05:56 -05:00
|
|
|
{
|
2016-09-12 16:01:47 -07:00
|
|
|
super.serialize(tag);
|
2016-01-12 17:05:56 -05:00
|
|
|
NBTTagCompound masterTag = new NBTTagCompound();
|
|
|
|
masterTag.setInteger(Constants.NBT.X_COORD, masterPos.getX());
|
|
|
|
masterTag.setInteger(Constants.NBT.Y_COORD, masterPos.getY());
|
|
|
|
masterTag.setInteger(Constants.NBT.Z_COORD, masterPos.getZ());
|
|
|
|
tag.setTag(Constants.NBT.ROUTING_MASTER, masterTag);
|
|
|
|
|
|
|
|
NBTTagList tags = new NBTTagList();
|
|
|
|
for (BlockPos pos : connectionList)
|
|
|
|
{
|
|
|
|
NBTTagCompound posTag = new NBTTagCompound();
|
|
|
|
posTag.setInteger(Constants.NBT.X_COORD, pos.getX());
|
|
|
|
posTag.setInteger(Constants.NBT.Y_COORD, pos.getY());
|
|
|
|
posTag.setInteger(Constants.NBT.Z_COORD, pos.getZ());
|
|
|
|
tags.appendTag(posTag);
|
|
|
|
}
|
|
|
|
tag.setTag(Constants.NBT.ROUTING_CONNECTION, tags);
|
2016-05-19 17:43:33 -07:00
|
|
|
return tag;
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-07 17:46:06 -07:00
|
|
|
public void deserialize(NBTTagCompound tag)
|
2016-01-12 17:05:56 -05:00
|
|
|
{
|
2016-09-07 17:46:06 -07:00
|
|
|
super.deserialize(tag);
|
2016-01-15 19:36:44 -05:00
|
|
|
connectionList.clear();
|
2016-01-12 17:05:56 -05:00
|
|
|
NBTTagCompound masterTag = tag.getCompoundTag(Constants.NBT.ROUTING_MASTER);
|
|
|
|
masterPos = new BlockPos(masterTag.getInteger(Constants.NBT.X_COORD), masterTag.getInteger(Constants.NBT.Y_COORD), masterTag.getInteger(Constants.NBT.Z_COORD));
|
|
|
|
|
|
|
|
NBTTagList tags = tag.getTagList(Constants.NBT.ROUTING_CONNECTION, 10);
|
|
|
|
for (int i = 0; i < tags.tagCount(); i++)
|
|
|
|
{
|
|
|
|
NBTTagCompound blockTag = tags.getCompoundTagAt(i);
|
|
|
|
BlockPos newPos = new BlockPos(blockTag.getInteger(Constants.NBT.X_COORD), blockTag.getInteger(Constants.NBT.Y_COORD), blockTag.getInteger(Constants.NBT.Z_COORD));
|
|
|
|
connectionList.add(newPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeAllConnections()
|
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
TileEntity testTile = getWorld().getTileEntity(getMasterPos());
|
2016-01-12 17:05:56 -05:00
|
|
|
if (testTile instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
((IMasterRoutingNode) testTile).removeConnection(pos); // Remove this node from the master
|
|
|
|
}
|
|
|
|
for (BlockPos testPos : connectionList)
|
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
TileEntity tile = getWorld().getTileEntity(testPos);
|
2016-01-12 17:05:56 -05:00
|
|
|
if (tile instanceof IRoutingNode)
|
|
|
|
{
|
|
|
|
((IRoutingNode) tile).removeConnection(pos);
|
2016-04-12 15:25:16 -04:00
|
|
|
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(testPos), getWorld().getBlockState(testPos), 3);
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-15 19:36:44 -05:00
|
|
|
|
|
|
|
connectionList.clear();
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 12:23:26 -05:00
|
|
|
@Override
|
|
|
|
public void connectMasterToRemainingNode(World world, List<BlockPos> alreadyChecked, IMasterRoutingNode master)
|
|
|
|
{
|
2016-01-12 17:05:56 -05:00
|
|
|
this.masterPos = master.getBlockPos();
|
2016-01-12 12:23:26 -05:00
|
|
|
List<BlockPos> connectedList = this.getConnected();
|
|
|
|
for (BlockPos testPos : connectedList)
|
|
|
|
{
|
|
|
|
if (alreadyChecked.contains(testPos))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
alreadyChecked.add(testPos);
|
|
|
|
TileEntity tile = world.getTileEntity(testPos);
|
|
|
|
if (!(tile instanceof IRoutingNode))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
IRoutingNode node = (IRoutingNode) tile;
|
|
|
|
if (node.getMasterPos().equals(BlockPos.ORIGIN)) //If getMasterPos() returns the origin, the node is not connected to any master.
|
|
|
|
{
|
|
|
|
master.addNodeToList(node);
|
|
|
|
node.connectMasterToRemainingNode(world, alreadyChecked, master);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
master.addConnections(this.getBlockPos(), connectedList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockPos getBlockPos()
|
|
|
|
{
|
|
|
|
return this.getPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<BlockPos> getConnected()
|
|
|
|
{
|
|
|
|
return connectionList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockPos getMasterPos()
|
|
|
|
{
|
|
|
|
return masterPos;
|
|
|
|
}
|
|
|
|
|
2016-01-12 17:05:56 -05:00
|
|
|
@Override
|
|
|
|
public boolean isMaster(IMasterRoutingNode master)
|
|
|
|
{
|
|
|
|
BlockPos checkPos = master.getBlockPos();
|
2016-05-19 17:43:33 -07:00
|
|
|
return checkPos.equals(getMasterPos());
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 12:23:26 -05:00
|
|
|
@Override
|
|
|
|
public boolean isConnectionEnabled(BlockPos testPos)
|
|
|
|
{
|
2016-06-26 12:51:25 -04:00
|
|
|
return currentInput <= 0;
|
2016-01-12 12:23:26 -05:00
|
|
|
}
|
2016-01-12 17:05:56 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addConnection(BlockPos pos1)
|
|
|
|
{
|
|
|
|
if (!connectionList.contains(pos1))
|
|
|
|
{
|
2016-03-18 13:05:57 -07:00
|
|
|
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
|
2016-01-12 17:05:56 -05:00
|
|
|
connectionList.add(pos1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeConnection(BlockPos pos1)
|
|
|
|
{
|
|
|
|
if (connectionList.contains(pos1))
|
|
|
|
{
|
|
|
|
connectionList.remove(pos1);
|
2016-03-18 13:05:57 -07:00
|
|
|
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
2016-06-17 09:55:30 -04:00
|
|
|
|
|
|
|
if (pos1.equals(masterPos))
|
|
|
|
{
|
|
|
|
this.masterPos = BlockPos.ORIGIN;
|
|
|
|
}
|
2016-01-12 17:05:56 -05:00
|
|
|
}
|
2016-01-12 21:17:26 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isInventoryConnectedToSide(EnumFacing side)
|
|
|
|
{
|
2016-01-14 14:11:16 -05:00
|
|
|
return false;
|
2016-01-12 21:17:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-14 14:11:16 -05:00
|
|
|
public int getPriority(EnumFacing side)
|
2016-01-12 21:17:26 -05:00
|
|
|
{
|
2016-01-14 14:11:16 -05:00
|
|
|
return 0;
|
2016-01-12 21:17:26 -05:00
|
|
|
}
|
2016-01-15 15:06:03 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public double getMaxRenderDistanceSquared()
|
|
|
|
{
|
|
|
|
return 10000;
|
|
|
|
}
|
2016-01-12 12:23:26 -05:00
|
|
|
}
|