BloodMagic/src/main/java/WayofTime/bloodmagic/compress/AdvancedCompressionHandler.java

141 lines
4.8 KiB
Java
Raw Normal View History

2015-12-28 19:09:51 -05:00
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.apibutnotreally.compress.CompressionHandler;
import WayofTime.bloodmagic.apibutnotreally.compress.CompressionRegistry;
2015-12-28 19:09:51 -05:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
2017-08-15 21:30:48 -07:00
public class AdvancedCompressionHandler extends CompressionHandler {
2015-12-28 19:09:51 -05:00
@Override
2017-08-15 21:30:48 -07:00
public ItemStack compressInventory(ItemStack[] inv, World world) {
2015-12-28 19:09:51 -05:00
return test(inv, true, world);
}
2017-08-15 21:30:48 -07:00
public ItemStack test(ItemStack[] inv, boolean doDrain, World world) {
for (ItemStack invStack : inv) {
if (invStack.isEmpty()) {
2015-12-28 19:09:51 -05:00
continue;
}
2017-08-15 21:30:48 -07:00
for (int i = 2; i <= 3; i++) {
2016-12-12 19:56:36 -08:00
ItemStack stack = getRecipe(invStack, world, i);
2017-08-15 21:30:48 -07:00
if (!stack.isEmpty()) {
2015-12-28 19:09:51 -05:00
int threshold = CompressionRegistry.getItemThreshold(invStack);
int needed = i * i;
int neededLeft = iterateThroughInventory(invStack, threshold + invStack.getMaxStackSize() - needed, inv, needed, false);
2017-08-15 21:30:48 -07:00
if (neededLeft <= 0) {
2015-12-28 19:09:51 -05:00
iterateThroughInventory(invStack, 0, inv, needed, true);
2016-12-12 19:56:36 -08:00
return stack;
2015-12-28 19:09:51 -05:00
}
}
}
}
2016-12-12 19:56:36 -08:00
return ItemStack.EMPTY;
2015-12-28 19:09:51 -05:00
}
2017-08-15 21:30:48 -07:00
public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) {
2015-12-28 19:09:51 -05:00
int i = -1;
2017-08-15 21:30:48 -07:00
for (ItemStack invStack : inv) {
2015-12-28 19:09:51 -05:00
i++;
2017-08-15 21:30:48 -07:00
if (invStack.isEmpty()) {
2015-12-28 19:09:51 -05:00
continue;
}
2017-08-15 21:30:48 -07:00
if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) {
2016-12-12 19:56:36 -08:00
int stackSize = invStack.getCount();
2015-12-28 19:09:51 -05:00
int used = 0;
2017-08-15 21:30:48 -07:00
if (kept > 0) {
2015-12-28 19:09:51 -05:00
int remainingFromStack = Math.max(stackSize - kept, 0);
used += stackSize - remainingFromStack;
}
kept -= used;
2017-08-15 21:30:48 -07:00
if (kept <= 0 && needed > 0) {
2015-12-28 19:09:51 -05:00
int remainingFromStack = Math.max(stackSize - used - needed, 0);
2017-08-15 21:30:48 -07:00
if (doDrain) {
2016-12-12 19:56:36 -08:00
invStack.setCount(remainingFromStack + used);
2017-08-15 21:30:48 -07:00
if (invStack.isEmpty()) {
2016-12-12 19:56:36 -08:00
inv[i] = ItemStack.EMPTY;
2015-12-28 19:09:51 -05:00
}
}
needed -= (stackSize - used - remainingFromStack);
}
2017-08-15 21:30:48 -07:00
if (needed <= 0) {
2015-12-28 19:09:51 -05:00
return 0;
}
}
}
return needed;
}
2017-08-15 21:30:48 -07:00
public static boolean isResultStackReversible(ItemStack stack, int gridSize, World world) {
if (stack.isEmpty()) {
2015-12-28 19:09:51 -05:00
return false;
}
2017-08-15 21:30:48 -07:00
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
2015-12-28 19:09:51 -05:00
return false;
}
}, 2, 2);
inventory.setInventorySlotContents(0, stack);
ItemStack returnStack = StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world);
2017-08-15 21:30:48 -07:00
if (returnStack.isEmpty()) {
2015-12-28 19:09:51 -05:00
return false;
}
2016-12-12 19:56:36 -08:00
ItemStack compressedStack = ItemStack.EMPTY;
2017-08-15 21:30:48 -07:00
switch (gridSize) {
case 2:
compressedStack = get22Recipe(returnStack, world);
break;
case 3:
compressedStack = get33Recipe(returnStack, world);
break;
2015-12-28 19:09:51 -05:00
}
2016-12-12 19:56:36 -08:00
return !compressedStack.isEmpty() && CompressionRegistry.areItemStacksEqual(stack, compressedStack);
2015-12-28 19:09:51 -05:00
}
2017-08-15 21:30:48 -07:00
public static ItemStack getRecipe(ItemStack stack, World world, int gridSize) {
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
2015-12-28 19:09:51 -05:00
return false;
}
}, gridSize, gridSize);
2017-08-15 21:30:48 -07:00
for (int i = 0; i < inventory.getSizeInventory(); i++) {
2015-12-28 19:09:51 -05:00
inventory.setInventorySlotContents(i, stack);
}
return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world);
}
2017-08-15 21:30:48 -07:00
public static boolean has22Recipe(ItemStack stack, World world) {
2016-12-12 19:56:36 -08:00
return !get22Recipe(stack, world).isEmpty();
2015-12-28 19:09:51 -05:00
}
2017-08-15 21:30:48 -07:00
public static ItemStack get22Recipe(ItemStack stack, World world) {
2015-12-28 19:09:51 -05:00
return getRecipe(stack, world, 2);
}
2017-08-15 21:30:48 -07:00
public static boolean has33Recipe(ItemStack stack, World world) {
2016-12-12 19:56:36 -08:00
return !get33Recipe(stack, world).isEmpty();
2015-12-28 19:09:51 -05:00
}
2017-08-15 21:30:48 -07:00
public static ItemStack get33Recipe(ItemStack stack, World world) {
2015-12-28 19:09:51 -05:00
return getRecipe(stack, world, 3);
}
}