Skip to content

Commit

Permalink
Validate non-subbed goal criteria on load
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Dec 19, 2024
1 parent c4110f2 commit 2c1f0f1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -188,6 +190,13 @@ public DataResult<BingoGoal> 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<String, BingoSub> getSubs() {
return subs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected void apply(Map<ResourceLocation, BingoGoal> 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);
}
Expand All @@ -73,4 +75,12 @@ protected void apply(Map<ResourceLocation, BingoGoal> 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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(Dynamic<?> serialized, Either<DataResult<T>, Codec<T>> valueOrCodec) {
Expand Down Expand Up @@ -70,4 +71,8 @@ public DataResult<T> substitute(SubstitutionContext context) {
public T substituteOrThrow(SubstitutionContext context) {
return substitute(context).getOrThrow(IllegalArgumentException::new);
}

public void ifParsed(Consumer<T> consumer) {
valueOrCodec.left().flatMap(DataResult::result).ifPresent(consumer);
}
}
18 changes: 5 additions & 13 deletions common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String, String> 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);
Expand Down

0 comments on commit 2c1f0f1

Please sign in to comment.