From 1f392721fa7596485125e4a11b851892116f3cbd Mon Sep 17 00:00:00 2001 From: AEon - Tobias Date: Sun, 26 Aug 2018 22:05:30 +0200 Subject: [PATCH] Reinstated Compression sigil. (#1374) * 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. --- .../compress/AdvancedCompressionHandler.java | 187 ++++++++---------- .../compress/BaseCompressionHandler.java | 57 +----- .../compress/CompressionHandler.java | 46 ++++- .../compress/CompressionRegistry.java | 12 +- .../compress/StorageBlockCraftingManager.java | 26 ++- .../item/sigil/ItemSigilCompression.java | 2 - .../bloodmagic/registry/ModRecipes.java | 3 +- 7 files changed, 153 insertions(+), 180 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/compress/AdvancedCompressionHandler.java b/src/main/java/WayofTime/bloodmagic/compress/AdvancedCompressionHandler.java index 2050f6b7..b5d78913 100644 --- a/src/main/java/WayofTime/bloodmagic/compress/AdvancedCompressionHandler.java +++ b/src/main/java/WayofTime/bloodmagic/compress/AdvancedCompressionHandler.java @@ -4,31 +4,98 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; + public class AdvancedCompressionHandler extends CompressionHandler { - @Override - public ItemStack compressInventory(ItemStack[] inv, World world) { - return test(inv, true, world); + + private static InventoryCrafting[] inventoryCrafting = { + new InventoryCrafting(new Container() { + public boolean canInteractWith(EntityPlayer player) { + return false; + } + }, + 3, 3), + new InventoryCrafting(new Container() { + public boolean canInteractWith(EntityPlayer player) { + return false; + } + }, + 2, 2), + new InventoryCrafting(new Container() { + public boolean canInteractWith(EntityPlayer player) { + return false; + } + }, + 1, 1) + + }; + + private static ItemStack reversibleCheck; + + public static boolean isResultStackReversible(ItemStack stack, World world) { + if (stack.isEmpty()) { + return false; + } + + inventoryCrafting[2].setInventorySlotContents(0, stack); + ItemStack returnStack = getNNRecipeOutput(inventoryCrafting[2], world); + + return !returnStack.isEmpty() && CompressionRegistry.areItemStacksEqual(reversibleCheck, returnStack); } - public ItemStack test(ItemStack[] inv, boolean doDrain, World world) { + public static ItemStack getRecipe(ItemStack stack, World world, int gridSize) { + StorageBlockCraftingManager craftingManagerSB = StorageBlockCraftingManager.getInstance(); + InventoryCrafting inventory = inventoryCrafting[3 - gridSize]; + for (int i = 0; i < inventory.getSizeInventory(); i++) { + inventory.setInventorySlotContents(i, stack); + } + ItemStack notEmptyRecipe = craftingManagerSB.findMatchingRecipe(inventory, world); + if (!notEmptyRecipe.isEmpty()) { + return notEmptyRecipe; + } + ItemStack result = getNNRecipeOutput(inventory, world); + + if (isResultStackReversible(result, world)) { + craftingManagerSB.addRecipe(CraftingManager.findMatchingRecipe(inventory, world)); + return result; + } + return ItemStack.EMPTY; + } + + public static ItemStack getNNRecipeOutput(InventoryCrafting inventory, World world) { + IRecipe checkForNull = CraftingManager.findMatchingRecipe(inventory, world); + if (checkForNull != null) { + return checkForNull.getRecipeOutput(); + } + return ItemStack.EMPTY; + } + + public static ItemStack get22Recipe(ItemStack stack, World world) { + return getRecipe(stack, world, 2); + } + + public static ItemStack get33Recipe(ItemStack stack, World world) { + return getRecipe(stack, world, 3); + } + + public ItemStack compressInventory(ItemStack[] inv, World world) { for (ItemStack invStack : inv) { if (invStack.isEmpty()) { continue; } - for (int i = 2; i <= 3; i++) { + for (int i = 3; i >= 2; i--) { + reversibleCheck = invStack; ItemStack stack = getRecipe(invStack, world, i); if (!stack.isEmpty()) { - int threshold = CompressionRegistry.getItemThreshold(invStack); - int needed = i * i; - int neededLeft = iterateThroughInventory(invStack, threshold + invStack.getMaxStackSize() - needed, inv, needed, false); - if (neededLeft <= 0) { - iterateThroughInventory(invStack, 0, inv, needed, true); + int needed = (i == 2 ? 4 : 9); + int remaining = iterateThroughInventory(invStack, invStack.getMaxStackSize() - needed, inv, needed, true); // if more than needed gets consumed at any point, the simulate test was needed after all + if (remaining <= 0) return stack; - } } } } @@ -36,103 +103,5 @@ public class AdvancedCompressionHandler extends CompressionHandler { return ItemStack.EMPTY; } - public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) { - int i = -1; - for (ItemStack invStack : inv) { - i++; - - if (invStack.isEmpty()) { - continue; - } - - if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) { - int stackSize = invStack.getCount(); - int used = 0; - if (kept > 0) { - int remainingFromStack = Math.max(stackSize - kept, 0); - used += stackSize - remainingFromStack; - } - - kept -= used; - - if (kept <= 0 && needed > 0) { - int remainingFromStack = Math.max(stackSize - used - needed, 0); - if (doDrain) { - invStack.setCount(remainingFromStack + used); - if (invStack.isEmpty()) { - inv[i] = ItemStack.EMPTY; - } - } - - needed -= (stackSize - used - remainingFromStack); - } - - if (needed <= 0) { - return 0; - } - } - } - - return needed; - } - - public static boolean isResultStackReversible(ItemStack stack, int gridSize, World world) { - if (stack.isEmpty()) { - return false; - } - InventoryCrafting inventory = new InventoryCrafting(new Container() { - public boolean canInteractWith(EntityPlayer player) { - return false; - } - }, 2, 2); - - inventory.setInventorySlotContents(0, stack); - - ItemStack returnStack = StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world); - if (returnStack.isEmpty()) { - return false; - } - - ItemStack compressedStack = ItemStack.EMPTY; - switch (gridSize) { - case 2: - compressedStack = get22Recipe(returnStack, world); - break; - case 3: - compressedStack = get33Recipe(returnStack, world); - break; - } - - return !compressedStack.isEmpty() && CompressionRegistry.areItemStacksEqual(stack, compressedStack); - } - - public static ItemStack getRecipe(ItemStack stack, World world, int gridSize) { - InventoryCrafting inventory = new InventoryCrafting(new Container() { - public boolean canInteractWith(EntityPlayer player) { - return false; - } - }, gridSize, gridSize); - for (int i = 0; i < inventory.getSizeInventory(); i++) { - inventory.setInventorySlotContents(i, stack); - } - - return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world); - } - - public static boolean has22Recipe(ItemStack stack, World world) { - return !get22Recipe(stack, world).isEmpty(); - } - - public static ItemStack get22Recipe(ItemStack stack, World world) { - return getRecipe(stack, world, 2); - } - - public static boolean has33Recipe(ItemStack stack, World world) { - return !get33Recipe(stack, world).isEmpty(); - } - - public static ItemStack get33Recipe(ItemStack stack, World world) { - return getRecipe(stack, world, 3); - } } diff --git a/src/main/java/WayofTime/bloodmagic/compress/BaseCompressionHandler.java b/src/main/java/WayofTime/bloodmagic/compress/BaseCompressionHandler.java index 1af4b63b..94b4ddf6 100644 --- a/src/main/java/WayofTime/bloodmagic/compress/BaseCompressionHandler.java +++ b/src/main/java/WayofTime/bloodmagic/compress/BaseCompressionHandler.java @@ -8,11 +8,11 @@ public class BaseCompressionHandler extends CompressionHandler { private final ItemStack result; private final int leftover; - public BaseCompressionHandler(ItemStack requested, ItemStack result, int leftover) { + public BaseCompressionHandler(ItemStack input, ItemStack output, int remainder) { super(); - this.required = requested; - this.result = result; - this.leftover = leftover; + this.required = input; + this.result = output; + this.leftover = remainder; } public ItemStack getResultStack() { @@ -35,57 +35,18 @@ public class BaseCompressionHandler extends CompressionHandler { } public int getRemainingNeeded(ItemStack[] inv) { - return iterateThroughInventory(inv, false); + int needed = this.required.getCount(); + int kept = this.getLeftover(); + return iterateThroughInventory(this.required, kept, inv, needed, true); } public int drainInventory(ItemStack[] inv) { - return iterateThroughInventory(inv, true); - } - - public int iterateThroughInventory(ItemStack[] inv, boolean doDrain) { int needed = this.required.getCount(); int kept = this.getLeftover(); - int i = -1; - - for (ItemStack invStack : inv) { - i++; - - if (invStack == null) { - continue; - } - - if (invStack.isItemEqual(this.required) && (invStack.getTagCompound() == null ? this.required.getTagCompound() == null : invStack.getTagCompound().equals(this.required.getTagCompound()))) { - int stackSize = invStack.getCount(); - int used = 0; - if (kept > 0) { - int remainingFromStack = Math.max(stackSize - kept, 0); - used += stackSize - remainingFromStack; - } - - kept -= used; - - if (kept <= 0 && needed > 0) { - int remainingFromStack = Math.max(stackSize - used - needed, 0); - if (doDrain) { - invStack.setCount(remainingFromStack + used); - if (invStack.isEmpty()) { - inv[i] = ItemStack.EMPTY; - } - } - - needed -= (stackSize - used - remainingFromStack); - } - - if (needed <= 0) { - return 0; - } - } - } - - return needed; + return iterateThroughInventory(this.required, kept, inv, needed, true); } public int getLeftover() { return this.leftover; } -} +} \ No newline at end of file diff --git a/src/main/java/WayofTime/bloodmagic/compress/CompressionHandler.java b/src/main/java/WayofTime/bloodmagic/compress/CompressionHandler.java index b7da216d..2cd74d14 100644 --- a/src/main/java/WayofTime/bloodmagic/compress/CompressionHandler.java +++ b/src/main/java/WayofTime/bloodmagic/compress/CompressionHandler.java @@ -12,4 +12,48 @@ public abstract class CompressionHandler { * @return The result of the compression */ public abstract ItemStack compressInventory(ItemStack[] inv, World world); -} + + public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) { + int i = -1; + + for (ItemStack invStack : inv) { + i++; + + if (invStack.isEmpty()) { + continue; + } + + if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) { + int stackSize = invStack.getCount(); + int used = 0; + if (kept > 0) { + int remainingFromStack = Math.max(stackSize - kept, 0); + used += stackSize - remainingFromStack; + } + + kept -= used; // 0 + + if (kept <= 0 && needed > 0) { + int remainingFromStack = Math.max(stackSize - used - needed, 0); + if (doDrain) { + invStack.setCount(remainingFromStack + used); + if (invStack.isEmpty()) { + inv[i] = ItemStack.EMPTY; + } + } + + needed -= (stackSize - used - remainingFromStack); + } + + if (needed <= 0) { + return 0; + } + } + } + + return needed; + + + } + +} \ No newline at end of file diff --git a/src/main/java/WayofTime/bloodmagic/compress/CompressionRegistry.java b/src/main/java/WayofTime/bloodmagic/compress/CompressionRegistry.java index 2d975531..8c10fa3a 100644 --- a/src/main/java/WayofTime/bloodmagic/compress/CompressionRegistry.java +++ b/src/main/java/WayofTime/bloodmagic/compress/CompressionRegistry.java @@ -38,7 +38,7 @@ public class CompressionRegistry { public static ItemStack compressInventory(ItemStack[] inv, World world) { for (CompressionHandler handler : compressionRegistry) { ItemStack stack = handler.compressInventory(inv, world); - if (stack != null) { + if (!stack.isEmpty()) { return stack; } } @@ -78,16 +78,10 @@ public class CompressionRegistry { } public static int getItemThreshold(ItemStack stack) { - for (Map.Entry entry : thresholdMap.entrySet()) { - if (areItemStacksEqual(entry.getKey(), stack)) { - return entry.getValue(); - } - } - - return 0; + 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())); } -} +} \ No newline at end of file diff --git a/src/main/java/WayofTime/bloodmagic/compress/StorageBlockCraftingManager.java b/src/main/java/WayofTime/bloodmagic/compress/StorageBlockCraftingManager.java index 19e06bb4..96d31140 100644 --- a/src/main/java/WayofTime/bloodmagic/compress/StorageBlockCraftingManager.java +++ b/src/main/java/WayofTime/bloodmagic/compress/StorageBlockCraftingManager.java @@ -14,11 +14,17 @@ import java.util.List; public class StorageBlockCraftingManager { private static final StorageBlockCraftingManager instance = new StorageBlockCraftingManager(); - private List recipes = new LinkedList(); + private List recipes = new LinkedList<>(); + public void addRecipe(IRecipe recipe){ + this.recipes.add(recipe); + } + + //recipes are currently added during runtime, this will only return recipes specifically added in init + //through BaseCompressionHandler public void addStorageBlockRecipes() { -// this.recipes = new StorageBlockCraftingRecipeAssimilator().getPackingRecipes(); + //this.recipes = new StorageBlockCraftingRecipeAssimilator().getPackingRecipes(); BMLog.DEBUG.info("Total number of compression recipes: " + this.recipes.size()); } @@ -26,7 +32,7 @@ public class StorageBlockCraftingManager { return this.findMatchingRecipe(craftingInventory, world, this.recipes); } - private ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world, List list) { + private ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world, List list) { int i = 0; ItemStack itemstack = ItemStack.EMPTY; ItemStack itemstack1 = ItemStack.EMPTY; @@ -62,7 +68,7 @@ public class StorageBlockCraftingManager { return new ItemStack(itemstack.getItem(), 1, i1); } else { for (j = 0; j < list.size(); ++j) { - IRecipe irecipe = (IRecipe) list.get(j); + IRecipe irecipe = list.get(j); if (irecipe.matches(craftingInventory, world)) { return irecipe.getCraftingResult(craftingInventory); @@ -77,7 +83,7 @@ public class StorageBlockCraftingManager { return instance; } - private static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List list) { + private static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List list) { if (stack.isEmpty()) { return false; } @@ -107,7 +113,7 @@ public class StorageBlockCraftingManager { return !compressedStack.isEmpty() && CompressionRegistry.areItemStacksEqual(stack, compressedStack); } - private static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List list) { + private static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List list) { InventoryCrafting inventory = new InventoryCrafting(new Container() { public boolean canInteractWith(EntityPlayer player) { return false; @@ -120,19 +126,19 @@ public class StorageBlockCraftingManager { return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world, list); } - private static boolean has22Recipe(ItemStack stack, World world, List list) { + private static boolean has22Recipe(ItemStack stack, World world, List list) { return !get22Recipe(stack, world, list).isEmpty(); } - private static ItemStack get22Recipe(ItemStack stack, World world, List list) { + private static ItemStack get22Recipe(ItemStack stack, World world, List list) { return getRecipe(stack, world, 2, list); } - private static boolean has33Recipe(ItemStack stack, World world, List list) { + private static boolean has33Recipe(ItemStack stack, World world, List list) { return !get33Recipe(stack, world, list).isEmpty(); } - private static ItemStack get33Recipe(ItemStack stack, World world, List list) { + private static ItemStack get33Recipe(ItemStack stack, World world, List list) { return getRecipe(stack, world, 3, list); } } diff --git a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilCompression.java b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilCompression.java index d23ced18..1359167a 100644 --- a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilCompression.java +++ b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilCompression.java @@ -17,8 +17,6 @@ public class ItemSigilCompression extends ItemSigilToggleableBase { @Override public void onSigilUpdate(ItemStack stack, World world, EntityPlayer player, int itemSlot, boolean isSelected) { - if (true) - return; // TODO - Rewrite compression system if (PlayerHelper.isFakePlayer(player)) return; diff --git a/src/main/java/WayofTime/bloodmagic/registry/ModRecipes.java b/src/main/java/WayofTime/bloodmagic/registry/ModRecipes.java index 25d54dd2..688c588a 100644 --- a/src/main/java/WayofTime/bloodmagic/registry/ModRecipes.java +++ b/src/main/java/WayofTime/bloodmagic/registry/ModRecipes.java @@ -85,6 +85,7 @@ public class ModRecipes addAlchemyTableRecipes(); addPotionRecipes(); addLivingArmourDowngradeRecipes(); + addCompressionHandlers(); } public static void initOreDict() @@ -140,11 +141,11 @@ public class ModRecipes { Stopwatch stopwatch = Stopwatch.createStarted(); StorageBlockCraftingManager.getInstance().addStorageBlockRecipes(); + CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.GLOWSTONE_DUST, 4, 0), new ItemStack(Blocks.GLOWSTONE), 64)); CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.SNOWBALL, 4, 0), new ItemStack(Blocks.SNOW), 8)); CompressionRegistry.registerHandler(new AdvancedCompressionHandler()); - CompressionRegistry.registerItemThreshold(new ItemStack(Blocks.COBBLESTONE), 64); stopwatch.stop(); BMLog.DEBUG.info("Added compression recipes in {}", stopwatch);