From 0719b14e2cebc5baa4d020368f2fe89431dd4387 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 28 Oct 2023 23:18:09 +0100 Subject: [PATCH] Fixed spell crafting --- .../magic/spell/crafting/TraitIngredient.java | 12 ++++++++++-- .../ability/magic/spell/trait/SpellTraits.java | 7 ++++--- .../unicopia/ability/magic/spell/trait/Trait.java | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java index 9e4ac345..71d049e4 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/TraitIngredient.java @@ -23,12 +23,20 @@ public record TraitIngredient ( RecordCodecBuilder.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"), + ingredient -> DataResult.success(ingredient) + ) ).flatXmap( either -> either.left().or(either::right).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Invalid traits")), - ingredient -> DataResult.success(ingredient.max.isPresent() ? Either.left(ingredient) : Either.right(ingredient)) + ingredient -> DataResult.success(ingredient.max.isEmpty() ? Either.left(ingredient) : Either.right(ingredient)) ); + public boolean isEmpty() { + return min.filter(SpellTraits::isPresent).isEmpty() + && max.filter(SpellTraits::isPresent).isEmpty(); + } + @Override public boolean test(SpellTraits t) { boolean minMatch = min.map(m -> t.includes(m)).orElse(true); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java index 85107bb6..d03288f6 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/SpellTraits.java @@ -45,9 +45,10 @@ public final class SpellTraits implements Iterable> { private static Map REGISTRY = new HashMap<>(); static final Map> ITEMS = new HashMap<>(); - public static final Codec CODEC = Codec.unboundedMap(Trait.CODEC, Codec.FLOAT).flatXmap(map -> { - return fromEntries(map.entrySet().stream()).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "No traits were supplied")); - }, traits -> DataResult.success(traits.traits)); + public static final Codec CODEC = Codec.unboundedMap(Trait.CODEC, Codec.FLOAT).flatXmap( + map -> DataResult.success(fromEntries(map.entrySet().stream()).orElse(EMPTY)), + traits -> DataResult.success(traits.traits) + ); public static void load(Map newRegistry) { REGISTRY = new HashMap<>(newRegistry); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java index e43b001a..bc39e514 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/trait/Trait.java @@ -65,7 +65,7 @@ public enum Trait implements CommandArgumentEnum { private static final Map IDS = Arrays.stream(values()).collect(Collectors.toMap(Trait::getId, Function.identity())); @Deprecated - private static final EnumCodec NAME_CODEC = StringIdentifiable.createCodec(Trait::values); + private static final EnumCodec NAME_CODEC = StringIdentifiable.createCodec(Trait::values, String::toLowerCase); @Deprecated private static final EnumCodec ID_CODEC = StringIdentifiable.createCodec(Trait::values, i -> "unicopia:" + i); @@ -173,7 +173,7 @@ public enum Trait implements CommandArgumentEnum { @Deprecated public static Optional fromName(String name) { - return Optional.ofNullable(NAME_CODEC.byId(name.toUpperCase())); + return Optional.ofNullable(NAME_CODEC.byId(name)); } public static EnumArgumentType argument() {