Transporters first try to send the item back home before idling

This commit is contained in:
Aidan Brady committed 2013-11-09 15:30:30 -05:00
1 parent 5a08cb1b90
commit dfd2985357
7 files changed
+312 -104

No files matched your search

+3 -1
View File
@@ -57,12 +57,14 @@ public class Object3D
return world.getBlockTileEntity(xCoord, yCoord, zCoord);
}
public void write(NBTTagCompound nbtTags)
public NBTTagCompound write(NBTTagCompound nbtTags)
{
nbtTags.setInteger("x", xCoord);
nbtTags.setInteger("y", yCoord);
nbtTags.setInteger("z", zCoord);
nbtTags.setInteger("dimensionId", dimensionId);
return nbtTags;
}
public void write(ArrayList data)
@@ -102,7 +102,7 @@ public class TileComponentEjector implements ITileComponent, IEjector
if(tile instanceof IInventory && !(tile instanceof TileEntityLogisticalTransporter))
{
stack = TransporterUtils.putStackInInventory((IInventory)tile, stack, side.ordinal());
stack = TransporterUtils.putStackInInventory((IInventory)tile, stack, side.ordinal(), false);
}
else if(tile instanceof TileEntityLogisticalTransporter)
{
@@ -14,6 +14,7 @@ import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.network.PacketTileEntity;
import mekanism.common.transporter.InvStack;
import mekanism.common.transporter.TransporterFilter;
import mekanism.common.transporter.TransporterStack;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.TransporterUtils;
import net.minecraft.entity.player.EntityPlayer;
@@ -367,6 +368,35 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen
return data;
}
public boolean canSendHome(ItemStack stack)
{
TileEntity back = Object3D.get(this).getFromSide(ForgeDirection.getOrientation(facing).getOpposite()).getTileEntity(worldObj);
if(back instanceof IInventory)
{
return TransporterUtils.canInsert(back, null, stack, ForgeDirection.getOrientation(facing).getOpposite().ordinal(), true);
}
return false;
}
public boolean hasInventory()
{
return Object3D.get(this).getFromSide(ForgeDirection.getOrientation(facing).getOpposite()).getTileEntity(worldObj) instanceof IInventory;
}
public ItemStack sendHome(ItemStack stack)
{
TileEntity back = Object3D.get(this).getFromSide(ForgeDirection.getOrientation(facing).getOpposite()).getTileEntity(worldObj);
if(back instanceof IInventory)
{
return TransporterUtils.putStackInInventory((IInventory)back, stack, ForgeDirection.getOrientation(facing).getOpposite().ordinal(), true);
}
return stack;
}
@Override
public boolean canExtractItem(int slotID, ItemStack itemstack, int side)
{
@@ -13,6 +13,7 @@ import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketDataRequest;
import mekanism.common.network.PacketTileEntity;
import mekanism.common.transporter.TransporterStack;
import mekanism.common.transporter.TransporterStack.Path;
import mekanism.common.util.TransporterUtils;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
@@ -93,7 +94,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
}
}
else {
if(!stack.noTarget)
if(stack.pathType != Path.NONE)
{
if(next != null && next.getTileEntity(worldObj) instanceof IInventory)
{
@@ -102,7 +103,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
if(inventory != null)
{
ItemStack rejected = TransporterUtils.putStackInInventory(inventory, stack.itemStack, stack.getSide(this));
ItemStack rejected = TransporterUtils.putStackInInventory(inventory, stack.itemStack, stack.getSide(this), stack.pathType == Path.HOME);
if(rejected == null)
{
@@ -140,7 +141,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{
if(stack.isFinal(this))
{
if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.color, stack.itemStack, stack.getSide(this)) && !stack.noTarget)
if(stack.pathType == Path.DEST && !TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.color, stack.itemStack, stack.getSide(this), false))
{
if(!recalculate(stack, null))
{
@@ -148,7 +149,15 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
continue;
}
}
else if(stack.noTarget)
else if(stack.pathType == Path.HOME && !TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.color, stack.itemStack, stack.getSide(this), true))
{
if(!recalculate(stack, null))
{
remove.add(stack);
continue;
}
}
else if(stack.pathType == Path.NONE)
{
if(!recalculate(stack, null))
{
@@ -214,6 +223,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
TransporterStack stack = new TransporterStack();
stack.itemStack = itemStack;
stack.originalLocation = original;
stack.homeLocation = original;
stack.color = color;
if(!stack.canInsertToTransporter(this))
@@ -236,6 +246,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
TransporterStack stack = new TransporterStack();
stack.itemStack = itemStack;
stack.originalLocation = Object3D.get(outputter);
stack.homeLocation = Object3D.get(outputter);
stack.color = color;
if(!stack.canInsertToTransporter(this))
@@ -470,7 +481,9 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{
if(doAdd)
{
if(TransporterUtils.insert(Object3D.get(this).getFromSide(from).getTileEntity(worldObj), this, stack, null))
TileEntity tile = Object3D.get(this).getFromSide(from).getTileEntity(worldObj);
if(TransporterUtils.insert(tile, this, stack, null))
{
return stack.stackSize;
}
@@ -9,6 +9,7 @@ import java.util.Set;
import mekanism.api.Object3D;
import mekanism.common.tileentity.TileEntityLogisticalSorter;
import mekanism.common.tileentity.TileEntityLogisticalTransporter;
import mekanism.common.transporter.TransporterStack.Path;
import mekanism.common.util.TransporterUtils;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
@@ -16,6 +17,102 @@ import net.minecraftforge.common.ForgeDirection;
public final class TransporterPathfinder
{
public static class HomePath
{
public World worldObj;
public List<Destination> destinations = new ArrayList<Destination>();
public Object3D start;
public Object3D homeLocation;
public TransporterStack transportStack;
public boolean existed;
public HomePath(World world, Object3D obj, Object3D home, TransporterStack stack)
{
worldObj = world;
start = obj;
homeLocation = home;
transportStack = stack;
}
public void loop(Object3D pointer, ArrayList<Object3D> currentPath, int dist)
{
if(pointer == null)
{
return;
}
currentPath.add(pointer);
if(pointer.getMetadata(worldObj) == 4)
{
dist += 1000;
}
else {
dist++;
}
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = pointer.getFromSide(side).getTileEntity(worldObj);
if(tile != null && Object3D.get(tile).equals(homeLocation))
{
if(TransporterUtils.canInsert(tile, transportStack.color, transportStack.itemStack, side.ordinal(), true))
{
Destination dest = new Destination(currentPath, Object3D.get(tile), dist);
if(!destinations.contains(dest))
{
destinations.add(dest);
}
}
if(tile instanceof TileEntityLogisticalSorter)
{
if(((TileEntityLogisticalSorter)tile).hasInventory())
{
existed = true;
}
}
else {
existed = true;
}
}
if(transportStack.canInsertToTransporter(tile) && !currentPath.contains(Object3D.get(tile)))
{
loop(Object3D.get(tile), (ArrayList<Object3D>)currentPath.clone(), dist);
}
}
}
public List<Object3D> find()
{
loop(start, new ArrayList<Object3D>(), 0);
Collections.sort(destinations);
Destination closest = null;
if(!destinations.isEmpty())
{
closest = destinations.get(0);
}
if(closest == null)
{
return null;
}
return closest.path;
}
}
public static class IdlePath
{
public World worldObj;
@@ -183,7 +280,7 @@ public final class TransporterPathfinder
{
TileEntity tile = pointer.getFromSide(side).getTileEntity(worldObj);
if(TransporterUtils.canInsert(tile, transportStack.color, transportStack.itemStack, side.ordinal()) && !(tile instanceof TileEntityLogisticalTransporter))
if(TransporterUtils.canInsert(tile, transportStack.color, transportStack.itemStack, side.ordinal(), false) && !(tile instanceof TileEntityLogisticalTransporter))
{
Destination dest = new Destination(currentPath, Object3D.get(tile), dist);
@@ -287,8 +384,27 @@ public final class TransporterPathfinder
public static List<Object3D> getIdlePath(TileEntityLogisticalTransporter start, TransporterStack stack)
{
if(stack.homeLocation != null)
{
HomePath d = new HomePath(start.worldObj, Object3D.get(start), stack.homeLocation, stack);
List<Object3D> path = d.find();
if(path != null)
{
stack.pathType = Path.HOME;
return path;
}
else {
if(stack.homeLocation.getTileEntity(start.worldObj) == null)
{
stack.homeLocation = null;
}
}
}
IdlePath d = new IdlePath(start.worldObj, Object3D.get(start), stack);
List<Object3D> path = d.find();
stack.pathType = Path.NONE;
if(path == null)
{
@@ -27,11 +27,12 @@ public class TransporterStack
public List<Object3D> pathToTarget = new ArrayList<Object3D>();
public Object3D originalLocation;
public Object3D homeLocation;
public Object3D clientNext;
public Object3D clientPrev;
public boolean noTarget = false;
public Path pathType;
public void write(TileEntityLogisticalTransporter tileEntity, ArrayList data)
{
@@ -44,7 +45,7 @@ public class TransporterStack
}
data.add(progress);
data.add(noTarget);
data.add(pathType.ordinal());
if(pathToTarget.indexOf(Object3D.get(tileEntity)) > 0)
{
@@ -75,7 +76,7 @@ public class TransporterStack
}
progress = dataStream.readInt();
noTarget = dataStream.readBoolean();
pathType = Path.values()[dataStream.readInt()];
if(dataStream.readBoolean())
{
@@ -95,8 +96,14 @@ public class TransporterStack
}
nbtTags.setInteger("progress", progress);
originalLocation.write(nbtTags);
nbtTags.setBoolean("noTarget", noTarget);
nbtTags.setCompoundTag("originalLocation", originalLocation.write(new NBTTagCompound()));
if(nbtTags.hasKey("homeLocation"))
{
nbtTags.setCompoundTag("homeLocation", homeLocation.write(new NBTTagCompound()));
}
nbtTags.setInteger("pathType", pathType.ordinal());
itemStack.writeToNBT(nbtTags);
}
@@ -108,8 +115,14 @@ public class TransporterStack
}
progress = nbtTags.getInteger("progress");
originalLocation = Object3D.read(nbtTags);
noTarget = nbtTags.getBoolean("noTarget");
originalLocation = Object3D.read(nbtTags.getCompoundTag("originalLocation"));
if(homeLocation != null)
{
homeLocation = Object3D.read(nbtTags.getCompoundTag("homeLocation"));
}
pathType = Path.values()[nbtTags.getInteger("pathType")];
itemStack = ItemStack.loadItemStackFromNBT(nbtTags);
}
@@ -145,7 +158,7 @@ public class TransporterStack
pathToTarget = newPath;
noTarget = false;
pathType = Path.DEST;
initiatedPath = true;
return true;
@@ -162,7 +175,7 @@ public class TransporterStack
pathToTarget = newPath;
noTarget = false;
pathType = Path.DEST;
initiatedPath = true;
return true;
@@ -179,7 +192,6 @@ public class TransporterStack
pathToTarget = newPath;
noTarget = true;
originalLocation = Object3D.get(tileEntity);
initiatedPath = true;
@@ -188,7 +200,7 @@ public class TransporterStack
public boolean isFinal(TileEntityLogisticalTransporter tileEntity)
{
return pathToTarget.indexOf(Object3D.get(tileEntity)) == (noTarget ? 0 : 1);
return pathToTarget.indexOf(Object3D.get(tileEntity)) == (pathType == Path.NONE ? 0 : 1);
}
public Object3D getNext(TileEntityLogisticalTransporter tileEntity)
@@ -270,4 +282,9 @@ public class TransporterStack
{
return pathToTarget.get(0);
}
public static enum Path
{
DEST, HOME, NONE
}
}
+115 -85
View File
@@ -167,14 +167,19 @@ public final class TransporterUtils
return tileEntity.insertRR(outputter, itemStack.copy(), color);
}
public static boolean canInsert(TileEntity tileEntity, EnumColor color, ItemStack itemStack, int side)
public static boolean canInsert(TileEntity tileEntity, EnumColor color, ItemStack itemStack, int side, boolean force)
{
if(!(tileEntity instanceof IInventory))
{
return false;
}
if(tileEntity instanceof IConfigurable)
if(force && tileEntity instanceof TileEntityLogisticalSorter)
{
return ((TileEntityLogisticalSorter)tileEntity).canSendHome(itemStack);
}
if(!force && tileEntity instanceof IConfigurable)
{
IConfigurable config = (IConfigurable)tileEntity;
int tileSide = config.getOrientation();
@@ -194,27 +199,32 @@ public final class TransporterUtils
for(int i = 0; i <= inventory.getSizeInventory() - 1; i++)
{
if(inventory.isItemValidForSlot(i, itemStack))
if(!force)
{
ItemStack inSlot = inventory.getStackInSlot(i);
if(!inventory.isItemValidForSlot(i, itemStack))
{
continue;
}
}
ItemStack inSlot = inventory.getStackInSlot(i);
if(inSlot == null)
if(inSlot == null)
{
return true;
}
else if(inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
{
return true;
}
else if(inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
else {
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
if(rejects < itemStack.stackSize)
{
return true;
}
else {
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
if(rejects < itemStack.stackSize)
{
return true;
}
}
}
}
@@ -230,27 +240,32 @@ public final class TransporterUtils
{
int slotID = slots[get];
if(sidedInventory.isItemValidForSlot(slotID, itemStack) && sidedInventory.canInsertItem(slotID, itemStack, ForgeDirection.getOrientation(side).getOpposite().ordinal()))
if(!force)
{
ItemStack inSlot = inventory.getStackInSlot(slotID);
if(inSlot == null)
if(!sidedInventory.isItemValidForSlot(slotID, itemStack) || !sidedInventory.canInsertItem(slotID, itemStack, ForgeDirection.getOrientation(side).getOpposite().ordinal()))
{
continue;
}
}
ItemStack inSlot = inventory.getStackInSlot(slotID);
if(inSlot == null)
{
return true;
}
else if(inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
{
return true;
}
else if(inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
else {
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
if(rejects < itemStack.stackSize)
{
return true;
}
else {
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
if(rejects < itemStack.stackSize)
{
return true;
}
}
}
}
@@ -294,8 +309,13 @@ public final class TransporterUtils
return inv;
}
public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, int side)
public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, int side, boolean force)
{
if(force && inventory instanceof TileEntityLogisticalSorter)
{
return ((TileEntityLogisticalSorter)inventory).sendHome(itemStack.copy());
}
ItemStack toInsert = itemStack.copy();
if(!(inventory instanceof ISidedInventory))
@@ -304,38 +324,43 @@ public final class TransporterUtils
for(int i = 0; i <= inventory.getSizeInventory() - 1; i++)
{
if(inventory.isItemValidForSlot(i, toInsert))
if(!force)
{
ItemStack inSlot = inventory.getStackInSlot(i);
if(inSlot == null)
if(!inventory.isItemValidForSlot(i, toInsert))
{
inventory.setInventorySlotContents(i, toInsert);
continue;
}
}
ItemStack inSlot = inventory.getStackInSlot(i);
if(inSlot == null)
{
inventory.setInventorySlotContents(i, toInsert);
return null;
}
else if(inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = toInsert.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(i, toSet);
return null;
}
else if(inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = toInsert.copy();
toSet.stackSize += inSlot.stackSize;
else {
int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
inventory.setInventorySlotContents(i, toSet);
return null;
}
else {
int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = toInsert.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack toSet = toInsert.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = toInsert.copy();
remains.stackSize = rejects;
ItemStack remains = toInsert.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(i, toSet);
toInsert = remains;
}
inventory.setInventorySlotContents(i, toSet);
toInsert = remains;
}
}
}
@@ -350,38 +375,43 @@ public final class TransporterUtils
{
int slotID = slots[get];
if(sidedInventory.isItemValidForSlot(slotID, toInsert) && sidedInventory.canInsertItem(slotID, toInsert, ForgeDirection.getOrientation(side).getOpposite().ordinal()))
if(!force)
{
ItemStack inSlot = inventory.getStackInSlot(slotID);
if(inSlot == null)
if(!sidedInventory.isItemValidForSlot(slotID, toInsert) && !sidedInventory.canInsertItem(slotID, toInsert, ForgeDirection.getOrientation(side).getOpposite().ordinal()))
{
inventory.setInventorySlotContents(slotID, toInsert);
continue;
}
}
ItemStack inSlot = inventory.getStackInSlot(slotID);
if(inSlot == null)
{
inventory.setInventorySlotContents(slotID, toInsert);
return null;
}
else if(inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = toInsert.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(slotID, toSet);
return null;
}
else if(inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if(inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = toInsert.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(slotID, toSet);
return null;
}
else {
int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = toInsert.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = toInsert.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(slotID, toSet);
toInsert = remains;
}
else {
int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = toInsert.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = toInsert.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(slotID, toSet);
toInsert = remains;
}
}
}