Fix codec issues (again)

This commit is contained in:
Sollace 2024-04-05 23:30:43 +01:00
parent dc7a5debaa
commit fcba23c8ed
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB

View file

@ -23,7 +23,10 @@ public record TraitIngredient (
private static final Codec<TraitIngredient> STRUCTURED_CODEC = RecordCodecBuilder.<TraitIngredient>create(instance -> instance.group(
SpellTraits.CODEC.optionalFieldOf("min").forGetter(TraitIngredient::min),
SpellTraits.CODEC.optionalFieldOf("max").forGetter(TraitIngredient::max)
).apply(instance, TraitIngredient::new));
).apply(instance, TraitIngredient::new)).flatXmap(
ingredient -> !ingredient.isEmpty() ? DataResult.success(ingredient) : DataResult.error(() -> "No min or max supplied for ingredient"),
DataResult::success
);
public static final Codec<TraitIngredient> CODEC = Codecs.xor(INLINE_CODEC, STRUCTURED_CODEC).flatXmap(
either -> either.left().or(either::right).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Invalid traits")),
ingredient -> DataResult.success(ingredient.max.isEmpty() ? Either.left(ingredient) : Either.right(ingredient))