From 2c1f0f1925258bd90129805c4505d910b3e75025 Mon Sep 17 00:00:00 2001 From: Josiah Glosson Date: Wed, 18 Dec 2024 22:13:51 -0600 Subject: [PATCH] Validate non-subbed goal criteria on load --- .../gaming32/bingo/data/goal/BingoGoal.java | 9 +++++++++ .../gaming32/bingo/data/goal/GoalManager.java | 10 ++++++++++ .../gaming32/bingo/data/subs/ParsedOrSub.java | 5 +++++ .../github/gaming32/bingo/game/ActiveGoal.java | 18 +++++------------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/io/github/gaming32/bingo/data/goal/BingoGoal.java b/common/src/main/java/io/github/gaming32/bingo/data/goal/BingoGoal.java index 5608374c..258e6afd 100644 --- a/common/src/main/java/io/github/gaming32/bingo/data/goal/BingoGoal.java +++ b/common/src/main/java/io/github/gaming32/bingo/data/goal/BingoGoal.java @@ -20,6 +20,7 @@ import net.minecraft.advancements.AdvancementRequirements; import net.minecraft.advancements.Criterion; import net.minecraft.advancements.CriterionTrigger; +import net.minecraft.advancements.critereon.CriterionValidator; import net.minecraft.core.Holder; import net.minecraft.core.HolderGetter; import net.minecraft.core.HolderSet; @@ -33,6 +34,7 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.ExtraCodecs; +import net.minecraft.util.ProblemReporter; import net.minecraft.util.RandomSource; import java.util.Collection; @@ -188,6 +190,13 @@ public DataResult validate() { return result; } + public void validateParsedCriteria(ProblemReporter reporter, HolderGetter.Provider lootData) { + criteria.forEach((key, criterionOrSub) -> criterionOrSub.ifParsed(criterion -> { + final CriterionValidator validator = new CriterionValidator(reporter.forChild(key), lootData); + criterion.triggerInstance().validate(validator); + })); + } + public Map getSubs() { return subs; } diff --git a/common/src/main/java/io/github/gaming32/bingo/data/goal/GoalManager.java b/common/src/main/java/io/github/gaming32/bingo/data/goal/GoalManager.java index 743428ff..e8a144c7 100644 --- a/common/src/main/java/io/github/gaming32/bingo/data/goal/GoalManager.java +++ b/common/src/main/java/io/github/gaming32/bingo/data/goal/GoalManager.java @@ -9,6 +9,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; +import net.minecraft.util.ProblemReporter; import net.minecraft.util.profiling.ProfilerFiller; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -61,6 +62,7 @@ protected void apply(Map goals, ResourceManager res for (final var entry : goals.entrySet()) { final var goal = entry.getValue(); final GoalHolder holder = new GoalHolder(entry.getKey(), goal); + validate(holder); result.put(holder.id(), holder); byDifficulty.computeIfAbsent(goal.getDifficulty().value().number(), k -> ImmutableList.builder()).add(holder); } @@ -73,4 +75,12 @@ protected void apply(Map goals, ResourceManager res )); Bingo.LOGGER.info("Loaded {} bingo goals", GoalManager.goals.size()); } + + private void validate(GoalHolder holder) { + final var collector = new ProblemReporter.Collector(); + holder.goal().validateParsedCriteria(collector, registries); + collector.getReport().ifPresent(report -> + Bingo.LOGGER.warn("Found validation problems in goal {}:\n{}", holder.id(), report) + ); + } } diff --git a/common/src/main/java/io/github/gaming32/bingo/data/subs/ParsedOrSub.java b/common/src/main/java/io/github/gaming32/bingo/data/subs/ParsedOrSub.java index 2daf6f71..a9513933 100644 --- a/common/src/main/java/io/github/gaming32/bingo/data/subs/ParsedOrSub.java +++ b/common/src/main/java/io/github/gaming32/bingo/data/subs/ParsedOrSub.java @@ -10,6 +10,7 @@ import io.github.gaming32.bingo.util.BingoUtil; import java.util.Optional; +import java.util.function.Consumer; import java.util.function.Function; public record ParsedOrSub(Dynamic serialized, Either, Codec> valueOrCodec) { @@ -70,4 +71,8 @@ public DataResult substitute(SubstitutionContext context) { public T substituteOrThrow(SubstitutionContext context) { return substitute(context).getOrThrow(IllegalArgumentException::new); } + + public void ifParsed(Consumer consumer) { + valueOrCodec.left().flatMap(DataResult::result).ifPresent(consumer); + } } diff --git a/common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java b/common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java index 0f1d0163..c83643ef 100644 --- a/common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java +++ b/common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java @@ -1,6 +1,5 @@ package io.github.gaming32.bingo.game; -import com.google.common.collect.Multimap; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import io.github.gaming32.bingo.Bingo; @@ -36,7 +35,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; public record ActiveGoal( ResourceLocation id, @@ -112,20 +110,14 @@ public ItemStack getFallbackWithComponents(RegistryAccess access) { } public void validateAndLog(HolderGetter.Provider lootData) { - final ProblemReporter.Collector collector = new ProblemReporter.Collector(); + final var collector = new ProblemReporter.Collector(); validate(collector, lootData); - final Multimap errors = collector.get(); - if (!errors.isEmpty()) { - final String message = errors.asMap() - .entrySet() - .stream() - .map(entry -> " at " + entry.getKey() + ": " + String.join("; ", entry.getValue())) - .collect(Collectors.joining("\n")); - Bingo.LOGGER.warn("Found validation problems in goal {}:\n{}", id, message); - } + collector.getReport().ifPresent(report -> + Bingo.LOGGER.warn("Found validation problems in goal {}:\n{}", id, report) + ); } - public void validate(ProblemReporter reporter, HolderGetter.Provider lootData) { + private void validate(ProblemReporter reporter, HolderGetter.Provider lootData) { criteria.forEach((key, criterion) -> { final CriterionValidator validator = new CriterionValidator(reporter.forChild(key), lootData); criterion.triggerInstance().validate(validator);