| 1 | package net.minecraft.tags;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.util.ExtraCodecs;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class TagEntry {
|
| 14 | private static final Codec<TagEntry> FULL_CODEC = RecordCodecBuilder.create(
|
| 15 | i -> i.group(
|
| 16 | ExtraCodecs.TAG_OR_ELEMENT_ID.fieldOf("id").forGetter(TagEntry::elementOrTag), Codec.BOOL.optionalFieldOf("required", true).forGetter(e -> e.required)
|
| 17 | )
|
| 18 | .apply(i, TagEntry::new)
|
| 19 | );
|
| 20 | public static final Codec<TagEntry> CODEC = Codec.either(ExtraCodecs.TAG_OR_ELEMENT_ID, FULL_CODEC)
|
| 21 | .xmap(e -> e.map(l -> new TagEntry(l, true), r -> (TagEntry)r), entry -> entry.required ? Either.left(entry.elementOrTag()) : Either.right(entry));
|
| 22 | private final Identifier id;
|
| 23 | private final boolean tag;
|
| 24 | private final boolean required;
|
| 25 |
|
| 26 | private TagEntry(final Identifier id, final boolean tag, final boolean required) {
|
| 27 | this.id = id;
|
| 28 | this.tag = tag;
|
| 29 | this.required = required;
|
| 30 | }
|
| 31 |
|
| 32 | private TagEntry(final ExtraCodecs.TagOrElementLocation elementOrTag, final boolean required) {
|
| 33 | this.id = elementOrTag.id();
|
| 34 | this.tag = elementOrTag.tag();
|
| 35 | this.required = required;
|
| 36 | }
|
| 37 |
|
| 38 | private ExtraCodecs.TagOrElementLocation elementOrTag() {
|
| 39 | return new ExtraCodecs.TagOrElementLocation(this.id, this.tag);
|
| 40 | }
|
| 41 |
|
| 42 | public static TagEntry element(final Identifier id) {
|
| 43 | return new TagEntry(id, false, true);
|
| 44 | }
|
| 45 |
|
| 46 | public static TagEntry optionalElement(final Identifier id) {
|
| 47 | return new TagEntry(id, false, false);
|
| 48 | }
|
| 49 |
|
| 50 | public static TagEntry tag(final Identifier id) {
|
| 51 | return new TagEntry(id, true, true);
|
| 52 | }
|
| 53 |
|
| 54 | public static TagEntry optionalTag(final Identifier id) {
|
| 55 | return new TagEntry(id, true, false);
|
| 56 | }
|
| 57 |
|
| 58 | public <T> boolean build(final TagEntry.Lookup<T> lookup, final Consumer<T> output) {
|
| 59 | if (this.tag) {
|
| 60 | Collection<T> result = lookup.tag(this.id);
|
| 61 | if (result == null) {
|
| 62 | return !this.required;
|
| 63 | }
|
| 64 |
|
| 65 | result.forEach(output);
|
| 66 | } else {
|
| 67 | T result = lookup.element(this.id, this.required);
|
| 68 | if (result == null) {
|
| 69 | return !this.required;
|
| 70 | }
|
| 71 |
|
| 72 | output.accept(result);
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | public void visitRequiredDependencies(final Consumer<Identifier> output) {
|
| 79 | if (this.tag && this.required) {
|
| 80 | output.accept(this.id);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public void visitOptionalDependencies(final Consumer<Identifier> output) {
|
| 85 | if (this.tag && !this.required) {
|
| 86 | output.accept(this.id);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public boolean verifyIfPresent(final Predicate<Identifier> elementCheck, final Predicate<Identifier> tagCheck) {
|
| 91 | return !this.required || (this.tag ? tagCheck : elementCheck).test(this.id);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public String toString() {
|
| 96 | StringBuilder result = new StringBuilder();
|
| 97 | if (this.tag) {
|
| 98 | result.append('#');
|
| 99 | }
|
| 100 |
|
| 101 | result.append(this.id);
|
| 102 | if (!this.required) {
|
| 103 | result.append('?');
|
| 104 | }
|
| 105 |
|
| 106 | return result.toString();
|
| 107 | }
|
| 108 |
|
| 109 | public interface Lookup<T> {
|
| 110 | @Nullable T element(Identifier key, boolean required);
|
| 111 |
|
| 112 | @Nullable Collection<T> tag(Identifier key);
|
| 113 | }
|
| 114 | }
|
| 115 |
|