| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.DynamicOps;
|
| 6 | import com.mojang.serialization.JavaOps;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.HolderGetter;
|
| 12 | import net.minecraft.core.HolderLookup;
|
| 13 | import net.minecraft.core.HolderSet;
|
| 14 | import net.minecraft.core.Registry;
|
| 15 | import net.minecraft.resources.RegistryOps;
|
| 16 | import net.minecraft.resources.ResourceKey;
|
| 17 | import net.minecraft.tags.TagKey;
|
| 18 |
|
| 19 | public class PlaceholderLookupProvider implements HolderGetter.Provider {
|
| 20 | private final HolderLookup.Provider context;
|
| 21 | private final PlaceholderLookupProvider.UniversalLookup lookup = new PlaceholderLookupProvider.UniversalLookup();
|
| 22 | private final Map<ResourceKey<Object>, Holder.Reference<Object>> holders = new HashMap<>();
|
| 23 | private final Map<TagKey<Object>, HolderSet.Named<Object>> holderSets = new HashMap<>();
|
| 24 |
|
| 25 | public PlaceholderLookupProvider(final HolderLookup.Provider context) {
|
| 26 | this.context = context;
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public <T> Optional<? extends HolderGetter<T>> lookup(final ResourceKey<? extends Registry<? extends T>> key) {
|
| 31 | return Optional.of(this.lookup.castAsLookup());
|
| 32 | }
|
| 33 |
|
| 34 | public <V> RegistryOps<V> createSerializationContext(final DynamicOps<V> parent) {
|
| 35 | return RegistryOps.create(parent, new RegistryOps.RegistryInfoLookup() {
|
| 36 | @Override
|
| 37 | public <T> Optional<HolderGetter<T>> lookup(final ResourceKey<? extends Registry<? extends T>> registryKey) {
|
| 38 | Optional<HolderGetter<T>> result = PlaceholderLookupProvider.this.context.lookup(registryKey).map(e -> (HolderGetter<T>)e);
|
| 39 | return result.or(() -> Optional.of(PlaceholderLookupProvider.this.lookup.castAsLookup()));
|
| 40 | }
|
| 41 | });
|
| 42 | }
|
| 43 |
|
| 44 | public RegistryContextSwapper createSwapper() {
|
| 45 | return new RegistryContextSwapper() {
|
| 46 | @Override
|
| 47 | public <T> DataResult<T> swapTo(final Codec<T> codec, final T value, final HolderLookup.Provider newContext) {
|
| 48 | return codec.encodeStart(PlaceholderLookupProvider.this.createSerializationContext(JavaOps.INSTANCE), value)
|
| 49 | .flatMap(v -> codec.parse(newContext.createSerializationContext(JavaOps.INSTANCE), v));
|
| 50 | }
|
| 51 | };
|
| 52 | }
|
| 53 |
|
| 54 | public boolean hasRegisteredPlaceholders() {
|
| 55 | return !this.holders.isEmpty() || !this.holderSets.isEmpty();
|
| 56 | }
|
| 57 |
|
| 58 | private class UniversalLookup implements HolderGetter<Object> {
|
| 59 | @Override
|
| 60 | public Optional<Holder.Reference<Object>> get(final ResourceKey<Object> id) {
|
| 61 | return Optional.of(this.getOrCreate(id));
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Holder.Reference<Object> getOrThrow(final ResourceKey<Object> id) {
|
| 66 | return this.getOrCreate(id);
|
| 67 | }
|
| 68 |
|
| 69 | private Holder.Reference<Object> getOrCreate(final ResourceKey<Object> id) {
|
| 70 | return PlaceholderLookupProvider.this.holders.computeIfAbsent(id, k -> Holder.Reference.createStandAlone(this, (ResourceKey<Object>)k));
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public Optional<HolderSet.Named<Object>> get(final TagKey<Object> id) {
|
| 75 | return Optional.of(this.getOrCreate(id));
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public HolderSet.Named<Object> getOrThrow(final TagKey<Object> id) {
|
| 80 | return this.getOrCreate(id);
|
| 81 | }
|
| 82 |
|
| 83 | private HolderSet.Named<Object> getOrCreate(final TagKey<Object> id) {
|
| 84 | return PlaceholderLookupProvider.this.holderSets.computeIfAbsent(id, k -> HolderSet.emptyNamed(this, (TagKey<Object>)k));
|
| 85 | }
|
| 86 |
|
| 87 | public <T> HolderGetter<T> castAsLookup() {
|
| 88 | return this;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|