|
| 1 | +package com.github.gtexpert.core.integration.deda.recipemaps; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import net.minecraft.item.ItemStack; |
| 6 | +import net.minecraft.nbt.NBTTagCompound; |
| 7 | +import net.minecraftforge.common.util.Constants; |
| 8 | + |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | +import org.jetbrains.annotations.Nullable; |
| 11 | + |
| 12 | +import com.brandon3055.brandonscore.utils.ItemNBTHelper; |
| 13 | +import com.brandon3055.draconicevolution.api.fusioncrafting.IFusionRecipe; |
| 14 | +import com.brandon3055.draconicevolution.api.fusioncrafting.SimpleFusionRecipe; |
| 15 | +import com.brandon3055.draconicevolution.api.itemupgrade.FusionUpgradeRecipe; |
| 16 | +import com.brandon3055.draconicevolution.api.itemupgrade.IUpgradableItem; |
| 17 | +import com.brandon3055.draconicevolution.api.itemupgrade.UpgradeHelper; |
| 18 | +import com.brandon3055.draconicevolution.items.ToolUpgrade; |
| 19 | + |
| 20 | +import gregtech.api.capability.FeCompat; |
| 21 | +import gregtech.api.capability.GregtechCapabilities; |
| 22 | +import gregtech.api.capability.IElectricItem; |
| 23 | +import gregtech.api.recipes.Recipe; |
| 24 | + |
| 25 | +import com.github.gtexpert.core.api.util.GTELog; |
| 26 | + |
| 27 | +import cofh.redstoneflux.api.IEnergyContainerItem; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility methods shared between TierUp and Upgrade recipe maps. |
| 31 | + */ |
| 32 | +public final class DraconicRecipeUtils { |
| 33 | + |
| 34 | + private DraconicRecipeUtils() {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Applies default upgrade tags to upgradable items. |
| 38 | + * This ensures consistent NBT state for recipe matching. |
| 39 | + */ |
| 40 | + public static void applyDefaultUpgradeTag(List<ItemStack> inputs) { |
| 41 | + for (ItemStack input : inputs) { |
| 42 | + if (!(input.getItem() instanceof IUpgradableItem item)) continue; |
| 43 | + for (String upgradeName : ToolUpgrade.NAME_TO_ID.keySet()) { |
| 44 | + if (!item.getValidUpgrades(input).contains(upgradeName)) continue; |
| 45 | + NBTTagCompound upgradeTag = input.getOrCreateSubCompound(UpgradeHelper.UPGRADE_TAG); |
| 46 | + if (upgradeTag.hasKey(upgradeName, Constants.NBT.TAG_BYTE)) continue; |
| 47 | + upgradeTag.setByte(upgradeName, (byte) 0); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Sets up the output item for a TierUp or Upgrade recipe. |
| 54 | + * Handles NBT copying and energy conversion. |
| 55 | + */ |
| 56 | + @Nullable |
| 57 | + public static Recipe setupOutput(Recipe gtRecipe, List<ItemStack> inputs, IFusionRecipe fusionRecipe) { |
| 58 | + if (fusionRecipe == null) { |
| 59 | + GTELog.logger.warn("Recipe found, but property not found"); |
| 60 | + GTELog.logger.warn("Recipe: " + gtRecipe); |
| 61 | + return null; |
| 62 | + } |
| 63 | + ItemStack catalyst = findCatalyst(inputs, fusionRecipe); |
| 64 | + if (catalyst.isEmpty()) { |
| 65 | + GTELog.logger.warn("Recipe found, but actual catalyst not found in the GT recipe"); |
| 66 | + GTELog.logger.warn("Recipe: " + gtRecipe); |
| 67 | + GTELog.logger.warn("Expected catalyst: " + fusionRecipe.getRecipeCatalyst()); |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + ItemStack outputStack = fusionRecipe.getRecipeOutput(catalyst); |
| 72 | + |
| 73 | + // convert GTEU to FE |
| 74 | + IElectricItem inputElectricItem = catalyst.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null); |
| 75 | + if (inputElectricItem != null) { |
| 76 | + long euCharge = inputElectricItem.getCharge(); |
| 77 | + int feCharge = (int) Math.min(euCharge * FeCompat.ratio(false), Integer.MAX_VALUE); |
| 78 | + if (outputStack.getItem() instanceof IEnergyContainerItem outputEnergyItem) { |
| 79 | + ItemNBTHelper.setInteger(outputStack, "Energy", |
| 80 | + Math.min(feCharge, outputEnergyItem.getMaxEnergyStored(outputStack))); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Recipe retRecipe = gtRecipe.copy(); |
| 85 | + retRecipe.getOutputs().clear(); // This assumes there's only 1 output |
| 86 | + retRecipe.getOutputs().add(outputStack); |
| 87 | + return retRecipe; |
| 88 | + } |
| 89 | + |
| 90 | + @NotNull |
| 91 | + public static ItemStack findCatalyst(List<ItemStack> inputs, IFusionRecipe fusionRecipe) { |
| 92 | + ItemStack expectedCatalyst = getCatalyst(fusionRecipe); |
| 93 | + if (expectedCatalyst == null || expectedCatalyst.isEmpty()) { |
| 94 | + return ItemStack.EMPTY; |
| 95 | + } |
| 96 | + for (ItemStack input : inputs) { |
| 97 | + if (expectedCatalyst.getItem() == input.getItem() && |
| 98 | + expectedCatalyst.getItemDamage() == input.getItemDamage() && fusionRecipe.isRecipeCatalyst(input)) { |
| 99 | + return input; |
| 100 | + } |
| 101 | + } |
| 102 | + return ItemStack.EMPTY; |
| 103 | + } |
| 104 | + |
| 105 | + @Nullable |
| 106 | + public static ItemStack getCatalyst(IFusionRecipe fusionRecipe) { |
| 107 | + if (fusionRecipe instanceof SimpleFusionRecipe) { |
| 108 | + return fusionRecipe.getRecipeCatalyst(); |
| 109 | + } else if (fusionRecipe instanceof FusionUpgradeRecipe) { |
| 110 | + List<Object> ingredients = ((FusionUpgradeRecipe) fusionRecipe).getRecipeIngredients(); |
| 111 | + if (ingredients.isEmpty() || !(ingredients.get(0) instanceof ItemStack)) { |
| 112 | + GTELog.logger.warn("Unknown ingredient: " + (ingredients.isEmpty() ? "empty" : ingredients.get(0))); |
| 113 | + GTELog.logger.warn("Recipe: " + fusionRecipe); |
| 114 | + return null; |
| 115 | + } |
| 116 | + return (ItemStack) ingredients.get(0); |
| 117 | + } else { |
| 118 | + throw new RuntimeException("Unknown type of IFusionRecipe: " + fusionRecipe.getClass().getName()); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments