| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.core.HolderGetter;
|
| 8 | import net.minecraft.resources.ResourceKey;
|
| 9 | import net.minecraft.tags.TagKey;
|
| 10 | import net.minecraft.util.ProblemReporter;
|
| 11 | import net.minecraft.util.context.ContextKey;
|
| 12 | import net.minecraft.util.context.ContextKeySet;
|
| 13 |
|
| 14 | public class ValidationContext {
|
| 15 | private final ProblemReporter reporter;
|
| 16 | private final ContextKeySet contextKeySet;
|
| 17 | private final Optional<HolderGetter.Provider> resolver;
|
| 18 | private final Set<Object> visitedObjects;
|
| 19 |
|
| 20 | public ValidationContext(final ProblemReporter reporter, final ContextKeySet contextKeySet, final HolderGetter.Provider resolver) {
|
| 21 | this(reporter, contextKeySet, Optional.of(resolver), Set.of());
|
| 22 | }
|
| 23 |
|
| 24 | public ValidationContext(final ProblemReporter reporter, final ContextKeySet contextKeySet) {
|
| 25 | this(reporter, contextKeySet, Optional.empty(), Set.of());
|
| 26 | }
|
| 27 |
|
| 28 | private ValidationContext(
|
| 29 | final ProblemReporter reporter, final ContextKeySet contextKeySet, final Optional<HolderGetter.Provider> resolver, final Set<Object> visitedObjects
|
| 30 | ) {
|
| 31 | this.reporter = reporter;
|
| 32 | this.contextKeySet = contextKeySet;
|
| 33 | this.resolver = resolver;
|
| 34 | this.visitedObjects = visitedObjects;
|
| 35 | }
|
| 36 |
|
| 37 | public ValidationContext forChild(final ProblemReporter.PathElement subContext) {
|
| 38 | return new ValidationContext(this.reporter.forChild(subContext), this.contextKeySet, this.resolver, this.visitedObjects);
|
| 39 | }
|
| 40 |
|
| 41 | public ValidationContext forField(final String name) {
|
| 42 | return this.forChild(new ProblemReporter.FieldPathElement(name));
|
| 43 | }
|
| 44 |
|
| 45 | public ValidationContext forIndexedField(final String name, final int index) {
|
| 46 | return this.forChild(new ProblemReporter.IndexedFieldPathElement(name, index));
|
| 47 | }
|
| 48 |
|
| 49 | public ValidationContext forMapField(final String name, final String key) {
|
| 50 | return this.forChild(new ProblemReporter.MapEntryPathElement(name, key));
|
| 51 | }
|
| 52 |
|
| 53 | private ValidationContext enterObject(final ProblemReporter.PathElement subContext, final Object object) {
|
| 54 | Set<Object> newVisitedElements = ImmutableSet.<Object>builder().addAll(this.visitedObjects).add(object).build();
|
| 55 | return new ValidationContext(this.reporter.forChild(subContext), this.contextKeySet, this.resolver, newVisitedElements);
|
| 56 | }
|
| 57 |
|
| 58 | public ValidationContext enterElement(final ProblemReporter.PathElement subContext, final ResourceKey<?> element) {
|
| 59 | return this.enterObject(subContext, element);
|
| 60 | }
|
| 61 |
|
| 62 | public boolean hasVisitedElement(final ResourceKey<?> element) {
|
| 63 | return this.visitedObjects.contains(element);
|
| 64 | }
|
| 65 |
|
| 66 | public ValidationContext enterTag(final ProblemReporter.PathElement subContext, final TagKey<?> tag) {
|
| 67 | return this.enterObject(subContext, tag);
|
| 68 | }
|
| 69 |
|
| 70 | public boolean hasVisitedTag(final TagKey<?> tag) {
|
| 71 | return this.visitedObjects.contains(tag);
|
| 72 | }
|
| 73 |
|
| 74 | public void reportProblem(final ProblemReporter.Problem description) {
|
| 75 | this.reporter.report(description);
|
| 76 | }
|
| 77 |
|
| 78 | public void validateContextUsage(final LootContextUser lootContextUser) {
|
| 79 | Set<ContextKey<?>> allReferenced = lootContextUser.getReferencedContextParams();
|
| 80 | Set<ContextKey<?>> notProvided = Sets.difference(allReferenced, this.contextKeySet.allowed());
|
| 81 | if (!notProvided.isEmpty()) {
|
| 82 | this.reporter.report(new ValidationContext.ParametersNotProvidedProblem(notProvided));
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public HolderGetter.Provider resolver() {
|
| 87 | return this.resolver.orElseThrow(() -> new UnsupportedOperationException("References not allowed"));
|
| 88 | }
|
| 89 |
|
| 90 | public ProblemReporter reporter() {
|
| 91 | return this.reporter;
|
| 92 | }
|
| 93 |
|
| 94 | public record MinBoundsProblem(int minBounds) implements ProblemReporter.Problem {
|
| 95 | @Override
|
| 96 | public String description() {
|
| 97 | return "List must contain at least " + this.minBounds + " element(s)";
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | public record ParametersNotProvidedProblem(Set<ContextKey<?>> notProvided) implements ProblemReporter.Problem {
|
| 102 | @Override
|
| 103 | public String description() {
|
| 104 | return "Parameters " + this.notProvided + " are not provided in this context";
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | public record RecursiveElementReferenceProblem(ResourceKey<?> referenced) implements ProblemReporter.Problem {
|
| 109 | @Override
|
| 110 | public String description() {
|
| 111 | return this.referenced.identifier() + " of type " + this.referenced.registry() + " is recursively called";
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public boolean isFatal() {
|
| 116 | return true;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | public record RecursiveTagReferenceProblem(TagKey<?> referenced) implements ProblemReporter.Problem {
|
| 121 | @Override
|
| 122 | public String description() {
|
| 123 | return "#" + this.referenced.location() + " of type " + this.referenced.registry() + " is recursively called";
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public boolean isFatal() {
|
| 128 | return true;
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|