Implement IE recipe handler

This commit is contained in:
LLytho 2022-06-21 17:01:29 +02:00
parent e746cf95f4
commit 804cd45128
18 changed files with 196 additions and 51 deletions

View file

@ -19,9 +19,13 @@ public class AlmostUnified {
return RUNTIME;
}
public static boolean runtimeInitialized() {
return RUNTIME != null;
}
static void initializeRuntime() {
RecipeHandlerFactory factory = new RecipeHandlerFactory();
// factory.registerForMod("immersiveengineering", new IERecipeTransformerFactory());
RUNTIME = new AlmostUnifiedRuntime(factory);
AlmostUnifiedPlatform.INSTANCE.bindRecipeHandlers(factory);
RUNTIME = AlmostUnifiedPlatform.INSTANCE.createRuntime(factory);
}
}

View file

@ -1,5 +1,7 @@
package com.almostreliable.unified;
import com.almostreliable.unified.handler.RecipeHandlerFactory;
import java.nio.file.Path;
public interface AlmostUnifiedPlatform {
@ -29,4 +31,8 @@ public interface AlmostUnifiedPlatform {
boolean isDevelopmentEnvironment();
Path getConfigPath();
void bindRecipeHandlers(RecipeHandlerFactory factory);
AlmostUnifiedRuntime createRuntime(RecipeHandlerFactory factory);
}

View file

@ -10,16 +10,14 @@ import net.minecraft.tags.TagManager;
import net.minecraft.world.item.Item;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class AlmostUnifiedRuntime {
public abstract class AlmostUnifiedRuntime {
protected final ModConfig config;
protected final RecipeHandlerFactory recipeHandlerFactory;
@Nullable protected TagManager tagManager;
protected List<String> modPriorities = new ArrayList<>();
public AlmostUnifiedRuntime(RecipeHandlerFactory recipeHandlerFactory) {
this.recipeHandlerFactory = recipeHandlerFactory;
@ -28,7 +26,9 @@ public class AlmostUnifiedRuntime {
public void run(Map<ResourceLocation, JsonElement> recipes) {
config.load();
ReplacementMap replacementMap = createContext(config.getAllowedTags(), config.getModPriorities());
modPriorities = config.getModPriorities();
onRun();
ReplacementMap replacementMap = createContext(config.getAllowedTags(), modPriorities);
transformRecipes(recipes, replacementMap);
}
@ -60,7 +60,6 @@ public class AlmostUnifiedRuntime {
}
RecipeContextImpl ctx = new RecipeContextImpl(recipeType, id, json, replacementMap);
// for (var entry : json.entrySet()) {
try {
RecipeHandler recipeHandler = recipeHandlerFactory.create(ctx);
@ -71,10 +70,12 @@ public class AlmostUnifiedRuntime {
JsonObject copy = json.deepCopy();
recipeHandler.transformRecipe(copy, ctx);
if (!json.equals(copy)) {
AlmostUnified.LOG.info("Transformed recipe '{}' for type '{}' ========> {}",
id,
recipeType,
copy);
if (ctx.getType().getNamespace().equals("immersiveengineering")) {
AlmostUnified.LOG.info("Transformed recipe '{}' for type '{}' ========> {}",
id,
recipeType,
copy);
}
return copy;
}
@ -124,4 +125,6 @@ public class AlmostUnifiedRuntime {
return new ReplacementMap(tagMap, itemToTagMapping, modPriorities);
}
protected abstract void onRun();
}

View file

@ -0,0 +1,5 @@
package com.almostreliable.unified;
public class ModConstants {
public static final String IE = "immersiveengineering";
}

View file

@ -10,12 +10,18 @@ import java.util.Map;
public class RecipeHandlerFactory {
private final Map<ResourceLocation, RecipeHandler> transformersByType = new HashMap<>();
private final Map<String, RecipeHandler> transformersByModId = new HashMap<>();
@Nullable
public RecipeHandler create(RecipeContext context) {
RecipeHandler transformer = transformersByType.get(context.getType());
if (transformer != null) {
return transformer;
RecipeHandler byType = transformersByType.get(context.getType());
if (byType != null) {
return byType;
}
RecipeHandler byMod = transformersByModId.get(context.getModId());
if (byMod != null) {
return byMod;
}
if (context.hasProperty(ShapedRecipeKeyHandler.PATTERN_PROPERTY) &&
@ -30,7 +36,15 @@ public class RecipeHandlerFactory {
return null;
}
public void register(ResourceLocation type, RecipeHandler transformer) {
public void registerForType(String type, RecipeHandler transformer) {
transformersByType.put(new ResourceLocation(type), transformer);
}
public void registerForType(ResourceLocation type, RecipeHandler transformer) {
transformersByType.put(type, transformer);
}
public void registerForMod(String mod, RecipeHandler transformer) {
transformersByModId.put(mod, transformer);
}
}

View file

@ -1,14 +0,0 @@
package com.almostreliable.unified.handler.ie;
import com.almostreliable.unified.api.RecipeContext;
import com.almostreliable.unified.api.RecipeHandler;
import com.google.gson.JsonObject;
public class IEBaseRecipeHandler implements RecipeHandler {
@Override
public void transformRecipe(JsonObject json, RecipeContext context) {
}
}

View file

@ -1,6 +0,0 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.almostreliable.unified.handler.ie;
import net.minecraft.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;

View file

@ -1,6 +1,6 @@
package com.almostreliable.unified.impl;
package com.almostreliable.unified;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.handler.RecipeHandlerFactory;
import net.fabricmc.loader.api.FabricLoader;
import java.nio.file.Path;
@ -26,4 +26,14 @@ public class AlmostUnifiedPlatformFabric implements AlmostUnifiedPlatform {
public Path getConfigPath() {
return FabricLoader.getInstance().getConfigDir();
}
@Override
public void bindRecipeHandlers(RecipeHandlerFactory factory) {
}
@Override
public AlmostUnifiedRuntime createRuntime(RecipeHandlerFactory factory) {
return new AlmostUnifiedRuntimeFabric(factory);
}
}

View file

@ -0,0 +1,15 @@
package com.almostreliable.unified;
import com.almostreliable.unified.handler.RecipeHandlerFactory;
public class AlmostUnifiedRuntimeFabric extends AlmostUnifiedRuntime{
AlmostUnifiedRuntimeFabric(RecipeHandlerFactory recipeHandlerFactory) {
super(recipeHandlerFactory);
}
@Override
protected void onRun() {
}
}

View file

@ -1,6 +0,0 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.almostreliable.unified.impl;
import net.minecraft.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;

View file

@ -1 +1 @@
com.almostreliable.unified.impl.AlmostUnifiedPlatformFabric
com.almostreliable.unified.AlmostUnifiedPlatformFabric

View file

@ -68,7 +68,10 @@ dependencies {
compileOnly( fg.deobf("mezz.jei:jei-${minecraftVersion}:${jeiVersion}:api"))
runtimeOnly( fg.deobf("mezz.jei:jei-${minecraftVersion}:${jeiVersion}"))
runtimeOnly(fg.deobf("curse.maven:IE-231951:3755665"))
// required in dev for mixins
implementation(fg.deobf("curse.maven:IE-231951:3755665"))
// used for testing only
runtimeOnly(fg.deobf("curse.maven:JAOPCA-266936:3802370"))
runtimeOnly(fg.deobf("curse.maven:cofh-69162:3803484"))
runtimeOnly(fg.deobf("curse.maven:thermalfoundation-222880:3803495"))

View file

@ -1,6 +1,7 @@
package com.almostreliable.unified.impl;
package com.almostreliable.unified;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.compat.ie.IERecipeHandler;
import com.almostreliable.unified.handler.RecipeHandlerFactory;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.loading.FMLPaths;
@ -28,4 +29,14 @@ public class AlmostUnifiedPlatformForge implements AlmostUnifiedPlatform {
public Path getConfigPath() {
return FMLPaths.CONFIGDIR.get();
}
@Override
public void bindRecipeHandlers(RecipeHandlerFactory factory) {
factory.registerForMod(ModConstants.IE, new IERecipeHandler());
}
@Override
public AlmostUnifiedRuntime createRuntime(RecipeHandlerFactory factory) {
return new AlmostUnifiedRuntimeForge(factory);
}
}

View file

@ -0,0 +1,18 @@
package com.almostreliable.unified;
import com.almostreliable.unified.compat.ie.IEModPriorityOverride;
import com.almostreliable.unified.handler.RecipeHandlerFactory;
public class AlmostUnifiedRuntimeForge extends AlmostUnifiedRuntime {
AlmostUnifiedRuntimeForge(RecipeHandlerFactory recipeHandlerFactory) {
super(recipeHandlerFactory);
}
@Override
protected void onRun() {
if(AlmostUnifiedPlatform.INSTANCE.isModLoaded(ModConstants.IE)) {
IEModPriorityOverride.overrideModPriorities(modPriorities).run();
}
}
}

View file

@ -0,0 +1,15 @@
package com.almostreliable.unified.compat.ie;
import blusunrize.immersiveengineering.api.IEApi;
import java.util.List;
/**
* Overrides the mod preference config from IE. As an example, this is used for the IE recycler to generate
* dynamically recipes.
*/
public class IEModPriorityOverride {
public static Runnable overrideModPriorities(List<? extends String> modPriorities) {
return () -> IEApi.modPreference = modPriorities;
}
}

View file

@ -0,0 +1,67 @@
package com.almostreliable.unified.compat.ie;
import com.almostreliable.unified.api.RecipeContext;
import com.almostreliable.unified.api.RecipeHandler;
import com.almostreliable.unified.handler.RecipeConstants;
import com.almostreliable.unified.utils.JsonUtils;
import com.almostreliable.unified.utils.Utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import javax.annotation.Nullable;
public class IERecipeHandler implements RecipeHandler {
// From IE
protected static final String BASE_KEY = "base_ingredient";
@Override
public void transformRecipe(JsonObject json, RecipeContext context) {
// alloy recipes
replaceIEIngredient(json.get("input0"), context);
replaceIEIngredient(json.get("input1"), context);
replaceIEResult(json.get("secondaries"), context);
replaceIEResult(json.get(RecipeConstants.RESULT), context);
replaceIEResult(json.get(RecipeConstants.RESULTS), context);
}
protected void replaceIEResult(@Nullable JsonElement element, RecipeContext context) {
if (element == null) {
return;
}
JsonUtils.arrayForEach(element, JsonObject.class, json -> {
if (json.has(RecipeConstants.ITEM)) {
context.replaceResult(json);
} else if (json.has(RecipeConstants.TAG)) {
/*
* Immersive Engineering allows tags in result and filters them. So we replace the tags with
* the preferred item from our config.
*/
ResourceLocation item = context.getPreferredItemByTag(Utils.toItemTag(json
.get(RecipeConstants.TAG)
.getAsString()));
if (item != null) {
json.remove(RecipeConstants.TAG);
json.addProperty(RecipeConstants.ITEM, item.toString());
}
} else if (json.has(BASE_KEY)) {
replaceIEResult(json.get(BASE_KEY), context);
}
});
}
protected void replaceIEIngredient(@Nullable JsonElement element, RecipeContext context) {
if (element == null) {
return;
}
if (element instanceof JsonObject json && json.has(BASE_KEY)) {
context.replaceIngredient(json.get(BASE_KEY));
} else {
context.replaceIngredient(element);
}
}
}

View file

@ -1,5 +1,5 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.almostreliable.unified.impl;
package com.almostreliable.unified.compat.ie;
import net.minecraft.MethodsReturnNonnullByDefault;

View file

@ -1 +1 @@
com.almostreliable.unified.impl.AlmostUnifiedPlatformForge
com.almostreliable.unified.AlmostUnifiedPlatformForge