/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.serialization.Codec * com.mojang.serialization.DataResult * com.mojang.serialization.DynamicOps * com.mojang.serialization.JavaOps * com.mojang.serialization.Lifecycle */ package net.minecraft.util; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import com.mojang.serialization.DynamicOps; import com.mojang.serialization.JavaOps; import com.mojang.serialization.Lifecycle; import java.util.HashMap; import java.util.Map; import java.util.Optional; import net.minecraft.core.Holder; import net.minecraft.core.HolderGetter; import net.minecraft.core.HolderLookup; import net.minecraft.core.HolderOwner; import net.minecraft.core.HolderSet; import net.minecraft.core.Registry; import net.minecraft.resources.RegistryOps; import net.minecraft.resources.ResourceKey; import net.minecraft.tags.TagKey; import net.minecraft.util.RegistryContextSwapper; public class PlaceholderLookupProvider implements HolderGetter.Provider { private final HolderLookup.Provider context; private final UniversalLookup lookup = new UniversalLookup(); private final Map, Holder.Reference> holders = new HashMap, Holder.Reference>(); private final Map, HolderSet.Named> holderSets = new HashMap, HolderSet.Named>(); public PlaceholderLookupProvider(HolderLookup.Provider context) { this.context = context; } @Override public Optional> lookup(ResourceKey> key) { return Optional.of(this.lookup.castAsLookup()); } public RegistryOps createSerializationContext(DynamicOps parent) { return RegistryOps.create(parent, new RegistryOps.RegistryInfoLookup(){ @Override public Optional> lookup(ResourceKey> registryKey) { return PlaceholderLookupProvider.this.context.lookup(registryKey).map(RegistryOps.RegistryInfo::fromRegistryLookup).or(() -> Optional.of(new RegistryOps.RegistryInfo(PlaceholderLookupProvider.this.lookup.castAsOwner(), PlaceholderLookupProvider.this.lookup.castAsLookup(), Lifecycle.experimental()))); } }); } public RegistryContextSwapper createSwapper() { return new RegistryContextSwapper(){ @Override public DataResult swapTo(Codec codec, T value, HolderLookup.Provider newContext) { return codec.encodeStart(PlaceholderLookupProvider.this.createSerializationContext(JavaOps.INSTANCE), value).flatMap(v -> codec.parse(newContext.createSerializationContext(JavaOps.INSTANCE), v)); } }; } public boolean hasRegisteredPlaceholders() { return !this.holders.isEmpty() || !this.holderSets.isEmpty(); } private class UniversalLookup implements HolderGetter, HolderOwner { private UniversalLookup() { } @Override public Optional> get(ResourceKey id) { return Optional.of(this.getOrCreate(id)); } @Override public Holder.Reference getOrThrow(ResourceKey id) { return this.getOrCreate(id); } private Holder.Reference getOrCreate(ResourceKey id) { return PlaceholderLookupProvider.this.holders.computeIfAbsent(id, k -> Holder.Reference.createStandAlone(this, k)); } @Override public Optional> get(TagKey id) { return Optional.of(this.getOrCreate(id)); } @Override public HolderSet.Named getOrThrow(TagKey id) { return this.getOrCreate(id); } private HolderSet.Named getOrCreate(TagKey id) { return PlaceholderLookupProvider.this.holderSets.computeIfAbsent(id, k -> HolderSet.emptyNamed(this, k)); } public HolderGetter castAsLookup() { return this; } public HolderOwner castAsOwner() { return this; } } }