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