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)]); } }
public static boolean checkStructure( World world, ItemStack[][][] structure, ForgeDirection dir, Vec3i pos) { Matrix4D matrix = new Matrix4D(); int maxXLength = structure.length; int maxYLength = 0; int maxZLength = 0; for (ItemStack[][] yz : structure) { maxYLength = Math.max(maxYLength, yz.length); for (ItemStack[] line : yz) { maxZLength = Math.max(maxZLength, line.length); } } matrix.translate(maxXLength * -0.5, maxYLength * -0.5, maxZLength * -0.5); switch (dir) { case EAST: matrix.rotate(0.0, 90.0, 0.0); break; case SOUTH: matrix.rotate(0.0, 180.0, 0.0); break; case WEST: matrix.rotate(0.0, 270.0, 0.0); break; } matrix.translate(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5); matrix.translate( maxXLength * -0.5 * dir.offsetX, maxYLength * -0.5 * dir.offsetY, maxZLength * -0.5 * dir.offsetZ); // 基準点調整 switch (dir) { case NORTH: matrix.translate(0.0, 0.0, 1.0); break; case SOUTH: matrix.translate(-1.0, 0.0, -1.0); break; case EAST: matrix.translate(0.0, 0.0, -1.0); break; case WEST: matrix.translate(0.0, 0.0, 0.0); break; } for (int x = 0; x < structure.length; x++) { ItemStack[][] yz = structure[x]; for (int y = 0; y < yz.length; y++) { ItemStack[] line = yz[y]; for (int z = 0; z < line.length; z++) { ItemStack itemStack = line[z]; if (itemStack == null) { continue; } Vec3 target = matrix.transform(x, y, z); int ix = (int) Math.floor(target.xCoord); int iy = (int) Math.floor(target.yCoord); int iz = (int) Math.floor(target.zCoord); if (itemStack.itemID != world.getBlockId(ix, iy, iz)) { return false; } int damage = itemStack.getItemDamage(); if (damage != Short.MAX_VALUE && damage != world.getBlockMetadata(ix, iy, iz)) { return false; } } } } return true; }