diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java index 14e5015..b9e094e 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java @@ -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); } } diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java index c398982..acf61cf 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java @@ -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); } diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java index 632655a..bdac8a0 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntime.java @@ -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 modPriorities = new ArrayList<>(); public AlmostUnifiedRuntime(RecipeHandlerFactory recipeHandlerFactory) { this.recipeHandlerFactory = recipeHandlerFactory; @@ -28,7 +26,9 @@ public class AlmostUnifiedRuntime { public void run(Map 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(); } diff --git a/Common/src/main/java/com/almostreliable/unified/ModConstants.java b/Common/src/main/java/com/almostreliable/unified/ModConstants.java new file mode 100644 index 0000000..9a90d54 --- /dev/null +++ b/Common/src/main/java/com/almostreliable/unified/ModConstants.java @@ -0,0 +1,5 @@ +package com.almostreliable.unified; + +public class ModConstants { + public static final String IE = "immersiveengineering"; +} diff --git a/Common/src/main/java/com/almostreliable/unified/handler/RecipeHandlerFactory.java b/Common/src/main/java/com/almostreliable/unified/handler/RecipeHandlerFactory.java index 67f8f4f..d632e71 100644 --- a/Common/src/main/java/com/almostreliable/unified/handler/RecipeHandlerFactory.java +++ b/Common/src/main/java/com/almostreliable/unified/handler/RecipeHandlerFactory.java @@ -10,12 +10,18 @@ import java.util.Map; public class RecipeHandlerFactory { private final Map transformersByType = new HashMap<>(); + private final Map 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); + } } diff --git a/Common/src/main/java/com/almostreliable/unified/handler/ie/IEBaseRecipeHandler.java b/Common/src/main/java/com/almostreliable/unified/handler/ie/IEBaseRecipeHandler.java deleted file mode 100644 index 58ec19a..0000000 --- a/Common/src/main/java/com/almostreliable/unified/handler/ie/IEBaseRecipeHandler.java +++ /dev/null @@ -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) { - - - } -} diff --git a/Common/src/main/java/com/almostreliable/unified/handler/ie/package-info.java b/Common/src/main/java/com/almostreliable/unified/handler/ie/package-info.java deleted file mode 100644 index d22cd8e..0000000 --- a/Common/src/main/java/com/almostreliable/unified/handler/ie/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.almostreliable.unified.handler.ie; - -import net.minecraft.MethodsReturnNonnullByDefault; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/Fabric/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformFabric.java b/Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformFabric.java similarity index 64% rename from Fabric/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformFabric.java rename to Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformFabric.java index a534852..6341225 100644 --- a/Fabric/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformFabric.java +++ b/Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformFabric.java @@ -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); + } } diff --git a/Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeFabric.java b/Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeFabric.java new file mode 100644 index 0000000..44a0f3e --- /dev/null +++ b/Fabric/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeFabric.java @@ -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() { + + } +} diff --git a/Fabric/src/main/java/com/almostreliable/unified/impl/package-info.java b/Fabric/src/main/java/com/almostreliable/unified/impl/package-info.java deleted file mode 100644 index 39e4d22..0000000 --- a/Fabric/src/main/java/com/almostreliable/unified/impl/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.almostreliable.unified.impl; - -import net.minecraft.MethodsReturnNonnullByDefault; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/Fabric/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform b/Fabric/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform index 79dc098..3341490 100644 --- a/Fabric/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform +++ b/Fabric/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform @@ -1 +1 @@ -com.almostreliable.unified.impl.AlmostUnifiedPlatformFabric +com.almostreliable.unified.AlmostUnifiedPlatformFabric diff --git a/Forge/build.gradle.kts b/Forge/build.gradle.kts index 799f390..3e3dbe9 100644 --- a/Forge/build.gradle.kts +++ b/Forge/build.gradle.kts @@ -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")) diff --git a/Forge/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformForge.java b/Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformForge.java similarity index 57% rename from Forge/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformForge.java rename to Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformForge.java index 1f2d2a6..12321b1 100644 --- a/Forge/src/main/java/com/almostreliable/unified/impl/AlmostUnifiedPlatformForge.java +++ b/Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatformForge.java @@ -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); + } } diff --git a/Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeForge.java b/Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeForge.java new file mode 100644 index 0000000..8a87e04 --- /dev/null +++ b/Forge/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeForge.java @@ -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(); + } + } +} diff --git a/Forge/src/main/java/com/almostreliable/unified/compat/ie/IEModPriorityOverride.java b/Forge/src/main/java/com/almostreliable/unified/compat/ie/IEModPriorityOverride.java new file mode 100644 index 0000000..fd17853 --- /dev/null +++ b/Forge/src/main/java/com/almostreliable/unified/compat/ie/IEModPriorityOverride.java @@ -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 modPriorities) { + return () -> IEApi.modPreference = modPriorities; + } +} diff --git a/Forge/src/main/java/com/almostreliable/unified/compat/ie/IERecipeHandler.java b/Forge/src/main/java/com/almostreliable/unified/compat/ie/IERecipeHandler.java new file mode 100644 index 0000000..4d512b2 --- /dev/null +++ b/Forge/src/main/java/com/almostreliable/unified/compat/ie/IERecipeHandler.java @@ -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); + } + } +} diff --git a/Forge/src/main/java/com/almostreliable/unified/impl/package-info.java b/Forge/src/main/java/com/almostreliable/unified/compat/ie/package-info.java similarity index 78% rename from Forge/src/main/java/com/almostreliable/unified/impl/package-info.java rename to Forge/src/main/java/com/almostreliable/unified/compat/ie/package-info.java index 39e4d22..0b3c250 100644 --- a/Forge/src/main/java/com/almostreliable/unified/impl/package-info.java +++ b/Forge/src/main/java/com/almostreliable/unified/compat/ie/package-info.java @@ -1,5 +1,5 @@ @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.almostreliable.unified.impl; +package com.almostreliable.unified.compat.ie; import net.minecraft.MethodsReturnNonnullByDefault; diff --git a/Forge/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform b/Forge/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform index b26a2b0..cbca74b 100644 --- a/Forge/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform +++ b/Forge/src/main/resources/META-INF/services/com.almostreliable.unified.AlmostUnifiedPlatform @@ -1 +1 @@ -com.almostreliable.unified.impl.AlmostUnifiedPlatformForge +com.almostreliable.unified.AlmostUnifiedPlatformForge