public static ItemStack insertItem( IInventory inventory, ItemStack itemStack, boolean test, boolean force) { if (test) { itemStack = itemStack.copy(); } int size = inventory.getSizeInventory(); for (int i = 0; i < size; i++) { if (inventory.isItemValidForSlot(i, itemStack) || force) { ItemStack storedItem = inventory.getStackInSlot(i); if (storedItem != null) { if (equalItemAndNBT(storedItem, itemStack)) { int maxStackSize = Math.min(itemStack.getMaxStackSize(), inventory.getInventoryStackLimit()); int add = Math.min(itemStack.stackSize, maxStackSize - storedItem.stackSize); if (!test) { storedItem.stackSize += add; } itemStack.stackSize -= add; inventory.setInventorySlotContents(i, storedItem); } } else { storedItem = itemStack.copy(); storedItem.stackSize = Math.min(itemStack.stackSize, itemStack.getMaxStackSize()); if (!test) { inventory.setInventorySlotContents(i, storedItem); } itemStack.stackSize -= storedItem.stackSize; } if (itemStack.stackSize <= 0) { return null; } } } return itemStack; }
public static void dropItem(ItemStack itemStack, World world, int x, int y, int z) { double posX = x + rand.nextDouble() * 0.8 + 0.1; double posY = y + rand.nextDouble() * 0.8 + 0.1; double posZ = z + rand.nextDouble() * 0.8 + 0.1; while (itemStack.stackSize > 0) { int size = Math.min(itemStack.stackSize, rand.nextInt(21) + 10); itemStack.stackSize -= size; EntityItem entity = new EntityItem( world, posX, posY, posZ, new ItemStack(itemStack.itemID, size, itemStack.getItemDamage())); if (itemStack.hasTagCompound()) { entity.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy()); } entity.motionX = rand.nextGaussian() * 0.05; entity.motionY = rand.nextGaussian() * 0.05 + 0.2; entity.motionZ = rand.nextGaussian() * 0.05; world.spawnEntityInWorld(entity); } }
public static void injectEnergy(TileEntityMachineBase tileEntity, ForgeDirection dir) { TileEntity connectedTileEntity = tileEntity.worldObj.getBlockTileEntity( tileEntity.xCoord + dir.offsetX, tileEntity.yCoord + dir.offsetY, tileEntity.zCoord + dir.offsetZ); if (connectedTileEntity != null) { if (connectedTileEntity instanceof ILargeEnergyStorage) { ILargeEnergyStorage storage = (ILargeEnergyStorage) connectedTileEntity; if (tileEntity instanceof ILargeEnergyStorage) { ILargeEnergyStorage source = (ILargeEnergyStorage) tileEntity; BigInteger demand = source .getAllStoredEnergy() .min(storage.getFullCapacity().subtract(storage.getAllStoredEnergy())); storage.setAllEnergy(storage.getAllStoredEnergy().add(demand)); source.useLargeEnergy(demand); } else { BigInteger demand = Util.toBigInteger(tileEntity.getStored()) .min(storage.getFullCapacity().subtract(storage.getAllStoredEnergy())); storage.setAllEnergy(storage.getAllStoredEnergy().add(demand)); tileEntity.useEnergy(demand.intValue()); } } else if (connectedTileEntity instanceof IEnergyStorage) { IEnergyStorage storage = (IEnergyStorage) connectedTileEntity; int demand = Math.min(tileEntity.getStored(), storage.getCapacity() - storage.getStored()); storage.setStored(storage.getStored() + demand); tileEntity.useEnergy(demand); } else if (connectedTileEntity instanceof IEnergySink) { IEnergySink sink = (IEnergySink) connectedTileEntity; int demand = 0; int prevStoredEnergy = 0; while ((demand = Math.min(sink.getMaxSafeInput() - 1, tileEntity.getStored())) > 0 && tileEntity.getStored() != prevStoredEnergy) { prevStoredEnergy = tileEntity.getStored(); sink.injectEnergy(DIRECTION_MAP.get(dir.getOpposite()), demand); tileEntity.useEnergy(demand); } } } }
public static String getFormattedBigDecimal(BigDecimal value) { if (value.compareTo(DISPLAY_MAX) > 0) { return "very high"; } int unitIndex = 0; for (; unitIndex < UNIT.length && value.compareTo(THOUSAND) > 0; unitIndex++) { value = value.divide(THOUSAND); } if (unitIndex == 0) { return String.format("%d", value.round(MathContext.DECIMAL32).intValue()); } else { return String.format( "%.2f%s", value.round(MathContext.DECIMAL64).doubleValue(), UNIT[Math.min(unitIndex, UNIT.length - 1)]); } }