Replaced StackUtil.addToRandomInventory.

This commit is contained in:
SirSengir 2012-09-08 16:20:21 +02:00
parent 5059cd9abe
commit d30717b271
4 changed files with 54 additions and 32 deletions

View file

@ -27,6 +27,8 @@ import buildcraft.core.EntityPassiveItem;
import buildcraft.core.IDropControlInventory;
import buildcraft.core.IFramePipeConnection;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.network.ISynchronizedTile;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.proxy.CoreProxy;
@ -49,6 +51,35 @@ public class Utils {
public static final float pipeMaxPos = 0.75F;
public static float pipeNormalSpeed = 0.01F;
public static ItemStack addToRandomInventory(ItemStack stack, World world, int x, int y, int z, Orientations from) {
LinkedList<ITransactor> possibleInventories = new LinkedList<ITransactor>();
// Determine inventories which can accept (at least part of) this stack.
for(Orientations orientation : Orientations.values()) {
if(from.reverse() == orientation)
continue;
Position pos = new Position(x, y, z, orientation);
pos.moveForwards(1.0);
TileEntity tileInventory = world.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
ITransactor transactor = Transactor.getTransactorFor(tileInventory);
if(transactor != null
&& transactor.add(stack, from, false).stackSize > 0)
possibleInventories.add(transactor);
}
if (possibleInventories.size() > 0) {
int choice = world.rand.nextInt(possibleInventories.size());
return possibleInventories.get(choice).add(stack, from, true);
}
ItemStack added = stack.copy();
added.stackSize = 0;
return added;
}
/**
* Depending on the kind of item in the pipe, set the floor at a different
* level to optimize graphical aspect.

View file

@ -12,7 +12,6 @@ import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.network.TilePacketWrapper;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.StackUtil;
import buildcraft.core.utils.Utils;
import net.minecraft.src.Container;
@ -147,14 +146,14 @@ public class TileAssemblyTable extends TileEntity implements IMachine, IInventor
}
}
StackUtil stackUtils = new StackUtil(currentRecipe.output.copy());
boolean added = stackUtils.addToRandomInventory(this, Orientations.Unknown);
ItemStack remaining = currentRecipe.output.copy();
ItemStack added = Utils.addToRandomInventory(remaining, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown);
remaining.stackSize -= added.stackSize;
if (!added || stackUtils.items.stackSize > 0) {
added = Utils.addToRandomPipeEntry(this, Orientations.Unknown, stackUtils.items);
}
if (remaining.stackSize > 0)
Utils.addToRandomPipeEntry(this, Orientations.Unknown, remaining);
if (!added) {
if (remaining.stackSize > 0) {
EntityItem entityitem = new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5,
currentRecipe.output.copy());

View file

@ -19,7 +19,6 @@ import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.core.IMachine;
import buildcraft.core.utils.StackUtil;
import buildcraft.core.utils.Utils;
import net.minecraft.src.Block;
@ -80,16 +79,14 @@ public class TileMiningWell extends TileMachine implements IMachine, IPowerRecep
return;
}
for (ItemStack s : stacks) {
StackUtil stackUtil = new StackUtil(s);
for (ItemStack stack : stacks) {
if (stackUtil.addToRandomInventory(this, Orientations.Unknown) && stackUtil.items.stackSize == 0) {
// The object has been added to a nearby chest.
return;
}
ItemStack added = Utils.addToRandomInventory(stack, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown);
stack.stackSize -= added.stackSize;
if (stack.stackSize <= 0)
continue;
if (Utils.addToRandomPipeEntry(this, Orientations.Unknown, s) && stackUtil.items.stackSize == 0) {
// The object has been added to a nearby pipe.
if (Utils.addToRandomPipeEntry(this, Orientations.Unknown, stack) && stack.stackSize <= 0) {
return;
}
@ -100,7 +97,7 @@ public class TileMiningWell extends TileMachine implements IMachine, IPowerRecep
float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
float f2 = world.rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem = new EntityItem(world, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stackUtil.items);
EntityItem entityitem = new EntityItem(world, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stack);
float f3 = 0.05F;
entityitem.motionX = (float) world.rand.nextGaussian() * f3;

View file

@ -33,7 +33,6 @@ import buildcraft.core.blueprints.BptBuilderBlueprint;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.StackUtil;
import buildcraft.core.utils.Utils;
import net.minecraft.src.AxisAlignedBB;
@ -400,27 +399,23 @@ public class TileQuarry extends TileMachine implements IArmListener, IMachine, I
}
}
private void mineStack(ItemStack s) {
boolean added = false;
private void mineStack(ItemStack stack) {
// First, try to add to a nearby chest
ItemStack added = Utils.addToRandomInventory(stack, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown);
stack.stackSize -= added.stackSize;
StackUtil stackUtils = new StackUtil(s);
// Second, try to add to adjacent pipes
if (stack.stackSize > 0)
Utils.addToRandomPipeEntry(this, Orientations.Unknown, stack);
added = stackUtils.addToRandomInventory(this, Orientations.Unknown);
if (!added || stackUtils.items.stackSize > 0) {
added = Utils.addToRandomPipeEntry(this, Orientations.Unknown, stackUtils.items);
}
// Last, throw the object away
if (!added) {
// Lastly, throw the object away
if (stack.stackSize > 0) {
float f = worldObj.rand.nextFloat() * 0.8F + 0.1F;
float f1 = worldObj.rand.nextFloat() * 0.8F + 0.1F;
float f2 = worldObj.rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem = new EntityItem(worldObj, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stackUtils.items);
EntityItem entityitem = new EntityItem(worldObj, xCoord + f, yCoord + f1 + 0.5F, zCoord + f2, stack);
float f3 = 0.05F;
entityitem.motionX = (float) worldObj.rand.nextGaussian() * f3;