diff options
Diffstat (limited to 'src/main/java/gregtech/api/util/GT_Utility.java')
| -rw-r--r-- | src/main/java/gregtech/api/util/GT_Utility.java | 4859 |
1 files changed, 0 insertions, 4859 deletions
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java deleted file mode 100644 index 84e67ab728..0000000000 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ /dev/null @@ -1,4859 +0,0 @@ -package gregtech.api.util; - -import static gregtech.GT_Mod.GT_FML_LOGGER; -import static gregtech.api.enums.GT_Values.COMPASS_DIRECTIONS; -import static gregtech.api.enums.GT_Values.D1; -import static gregtech.api.enums.GT_Values.E; -import static gregtech.api.enums.GT_Values.GT; -import static gregtech.api.enums.GT_Values.L; -import static gregtech.api.enums.GT_Values.M; -import static gregtech.api.enums.GT_Values.NW; -import static gregtech.api.enums.GT_Values.V; -import static gregtech.api.enums.GT_Values.W; -import static gregtech.api.enums.Materials.FLUID_MAP; -import static gregtech.api.enums.Mods.Translocator; -import static gregtech.common.GT_UndergroundOil.undergroundOilReadInformation; -import static net.minecraftforge.common.util.ForgeDirection.DOWN; -import static net.minecraftforge.common.util.ForgeDirection.EAST; -import static net.minecraftforge.common.util.ForgeDirection.NORTH; -import static net.minecraftforge.common.util.ForgeDirection.SOUTH; -import static net.minecraftforge.common.util.ForgeDirection.UNKNOWN; -import static net.minecraftforge.common.util.ForgeDirection.UP; -import static net.minecraftforge.common.util.ForgeDirection.WEST; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.math.BigInteger; -import java.math.RoundingMode; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.AbstractCollection; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; -import java.util.function.Function; -import java.util.function.IntFunction; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.stream.Collector; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; -import net.minecraft.enchantment.Enchantment; -import net.minecraft.enchantment.EnchantmentHelper; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityList; -import net.minecraft.entity.EntityLiving; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.EnumCreatureAttribute; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.init.Blocks; -import net.minecraft.init.Items; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.ISidedInventory; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTBase.NBTPrimitive; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.nbt.NBTTagString; -import net.minecraft.network.play.server.S07PacketRespawn; -import net.minecraft.network.play.server.S1DPacketEntityEffect; -import net.minecraft.network.play.server.S1FPacketSetExperience; -import net.minecraft.potion.Potion; -import net.minecraft.potion.PotionEffect; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.tileentity.TileEntityChest; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.DamageSource; -import net.minecraft.util.EnumChatFormatting; -import net.minecraft.util.MathHelper; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.Vec3; -import net.minecraft.world.World; -import net.minecraft.world.WorldServer; -import net.minecraft.world.chunk.Chunk; -import net.minecraftforge.common.DimensionManager; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.util.BlockSnapshot; -import net.minecraftforge.common.util.Constants; -import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.common.util.FakePlayerFactory; -import net.minecraftforge.common.util.ForgeDirection; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.event.world.BlockEvent; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData; -import net.minecraftforge.fluids.FluidRegistry; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.FluidTankInfo; -import net.minecraftforge.fluids.IFluidContainerItem; -import net.minecraftforge.fluids.IFluidHandler; -import net.minecraftforge.oredict.OreDictionary; - -import com.google.auto.value.AutoValue; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import com.google.common.collect.SetMultimap; -import com.gtnewhorizon.structurelib.alignment.IAlignment; -import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider; -import com.mojang.authlib.GameProfile; - -import buildcraft.api.transport.IPipeTile; -import cofh.api.energy.IEnergyReceiver; -import cofh.api.transport.IItemDuct; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.registry.GameRegistry; -import gregtech.api.GregTech_API; -import gregtech.api.damagesources.GT_DamageSources; -import gregtech.api.damagesources.GT_DamageSources.DamageSourceHotItem; -import gregtech.api.enchants.Enchantment_Hazmat; -import gregtech.api.enchants.Enchantment_Radioactivity; -import gregtech.api.enums.GT_Values; -import gregtech.api.enums.ItemList; -import gregtech.api.enums.Materials; -import gregtech.api.enums.Mods; -import gregtech.api.enums.OrePrefixes; -import gregtech.api.enums.SoundResource; -import gregtech.api.enums.SubTag; -import gregtech.api.enums.Textures; -import gregtech.api.enums.ToolDictNames; -import gregtech.api.events.BlockScanningEvent; -import gregtech.api.interfaces.IBlockContainer; -import gregtech.api.interfaces.IDebugableBlock; -import gregtech.api.interfaces.IHasIndexedTexture; -import gregtech.api.interfaces.IProjectileItem; -import gregtech.api.interfaces.ITexture; -import gregtech.api.interfaces.tileentity.IBasicEnergyContainer; -import gregtech.api.interfaces.tileentity.ICoverable; -import gregtech.api.interfaces.tileentity.IGregTechDeviceInformation; -import gregtech.api.interfaces.tileentity.IGregTechTileEntity; -import gregtech.api.interfaces.tileentity.IMachineProgress; -import gregtech.api.interfaces.tileentity.IUpgradableMachine; -import gregtech.api.items.GT_EnergyArmor_Item; -import gregtech.api.items.GT_Generic_Item; -import gregtech.api.items.GT_MetaGenerated_Tool; -import gregtech.api.metatileentity.MetaTileEntity; -import gregtech.api.net.GT_Packet_Sound; -import gregtech.api.objects.CollectorUtils; -import gregtech.api.objects.GT_ItemStack; -import gregtech.api.objects.GT_ItemStack2; -import gregtech.api.objects.ItemData; -import gregtech.api.recipe.RecipeMaps; -import gregtech.api.threads.GT_Runnable_Sound; -import gregtech.api.util.extensions.ArrayExt; -import gregtech.common.GT_Pollution; -import gregtech.common.blocks.GT_Block_Ores_Abstract; -import ic2.api.recipe.IRecipeInput; -import ic2.api.recipe.RecipeInputItemStack; -import ic2.api.recipe.RecipeInputOreDict; -import ic2.api.recipe.RecipeOutput; -import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; -import it.unimi.dsi.fastutil.objects.Reference2LongOpenHashMap; - -/** - * NEVER INCLUDE THIS FILE IN YOUR MOD!!! - * <p/> - * Just a few Utility Functions I use. - */ -public class GT_Utility { - - /** - * Formats a number with group separator and at most 2 fraction digits. - */ - private static final Map<Locale, DecimalFormat> decimalFormatters = new HashMap<>(); - - /** - * Forge screwed the Fluid Registry up again, so I make my own, which is also much more efficient than the stupid - * Stuff over there. - */ - private static final List<FluidContainerData> sFluidContainerList = new ArrayList<>(); - - private static final Map<GT_ItemStack, FluidContainerData> sFilledContainerToData = new /* Concurrent */ HashMap<>(); - private static final Map<GT_ItemStack, Map<String, FluidContainerData>> sEmptyContainerToFluidToData = new HashMap<>(); - private static final Map<String, List<ItemStack>> sFluidToContainers = new HashMap<>(); - /** - * Must use {@code Supplier} here because the ore prefixes have not yet been registered at class load time. - */ - private static final Map<OrePrefixes, Supplier<ItemStack>> sOreToCobble = new HashMap<>(); - - private static final Map<Integer, Boolean> sOreTable = new HashMap<>(); - public static boolean TE_CHECK = false, BC_CHECK = false, CHECK_ALL = true, RF_CHECK = false; - public static Map<GT_PlayedSound, Integer> sPlayedSoundMap = new /* Concurrent */ HashMap<>(); - private static int sBookCount = 0; - public static UUID defaultUuid = null; // maybe default non-null? - // UUID.fromString("00000000-0000-0000-0000-000000000000"); - - static { - GregTech_API.sItemStackMappings.add(sFilledContainerToData); - GregTech_API.sItemStackMappings.add(sEmptyContainerToFluidToData); - - // 1 is the magic index to get the cobblestone block. - // See: GT_Block_Stones.java, GT_Block_Granites.java - Function<Materials, Supplier<ItemStack>> materialToCobble = m -> Suppliers.memoize( - () -> GT_OreDictUnificator.getOres(OrePrefixes.stone, m) - .get(1))::get; - sOreToCobble.put(OrePrefixes.oreBlackgranite, materialToCobble.apply(Materials.GraniteBlack)); - sOreToCobble.put(OrePrefixes.oreRedgranite, materialToCobble.apply(Materials.GraniteRed)); - sOreToCobble.put(OrePrefixes.oreMarble, materialToCobble.apply(Materials.Marble)); - sOreToCobble.put(OrePrefixes.oreBasalt, materialToCobble.apply(Materials.Basalt)); - sOreToCobble.put(OrePrefixes.oreNetherrack, () -> new ItemStack(Blocks.netherrack)); - sOreToCobble.put(OrePrefixes.oreEndstone, () -> new ItemStack(Blocks.end_stone)); - } - - public static int safeInt(long number, int margin) { - return number > Integer.MAX_VALUE - margin ? Integer.MAX_VALUE - margin : (int) number; - } - - public static int safeInt(long number) { - return number > V[V.length - 1] ? safeInt(V[V.length - 1], 1) - : number < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) number; - } - - public static Field getPublicField(Object aObject, String aField) { - Field rField = null; - try { - rField = aObject.getClass() - .getDeclaredField(aField); - } catch (Throwable e) { - /* Do nothing */ - } - return rField; - } - - public static Field getField(Object aObject, String aField) { - Field rField = null; - try { - rField = aObject.getClass() - .getDeclaredField(aField); - rField.setAccessible(true); - } catch (Throwable e) { - /* Do nothing */ - } - return rField; - } - - public static Field getField(Class<?> aObject, String aField) { - Field rField = null; - try { - rField = aObject.getDeclaredField(aField); - rField.setAccessible(true); - } catch (Throwable e) { - /* Do nothing */ - } - return rField; - } - - public static Method getMethod(Class<?> aObject, String aMethod, Class<?>... aParameterTypes) { - Method rMethod = null; - try { - rMethod = aObject.getMethod(aMethod, aParameterTypes); - rMethod.setAccessible(true); - } catch (Throwable e) { - /* Do nothing */ - } - return rMethod; - } - - public static Method getMethod(Object aObject, String aMethod, Class<?>... aParameterTypes) { - Method rMethod = null; - try { - rMethod = aObject.getClass() - .getMethod(aMethod, aParameterTypes); - rMethod.setAccessible(true); - } catch (Throwable e) { - /* Do nothing */ - } - return rMethod; - } - - public static Field getField(Object aObject, String aField, boolean aPrivate, boolean aLogErrors) { - try { - Field tField = (aObject instanceof Class) ? ((Class<?>) aObject).getDeclaredField(aField) - : (aObject instanceof String) ? Class.forName((String) aObject) - .getDeclaredField(aField) - : aObject.getClass() - .getDeclaredField(aField); - if (aPrivate) tField.setAccessible(true); - return tField; - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - return null; - } - - public static Object getFieldContent(Object aObject, String aField, boolean aPrivate, boolean aLogErrors) { - try { - Field tField = (aObject instanceof Class) ? ((Class<?>) aObject).getDeclaredField(aField) - : (aObject instanceof String) ? Class.forName((String) aObject) - .getDeclaredField(aField) - : aObject.getClass() - .getDeclaredField(aField); - if (aPrivate) tField.setAccessible(true); - return tField.get(aObject instanceof Class || aObject instanceof String ? null : aObject); - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - return null; - } - - public static Object callPublicMethod(Object aObject, String aMethod, Object... aParameters) { - return callMethod(aObject, aMethod, false, false, true, aParameters); - } - - public static Object callPrivateMethod(Object aObject, String aMethod, Object... aParameters) { - return callMethod(aObject, aMethod, true, false, true, aParameters); - } - - public static Object callMethod(Object aObject, String aMethod, boolean aPrivate, boolean aUseUpperCasedDataTypes, - boolean aLogErrors, Object... aParameters) { - try { - Class<?>[] tParameterTypes = new Class<?>[aParameters.length]; - for (byte i = 0; i < aParameters.length; i++) { - if (aParameters[i] instanceof Class) { - tParameterTypes[i] = (Class<?>) aParameters[i]; - aParameters[i] = null; - } else { - tParameterTypes[i] = aParameters[i].getClass(); - } - if (!aUseUpperCasedDataTypes) { - if (tParameterTypes[i] == Boolean.class) tParameterTypes[i] = boolean.class; - else if (tParameterTypes[i] == Byte.class) tParameterTypes[i] = byte.class; - else if (tParameterTypes[i] == Short.class) tParameterTypes[i] = short.class; - else if (tParameterTypes[i] == Integer.class) tParameterTypes[i] = int.class; - else if (tParameterTypes[i] == Long.class) tParameterTypes[i] = long.class; - else if (tParameterTypes[i] == Float.class) tParameterTypes[i] = float.class; - else if (tParameterTypes[i] == Double.class) tParameterTypes[i] = double.class; - } - } - - Method tMethod = (aObject instanceof Class) ? ((Class<?>) aObject).getMethod(aMethod, tParameterTypes) - : aObject.getClass() - .getMethod(aMethod, tParameterTypes); - if (aPrivate) tMethod.setAccessible(true); - return tMethod.invoke(aObject, aParameters); - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - return null; - } - - public static Object callConstructor(String aClass, int aConstructorIndex, Object aReplacementObject, - boolean aLogErrors, Object... aParameters) { - try { - return callConstructor( - Class.forName(aClass), - aConstructorIndex, - aReplacementObject, - aLogErrors, - aParameters); - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - return aReplacementObject; - } - - public static Object callConstructor(Class<?> aClass, int aConstructorIndex, Object aReplacementObject, - boolean aLogErrors, Object... aParameters) { - if (aConstructorIndex < 0) { - try { - for (Constructor<?> tConstructor : aClass.getConstructors()) { - try { - return tConstructor.newInstance(aParameters); - } catch (Throwable ignored) {} - } - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - } else { - try { - return aClass.getConstructors()[aConstructorIndex].newInstance(aParameters); - } catch (Throwable e) { - if (aLogErrors) e.printStackTrace(GT_Log.err); - } - } - return aReplacementObject; - } - - public static String capitalizeString(String aString) { - if (aString != null && aString.length() > 0) return aString.substring(0, 1) - .toUpperCase() + aString.substring(1); - return E; - } - - public static boolean getPotion(EntityLivingBase aPlayer, int aPotionIndex) { - try { - Field tPotionHashmap = null; - - Field[] fields = EntityLiving.class.getDeclaredFields(); - - for (Field field : fields) { - if (field.getType() == HashMap.class) { - tPotionHashmap = field; - tPotionHashmap.setAccessible(true); - break; - } - } - - if (tPotionHashmap != null) return ((HashMap<?, ?>) tPotionHashmap.get(aPlayer)).get(aPotionIndex) != null; - } catch (Throwable e) { - if (D1) e.printStackTrace(GT_Log.err); - } - return false; - } - - public static String getClassName(Object aObject) { - if (aObject == null) return "null"; - return aObject.getClass() - .getName() - .substring( - aObject.getClass() - .getName() - .lastIndexOf(".") + 1); - } - - public static void removePotion(EntityLivingBase aPlayer, int aPotionIndex) { - try { - Field tPotionHashmap = null; - - Field[] fields = EntityLiving.class.getDeclaredFields(); - - for (Field field : fields) { - if (field.getType() == HashMap.class) { - tPotionHashmap = field; - tPotionHashmap.setAccessible(true); - break; - } - } - - if (tPotionHashmap != null) ((HashMap<?, ?>) tPotionHashmap.get(aPlayer)).remove(aPotionIndex); - } catch (Throwable e) { - if (D1) e.printStackTrace(GT_Log.err); - } - } - - public static boolean getFullInvisibility(EntityPlayer aPlayer) { - try { - if (aPlayer.isInvisible()) { - for (int i = 0; i < 4; i++) { - if (aPlayer.inventory.armorInventory[i] != null) { - if (aPlayer.inventory.armorInventory[i].getItem() instanceof GT_EnergyArmor_Item) { - if ((((GT_EnergyArmor_Item) aPlayer.inventory.armorInventory[i].getItem()).mSpecials & 512) - != 0) { - if (GT_ModHandler.canUseElectricItem(aPlayer.inventory.armorInventory[i], 10000)) { - return true; - } - } - } - } - } - } - } catch (Throwable e) { - if (D1) e.printStackTrace(GT_Log.err); - } - return false; - } - - public static ItemStack suckOneItemStackAt(World aWorld, double aX, double aY, double aZ, double aL, double aH, - double aW) { - for (EntityItem tItem : aWorld.getEntitiesWithinAABB( - EntityItem.class, - AxisAlignedBB.getBoundingBox(aX, aY, aZ, aX + aL, aY + aH, aZ + aW))) { - if (!tItem.isDead) { - aWorld.removeEntity(tItem); - tItem.setDead(); - return tItem.getEntityItem(); - } - } - return null; - } - - public static byte getOppositeSide(ForgeDirection side) { - return (byte) side.getOpposite() - .ordinal(); - } - - public static byte getTier(long l) { - byte i = -1; - while (++i < V.length) if (l <= V[i]) return i; - return (byte) (V.length - 1); - } - - public static long getAmperageForTier(long voltage, byte tier) { - return ceilDiv(voltage, GT_Values.V[tier]); - } - - /** - * Rounds up partial voltage that exceeds tiered voltage, e.g. 4,096 -> 8,192(IV) - */ - public static long roundUpVoltage(long voltage) { - if (voltage > V[V.length - 1]) { - return voltage; - } - return V[GT_Utility.getTier(voltage)]; - } - - public static String getColoredTierNameFromVoltage(long voltage) { - return getColoredTierNameFromTier(getTier(voltage)); - } - - public static String getColoredTierNameFromTier(byte tier) { - return GT_Values.TIER_COLORS[tier] + GT_Values.VN[tier] + EnumChatFormatting.RESET; - } - - /** - * @return e.g. {@code " (LV)"} - */ - @Nonnull - public static String getTierNameWithParentheses(long voltage) { - byte tier = getTier(voltage); - if (tier < 0) { - return ""; - } else if (tier == 0) { - return " (" + GT_Values.VN[1] + ")"; - } else if (tier >= GT_Values.VN.length - 1) { - return " (MAX+)"; - } - return " (" + GT_Values.VN[tier] + ")"; - } - - public static void sendChatToPlayer(EntityPlayer aPlayer, String aChatMessage) { - if (aPlayer instanceof EntityPlayerMP && aChatMessage != null) { - aPlayer.addChatComponentMessage(new ChatComponentText(aChatMessage)); - } - } - - public static void checkAvailabilities() { - if (CHECK_ALL) { - try { - Class<IItemDuct> tClass = IItemDuct.class; - tClass.getCanonicalName(); - TE_CHECK = true; - } catch (Throwable e) { - /**/ - } - try { - Class<IPipeTile> tClass = buildcraft.api.transport.IPipeTile.class; - tClass.getCanonicalName(); - BC_CHECK = true; - } catch (Throwable e) { - /**/ - } - try { - Class<IEnergyReceiver> tClass = cofh.api.energy.IEnergyReceiver.class; - tClass.getCanonicalName(); - RF_CHECK = true; - } catch (Throwable e) { - /**/ - } - CHECK_ALL = false; - } - } - - public static boolean isConnectableNonInventoryPipe(TileEntity tileEntity, ForgeDirection side) { - if (tileEntity == null) return false; - checkAvailabilities(); - if (TE_CHECK && tileEntity instanceof IItemDuct) return true; - if (BC_CHECK && tileEntity instanceof buildcraft.api.transport.IPipeTile pipeTile) - return pipeTile.isPipeConnected(side); - return Translocator.isModLoaded() && tileEntity instanceof codechicken.translocator.TileItemTranslocator; - } - - /** - * Moves Stack from Inv-Slot to Inv-Slot, without checking if its even allowed. - * - * @return the Amount of moved Items - */ - public static byte moveStackIntoPipe(IInventory aTileEntity1, Object aTileEntity2, int[] aGrabSlots, - ForgeDirection fromSide, ForgeDirection putSide, List<ItemStack> aFilter, boolean aInvertFilter, - byte aMaxTargetStackSize, byte aMinTargetStackSize, byte aMaxMoveAtOnce, byte aMinMoveAtOnce) { - return moveStackIntoPipe( - aTileEntity1, - aTileEntity2, - aGrabSlots, - fromSide, - putSide, - aFilter, - aInvertFilter, - aMaxTargetStackSize, - aMinTargetStackSize, - aMaxMoveAtOnce, - aMinMoveAtOnce, - true); - } - - /** - * Moves Stack from Inv-Slot to Inv-Slot, without checking if it is even allowed. - * - * @return the Amount of moved Items - */ - public static byte moveStackIntoPipe(IInventory fromInventory, Object toObject, int[] fromSlots, - ForgeDirection fromSide, ForgeDirection putSide, List<ItemStack> aFilter, boolean aInvertFilter, - byte aMaxTargetStackSize, byte aMinTargetStackSize, byte aMaxMoveAtOnce, byte aMinMoveAtOnce, - boolean dropItem) { - if (fromInventory == null || aMaxTargetStackSize <= 0 - || aMinTargetStackSize <= 0 - || aMinTargetStackSize > aMaxTargetStackSize - || aMaxMoveAtOnce <= 0 - || aMinMoveAtOnce > aMaxMoveAtOnce) return 0; - if (toObject != null) { - checkAvailabilities(); - if (TE_CHECK && toObject instanceof IItemDuct itemDuct) { - for (final int aGrabSlot : fromSlots) { - if (listContainsItem(aFilter, fromInventory.getStackInSlot(aGrabSlot), true, aInvertFilter)) { - if (isAllowedToTakeFromSlot( - fromInventory, - aGrabSlot, - fromSide, - fromInventory.getStackInSlot(aGrabSlot))) { - if (Math.max(aMinMoveAtOnce, aMinTargetStackSize) - <= fromInventory.getStackInSlot(aGrabSlot).stackSize) { - ItemStack tStack = copyAmount( - Math.min( - fromInventory.getStackInSlot(aGrabSlot).stackSize, - Math.min(aMaxMoveAtOnce, aMaxTargetStackSize)), - fromInventory.getStackInSlot(aGrabSlot)); - ItemStack rStack = itemDuct.insertItem(putSide, copyOrNull(tStack)); - byte tMovedItemCount = (byte) (tStack.stackSize - - (rStack == null ? 0 : rStack.stackSize)); - if (tMovedItemCount >= 1 /* Math.max(aMinMoveAtOnce, aMinTargetStackSize) */) { - fromInventory.decrStackSize(aGrabSlot, tMovedItemCount); - fromInventory.markDirty(); - return tMovedItemCount; - } - } - } - } - } - return 0; - } - if (BC_CHECK && toObject instanceof buildcraft.api.transport.IPipeTile bcPipe) { - for (int fromSlot : fromSlots) { - if (listContainsItem(aFilter, fromInventory.getStackInSlot(fromSlot), true, aInvertFilter)) { - if (isAllowedToTakeFromSlot( - fromInventory, - fromSlot, - fromSide, - fromInventory.getStackInSlot(fromSlot))) { - if (Math.max(aMinMoveAtOnce, aMinTargetStackSize) - <= fromInventory.getStackInSlot(fromSlot).stackSize) { - ItemStack tStack = copyAmount( - Math.min( - fromInventory.getStackInSlot(fromSlot).stackSize, - Math.min(aMaxMoveAtOnce, aMaxTargetStackSize)), - fromInventory.getStackInSlot(fromSlot)); - byte tMovedItemCount = (byte) bcPipe.injectItem(copyOrNull(tStack), false, putSide); - if (tMovedItemCount >= Math.max(aMinMoveAtOnce, aMinTargetStackSize)) { - tMovedItemCount = (byte) (bcPipe - .injectItem(copyAmount(tMovedItemCount, tStack), true, putSide)); - fromInventory.decrStackSize(fromSlot, tMovedItemCount); - fromInventory.markDirty(); - return tMovedItemCount; - } - } - } - } - } - return 0; - } - } - - if (fromInventory instanceof TileEntity fromTileEntity && fromSide != ForgeDirection.UNKNOWN - && fromSide.getOpposite() == ForgeDirection.getOrientation(putSide.ordinal())) { - int tX = fromTileEntity.xCoord + fromSide.offsetX, tY = fromTileEntity.yCoord + fromSide.offsetY, - tZ = fromTileEntity.zCoord + fromSide.offsetZ; - if (!hasBlockHitBox(((TileEntity) fromInventory).getWorldObj(), tX, tY, tZ) && dropItem) { - for (final int fromSlot : fromSlots) { - if (listContainsItem(aFilter, fromInventory.getStackInSlot(fromSlot), true, aInvertFilter)) { - if (isAllowedToTakeFromSlot( - fromInventory, - fromSlot, - fromSide, - fromInventory.getStackInSlot(fromSlot))) { - if (Math.max(aMinMoveAtOnce, aMinTargetStackSize) - <= fromInventory.getStackInSlot(fromSlot).stackSize) { - final ItemStack tStack = copyAmount( - Math.min( - fromInventory.getStackInSlot(fromSlot).stackSize, - Math.min(aMaxMoveAtOnce, aMaxTargetStackSize)), - fromInventory.getStackInSlot(fromSlot)); - final EntityItem tEntity = new EntityItem( - ((TileEntity) fromInventory).getWorldObj(), - tX + 0.5, - tY + 0.5, - tZ + 0.5, - tStack); - tEntity.motionX = tEntity.motionY = tEntity.motionZ = 0; - ((TileEntity) fromInventory).getWorldObj() - .spawnEntityInWorld(tEntity); - assert tStack != null; - fromInventory.decrStackSize(fromSlot, tStack.stackSize); - fromInventory.markDirty(); - return (byte) tStack.stackSize; - } - } - } - } - } - } - return 0; - } - - /** - * Moves Stack from Inv-Slot to Inv-Slot, without checking if its even allowed. (useful for internal Inventory - * Operations) - * - * @return the Amount of moved Items - */ - public static byte moveStackFromSlotAToSlotB(IInventory aTileEntity1, IInventory aTileEntity2, int aGrabFrom, - int aPutTo, byte aMaxTargetStackSize, byte aMinTargetStackSize, byte aMaxMoveAtOnce, byte aMinMoveAtOnce) { - if (aTileEntity1 == null || aTileEntity2 == null - || aMinTargetStackSize <= 0 - || aMinTargetStackSize > aMaxTargetStackSize - || aMaxMoveAtOnce <= 0 - || aMinMoveAtOnce > aMaxMoveAtOnce) return 0; - - ItemStack tStack1 = aTileEntity1.getStackInSlot(aGrabFrom), tStack2 = aTileEntity2.getStackInSlot(aPutTo), - tStack3; - if (tStack1 != null) { - if (tStack2 != null && !areStacksEqual(tStack1, tStack2)) return 0; - tStack3 = copyOrNull(tStack1); - aMaxTargetStackSize = (byte) Math.min( - aMaxTargetStackSize, - Math.min( - tStack3.getMaxStackSize(), - Math.min( - tStack2 == null ? Integer.MAX_VALUE : tStack2.getMaxStackSize(), - aTileEntity2.getInventoryStackLimit()))); - tStack3.stackSize = Math - .min(tStack3.stackSize, aMaxTargetStackSize - (tStack2 == null ? 0 : tStack2.stackSize)); - if (tStack3.stackSize > aMaxMoveAtOnce) tStack3.stackSize = aMaxMoveAtOnce; - if (tStack3.stackSize + (tStack2 == null ? 0 : tStack2.stackSize) - >= Math.min(tStack3.getMaxStackSize(), aMinTargetStackSize) && tStack3.stackSize >= aMinMoveAtOnce) { - tStack3 = aTileEntity1.decrStackSize(aGrabFrom, tStack3.stackSize); - aTileEntity1.markDirty(); - if (tStack3 != null) { - if (tStack2 == null) { - aTileEntity2.setInventorySlotContents(aPutTo, copyOrNull(tStack3)); - } else { - tStack2.stackSize += tStack3.stackSize; - } - aTileEntity2.markDirty(); - return (byte) tStack3.stackSize; - } - } - } - return 0; - } - - public static boolean isAllowedToTakeFromSlot(IInventory aTileEntity, int aSlot, ForgeDirection side, - ItemStack aStack) { - if (side == ForgeDirection.UNKNOWN) { - return Arrays.stream(ForgeDirection.VALID_DIRECTIONS) - .anyMatch(d -> isAllowedToTakeFromSlot(aTileEntity, aSlot, d, aStack)); - } - if (aTileEntity instanceof ISidedInventory sided) return sided.canExtractItem(aSlot, aStack, side.ordinal()); - return true; - } - - public static boolean isAllowedToPutIntoSlot(IInventory aTileEntity, int aSlot, ForgeDirection side, - ItemStack aStack, byte aMaxStackSize) { - ItemStack tStack = aTileEntity.getStackInSlot(aSlot); - if (tStack != null && (!areStacksEqual(tStack, aStack) || tStack.stackSize >= tStack.getMaxStackSize())) - return false; - if (side == ForgeDirection.UNKNOWN) { - return Arrays.stream(ForgeDirection.VALID_DIRECTIONS) - .anyMatch(d -> isAllowedToPutIntoSlot(aTileEntity, aSlot, d, aStack, aMaxStackSize)); - } - if (aTileEntity instanceof ISidedInventory - && !((ISidedInventory) aTileEntity).canInsertItem(aSlot, aStack, side.ordinal())) return false; - return aSlot < aTileEntity.getSizeInventory() && aTileEntity.isItemValidForSlot(aSlot, aStack); - } - - /** - * moves multiple stacks from Inv-Side to Inv-Side - * - * @return the Amount of moved Items - */ - public static int moveMultipleItemStacks(Object aTileEntity1, Object aTileEntity2, ForgeDirection fromSide, - ForgeDirection putSide, List<ItemStack> aFilter, boolean aInvertFilter, byte aMaxTargetStackSize, - byte aMinTargetStackSize, byte aMaxMoveAtOnce, byte aMinMoveAtOnce, int aStackAmount) { - if (aTileEntity1 instanceof IInventory) return moveMultipleItemStacks( - (IInventory) aTileEntity1, - aTileEntity2, - fromSide, - putSide, - aFilter, - aInvertFilter, - aMaxTargetStackSize, - aMinTargetStackSize, - aMaxMoveAtOnce, - aMinMoveAtOnce, - aStackAmount, - true); - return 0; - } - - public static int moveMultipleItemStacks(IInventory fromInventory, Object toObject, ForgeDirection fromSide, - ForgeDirection putSide, List<ItemStack> aFilter, boolean aInvertFilter, byte aMaxTargetStackSize, - byte aMinTargetStackSize, byte aMaxMoveAtOnce, byte aMinMoveAtOnce, int aMaxStackTransfer, - boolean aDoCheckChests) { - if (fromInventory == null || aMaxTargetStackSize <= 0 - || aMinTargetStackSize <= 0 - || aMaxMoveAtOnce <= 0 - || aMinTargetStackSize > aMaxTargetStackSize - || aMinMoveAtOnce > aMaxMoveAtOnce - || aMaxStackTransfer == 0) return 0; - - // find where to take from - final int[] tGrabSlots = new int[fromInventory.getSizeInventory()]; - int tGrabSlotsSize = 0; - if (fromInventory instanceof ISidedInventory) { - for (int i : ((ISidedInventory) fromInventory).getAccessibleSlotsFromSide(fromSide.ordinal())) { - final ItemStack s = fromInventory.getStackInSlot(i); - if (s == null || !isAllowedToTakeFromSlot(fromInventory, i, fromSide, s) - || s.stackSize < aMinMoveAtOnce - || !listContainsItem(aFilter, s, true, aInvertFilter)) continue; - tGrabSlots[tGrabSlotsSize++] = i; - } - } else { - for (int i = 0; i < tGrabSlots.length; i++) { - ItemStack s = fromInventory.getStackInSlot(i); - if (s == null || s.stackSize < aMinMoveAtOnce || !listContainsItem(aFilter, s, true, aInvertFilter)) - continue; - tGrabSlots[tGrabSlotsSize++] = i; - } - } - - // no source, bail out - if (tGrabSlotsSize == 0) { - // maybe source is a double chest. check it - if (aDoCheckChests && fromInventory instanceof TileEntityChest chest) return moveFromAdjacentChests( - chest, - toObject, - fromSide, - putSide, - aFilter, - aInvertFilter, - aMaxTargetStackSize, - aMinTargetStackSize, - aMaxMoveAtOnce, - aMinMoveAtOnce, - aMaxStackTransfer); - return 0; - } - - // if target is an inventory, e.g. chest, machine, drawers... - if (toObject instanceof IInventory toInventory) { - - // partially filled slot spare space mapping. - // value is the sum of all spare space left not counting completely empty slot - final HashMap<ItemId, Integer> tPutItems = |
