Skip to content

Commit

Permalink
DataResult cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNeedsSnacks committed Oct 6, 2024
1 parent 3eeeb3e commit d4ec215
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 99 deletions.
26 changes: 21 additions & 5 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.serialization.DataResult;
import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.core.RecipeLikeKJS;
Expand Down Expand Up @@ -35,12 +36,14 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.apache.commons.lang3.mutable.MutableObject;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class KubeRecipe implements RecipeLikeKJS, CustomJavaToJsWrapper {
public static final String CHANGED_MARKER = "_kubejs_changed_marker";
Expand Down Expand Up @@ -504,11 +507,24 @@ public Recipe<?> getOriginalRecipe() {
if (originalRecipe == null) {
originalRecipe = new MutableObject<>();
try {
var holder = RecipeHelper.fromJson(type.event.jsonOps, type.schemaType.getSerializer(), getOrCreateId(), originalJson, DevProperties.get().logErroringParsedRecipes);

if (holder != null) {
originalRecipe.setValue(holder.value());
}
var serializer = type.schemaType.getSerializer();
var ops = type.event.jsonOps;

// people apparently violate the contract here?!
//noinspection OptionalOfNullableMisuse
Optional.ofNullable(serializer.codec())
.map(DataResult::success)
.orElseGet(() -> DataResult.error(() -> "Codec for " + serializer.getClass().getName() + " is null!"))
.flatMap(codec -> ops.getMap(json).flatMap(map -> codec.decode(ops, map)))
.mapError(err -> "Error parsing recipe " + id + ": " + err)
.ifSuccess(originalRecipe::setValue)
.ifError(err -> {
if (DevProperties.get().logErroringParsedRecipes) {
ConsoleJS.SERVER.error(err.message());
} else {
RecipeManager.LOGGER.error(err.message());
}
});
} catch (Throwable e) {
ConsoleJS.SERVER.error("Could not create recipe from json for " + this, e);
}
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipeHelper.java

This file was deleted.

26 changes: 10 additions & 16 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
Expand Down Expand Up @@ -210,23 +211,16 @@ public void discoverRecipes(RecipeManagerKJS recipeManager, Map<ResourceLocation
}

var codec = ConditionalOps.createConditionalCodec(Codec.unit(originalJson));

var jsonResult = codec.parse(jsonOps, originalJson);

if (jsonResult.isError()) {
errorSkip("Skipping recipe %s, error parsing conditions: %s".formatted(recipeId, jsonResult.error().get().message()));
continue;
}

var result = jsonResult.getOrThrow();

if (result.isEmpty()) {
infoSkip("Skipping recipe %s, conditions not met".formatted(recipeId));
continue;
switch (codec.parse(jsonOps, originalJson)) {
case DataResult.Success(var jsonResult, var lifecycle) -> {
if (jsonResult.isEmpty()) {
infoSkip("Skipping recipe %s, conditions not met".formatted(recipeId));
} else {
parseOriginalRecipe(jsonResult.get(), recipeId);
}
}
case DataResult.Error<?> error -> errorSkip("Skipping recipe %s, error parsing conditions: %s".formatted(recipeId, error.message()));
}

var json = result.get();
parseOriginalRecipe(json, recipeId);
}

takenIds.putAll(originalRecipes);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.latvian.mods.kubejs.recipe.component;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import dev.latvian.mods.kubejs.error.EmptyRecipeComponentValueException;
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.RecipeKey;
Expand Down Expand Up @@ -114,13 +116,9 @@ default void writeToJson(KubeRecipe recipe, RecipeComponentValue<T> cv, JsonObje
}
}

var encoded = cv.key.codec.encodeStart(recipe.type.event.registries.json(), cv.value);

if (encoded.error().isPresent()) {
ConsoleJS.SERVER.error("Failed to encode " + cv.key.name + " for " + recipe.id + " from " + cv.value + ": " + encoded.error().get().message(), recipe.sourceLine, null, RecipesKubeEvent.POST_SKIP_ERROR);
} else if (encoded.isSuccess()) {
var e = encoded.getOrThrow();
json.add(cv.key.name, e);
switch (cv.key.codec.encodeStart(recipe.type.event.registries.json(), cv.value)) {
case DataResult.Success(var value, var lifecycle) -> json.add(cv.key.name, value);
case DataResult.Error<JsonElement> error -> ConsoleJS.SERVER.error("Failed to encode " + cv.key.name + " for recipe " + recipe.id + " from value" + cv.value + ": " + error.message(), recipe.sourceLine, null, RecipesKubeEvent.POST_SKIP_ERROR);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/script/ConsoleJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,22 @@ public ConsoleLine warn(String message, Throwable error, @Nullable Pattern exitP

public ConsoleLine warn(String message, SourceLine sourceLine, Throwable error, @Nullable Pattern exitPattern) {
if (shouldPrint()) {
var l = log(LogType.WARN, sourceLine, error, message.isEmpty() ? error.toString() : (message + ": " + error.toString()));
var l = log(LogType.WARN, sourceLine, error, messageForPrint(message, error));
handleError(l, error, exitPattern, !capturingErrors);
return l;
}

return null;
}

private static String messageForPrint(String message, @Nullable Throwable error) {
if (message.isEmpty()) {
return Objects.requireNonNull(error, "Both message and error are empty!").toString();
} else {
return error == null ? message : (message + ": " + error);
}
}

public ConsoleLine warn(String message, Throwable error) {
return warn(message, error, null);
}
Expand All @@ -361,7 +369,7 @@ public ConsoleLine error(String message, Throwable error, @Nullable Pattern exit

public ConsoleLine error(String message, SourceLine sourceLine, Throwable error, @Nullable Pattern exitPattern) {
if (shouldPrint()) {
var l = log(LogType.ERROR, sourceLine, error, message.isEmpty() ? error.toString() : (message + ": " + error.toString()));
var l = log(LogType.ERROR, sourceLine, error, messageForPrint(message, error));
handleError(l, error, exitPattern, true);
return l;
}
Expand Down

0 comments on commit d4ec215

Please sign in to comment.