Skip to content

Commit

Permalink
Make addToRandomInventory type safe
Browse files Browse the repository at this point in the history
Better, Faster, Stronger

Closes #877
  • Loading branch information
CovertJaguar committed May 25, 2013
1 parent 3cf2ff9 commit a70eacc
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions common/buildcraft/core/utils/Utils.java
Expand Up @@ -46,16 +46,21 @@
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.energy.TileEngine;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Utils {

public static final float pipeMinPos = 0.25F;
public static final float pipeMaxPos = 0.75F;
public static float pipeNormalSpeed = 0.01F;
private static final List<ForgeDirection> directions = new ArrayList<ForgeDirection>(Arrays.asList(ForgeDirection.VALID_DIRECTIONS));

/**
* Tries to add the passed stack to any valid inventories around the given coordinates.
*
* Tries to add the passed stack to any valid inventories around the given
* coordinates.
*
* @param stack
* @param world
* @param x
Expand All @@ -64,26 +69,18 @@ public class Utils {
* @return ItemStack representing what was added.
*/
public static ItemStack addToRandomInventory(ItemStack stack, World world, int x, int y, int z) {
LinkedList<Object[]> possibleInventories = new LinkedList<Object[]>();

// Determine inventories which can accept (at least part of) this stack.
for (ForgeDirection orientation : ForgeDirection.values()) {
Collections.shuffle(directions);
for (ForgeDirection orientation : directions) {
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 && !(tileInventory instanceof TileEngine) && transactor.add(stack, orientation.getOpposite(), false).stackSize > 0) {
possibleInventories.add(new Object[] { orientation, transactor });
return transactor.add(stack, orientation.getOpposite(), true);
}
}

if (possibleInventories.size() > 0) {
int choice = world.rand.nextInt(possibleInventories.size());
Object[] chosen = possibleInventories.get(choice);
return ((ITransactor) chosen[0]).add(stack, ((ForgeDirection) chosen[1]).getOpposite(), true);
}

ItemStack added = stack.copy();
added.stackSize = 0;
return added;
Expand Down

1 comment on commit a70eacc

@Flow86
Copy link
Contributor

@Flow86 Flow86 commented on a70eacc May 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes thats better

Please sign in to comment.