Skip to content

Commit

Permalink
Added log_erroring_parsed_recipes dev option
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 17, 2024
1 parent 4262852 commit 198f055
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/DevProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void reload() {
public boolean logRecipeDebug;
public boolean logSkippedTags;
public boolean logErroringRecipes;
public boolean logErroringParsedRecipes;
public boolean logInvalidRecipeHandlers;
public boolean logSkippedPlugins;
public boolean logGeneratedData;
Expand All @@ -51,6 +52,7 @@ protected void load() {
logRecipeDebug = get("log_recipe_debug", false);
logSkippedTags = get("log_skipped_tags", false);
logErroringRecipes = get("log_erroring_recipes", true);
logErroringParsedRecipes = get("log_erroring_parsed_recipes", false);
logInvalidRecipeHandlers = get("log_invalid_recipe_handlers", true);
logSkippedPlugins = get("log_skipped_plugins", true);
logGeneratedData = get("log_generated_data", false);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.neoforged.fml.loading.FMLLoader;
import org.apache.commons.lang3.mutable.MutableObject;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -505,7 +504,7 @@ public RecipeHolder<?> createRecipe() {
return new RecipeHolder<>(getOrCreateId(), originalRecipe.getValue());
}

return RecipeHelper.fromJson(type.event.registries.json(), getSerializationTypeFunction().schemaType.getSerializer(), getOrCreateId(), json, !FMLLoader.isProduction());
return RecipeHelper.fromJson(type.event.registries.json(), getSerializationTypeFunction().schemaType.getSerializer(), getOrCreateId(), json, DevProperties.get().logErroringParsedRecipes);
}

@Nullable
Expand All @@ -514,7 +513,11 @@ public Recipe<?> getOriginalRecipe() {
originalRecipe = new MutableObject<>();
try {
// todo: this sucks
originalRecipe.setValue(RecipeHelper.fromJson(type.event.registries.json(), type.schemaType.getSerializer(), getOrCreateId(), json, !FMLLoader.isProduction()).value());
var holder = RecipeHelper.fromJson(type.event.registries.json(), type.schemaType.getSerializer(), getOrCreateId(), json, DevProperties.get().logErroringParsedRecipes);

if (holder != null) {
originalRecipe.setValue(holder.value());
}
} catch (Throwable e) {
ConsoleJS.SERVER.error("Could not create recipe from json for " + this, e);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ static RecipeHolder<?> fromJson(DynamicOps<JsonElement> ops, RecipeSerializer<?>
try {
var recipe = codec.decode(ops, map.get());

if (recipe.error().isPresent()) {
if (recipe.isSuccess()) {
return new RecipeHolder<>(id, recipe.getOrThrow());
} else if (recipe.error().isPresent()) {
if (errors) {
ConsoleJS.SERVER.error("Error parsing recipe " + id + ": " + recipe.error().get().message());
}
} else if (recipe.isSuccess()) {
return new RecipeHolder<>(id, recipe.getOrThrow());
} else {
if (errors) {
ConsoleJS.SERVER.error("Error parsing recipe " + id + ": Unknown");
}
}
} catch (Exception e) {
if (errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ public void serialize() {

@Override
public boolean hasInput(Context cx, ReplacementMatchInfo match) {
if (CommonProperties.get().matchJsonRecipes && match.match() instanceof ItemMatch m && getOriginalRecipe() != null) {
var arr = getOriginalRecipe().getIngredients();
if (CommonProperties.get().matchJsonRecipes && match.match() instanceof ItemMatch m) {
var original = getOriginalRecipe();

if (original == null) {
return false;
}

var arr = original.getIngredients();

//noinspection ConstantValue
if (arr == null || arr.isEmpty()) {
Expand All @@ -47,8 +53,14 @@ public boolean replaceInput(Context cx, ReplacementMatchInfo match, Object with)

@Override
public boolean hasOutput(Context cx, ReplacementMatchInfo match) {
if (CommonProperties.get().matchJsonRecipes && match.match() instanceof ItemMatch m && getOriginalRecipe() != null) {
var result = getOriginalRecipe().getResultItem(type.event.registries.access());
if (CommonProperties.get().matchJsonRecipes && match.match() instanceof ItemMatch m) {
var original = getOriginalRecipe();

if (original == null) {
return false;
}

var result = original.getResultItem(type.event.registries.access());
//noinspection ConstantValue
return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.matches(cx, result, match.exact());
}
Expand Down

0 comments on commit 198f055

Please sign in to comment.