Skip to content

Instantly share code, notes, and snippets.

@Flow86
Created May 23, 2013 11:51
Show Gist options
  • Save Flow86/5635531 to your computer and use it in GitHub Desktop.
Save Flow86/5635531 to your computer and use it in GitHub Desktop.
public static ItemStack addToRandomInventory(ItemStack stack, World world, int x, int y, int z, ForgeDirection exclude_from) {
LinkedList<Object[]> possibleInventories = new LinkedList<Object[]>();
// Determine inventories which can accept (at least part of) this stack.
for (ForgeDirection orientation : ForgeDirection.values()) {
if (exclude_from.getOpposite() == 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 && !(tileInventory instanceof TileEngine) && transactor.add(stack, orientation.getOpposite(), false).stackSize > 0) {
possibleInventories.add(new Object[] { orientation, transactor });
}
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment