Skip to content

Commit

Permalink
Removed recipe async stuff, added componentType to recipe replaceme…
Browse files Browse the repository at this point in the history
…nt match info, KubeJSPlugin#afterScriptsLoaded, beforeRecipeLoading
  • Loading branch information
LatvianModder committed Jul 18, 2024
1 parent 198f055 commit 5f42258
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ResourceLocation, JsonElement> 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!
*/
Expand All @@ -123,4 +128,7 @@ default void injectRuntimeRecipes(RecipesKubeEvent event, RecipeManagerKJS manag

default void beforeScriptsLoaded(ScriptManager manager) {
}

default void afterScriptsLoaded(ScriptManager manager) {
}
}
12 changes: 8 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ingredient> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ static RecipeHolder<?> fromJson(DynamicOps<JsonElement> 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());
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public void post(RecipeManagerKJS recipeManager, Map<ResourceLocation, JsonEleme

var timer = Stopwatch.createStarted();

KubeJSPlugins.forEachPlugin(p -> p.beforeRecipeLoading(this, recipeManager, datapackRecipeMap));

for (var entry : datapackRecipeMap.entrySet()) {
var recipeId = entry.getKey();

Expand Down Expand Up @@ -267,8 +269,9 @@ public void post(RecipeManagerKJS recipeManager, Map<ResourceLocation, JsonEleme
originalRecipes.put(recipeId, recipe);

if (ConsoleJS.SERVER.shouldPrintDebug()) {
var originalRecipe = recipe.getOriginalRecipe();
if (originalRecipe == null || SpecialRecipeSerializerManager.INSTANCE.isSpecial(originalRecipe)) {
var original = recipe.getOriginalRecipe();

if (original == null || SpecialRecipeSerializerManager.INSTANCE.isSpecial(original)) {
ConsoleJS.SERVER.debug("Loaded recipe " + recipeIdAndType + ": <dynamic>");
} else {
ConsoleJS.SERVER.debug("Loaded recipe " + recipeIdAndType + ": " + recipe.getFromToString());
Expand Down Expand Up @@ -428,16 +431,14 @@ public boolean test(KubeRecipe r) {
}
}

public Stream<KubeRecipe> recipeStream(Context cx, RecipeFilter filter, boolean parallel) {
public Stream<KubeRecipe> recipeStream(Context cx, RecipeFilter filter) {
if (filter == ConstantFilter.FALSE) {
return Stream.empty();
} else if (filter instanceof IDFilter id) {
var r = originalRecipes.get(id.id);
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()) {
Expand All @@ -450,23 +451,18 @@ public Stream<KubeRecipe> 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<KubeRecipe> consumer) {
var stream = recipeStream(cx, filter, true);
stream.forEach(consumer);
return originalRecipes.values().stream().filter(new RecipeStreamFilter(cx, filter));
}

private <T> T reduceRecipesAsync(Context cx, RecipeFilter filter, Function<Stream<KubeRecipe>, T> function) {
return function.apply(recipeStream(cx, filter, true));
return function.apply(recipeStream(cx, filter));
}

public void forEachRecipe(Context cx, RecipeFilter filter, Consumer<KubeRecipe> consumer) {
recipeStream(cx, filter, false).forEach(consumer);
recipeStream(cx, filter).forEach(consumer);
}

public int countRecipes(Context cx, RecipeFilter filter) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RecipeComponentValue<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RecipeComponent<?>> 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) {
Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5f42258

Please sign in to comment.