mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Add an enhanced infusion recipe that supports replacements (#270)
Co-authored-by: koolkrafter5 <[email protected]>
This commit is contained in:
1 parent
8e287c88d3
commit
da534c32f2
7 files changed
+249
-2
No files matched your search
@@ -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<Replacement> 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<Replacement> replacements) {
|
||||
super(research, output, inst, aspects2, input, recipe);
|
||||
this.replacements = replacements;
|
||||
}
|
||||
|
||||
public List<Replacement> 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) {}
|
||||
}
|
||||
@@ -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<String> getMixins(Set<String> loadedMods) {
|
||||
return IMixins.getLateMixins(Mixins.class, loadedMods);
|
||||
}
|
||||
}
|
||||
@@ -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)),
|
||||
//
|
||||
;
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<ItemStack> recipeIngredients;
|
||||
@Unique
|
||||
private List<EnhancedInfusionRecipe.Replacement> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user