package com.yolp900.itsjustacharm.common.crafting;
import baubles.api.IBauble;
import com.yolp900.itsjustacharm.api.IJCConstants;
import com.yolp900.itsjustacharm.common.items.ItemBaubleRingHolder;
import com.yolp900.itsjustacharm.common.items.ModItems;
import com.yolp900.itsjustacharm.util.NBTHelper;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class RingRemovalRecipe implements IRecipe {
@Override
public boolean matches(@Nonnull InventoryCrafting inv, @Nonnull World worldIn) {
ItemStack ring = null;
for(int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack != null) {
if (ring != null) {
return false;
}
if (stack.getItem() instanceof IBauble) {
ring = stack.copy();
} else {
return false;
}
}
}
return ring != null && NBTHelper.getBoolean(ring, IJCConstants.NBT.BAUBLE_HOLDER);
}
@Nullable
@Override
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
ItemStack ring = null;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack != null)
ring = stack.copy();
}
if (ring == null || !(ring.getItem() instanceof IBauble) || !NBTHelper.getBoolean(ring, IJCConstants.NBT.BAUBLE_HOLDER)) {
return null;
}
ItemStack output;
ItemStack ring0 = NBTHelper.getItemStack(ring, IJCConstants.NBT.BAUBLE_ATTACHED(0));
boolean advanced = NBTHelper.getBoolean(ring, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER);
if (ring0 == null) {
int meta = 0;
if (advanced) {
meta = 1;
}
output = new ItemStack(ModItems.BaubleRingHolder, 1, meta);
} else {
if (advanced) {
ItemStack ring1 = NBTHelper.getItemStack(ring, IJCConstants.NBT.BAUBLE_ATTACHED(1));
if (ring1 == null) {
output = ring0.copy();
} else {
output = ring1.copy();
}
} else {
output = ring0.copy();
}
}
return output;
}
@Override
public int getRecipeSize() {
return 10;
}
@Nullable
@Override
public ItemStack getRecipeOutput() {
return null;
}
@Override
@Nonnull
public ItemStack[] getRemainingItems(@Nonnull InventoryCrafting inv) {
ItemStack ring = null;
for(int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack != null) {
if (ring != null) {
return ForgeHooks.defaultRecipeGetRemainingItems(inv);
}
if (stack.getItem() instanceof IBauble) {
ring = stack.copy();
} else {
return ForgeHooks.defaultRecipeGetRemainingItems(inv);
}
}
}
if (ring != null && NBTHelper.getBoolean(ring, IJCConstants.NBT.BAUBLE_HOLDER)) {
ItemStack output = getCraftingResult(inv);
if (output == null) {
return ForgeHooks.defaultRecipeGetRemainingItems(inv);
}
if (output.getItem() instanceof ItemBaubleRingHolder) {
NBTHelper.getTag(ring).removeTag(IJCConstants.NBT.BAUBLE_HOLDER);
if (NBTHelper.getBoolean(ring, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER)) {
NBTHelper.getTag(ring).removeTag(IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER);
}
} else {
boolean advanced = NBTHelper.getBoolean(ring, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER);
ItemStack ring0 = NBTHelper.getItemStack(ring, IJCConstants.NBT.BAUBLE_ATTACHED(0));
if (ring0 == null) {
return ForgeHooks.defaultRecipeGetRemainingItems(inv);
}
if (advanced) {
ItemStack ring1 = NBTHelper.getItemStack(ring, IJCConstants.NBT.BAUBLE_ATTACHED(1));
if (ring1 == null) {
NBTHelper.getTag(ring).removeTag(IJCConstants.NBT.BAUBLE_ATTACHED(0));
} else {
NBTHelper.getTag(ring).removeTag(IJCConstants.NBT.BAUBLE_ATTACHED(1));
}
} else {
NBTHelper.getTag(ring).removeTag(IJCConstants.NBT.BAUBLE_ATTACHED(0));
}
}
inv.clear();
return new ItemStack[] {ring};
} else {
return ForgeHooks.defaultRecipeGetRemainingItems(inv);
}
}
}