2015-12-28 19:09:51 -05:00
|
|
|
package WayofTime.bloodmagic.api.compress;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-11-03 10:52:14 -04:00
|
|
|
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 WayofTime.bloodmagic.util.Utils;
|
|
|
|
|
2015-12-28 19:09:51 -05:00
|
|
|
/**
|
2015-12-30 15:34:40 -05:00
|
|
|
* A registry aimed to help compress items in an inventory into its compressible
|
|
|
|
* form.
|
2015-12-28 19:09:51 -05:00
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
public class CompressionRegistry
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
public static List<CompressionHandler> compressionRegistry = new ArrayList<CompressionHandler>();
|
|
|
|
public static Map<ItemStack, Integer> thresholdMap = new HashMap<ItemStack, Integer>();
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static void registerHandler(CompressionHandler handler)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
compressionRegistry.add(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an item so that it only compresses while above this threshold
|
2015-12-30 15:34:40 -05:00
|
|
|
*
|
|
|
|
* @param stack
|
2016-01-02 17:56:37 -05:00
|
|
|
* item/block to be compressed
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param threshold
|
2016-01-02 17:56:37 -05:00
|
|
|
* amount that is to be compressed
|
2015-12-28 19:09:51 -05:00
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
public static void registerItemThreshold(ItemStack stack, int threshold)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
thresholdMap.put(stack, threshold);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static ItemStack compressInventory(ItemStack[] inv, World world)
|
|
|
|
{
|
|
|
|
for (CompressionHandler handler : compressionRegistry)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
ItemStack stack = handler.compressInventory(inv, world);
|
2015-12-30 15:34:40 -05:00
|
|
|
if (stack != null)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-03 10:52:14 -04:00
|
|
|
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] = ItemStack.copyItemStack(inventory[slot]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CompressionHandler handler : compressionRegistry)
|
|
|
|
{
|
|
|
|
ItemStack stack = handler.compressInventory(copyInventory, world);
|
|
|
|
if (stack != null)
|
|
|
|
{
|
|
|
|
for (int slot = 0; slot < itemHandler.getSlots(); slot++)
|
|
|
|
{
|
|
|
|
if (inventory[slot] != null && !ItemStack.areItemStacksEqual(inventory[slot], copyInventory[slot]))
|
|
|
|
{
|
|
|
|
itemHandler.extractItem(slot, inventory[slot].stackSize, false);
|
|
|
|
if (copyInventory[slot] != null)
|
|
|
|
{
|
|
|
|
itemHandler.insertItem(slot, copyInventory[slot], false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pair.of(Utils.insertStackIntoTile(stack, itemHandler), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pair.of(null, false);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static int getItemThreshold(ItemStack stack)
|
|
|
|
{
|
|
|
|
for (Map.Entry<ItemStack, Integer> entry : thresholdMap.entrySet())
|
|
|
|
{
|
|
|
|
if (areItemStacksEqual(entry.getKey(), stack))
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? compressedStack.getTagCompound() == null : stack.getTagCompound().equals(compressedStack.getTagCompound()));
|
|
|
|
}
|
|
|
|
}
|