From 5224b808c33070d42456e5eb549fe1648430d7da Mon Sep 17 00:00:00 2001
From: WayofTime <wtime@live.ca>
Date: Thu, 20 Nov 2014 10:49:21 -0500
Subject: [PATCH] Fixing compression glitches

---
 .../AlchemicalWizardry.java                   |  4 ++-
 .../api/compress/CompressionRegistry.java     | 28 +++++++++++++++++++
 .../compress/AdvancedCompressionHandler.java  |  8 ++++--
 .../compress/BaseCompressionHandler.java      |  3 +-
 .../common/items/sigil/ItemPackRatSigil.java  |  1 -
 .../spell/complex/effect/SpellHelper.java     |  5 ++++
 .../assets/alchemicalwizardry/lang/en_US.lang |  1 +
 7 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java
index 120dbd38..715e8fa6 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java
@@ -1303,7 +1303,9 @@ public class AlchemicalWizardry
     
     public void initCompressionHandlers()
     {
-    	CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.glowstone_dust, 4, 0), new ItemStack(Blocks.glowstone), 0));
+    	CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.glowstone_dust, 4, 0), new ItemStack(Blocks.glowstone), 64));
     	CompressionRegistry.registerHandler(new AdvancedCompressionHandler());
+    	
+    	CompressionRegistry.registerItemThreshold(new ItemStack(Blocks.cobblestone), 64);
     }
 }
diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/compress/CompressionRegistry.java b/src/main/java/WayofTime/alchemicalWizardry/api/compress/CompressionRegistry.java
index f1e05742..12c95a5a 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/api/compress/CompressionRegistry.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/api/compress/CompressionRegistry.java
@@ -1,8 +1,12 @@
 package WayofTime.alchemicalWizardry.api.compress;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
+import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
 import net.minecraft.item.ItemStack;
 import net.minecraft.world.World;
 
@@ -13,12 +17,23 @@ import net.minecraft.world.World;
 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
+	 * @param threshold
+	 */
+	public static void registerItemThreshold(ItemStack stack, int threshold)
+	{
+		thresholdMap.put(stack, new Integer(threshold));
+	}
+	
 	public static ItemStack compressInventory(ItemStack[] inv, World world)
 	{
 		for(CompressionHandler handler : compressionRegistry)
@@ -32,4 +47,17 @@ public class CompressionRegistry
 		
 		return null;
 	}
+	
+	public static int getItemThreshold(ItemStack stack)
+	{
+		for(Entry<ItemStack, Integer> entry : thresholdMap.entrySet())
+		{
+			if(SpellHelper.areItemStacksEqual(entry.getKey(), stack))
+			{
+				return entry.getValue();
+			}
+		}
+		
+		return 0;
+	}
 }
diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/compress/AdvancedCompressionHandler.java b/src/main/java/WayofTime/alchemicalWizardry/common/compress/AdvancedCompressionHandler.java
index 118211b1..6a407234 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/common/compress/AdvancedCompressionHandler.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/common/compress/AdvancedCompressionHandler.java
@@ -7,6 +7,8 @@ import net.minecraft.item.ItemStack;
 import net.minecraft.item.crafting.CraftingManager;
 import net.minecraft.world.World;
 import WayofTime.alchemicalWizardry.api.compress.CompressionHandler;
+import WayofTime.alchemicalWizardry.api.compress.CompressionRegistry;
+import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
 
 public class AdvancedCompressionHandler extends CompressionHandler
 {	
@@ -30,13 +32,15 @@ public class AdvancedCompressionHandler extends CompressionHandler
 				continue;
 			}
 			
+			int threshold = CompressionRegistry.getItemThreshold(invStack);
+			
 			for(int i=2; i<=3; i++)
 			{
 				ItemStack stacky = getRecipe(invStack, world, i);
 				if(isResultStackReversible(stacky, i, world))
 				{
 					int needed = i*i;
-					int neededLeft = iterateThroughInventory(invStack, 0, inv, needed, false);
+					int neededLeft = iterateThroughInventory(invStack, threshold, inv, needed, false);
 					if(neededLeft <= 0)
 					{
 						iterateThroughInventory(invStack, 0, inv, needed, true);
@@ -132,7 +136,7 @@ public class AdvancedCompressionHandler extends CompressionHandler
 			return false;
 		}else
 		{
-			return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? compressedStack.getTagCompound() == null : stack.getTagCompound().equals(compressedStack.getTagCompound()));
+			return SpellHelper.areItemStacksEqual(stack, compressedStack);
 		}
 	}
 	
diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/compress/BaseCompressionHandler.java b/src/main/java/WayofTime/alchemicalWizardry/common/compress/BaseCompressionHandler.java
index 7e356d51..e4ba0be9 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/common/compress/BaseCompressionHandler.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/common/compress/BaseCompressionHandler.java
@@ -81,7 +81,6 @@ public class BaseCompressionHandler extends CompressionHandler
 				if(kept <= 0 && needed > 0)
 				{
 					int remainingFromStack = Math.max(stackSize - used - needed, 0);
-					needed -= (stackSize - used - remainingFromStack);
 					if(doDrain)
 					{
 						invStack.stackSize = remainingFromStack;
@@ -90,6 +89,8 @@ public class BaseCompressionHandler extends CompressionHandler
 							inv[i] = null;
 						}
 					}
+					
+					needed -= (stackSize - used - remainingFromStack);
 				}
 				
 				if(needed <= 0)
diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/sigil/ItemPackRatSigil.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/sigil/ItemPackRatSigil.java
index 2f664bc7..c12ebee8 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/common/items/sigil/ItemPackRatSigil.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/sigil/ItemPackRatSigil.java
@@ -14,7 +14,6 @@ import WayofTime.alchemicalWizardry.AlchemicalWizardry;
 import WayofTime.alchemicalWizardry.api.compress.CompressionRegistry;
 import WayofTime.alchemicalWizardry.api.items.interfaces.ArmourUpgrade;
 import WayofTime.alchemicalWizardry.api.items.interfaces.IHolding;
-import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler;
 import WayofTime.alchemicalWizardry.common.items.EnergyItems;
 import cpw.mods.fml.relauncher.Side;
 import cpw.mods.fml.relauncher.SideOnly;
diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java b/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java
index 4d8f6c21..87b08d40 100644
--- a/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java
+++ b/src/main/java/WayofTime/alchemicalWizardry/common/spell/complex/effect/SpellHelper.java
@@ -562,4 +562,9 @@ public class SpellHelper
         }
         return null;
     }
+    
+    public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack)
+    {
+    	return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? compressedStack.getTagCompound() == null : stack.getTagCompound().equals(compressedStack.getTagCompound()));
+    }
 }
diff --git a/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang b/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang
index dbf3eebd..1bc12184 100644
--- a/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang
+++ b/src/main/resources/assets/alchemicalwizardry/lang/en_US.lang
@@ -189,6 +189,7 @@ item.demonPlacer.name=Demon Crystal
 item.creativeDagger.name=Creative Sacrificial Knife
 item.itemBloodPack.name=Blood Letter's Pack
 item.itemHarvestSigil.name=Harvest Goddess Sigil
+item.itemCompressionSigil.name=Sigil of Compression
 #Creative Tab
 itemGroup.tabBloodMagic=Blood Magic