From 5f422584e156c51ba4624fe7679496d7767976c7 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Thu, 18 Jul 2024 13:53:16 +0300 Subject: [PATCH] Removed recipe async stuff, added `componentType` to recipe replacement match info, KubeJSPlugin#afterScriptsLoaded, beforeRecipeLoading --- .../mods/kubejs/BuiltinKubeJSPlugin.java | 1 + .../mods/kubejs/plugin/KubeJSPlugin.java | 8 +++++ .../mods/kubejs/recipe/KubeRecipe.java | 12 ++++--- .../mods/kubejs/recipe/RecipeHelper.java | 3 +- .../mods/kubejs/recipe/RecipesKubeEvent.java | 32 ++++++++----------- .../component/RecipeComponentValue.java | 2 +- .../recipe/match/ReplacementMatchInfo.java | 8 +++-- .../mods/kubejs/script/ScriptManager.java | 1 + .../kubejs/server/ServerScriptManager.java | 3 +- 9 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index 340cf6047..e890dd540 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -686,6 +686,7 @@ public void registerIngredientActionTypes(IngredientActionTypeRegistry registry) } @Override + @SuppressWarnings("deprecation") public void clearCaches() { ItemStackJS.CACHED_ITEM_MAP.forget(); ItemStackJS.CACHED_ITEM_LIST.forget(); diff --git a/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java index 9677f030c..01e40b490 100644 --- a/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java @@ -1,5 +1,6 @@ package dev.latvian.mods.kubejs.plugin; +import com.google.gson.JsonElement; import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType; import dev.latvian.mods.kubejs.client.LangKubeEvent; import dev.latvian.mods.kubejs.core.RecipeManagerKJS; @@ -109,12 +110,16 @@ default void generateAssets(KubeAssetGenerator generator) { default void generateLang(LangKubeEvent event) { } + @Deprecated default void clearCaches() { } default void exportServerData(DataExport export) { } + default void beforeRecipeLoading(RecipesKubeEvent event, RecipeManagerKJS manager, Map recipeJsons) { + } + /** * Only use this method if your mod adds runtime recipes and is conflicting with KubeJS recipe manager. Disable your other hook if "kubejs" mod is loaded! */ @@ -123,4 +128,7 @@ default void injectRuntimeRecipes(RecipesKubeEvent event, RecipeManagerKJS manag default void beforeScriptsLoaded(ScriptManager manager) { } + + default void afterScriptsLoaded(ScriptManager manager) { + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java index 45d19266e..76972bf79 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java @@ -527,23 +527,27 @@ public Recipe getOriginalRecipe() { } public ItemStack getOriginalRecipeResult() { - if (getOriginalRecipe() == null) { + var original = getOriginalRecipe(); + + if (original == null) { ConsoleJS.SERVER.warn("Original recipe is null - could not get result"); return ItemStack.EMPTY; } - var result = getOriginalRecipe().getResultItem(type.event.registries.access()); + var result = original.getResultItem(type.event.registries.access()); //noinspection ConstantValue return result == null ? ItemStack.EMPTY : result; } public List getOriginalRecipeIngredients() { - if (getOriginalRecipe() == null) { + var original = getOriginalRecipe(); + + if (original == null) { ConsoleJS.SERVER.warn("Original recipe is null - could not get ingredients"); return List.of(); } - return List.copyOf(getOriginalRecipe().getIngredients()); + return List.copyOf(original.getIngredients()); } public KubeRecipe ingredientAction(SlotFilter filter, IngredientAction action) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java index fa0fb7001..535ebf9d3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java @@ -40,7 +40,8 @@ static RecipeHolder fromJson(DynamicOps ops, RecipeSerializer var recipe = codec.decode(ops, map.get()); if (recipe.isSuccess()) { - return new RecipeHolder<>(id, recipe.getOrThrow()); + var r = recipe.getOrThrow(); + return r == null ? null : new RecipeHolder<>(id, r); } else if (recipe.error().isPresent()) { if (errors) { ConsoleJS.SERVER.error("Error parsing recipe " + id + ": " + recipe.error().get().message()); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java index 0032bd3c0..267558b17 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java @@ -229,6 +229,8 @@ public void post(RecipeManagerKJS recipeManager, Map p.beforeRecipeLoading(this, recipeManager, datapackRecipeMap)); + for (var entry : datapackRecipeMap.entrySet()) { var recipeId = entry.getKey(); @@ -267,8 +269,9 @@ public void post(RecipeManagerKJS recipeManager, Map"); } else { ConsoleJS.SERVER.debug("Loaded recipe " + recipeIdAndType + ": " + recipe.getFromToString()); @@ -428,7 +431,7 @@ public boolean test(KubeRecipe r) { } } - public Stream recipeStream(Context cx, RecipeFilter filter, boolean parallel) { + public Stream recipeStream(Context cx, RecipeFilter filter) { if (filter == ConstantFilter.FALSE) { return Stream.empty(); } else if (filter instanceof IDFilter id) { @@ -436,8 +439,6 @@ public Stream recipeStream(Context cx, RecipeFilter filter, boolean return r == null || r.removed ? Stream.empty() : Stream.of(r); } - boolean actuallyParallel = parallel && CommonProperties.get().allowAsyncStreams; - exit: if (filter instanceof OrFilter or) { if (or.list.isEmpty()) { @@ -450,23 +451,18 @@ public Stream recipeStream(Context cx, RecipeFilter filter, boolean } } - return (actuallyParallel ? or.list.parallelStream() : or.list.stream()).map(idf -> originalRecipes.get(((IDFilter) idf).id)).filter(RECIPE_NOT_REMOVED); + return or.list.stream().map(idf -> originalRecipes.get(((IDFilter) idf).id)).filter(RECIPE_NOT_REMOVED); } - return (actuallyParallel ? originalRecipes.values().parallelStream() : originalRecipes.values().stream()).filter(new RecipeStreamFilter(cx, filter)); - } - - private void forEachRecipeAsync(Context cx, RecipeFilter filter, Consumer consumer) { - var stream = recipeStream(cx, filter, true); - stream.forEach(consumer); + return originalRecipes.values().stream().filter(new RecipeStreamFilter(cx, filter)); } private T reduceRecipesAsync(Context cx, RecipeFilter filter, Function, T> function) { - return function.apply(recipeStream(cx, filter, true)); + return function.apply(recipeStream(cx, filter)); } public void forEachRecipe(Context cx, RecipeFilter filter, Consumer consumer) { - recipeStream(cx, filter, false).forEach(consumer); + recipeStream(cx, filter).forEach(consumer); } public int countRecipes(Context cx, RecipeFilter filter) { @@ -493,14 +489,14 @@ public void remove(Context cx, RecipeFilter filter) { r.remove(); } } else { - forEachRecipeAsync(cx, filter, KubeRecipe::remove); + forEachRecipe(cx, filter, KubeRecipe::remove); } } public void replaceInput(Context cx, RecipeFilter filter, ReplacementMatchInfo match, Object with) { var dstring = (DevProperties.get().logModifiedRecipes || ConsoleJS.SERVER.shouldPrintDebug()) ? (": IN " + match + " -> " + with) : ""; - forEachRecipeAsync(cx, filter, r -> { + forEachRecipe(cx, filter, r -> { if (r.replaceInput(cx, match, with)) { if (DevProperties.get().logModifiedRecipes) { ConsoleJS.SERVER.info("~ " + r + dstring); @@ -514,7 +510,7 @@ public void replaceInput(Context cx, RecipeFilter filter, ReplacementMatchInfo m public void replaceOutput(Context cx, RecipeFilter filter, ReplacementMatchInfo match, Object with) { var dstring = (DevProperties.get().logModifiedRecipes || ConsoleJS.SERVER.shouldPrintDebug()) ? (": OUT " + match + " -> " + with) : ""; - forEachRecipeAsync(cx, filter, r -> { + forEachRecipe(cx, filter, r -> { if (r.replaceOutput(cx, match, with)) { if (DevProperties.get().logModifiedRecipes) { ConsoleJS.SERVER.info("~ " + r + dstring); @@ -618,6 +614,6 @@ public synchronized ResourceLocation takeId(KubeRecipe recipe, String prefix, St } public void stage(Context cx, RecipeFilter filter, String stage) { - forEachRecipeAsync(cx, filter, r -> r.stage(stage)); + forEachRecipe(cx, filter, r -> r.stage(stage)); } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java index 4045c6488..7626a87a5 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java @@ -32,7 +32,7 @@ public RecipeComponentValue copy() { } public boolean matches(Context cx, KubeRecipe recipe, ReplacementMatchInfo match) { - return value != null && key.component.matches(cx, recipe, value, match); + return value != null && (match.componentType().isEmpty() || key.component.equals(match.componentType().get())) && key.component.matches(cx, recipe, value, match); } public boolean replace(Context cx, KubeRecipe recipe, ReplacementMatchInfo match, Object with) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatchInfo.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatchInfo.java index dc11852fa..1354695ab 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatchInfo.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatchInfo.java @@ -1,16 +1,18 @@ package dev.latvian.mods.kubejs.recipe.match; +import dev.latvian.mods.kubejs.recipe.component.RecipeComponent; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.NativeJavaMap; import dev.latvian.mods.rhino.type.RecordTypeInfo; import dev.latvian.mods.rhino.type.TypeInfo; import java.util.Map; +import java.util.Optional; -public record ReplacementMatchInfo(ReplacementMatch match, boolean exact) { +public record ReplacementMatchInfo(ReplacementMatch match, boolean exact, Optional> componentType) { public static final RecordTypeInfo TYPE_INFO = (RecordTypeInfo) TypeInfo.of(ReplacementMatchInfo.class); - public static final ReplacementMatchInfo NONE = new ReplacementMatchInfo(ReplacementMatch.NONE, false); + public static final ReplacementMatchInfo NONE = new ReplacementMatchInfo(ReplacementMatch.NONE, false, Optional.empty()); public static ReplacementMatchInfo wrap(Context cx, Object o, TypeInfo target) { if (o == null) { @@ -21,7 +23,7 @@ public static ReplacementMatchInfo wrap(Context cx, Object o, TypeInfo target) { return (ReplacementMatchInfo) TYPE_INFO.wrap(cx, o, target); } else { var m = ReplacementMatch.wrap(cx, o); - return m == ReplacementMatch.NONE ? NONE : new ReplacementMatchInfo(m, false); + return m == ReplacementMatch.NONE ? NONE : new ReplacementMatchInfo(m, false, Optional.empty()); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java index 498a02d99..22acd082a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java @@ -52,6 +52,7 @@ public void reload() { KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::beforeScriptsLoaded); loadFromDirectory(); load(); + KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::afterScriptsLoaded); } public void loadPackFromDirectory(Path path, String name, boolean exampleFile) { diff --git a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java index 3348ca487..488dea07e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java @@ -40,6 +40,7 @@ import java.nio.file.Files; import java.util.ArrayList; +import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -227,7 +228,7 @@ public boolean recipes(RecipeManagerKJS recipeManager, ResourceManager resourceM ServerEvents.SPECIAL_RECIPES.post(ScriptType.SERVER, SpecialRecipeSerializerManager.INSTANCE); if (ServerEvents.RECIPES.hasListeners()) { - new RecipesKubeEvent(this).post(recipeManager, map); + new RecipesKubeEvent(this).post(recipeManager, new HashMap<>(map)); result = true; }