| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import com.mojang.serialization.Keyable;
|
| 8 | import com.mojang.serialization.Lifecycle;
|
| 9 | import java.util.Iterator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.Map.Entry;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import java.util.stream.StreamSupport;
|
| 16 | import net.fabricmc.fabric.api.event.registry.FabricRegistry;
|
| 17 | import net.minecraft.core.component.DataComponentLookup;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import net.minecraft.resources.ResourceKey;
|
| 20 | import net.minecraft.tags.TagKey;
|
| 21 | import net.minecraft.tags.TagLoader;
|
| 22 | import net.minecraft.util.ExtraCodecs;
|
| 23 | import net.minecraft.util.RandomSource;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public interface Registry<T> extends IdMap<T>, Keyable, HolderLookup.RegistryLookup<T>, FabricRegistry {
|
| 27 | @Override
|
| 28 | ResourceKey<? extends Registry<T>> key();
|
| 29 |
|
| 30 | default Codec<T> byNameCodec() {
|
| 31 | return this.referenceHolderWithLifecycle().flatComapMap(Holder.Reference::value, value -> this.safeCastToReference(this.wrapAsHolder((T)value)));
|
| 32 | }
|
| 33 |
|
| 34 | default Codec<Holder<T>> holderByNameCodec() {
|
| 35 | return this.referenceHolderWithLifecycle().flatComapMap(holder -> (Holder<T>)holder, this::safeCastToReference);
|
| 36 | }
|
| 37 |
|
| 38 | private Codec<Holder.Reference<T>> referenceHolderWithLifecycle() {
|
| 39 | Codec<Holder.Reference<T>> referenceCodec = Identifier.CODEC
|
| 40 | .comapFlatMap(
|
| 41 | name -> this.get(name).map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Unknown registry key in " + this.key() + ": " + name)),
|
| 42 | holder -> holder.key().identifier()
|
| 43 | );
|
| 44 | return ExtraCodecs.overrideLifecycle(referenceCodec, e -> this.registrationInfo(e.key()).map(RegistrationInfo::lifecycle).orElse(Lifecycle.experimental()));
|
| 45 | }
|
| 46 |
|
| 47 | private DataResult<Holder.Reference<T>> safeCastToReference(final Holder<T> holder) {
|
| 48 | return holder instanceof Holder.Reference<T> reference
|
| 49 | ? DataResult.success(reference)
|
| 50 | : DataResult.error(() -> "Unregistered holder in " + this.key() + ": " + holder);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | default <U> Stream<U> keys(final DynamicOps<U> ops) {
|
| 55 | return this.keySet().stream().map(k -> ops.createString(k.toString()));
|
| 56 | }
|
| 57 |
|
| 58 | @Nullable Identifier getKey(T thing);
|
| 59 |
|
| 60 | Optional<ResourceKey<T>> getResourceKey(T thing);
|
| 61 |
|
| 62 | @Override
|
| 63 | int getId(@Nullable T thing);
|
| 64 |
|
| 65 | @Nullable T getValue(@Nullable ResourceKey<T> key);
|
| 66 |
|
| 67 | @Nullable T getValue(@Nullable Identifier key);
|
| 68 |
|
| 69 | Optional<RegistrationInfo> registrationInfo(ResourceKey<T> element);
|
| 70 |
|
| 71 | default Optional<T> getOptional(final @Nullable Identifier key) {
|
| 72 | return Optional.ofNullable(this.getValue(key));
|
| 73 | }
|
| 74 |
|
| 75 | default Optional<T> getOptional(final @Nullable ResourceKey<T> key) {
|
| 76 | return Optional.ofNullable(this.getValue(key));
|
| 77 | }
|
| 78 |
|
| 79 | Optional<Holder.Reference<T>> getAny();
|
| 80 |
|
| 81 | default T getValueOrThrow(final ResourceKey<T> key) {
|
| 82 | T value = this.getValue(key);
|
| 83 | if (value == null) {
|
| 84 | throw new IllegalStateException("Missing key in " + this.key() + ": " + key);
|
| 85 | } else {
|
| 86 | return value;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | Set<Identifier> keySet();
|
| 91 |
|
| 92 | Set<Entry<ResourceKey<T>, T>> entrySet();
|
| 93 |
|
| 94 | Set<ResourceKey<T>> registryKeySet();
|
| 95 |
|
| 96 | Optional<Holder.Reference<T>> getRandom(RandomSource random);
|
| 97 |
|
| 98 | default Stream<T> stream() {
|
| 99 | return StreamSupport.stream(this.spliterator(), false);
|
| 100 | }
|
| 101 |
|
| 102 | boolean containsKey(Identifier key);
|
| 103 |
|
| 104 | boolean containsKey(ResourceKey<T> key);
|
| 105 |
|
| 106 | static <T> T register(final Registry<? super T> registry, final String name, final T value) {
|
| 107 | return register(registry, Identifier.parse(name), value);
|
| 108 | }
|
| 109 |
|
| 110 | static <V, T extends V> T register(final Registry<V> registry, final Identifier location, final T value) {
|
| 111 | return register(registry, ResourceKey.create(registry.key(), location), value);
|
| 112 | }
|
| 113 |
|
| 114 | static <V, T extends V> T register(final Registry<V> registry, final ResourceKey<V> key, final T value) {
|
| 115 | ((WritableRegistry)registry).register(key, (V)value, RegistrationInfo.BUILT_IN);
|
| 116 | return value;
|
| 117 | }
|
| 118 |
|
| 119 | static <R, T extends R> Holder.Reference<T> registerForHolder(final Registry<R> registry, final ResourceKey<R> key, final T value) {
|
| 120 | return ((WritableRegistry)registry).register(key, (R)value, RegistrationInfo.BUILT_IN);
|
| 121 | }
|
| 122 |
|
| 123 | static <R, T extends R> Holder.Reference<T> registerForHolder(final Registry<R> registry, final Identifier location, final T value) {
|
| 124 | return registerForHolder(registry, ResourceKey.create(registry.key(), location), value);
|
| 125 | }
|
| 126 |
|
| 127 | Registry<T> freeze();
|
| 128 |
|
| 129 | Holder.Reference<T> createIntrusiveHolder(T value);
|
| 130 |
|
| 131 | Optional<Holder.Reference<T>> get(int id);
|
| 132 |
|
| 133 | Optional<Holder.Reference<T>> get(Identifier id);
|
| 134 |
|
| 135 | Holder<T> wrapAsHolder(T value);
|
| 136 |
|
| 137 | default Iterable<Holder<T>> getTagOrEmpty(final TagKey<T> id) {
|
| 138 | return DataFixUtils.orElse(this.get(id), List.<T>of());
|
| 139 | }
|
| 140 |
|
| 141 | Stream<HolderSet.Named<T>> getTags();
|
| 142 |
|
| 143 | default IdMap<Holder<T>> asHolderIdMap() {
|
| 144 | return new IdMap<Holder<T>>() {
|
| 145 | public int getId(final Holder<T> thing) {
|
| 146 | return Registry.this.getId(thing.value());
|
| 147 | }
|
| 148 |
|
| 149 | public @Nullable Holder<T> byId(final int id) {
|
| 150 | return (Holder<T>)Registry.this.get(id).orElse(null);
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public int size() {
|
| 155 | return Registry.this.size();
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public Iterator<Holder<T>> iterator() {
|
| 160 | return Registry.this.listElements().map(e -> (Holder<T>)e).iterator();
|
| 161 | }
|
| 162 | };
|
| 163 | }
|
| 164 |
|
| 165 | Registry.PendingTags<T> prepareTagReload(TagLoader.LoadResult<T> tags);
|
| 166 |
|
| 167 | DataComponentLookup<T> componentLookup();
|
| 168 |
|
| 169 | interface PendingTags<T> {
|
| 170 | ResourceKey<? extends Registry<? extends T>> key();
|
| 171 |
|
| 172 | HolderLookup.RegistryLookup<T> lookup();
|
| 173 |
|
| 174 | void apply();
|
| 175 |
|
| 176 | int size();
|
| 177 | }
|
| 178 | }
|
| 179 |
|