diff --git a/dependencies.gradle b/dependencies.gradle index 6aa94d91..87672ae8 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -44,7 +44,17 @@ dependencies { compileOnly("thaumcraft:Thaumcraft:1.7.10-4.2.3.5:dev") {transitive = false} -// compileOnly('com.github.GTNewHorizons:Baubles-Expanded:2.2.2-GTNH:dev') // For testing Thaumcraft +// devOnlyNonPublishable('com.github.GTNewHorizons:Baubles-Expanded:2.2.2-GTNH:dev') {transitive = false} // For testing Thaumcraft + + // Replace Baubles with Baubles-Expanded for testing Thaumcraft +// project.getConfigurations().configureEach(c -> { +// final DependencySubstitutions ds = c.getResolutionStrategy() +// .getDependencySubstitution() +// ds.substitute(ds.module("com.github.GTNewHorizons:Baubles")) +// .using(ds.module("com.github.GTNewHorizons:Baubles-Expanded:2.2.2-GTNH")) +// .withClassifier("dev") +// .because("Baubles-Expanded replaces Baubles") +// }) afterEvaluate { dependencies { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/api/thaumcraft/EnhancedInfusionRecipe.java b/src/main/java/com/gtnewhorizon/gtnhlib/api/thaumcraft/EnhancedInfusionRecipe.java new file mode 100644 index 00000000..cc6e7a35 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/api/thaumcraft/EnhancedInfusionRecipe.java @@ -0,0 +1,53 @@ +package com.gtnewhorizon.gtnhlib.api.thaumcraft; + +import java.util.List; + +import net.minecraft.item.ItemStack; + +import com.github.bsideup.jabel.Desugar; + +import thaumcraft.api.aspects.AspectList; +import thaumcraft.api.crafting.InfusionRecipe; + +public class EnhancedInfusionRecipe extends InfusionRecipe { + + private final List replacements; + + // A guard value only returned when no replacement is found for an item + public static final Replacement NO_REPLACEMENT = new Replacement(null, null, true); + + /** + * Create a new EnhancedInfusionRecipe, capable of replacing items on the outer pedestals with other items rather + * than consuming them or leaving their containers behind. An example use case is Witching Gadgets' Primordial Armor + * recipes leaving the Inert Primordial Pearls on the outer pedestals in place of Primordial Pearls. + * + * @param research The required research for this infusion + * @param output The item created by this infusion + * @param inst The instability of this infusion + * @param aspects2 The required essentia for this infusion + * @param input The item required to be placed on the central pedestal + * @param recipe The items required to be placed on the outer pedestals + * @param replacements What items to replace consumed items with on the outer pedestals + */ + public EnhancedInfusionRecipe(String research, Object output, int inst, AspectList aspects2, ItemStack input, + ItemStack[] recipe, List replacements) { + super(research, output, inst, aspects2, input, recipe); + this.replacements = replacements; + } + + public List getReplacements() { + return this.replacements; + } + + /** + * A Replacement defines one type of ItemStack (e.g. all Iron Ingots) to replace with another ItemStack (e.g. + * Diamonds). Setting strict to false allows input to match with all metadata if input is given metadata of + * Short.MAX_VALUE. NBT data (such as enchantments) is ignored when comparing against input. + * + * @param input The input ItemStack to compare recipe ingredients against + * @param output The ItemStack to replace items that match the input + * @param strict Whether to use strict mode in OreDictionary.itemMatches while comparing recipe ingredients to input + */ + @Desugar + public record Replacement(ItemStack input, ItemStack output, boolean strict) {} +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibLateMixinLoader.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibLateMixinLoader.java new file mode 100644 index 00000000..e07daf6f --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibLateMixinLoader.java @@ -0,0 +1,23 @@ +package com.gtnewhorizon.gtnhlib.core; + +import java.util.List; +import java.util.Set; + +import com.gtnewhorizon.gtnhlib.mixins.Mixins; +import com.gtnewhorizon.gtnhmixins.ILateMixinLoader; +import com.gtnewhorizon.gtnhmixins.LateMixin; +import com.gtnewhorizon.gtnhmixins.builders.IMixins; + +@LateMixin +public class GTNHLibLateMixinLoader implements ILateMixinLoader { + + @Override + public String getMixinConfig() { + return "mixins.gtnhlib.late.json"; + } + + @Override + public List getMixins(Set loadedMods) { + return IMixins.getLateMixins(Mixins.class, loadedMods); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java index 478b04d9..d3195565 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java @@ -56,6 +56,11 @@ public enum Mixins implements IMixins { MULTI_RELEASE_JAR_FILTER(new MixinBuilder("Skip multi-release JAR entries in mod discovery") .addCommonMixins("fml.MixinJarDiscoverer").setPhase(Phase.EARLY).addExcludedMod(TargetMods.LWJGL3IFY)), PICK_BLOCK_TRAP(Side.CLIENT, "MixinMinecraft_PickBlockTrap"), + + ENHANCED_INFUSION_RECIPE(new MixinBuilder( + "Allow Thaumcraft Infusion Recipes to transform items instead of consuming them in an EnhancedInfusionRecipe") + .setPhase(Phase.LATE).addCommonMixins("MixinEnhancedInfusionRecipe") + .addRequiredMod(TargetMods.THAUMCRAFT)), // ; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/TargetMods.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/TargetMods.java index a1dedde5..4d6fbcf2 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/TargetMods.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/TargetMods.java @@ -7,7 +7,8 @@ import com.gtnewhorizon.gtnhmixins.builders.TargetModBuilder; public enum TargetMods implements ITargetMod { - LWJGL3IFY("me.eigenraven.lwjgl3ify.core.Lwjgl3ifyCoremod"); + LWJGL3IFY("me.eigenraven.lwjgl3ify.core.Lwjgl3ifyCoremod"), + THAUMCRAFT("", "Thaumcraft"); private final TargetModBuilder builder; @@ -15,6 +16,10 @@ public enum TargetMods implements ITargetMod { this.builder = new TargetModBuilder().setCoreModClass(coreModClass); } + TargetMods(String coreModClass, String modId) { + this.builder = new TargetModBuilder().setCoreModClass(coreModClass).setModId(modId); + } + @Nonnull @Override public TargetModBuilder getBuilder() { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/late/MixinEnhancedInfusionRecipe.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/late/MixinEnhancedInfusionRecipe.java new file mode 100644 index 00000000..ffd0ce3a --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/late/MixinEnhancedInfusionRecipe.java @@ -0,0 +1,139 @@ +package com.gtnewhorizon.gtnhlib.mixins.late; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.oredict.OreDictionary; + +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.gtnewhorizon.gtnhlib.api.thaumcraft.EnhancedInfusionRecipe; +import com.llamalad7.mixinextras.sugar.Local; + +import thaumcraft.api.TileThaumcraft; +import thaumcraft.api.crafting.InfusionRecipe; +import thaumcraft.common.tiles.TileInfusionMatrix; +import thaumcraft.common.tiles.TilePedestal; + +@Mixin(value = TileInfusionMatrix.class, remap = false) +public abstract class MixinEnhancedInfusionRecipe extends TileThaumcraft { + + @Shadow + private ArrayList recipeIngredients; + @Unique + private List gTNHLib$replacements = new ArrayList<>(); + + // Save the replacements for use in itemReplacement + @Inject( + method = "craftingStart", + at = @At( + value = "FIELD", + target = "Lthaumcraft/common/tiles/TileInfusionMatrix;recipeType:I", + ordinal = 0, + opcode = Opcodes.PUTFIELD)) + public void setRecipe(CallbackInfo ci, @Local InfusionRecipe recipe) { + if (recipe instanceof EnhancedInfusionRecipe r) { + this.gTNHLib$replacements = r.getReplacements(); + } + } + + // If one exists, replace the item on the pedestal with its replacement and skip the normal consumption logic + @Inject( + method = "craftCycle", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Lthaumcraft/common/tiles/TilePedestal;getStackInSlot(I)Lnet/minecraft/item/ItemStack;", + ordinal = 4)) + public void itemReplacement(CallbackInfo ci, @Local TileEntity te, @Local int slot) { + if (this.gTNHLib$replacements.isEmpty()) { + return; + } + TilePedestal pedestal = (TilePedestal) te; + ItemStack stack = pedestal.getStackInSlot(0); + EnhancedInfusionRecipe.Replacement replacement = gTNHLib$getReplacement(stack); + if (replacement == EnhancedInfusionRecipe.NO_REPLACEMENT) return; + this.recipeIngredients.remove(slot); + if (!OreDictionary.itemMatches(stack, replacement.output(), replacement.strict())) { + pedestal.setInventorySlotContents(0, replacement.output()); + } + ci.cancel(); + } + + @Inject(method = "craftingFinish", at = @At(value = "RETURN")) + public void clearRecipe(CallbackInfo ci) { + this.gTNHLib$replacements.clear(); + } + + @Inject(method = "writeToNBT", at = @At(value = "RETURN")) + public void writeToNBT(NBTTagCompound nbtCompound, CallbackInfo ci) { + if (this.gTNHLib$replacements == null || this.gTNHLib$replacements.isEmpty()) { + return; + } + NBTTagList nbttaglist = new NBTTagList(); + for (EnhancedInfusionRecipe.Replacement replacement : this.gTNHLib$replacements) { + if (replacement.input() == null) continue; + NBTTagCompound replacements = new NBTTagCompound(); + + replacements.setTag("input", replacement.input().writeToNBT(new NBTTagCompound())); + + ItemStack output = replacement.output(); + if (output != null) { + replacements.setTag("output", output.writeToNBT(new NBTTagCompound())); + } else { + replacements.setBoolean("nullOutput", true); + } + + replacements.setBoolean("strict", replacement.strict()); + nbttaglist.appendTag(replacements); + } + nbtCompound.setTag("replacements", nbttaglist); + } + + @Inject(method = "readFromNBT", at = @At(value = "RETURN")) + public void readFromNBT(NBTTagCompound nbtCompound, CallbackInfo ci) { + if (!nbtCompound.hasKey("replacements")) { + return; + } + gTNHLib$replacements.clear(); + NBTTagList nbttaglist = nbtCompound.getTagList("replacements", 10); // the list of replacements + for (int i = 0; i < nbttaglist.tagCount(); ++i) { // for every replacement entry in replacements + NBTTagCompound replacementEntry = nbttaglist.getCompoundTagAt(i); // get the replacament entry + ItemStack input = ItemStack.loadItemStackFromNBT(replacementEntry.getCompoundTag("input")); + ItemStack output = null; + if (!replacementEntry.getBoolean("nullOutput")) { + output = ItemStack.loadItemStackFromNBT(replacementEntry.getCompoundTag("output")); + } + + boolean strict = replacementEntry.getBoolean("strict"); + + this.gTNHLib$replacements.add(new EnhancedInfusionRecipe.Replacement(input, output, strict)); + } + } + + /** + * @param key The ItemStack to find a Replacement for. + * @return the Replacement for the given ItemStack. EnhancedInfusionRecipe.NO_REPLACEMENT if no replacement was + * found. + */ + @Unique + public EnhancedInfusionRecipe.Replacement gTNHLib$getReplacement(ItemStack key) { + for (EnhancedInfusionRecipe.Replacement replacement : this.gTNHLib$replacements) { + if (OreDictionary.itemMatches(replacement.input(), key, replacement.strict())) { + return replacement; + } + } + return EnhancedInfusionRecipe.NO_REPLACEMENT; + } + +} diff --git a/src/main/resources/mixins.gtnhlib.late.json b/src/main/resources/mixins.gtnhlib.late.json new file mode 100644 index 00000000..439c38ae --- /dev/null +++ b/src/main/resources/mixins.gtnhlib.late.json @@ -0,0 +1,12 @@ +{ + "required": true, + "minVersion": "0.8.3-GTNH", + "package": "com.gtnewhorizon.gtnhlib.mixins.late", + "refmap": "mixins.gtnhlib.refmap.json", + "target": "@env(DEFAULT)", + "mixinextras": { + "minVersion": "0.5.0" + }, + "compatibilityLevel": "JAVA_8" +} +