Mekanism-tilera-Edition/common/mekanism/common/TransporterStack.java

220 lines
4.5 KiB
Java
Raw Normal View History

2013-08-30 21:20:52 +02:00
package mekanism.common;
import java.util.ArrayList;
import java.util.List;
import mekanism.api.EnumColor;
import mekanism.api.Object3D;
import mekanism.common.tileentity.TileEntityLogisticalTransporter;
2013-08-30 21:20:52 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2013-08-30 21:20:52 +02:00
import com.google.common.io.ByteArrayDataInput;
2013-08-30 21:20:52 +02:00
public class TransporterStack
{
public ItemStack itemStack;
public int progress;
public EnumColor color;
public boolean initiatedPath = false;
public List<Object3D> pathToTarget = new ArrayList<Object3D>();
public Object3D originalLocation;
public Object3D clientNext;
public Object3D clientPrev;
public boolean noTarget = false;
2013-10-22 17:08:28 +02:00
public boolean clientFirstTick = true;
public void write(TileEntityLogisticalTransporter tileEntity, ArrayList data)
{
if(color != null)
{
data.add(color.ordinal());
}
else {
data.add(-1);
}
data.add(progress);
data.add(noTarget);
2013-10-22 05:03:54 +02:00
if(pathToTarget.indexOf(Object3D.get(tileEntity)) > 0)
2013-10-22 05:03:54 +02:00
{
data.add(true);
getNext(tileEntity).write(data);
}
else {
data.add(false);
}
getPrev(tileEntity).write(data);
2013-10-22 05:03:54 +02:00
data.add(itemStack.itemID);
data.add(itemStack.stackSize);
data.add(itemStack.getItemDamage());
}
public void read(ByteArrayDataInput dataStream)
{
int c = dataStream.readInt();
if(c != -1)
{
color = EnumColor.values()[c];
}
else {
color = null;
}
progress = dataStream.readInt();
noTarget = dataStream.readBoolean();
2013-10-22 05:03:54 +02:00
if(dataStream.readBoolean())
{
clientNext = Object3D.read(dataStream);
}
clientPrev = Object3D.read(dataStream);
2013-10-22 05:03:54 +02:00
itemStack = new ItemStack(dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
}
2013-08-30 21:20:52 +02:00
public void write(NBTTagCompound nbtTags)
{
if(color != null)
{
nbtTags.setInteger("color", color.ordinal());
}
nbtTags.setInteger("progress", progress);
originalLocation.write(nbtTags);
nbtTags.setBoolean("noTarget", noTarget);
2013-10-22 05:03:54 +02:00
itemStack.writeToNBT(nbtTags);
2013-08-30 21:20:52 +02:00
}
2013-10-22 17:08:28 +02:00
public void readFromNBT(NBTTagCompound nbtTags)
2013-08-30 21:20:52 +02:00
{
if(nbtTags.hasKey("color"))
{
color = EnumColor.values()[nbtTags.getInteger("color")];
}
progress = nbtTags.getInteger("progress");
originalLocation = Object3D.read(nbtTags);
noTarget = nbtTags.getBoolean("noTarget");
2013-10-22 05:03:54 +02:00
itemStack = ItemStack.loadItemStackFromNBT(nbtTags);
2013-08-30 21:20:52 +02:00
}
2013-10-22 17:08:28 +02:00
public static TransporterStack read(NBTTagCompound nbtTags)
{
TransporterStack stack = new TransporterStack();
stack.readFromNBT(nbtTags);
return stack;
}
public boolean hasPath()
{
return pathToTarget != null;
}
public boolean recalculatePath(TileEntityLogisticalTransporter tileEntity)
{
List<Object3D> newPath = TransporterPathfinder.getNewPath(tileEntity, this);
if(newPath == null)
{
return false;
}
pathToTarget = newPath;
noTarget = false;
initiatedPath = true;
return true;
}
public void calculateIdle(TileEntityLogisticalTransporter tileEntity)
{
pathToTarget = TransporterPathfinder.getIdlePath(tileEntity, this);
noTarget = true;
2013-10-22 05:03:54 +02:00
originalLocation = Object3D.get(tileEntity);
initiatedPath = true;
}
public boolean isFinal(TileEntityLogisticalTransporter tileEntity)
{
return pathToTarget.indexOf(Object3D.get(tileEntity)) == (noTarget ? 0 : 1);
}
public Object3D getNext(TileEntityLogisticalTransporter tileEntity)
{
if(!tileEntity.worldObj.isRemote)
{
int index = pathToTarget.indexOf(Object3D.get(tileEntity))-1;
return pathToTarget.get(index);
}
else {
return clientNext;
}
}
public Object3D getPrev(TileEntityLogisticalTransporter tileEntity)
{
if(!tileEntity.worldObj.isRemote)
{
int index = pathToTarget.indexOf(Object3D.get(tileEntity))+1;
if(index < pathToTarget.size())
{
return pathToTarget.get(index);
}
else {
return originalLocation;
}
}
else {
return clientPrev;
}
}
public int getSide(TileEntityLogisticalTransporter tileEntity)
{
if(progress < 50)
{
return Object3D.get(tileEntity).sideDifference(getPrev(tileEntity)).ordinal();
}
2013-10-22 05:03:54 +02:00
else if(progress > 50)
{
return getNext(tileEntity).sideDifference(Object3D.get(tileEntity)).ordinal();
}
2013-10-22 05:03:54 +02:00
return 0;
}
public boolean canInsert(TileEntity tileEntity)
{
if(!(tileEntity instanceof TileEntityLogisticalTransporter))
{
return false;
}
TileEntityLogisticalTransporter transporter = (TileEntityLogisticalTransporter)tileEntity;
return transporter.color == color || transporter.color == null;
}
public Object3D getDest()
{
return pathToTarget.get(0);
}
2013-08-30 21:20:52 +02:00
}