
* Reinstated Compression sigil. - does not compress wooden planks into crafting tables - searches for reversible 3x3/2x2 recipes with a single material type - probably has a lot of redundant stuff and looks silly - uses try/catch (might want to find a different approach, some people scoff at this) - should probably have spent the night sleeping, I'm taking exams tomorrow and will probably sleep the whole day through. * Learned how to properly handle the "NullPointerException"-situation. * Update BaseCompressionHandler.java * Update CompressionRegistry.java * Removed (almost) all code comments (only a one-liner remains that serves as pointer to a completly commented-out class (StorageBlockCraftingRecipeAssimilator)). Made methods and variables for long function calls to be more efficient. Fixed an oversight that resulted in a NullPointerException after removing redundant checks that were made to prevent exactly that. Rearranged and reformatted code. * corrected something that could be considered a typo but was an oversight * This should be it. An Array should be more efficient but you can correct me if I'm wrong. In either case it does what it is supposed to do. * Fixed. Needed a seperate inventory for the reversible check (or clear the previously used one, but then I'd had to repopulate again and that would just have been messy) * Forgot one of my lines. * Fix and cleanup. Could definitely clean more but that should suffice for now.
87 lines
No EOL
3.6 KiB
Java
87 lines
No EOL
3.6 KiB
Java
package WayofTime.bloodmagic.compress;
|
|
|
|
import WayofTime.bloodmagic.util.Utils;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
import net.minecraftforge.items.IItemHandler;
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* A registry aimed to help compress items in an inventory into its compressible
|
|
* form.
|
|
*/
|
|
public class CompressionRegistry {
|
|
public static List<CompressionHandler> compressionRegistry = new ArrayList<>();
|
|
public static Map<ItemStack, Integer> thresholdMap = new HashMap<>();
|
|
|
|
public static void registerHandler(CompressionHandler handler) {
|
|
compressionRegistry.add(handler);
|
|
}
|
|
|
|
/**
|
|
* Registers an item so that it only compresses while above this threshold
|
|
*
|
|
* @param stack item/block to be compressed
|
|
* @param threshold amount that is to be compressed
|
|
*/
|
|
public static void registerItemThreshold(ItemStack stack, int threshold) {
|
|
thresholdMap.put(stack, threshold);
|
|
}
|
|
|
|
public static ItemStack compressInventory(ItemStack[] inv, World world) {
|
|
for (CompressionHandler handler : compressionRegistry) {
|
|
ItemStack stack = handler.compressInventory(inv, world);
|
|
if (!stack.isEmpty()) {
|
|
return stack;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static Pair<ItemStack, Boolean> compressInventory(TileEntity tile, World world) {
|
|
if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
|
|
IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
|
|
ItemStack[] inventory = new ItemStack[itemHandler.getSlots()]; //THIS MUST NOT BE EDITED!
|
|
ItemStack[] copyInventory = new ItemStack[itemHandler.getSlots()];
|
|
|
|
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
|
|
inventory[slot] = itemHandler.extractItem(slot, 64, true);
|
|
copyInventory[slot] = inventory[slot].copy();
|
|
}
|
|
|
|
for (CompressionHandler handler : compressionRegistry) {
|
|
ItemStack stack = handler.compressInventory(copyInventory, world);
|
|
if (!stack.isEmpty()) {
|
|
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
|
|
if (inventory[slot] != null && !ItemStack.areItemStacksEqual(inventory[slot], copyInventory[slot])) {
|
|
itemHandler.extractItem(slot, inventory[slot].getCount(), false);
|
|
if (copyInventory[slot] != null) {
|
|
itemHandler.insertItem(slot, copyInventory[slot], false);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Pair.of(Utils.insertStackIntoTile(stack, itemHandler), true);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Pair.of(ItemStack.EMPTY, false);
|
|
}
|
|
|
|
public static int getItemThreshold(ItemStack stack) {
|
|
return stack.getItem().getItemStackLimit(stack); //this should work according to the guide, leaving behind a full stack of the source item (unless otherwise specified with a BaseCompressionHandler recipe)
|
|
}
|
|
|
|
public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack) {
|
|
return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? !compressedStack.hasTagCompound() : stack.getTagCompound().equals(compressedStack.getTagCompound()));
|
|
}
|
|
} |