Mekanism-tilera-Edition/src/main/java/mekanism/common/transporter/TransporterStack.java

311 lines
6.6 KiB
Java
Raw Normal View History

2013-10-25 14:51:13 +02:00
package mekanism.common.transporter;
2013-08-30 21:20:52 +02:00
import java.util.ArrayList;
import java.util.List;
import mekanism.api.Coord4D;
2013-12-21 06:32:15 +01:00
import mekanism.api.EnumColor;
import mekanism.common.ILogisticalTransporter;
import mekanism.common.PacketHandler;
import mekanism.common.tile.TileEntityLogisticalSorter;
import mekanism.common.transporter.TransporterPathfinder.Destination;
2013-10-24 02:34:33 +02:00
import mekanism.common.util.TransporterUtils;
2014-11-10 22:53:29 +01:00
2013-08-30 21:20:52 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
2013-08-30 21:20:52 +02:00
2014-11-10 22:53:29 +01:00
import io.netty.buffer.ByteBuf;
public class TransporterStack
2013-08-30 21:20:52 +02:00
{
public ItemStack itemStack;
public int progress;
2013-10-25 23:11:15 +02:00
public EnumColor color = null;
public boolean initiatedPath = false;
public ForgeDirection idleDir = ForgeDirection.UNKNOWN;
public List<Coord4D> pathToTarget = new ArrayList<Coord4D>();
public Coord4D originalLocation;
public Coord4D homeLocation;
public Coord4D clientNext;
public Coord4D clientPrev;
public Path pathType;
public void write(ILogisticalTransporter tileEntity, ArrayList data)
{
if(color != null)
{
2013-10-24 02:34:33 +02:00
data.add(TransporterUtils.colors.indexOf(color));
}
else {
data.add(-1);
}
data.add(progress);
originalLocation.write(data);
data.add(pathType.ordinal());
if(pathToTarget.indexOf(Coord4D.get(tileEntity.getTile())) > 0)
2013-10-22 05:03:54 +02:00
{
data.add(true);
getNext(tileEntity).write(data);
}
else {
data.add(false);
}
getPrev(tileEntity).write(data);
data.add(itemStack);
}
public void read(ByteBuf dataStream)
{
int c = dataStream.readInt();
if(c != -1)
{
2013-10-24 02:34:33 +02:00
color = TransporterUtils.colors.get(c);
}
else {
color = null;
}
progress = dataStream.readInt();
originalLocation = Coord4D.read(dataStream);
pathType = Path.values()[dataStream.readInt()];
2013-10-22 05:03:54 +02:00
if(dataStream.readBoolean())
{
clientNext = Coord4D.read(dataStream);
2013-10-22 05:03:54 +02:00
}
clientPrev = Coord4D.read(dataStream);
itemStack = PacketHandler.readStack(dataStream);
}
2013-08-30 21:20:52 +02:00
public void write(NBTTagCompound nbtTags)
{
if(color != null)
{
2013-10-24 02:34:33 +02:00
nbtTags.setInteger("color", TransporterUtils.colors.indexOf(color));
}
nbtTags.setInteger("progress", progress);
nbtTags.setTag("originalLocation", originalLocation.write(new NBTTagCompound()));
nbtTags.setInteger("idleDir", idleDir.ordinal());
2013-11-11 01:35:45 +01:00
if(homeLocation != null)
{
nbtTags.setTag("homeLocation", homeLocation.write(new NBTTagCompound()));
}
nbtTags.setInteger("pathType", pathType.ordinal());
2013-10-22 05:03:54 +02:00
itemStack.writeToNBT(nbtTags);
2013-08-30 21:20:52 +02:00
}
2013-10-25 15:51:13 +02:00
public void read(NBTTagCompound nbtTags)
2013-08-30 21:20:52 +02:00
{
if(nbtTags.hasKey("color"))
{
2013-10-24 02:34:33 +02:00
color = TransporterUtils.colors.get(nbtTags.getInteger("color"));
}
progress = nbtTags.getInteger("progress");
originalLocation = Coord4D.read(nbtTags.getCompoundTag("originalLocation"));
idleDir = ForgeDirection.values()[nbtTags.getInteger("idleDir")];
2013-11-11 01:35:45 +01:00
if(nbtTags.hasKey("homeLocation"))
{
homeLocation = Coord4D.read(nbtTags.getCompoundTag("homeLocation"));
}
pathType = Path.values()[nbtTags.getInteger("pathType")];
2013-10-22 05:03:54 +02:00
itemStack = ItemStack.loadItemStackFromNBT(nbtTags);
2013-08-30 21:20:52 +02:00
}
2013-10-25 15:51:13 +02:00
public static TransporterStack readFromNBT(NBTTagCompound nbtTags)
2013-10-22 17:08:28 +02:00
{
TransporterStack stack = new TransporterStack();
2013-10-25 15:51:13 +02:00
stack.read(nbtTags);
2013-10-25 15:51:13 +02:00
return stack;
}
public static TransporterStack readFromPacket(ByteBuf dataStream)
2013-10-25 15:51:13 +02:00
{
TransporterStack stack = new TransporterStack();
stack.read(dataStream);
2013-10-22 17:08:28 +02:00
return stack;
}
public boolean hasPath()
{
2013-11-25 22:34:59 +01:00
return pathToTarget != null && pathToTarget.size() >= 2;
}
public ItemStack recalculatePath(ILogisticalTransporter tileEntity, int min)
{
2013-11-25 22:34:59 +01:00
Destination newPath = TransporterPathfinder.getNewBasePath(tileEntity, this, min);
if(newPath == null)
{
return itemStack;
}
pathToTarget = newPath.path;
pathType = Path.DEST;
idleDir = ForgeDirection.UNKNOWN;
initiatedPath = true;
return newPath.rejected;
}
public ItemStack recalculateRRPath(TileEntityLogisticalSorter outputter, ILogisticalTransporter tileEntity, int min)
2013-11-08 17:13:51 +01:00
{
2013-11-25 22:34:59 +01:00
Destination newPath = TransporterPathfinder.getNewRRPath(tileEntity, this, outputter, min);
2013-11-08 17:13:51 +01:00
if(newPath == null)
{
return itemStack;
2013-11-08 17:13:51 +01:00
}
pathToTarget = newPath.path;
pathType = Path.DEST;
idleDir = ForgeDirection.UNKNOWN;
2013-11-08 17:13:51 +01:00
initiatedPath = true;
return newPath.rejected;
2013-11-08 17:13:51 +01:00
}
public boolean calculateIdle(ILogisticalTransporter tileEntity)
{
List<Coord4D> newPath = TransporterPathfinder.getIdlePath(tileEntity, this);
if(newPath == null)
{
return false;
}
if(pathType == Path.HOME)
{
idleDir = ForgeDirection.UNKNOWN;
}
pathToTarget = newPath;
originalLocation = Coord4D.get(tileEntity.getTile());
initiatedPath = true;
return true;
}
public boolean isFinal(ILogisticalTransporter tileEntity)
{
return pathToTarget.indexOf(Coord4D.get(tileEntity.getTile())) == (pathType == Path.NONE ? 0 : 1);
}
public Coord4D getNext(ILogisticalTransporter tileEntity)
{
if(!tileEntity.getTile().getWorldObj().isRemote)
{
int index = pathToTarget.indexOf(Coord4D.get(tileEntity.getTile()))-1;
if(index < 0)
{
return null;
}
return pathToTarget.get(index);
}
else {
return clientNext;
}
}
public Coord4D getPrev(ILogisticalTransporter tileEntity)
{
if(!tileEntity.getTile().getWorldObj().isRemote)
{
int index = pathToTarget.indexOf(Coord4D.get(tileEntity.getTile()))+1;
if(index < pathToTarget.size())
{
return pathToTarget.get(index);
}
else {
return originalLocation;
}
}
else {
return clientPrev;
}
}
public int getSide(ILogisticalTransporter tileEntity)
{
if(progress < 50)
{
if(getPrev(tileEntity) != null)
{
return Coord4D.get(tileEntity.getTile()).sideDifference(getPrev(tileEntity)).ordinal();
}
}
else if(progress == 50)
{
if(getNext(tileEntity) != null)
{
return getNext(tileEntity).sideDifference(Coord4D.get(tileEntity.getTile())).ordinal();
}
}
else if(progress > 50)
2013-10-22 05:03:54 +02:00
{
if(getNext(tileEntity) != null)
{
return getNext(tileEntity).sideDifference(Coord4D.get(tileEntity.getTile())).ordinal();
}
}
2013-10-22 05:03:54 +02:00
return 0;
}
2013-12-21 06:32:15 +01:00
public boolean canInsertToTransporter(TileEntity tileEntity, ForgeDirection side)
{
if(!(tileEntity instanceof ILogisticalTransporter))
{
return false;
}
TileEntity from = Coord4D.get(tileEntity).getFromSide(side.getOpposite()).getTileEntity(tileEntity.getWorldObj());
ILogisticalTransporter transporter = (ILogisticalTransporter)tileEntity;
if(!transporter.canConnectMutual(side.getOpposite()))
2013-12-21 06:32:15 +01:00
{
return false;
}
return transporter.getColor() == color || transporter.getColor() == null;
}
public Coord4D getDest()
{
return pathToTarget.get(0);
}
public static enum Path
{
DEST, HOME, NONE
}
2013-08-30 21:20:52 +02:00
}