| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Iterator;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.Spliterator;
|
| 11 | import java.util.function.Function;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.tags.TagKey;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public interface HolderSet<T> extends Iterable<Holder<T>> {
|
| 19 | Stream<Holder<T>> stream();
|
| 20 |
|
| 21 | int size();
|
| 22 |
|
| 23 | boolean isBound();
|
| 24 |
|
| 25 | Either<TagKey<T>, List<Holder<T>>> unwrap();
|
| 26 |
|
| 27 | Optional<Holder<T>> getRandomElement(RandomSource random);
|
| 28 |
|
| 29 | Holder<T> get(int index);
|
| 30 |
|
| 31 | boolean contains(final Holder<T> value);
|
| 32 |
|
| 33 | boolean canSerializeIn(HolderOwner<T> owner);
|
| 34 |
|
| 35 | Optional<TagKey<T>> unwrapKey();
|
| 36 |
|
| 37 | @Deprecated
|
| 38 | @VisibleForTesting
|
| 39 | static <T> HolderSet.Named<T> emptyNamed(final HolderOwner<T> owner, final TagKey<T> key) {
|
| 40 | return new HolderSet.Named<T>(owner, key) {
|
| 41 | @Override
|
| 42 | protected List<Holder<T>> contents() {
|
| 43 | throw new UnsupportedOperationException("Tag " + this.key() + " can't be dereferenced during construction");
|
| 44 | }
|
| 45 | };
|
| 46 | }
|
| 47 |
|
| 48 | static <T> HolderSet<T> empty() {
|
| 49 | return (HolderSet<T>)HolderSet.Direct.EMPTY;
|
| 50 | }
|
| 51 |
|
| 52 | @SafeVarargs
|
| 53 | static <T> HolderSet.Direct<T> direct(final Holder<T>... values) {
|
| 54 | return new HolderSet.Direct<>(List.of(values));
|
| 55 | }
|
| 56 |
|
| 57 | static <T> HolderSet.Direct<T> direct(final List<? extends Holder<T>> values) {
|
| 58 | return new HolderSet.Direct<>(List.copyOf(values));
|
| 59 | }
|
| 60 |
|
| 61 | @SafeVarargs
|
| 62 | static <E, T> HolderSet.Direct<T> direct(final Function<E, Holder<T>> holderGetter, final E... elements) {
|
| 63 | return direct(Stream.of(elements).map(holderGetter).toList());
|
| 64 | }
|
| 65 |
|
| 66 | static <E, T> HolderSet.Direct<T> direct(final Function<E, Holder<T>> holderGetter, final Collection<E> elements) {
|
| 67 | return direct(elements.stream().map(holderGetter).toList());
|
| 68 | }
|
| 69 |
|
| 70 | final class Direct<T> extends HolderSet.ListBacked<T> {
|
| 71 | private static final HolderSet.Direct<?> EMPTY = new HolderSet.Direct(List.of());
|
| 72 | private final List<Holder<T>> contents;
|
| 73 | private @Nullable Set<Holder<T>> contentsSet;
|
| 74 |
|
| 75 | private Direct(final List<Holder<T>> contents) {
|
| 76 | this.contents = contents;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected List<Holder<T>> contents() {
|
| 81 | return this.contents;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public boolean isBound() {
|
| 86 | return true;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public Either<TagKey<T>, List<Holder<T>>> unwrap() {
|
| 91 | return Either.right(this.contents);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public Optional<TagKey<T>> unwrapKey() {
|
| 96 | return Optional.empty();
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean contains(final Holder<T> value) {
|
| 101 | if (this.contentsSet == null) {
|
| 102 | this.contentsSet = Set.copyOf(this.contents);
|
| 103 | }
|
| 104 |
|
| 105 | return this.contentsSet.contains(value);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public String toString() {
|
| 110 | return "DirectSet[" + this.contents + "]";
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean equals(final Object obj) {
|
| 115 | return this == obj ? true : obj instanceof HolderSet.Direct<?> direct && this.contents.equals(direct.contents);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public int hashCode() {
|
| 120 | return this.contents.hashCode();
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | abstract class ListBacked<T> implements HolderSet<T> {
|
| 125 | protected abstract List<Holder<T>> contents();
|
| 126 |
|
| 127 | @Override
|
| 128 | public int size() {
|
| 129 | return this.contents().size();
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public Spliterator<Holder<T>> spliterator() {
|
| 134 | return this.contents().spliterator();
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public Iterator<Holder<T>> iterator() {
|
| 139 | return this.contents().iterator();
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public Stream<Holder<T>> stream() {
|
| 144 | return this.contents().stream();
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public Optional<Holder<T>> getRandomElement(final RandomSource random) {
|
| 149 | return Util.getRandomSafe(this.contents(), random);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public Holder<T> get(final int index) {
|
| 154 | return this.contents().get(index);
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public boolean canSerializeIn(final HolderOwner<T> owner) {
|
| 159 | return true;
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | class Named<T> extends HolderSet.ListBacked<T> {
|
| 164 | private final HolderOwner<T> owner;
|
| 165 | private final TagKey<T> key;
|
| 166 | private @Nullable List<Holder<T>> contents;
|
| 167 |
|
| 168 | Named(final HolderOwner<T> owner, final TagKey<T> key) {
|
| 169 | this.owner = owner;
|
| 170 | this.key = key;
|
| 171 | }
|
| 172 |
|
| 173 | void bind(final List<Holder<T>> contents) {
|
| 174 | this.contents = List.copyOf(contents);
|
| 175 | }
|
| 176 |
|
| 177 | public TagKey<T> key() {
|
| 178 | return this.key;
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | protected List<Holder<T>> contents() {
|
| 183 | if (this.contents == null) {
|
| 184 | throw new IllegalStateException("Trying to access unbound tag '" + this.key + "' from registry " + this.owner);
|
| 185 | } else {
|
| 186 | return this.contents;
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public boolean isBound() {
|
| 192 | return this.contents != null;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public Either<TagKey<T>, List<Holder<T>>> unwrap() {
|
| 197 | return Either.left(this.key);
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public Optional<TagKey<T>> unwrapKey() {
|
| 202 | return Optional.of(this.key);
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public boolean contains(final Holder<T> value) {
|
| 207 | return value.is(this.key);
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public String toString() {
|
| 212 | return "NamedSet(" + this.key + ")[" + this.contents + "]";
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public boolean canSerializeIn(final HolderOwner<T> context) {
|
| 217 | return context.canSerialize(this.owner);
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|