package com.yolp900.itsjustacharm.common.crafting; import baubles.api.BaubleType; 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; import java.util.ArrayList; import java.util.List; public class RingAttachmentRecipe implements IRecipe { @Override public boolean matches(@Nonnull InventoryCrafting inv, @Nonnull World worldIn) { List<ItemStack> ingredients = new ArrayList<>(); for (int i = 0; i < inv.getSizeInventory(); i++) { ItemStack stack = inv.getStackInSlot(i); if (stack != null) { if (stack.getItem() instanceof IBauble && ((IBauble)stack.getItem()).getBaubleType(stack) == BaubleType.RING) { ingredients.add(stack); } } } if (ingredients.size() != 2) return false; ItemStack ringHolder; ItemStack ring; if (isItemStackHolder(ingredients.get(0)) && !isItemStackHolder(ingredients.get(1))) { ringHolder = ingredients.get(0); ring = ingredients.get(1); } else if (isItemStackHolder(ingredients.get(1)) && !isItemStackHolder(ingredients.get(0))) { ringHolder = ingredients.get(1); ring = ingredients.get(0); } else return false; if (ring == null || ringHolder == null) return false; boolean canAttachRing = false; List<ItemStack> attachedRings = getAttachedRings(ringHolder); if (attachedRings.get(0) == null) { canAttachRing = true; } else if (isAdvancedHolder(ringHolder) && attachedRings.get(1) == null) { if (!ItemStack.areItemStacksEqual(ring, attachedRings.get(0))) { canAttachRing = true; } } else return false; return !ItemStack.areItemStacksEqual(ring, ringHolder) && canAttachRing; } @Nullable @Override public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) { List<ItemStack> ingredients = new ArrayList<>(); for (int i = 0; i < inv.getSizeInventory(); i++) { ItemStack stack = inv.getStackInSlot(i); if (stack != null) { if (stack.getItem() instanceof IBauble && ((IBauble) stack.getItem()).getBaubleType(stack) == BaubleType.RING) { ingredients.add(stack); } } } if (ingredients.size() != 2) return null; ItemStack ringHolder; ItemStack ring; if (isItemStackHolder(ingredients.get(0)) && !isItemStackHolder(ingredients.get(1))) { ringHolder = ingredients.get(0); ring = ingredients.get(1); } else if (isItemStackHolder(ingredients.get(1)) && !isItemStackHolder(ingredients.get(0))) { ringHolder = ingredients.get(1); ring = ingredients.get(0); } else return null; if (ring == null || ringHolder == null) return null; ItemStack output; boolean advanced; boolean firstStored; if (ringHolder.getItem() == ModItems.BaubleRingHolder) { output = ring.copy(); advanced = ringHolder.getItemDamage() == ItemBaubleRingHolder.EnumType.Advanced.ordinal(); firstStored = true; } else { output = ringHolder.copy(); advanced = NBTHelper.getBoolean(output, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER); firstStored = false; } NBTHelper.setBoolean(output, IJCConstants.NBT.BAUBLE_HOLDER, true); NBTHelper.setBoolean(output, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER, advanced); if (!firstStored) { if (NBTHelper.getItemStack(output, IJCConstants.NBT.BAUBLE_ATTACHED(0)) == null) { NBTHelper.setItemStack(output, IJCConstants.NBT.BAUBLE_ATTACHED(0), ring.copy()); } else if (advanced && NBTHelper.getItemStack(output, IJCConstants.NBT.BAUBLE_ATTACHED(1)) == null) { NBTHelper.setItemStack(output, IJCConstants.NBT.BAUBLE_ATTACHED(1), ring.copy()); } else { return null; } } return output; } @Override public int getRecipeSize() { return 10; } @Nullable @Override public ItemStack getRecipeOutput() { return null; } @Override @Nonnull public ItemStack[] getRemainingItems(@Nonnull InventoryCrafting inv) { return ForgeHooks.defaultRecipeGetRemainingItems(inv); } private boolean isItemStackHolder(@Nonnull ItemStack stack) { return stack.getItem() == ModItems.BaubleRingHolder || NBTHelper.getBoolean(stack, IJCConstants.NBT.BAUBLE_HOLDER); } private boolean isAdvancedHolder(@Nonnull ItemStack stack) { return (stack.getItem() == ModItems.BaubleRingHolder && stack.getItemDamage() == ItemBaubleRingHolder.EnumType.Advanced.ordinal()) || NBTHelper.getBoolean(stack, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER); } private ItemStack getAttachedBauble(ItemStack stack, int index) { return NBTHelper.getItemStack(stack, IJCConstants.NBT.BAUBLE_ATTACHED(index)); } private List<ItemStack> getAttachedRings(ItemStack stack) { List<ItemStack> ret = new ArrayList<>(); ret.add(getAttachedBauble(stack, 0)); if (isAdvancedHolder(stack)) { ret.add(getAttachedBauble(stack, 1)); } return ret; } }