Fixed spell crafting

This commit is contained in:
Sollace 2023-10-28 23:18:09 +01:00
parent 7f6b440265
commit 0719b14e2c
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 16 additions and 7 deletions

View file

@ -23,12 +23,20 @@ public record TraitIngredient (
RecordCodecBuilder.<TraitIngredient>create(instance -> instance.group( RecordCodecBuilder.<TraitIngredient>create(instance -> instance.group(
SpellTraits.CODEC.optionalFieldOf("min").forGetter(TraitIngredient::min), SpellTraits.CODEC.optionalFieldOf("min").forGetter(TraitIngredient::min),
SpellTraits.CODEC.optionalFieldOf("max").forGetter(TraitIngredient::max) 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( ).flatXmap(
either -> either.left().or(either::right).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Invalid traits")), 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 @Override
public boolean test(SpellTraits t) { public boolean test(SpellTraits t) {
boolean minMatch = min.map(m -> t.includes(m)).orElse(true); boolean minMatch = min.map(m -> t.includes(m)).orElse(true);

View file

@ -45,9 +45,10 @@ public final class SpellTraits implements Iterable<Map.Entry<Trait, Float>> {
private static Map<Identifier, SpellTraits> REGISTRY = new HashMap<>(); private static Map<Identifier, SpellTraits> REGISTRY = new HashMap<>();
static final Map<Trait, List<Item>> ITEMS = new HashMap<>(); static final Map<Trait, List<Item>> ITEMS = new HashMap<>();
public static final Codec<SpellTraits> CODEC = Codec.unboundedMap(Trait.CODEC, Codec.FLOAT).flatXmap(map -> { public static final Codec<SpellTraits> CODEC = Codec.unboundedMap(Trait.CODEC, Codec.FLOAT).flatXmap(
return fromEntries(map.entrySet().stream()).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "No traits were supplied")); map -> DataResult.success(fromEntries(map.entrySet().stream()).orElse(EMPTY)),
}, traits -> DataResult.success(traits.traits)); traits -> DataResult.success(traits.traits)
);
public static void load(Map<Identifier, SpellTraits> newRegistry) { public static void load(Map<Identifier, SpellTraits> newRegistry) {
REGISTRY = new HashMap<>(newRegistry); REGISTRY = new HashMap<>(newRegistry);

View file

@ -65,7 +65,7 @@ public enum Trait implements CommandArgumentEnum<Trait> {
private static final Map<Identifier, Trait> IDS = Arrays.stream(values()).collect(Collectors.toMap(Trait::getId, Function.identity())); private static final Map<Identifier, Trait> IDS = Arrays.stream(values()).collect(Collectors.toMap(Trait::getId, Function.identity()));
@Deprecated @Deprecated
private static final EnumCodec<Trait> NAME_CODEC = StringIdentifiable.createCodec(Trait::values); private static final EnumCodec<Trait> NAME_CODEC = StringIdentifiable.createCodec(Trait::values, String::toLowerCase);
@Deprecated @Deprecated
private static final EnumCodec<Trait> ID_CODEC = StringIdentifiable.createCodec(Trait::values, i -> "unicopia:" + i); private static final EnumCodec<Trait> ID_CODEC = StringIdentifiable.createCodec(Trait::values, i -> "unicopia:" + i);
@ -173,7 +173,7 @@ public enum Trait implements CommandArgumentEnum<Trait> {
@Deprecated @Deprecated
public static Optional<Trait> fromName(String name) { public static Optional<Trait> fromName(String name) {
return Optional.ofNullable(NAME_CODEC.byId(name.toUpperCase())); return Optional.ofNullable(NAME_CODEC.byId(name));
} }
public static EnumArgumentType<Trait> argument() { public static EnumArgumentType<Trait> argument() {