BloodMagic/src/main/java/WayofTime/bloodmagic/api/compress/CompressionRegistry.java

70 lines
2 KiB
Java
Raw Normal View History

2015-12-29 00:09:51 +00:00
package WayofTime.bloodmagic.api.compress;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
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.
2015-12-29 00:09:51 +00:00
*/
public class CompressionRegistry
{
2015-12-29 00:09:51 +00:00
public static List<CompressionHandler> compressionRegistry = new ArrayList<CompressionHandler>();
public static Map<ItemStack, Integer> thresholdMap = new HashMap<ItemStack, Integer>();
public static void registerHandler(CompressionHandler handler)
{
2015-12-29 00:09:51 +00:00
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
2015-12-29 00:09:51 +00:00
*/
public static void registerItemThreshold(ItemStack stack, int threshold)
{
2015-12-29 00:09:51 +00:00
thresholdMap.put(stack, threshold);
}
public static ItemStack compressInventory(ItemStack[] inv, World world)
{
for (CompressionHandler handler : compressionRegistry)
{
2015-12-29 00:09:51 +00:00
ItemStack stack = handler.compressInventory(inv, world);
if (stack != null)
{
2015-12-29 00:09:51 +00:00
return stack;
}
}
return null;
}
public static int getItemThreshold(ItemStack stack)
{
for (Map.Entry<ItemStack, Integer> entry : thresholdMap.entrySet())
{
if (areItemStacksEqual(entry.getKey(), stack))
{
2015-12-29 00:09:51 +00:00
return entry.getValue();
}
}
return 0;
}
public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack)
{
2015-12-29 00:09:51 +00:00
return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? compressedStack.getTagCompound() == null : stack.getTagCompound().equals(compressedStack.getTagCompound()));
}
}