All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.registry.MojangRegistryHandle Maven / Gradle / Ivy

package personthecat.catlib.registry;

import com.mojang.serialization.Lifecycle;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.WritableRegistry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import personthecat.catlib.mixin.MappedRegistryAccessor;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Stream;

public class MojangRegistryHandle implements RegistryHandle {

    private final Registry registry;

    public MojangRegistryHandle(final Registry registry) {
        this.registry = registry;
    }

    public Registry getRegistry() {
        return this.registry;
    }

    @Override
    public @Nullable ResourceLocation getKey(final T t) {
        return this.registry.m_7981_(t);
    }

    @Override
    public @Nullable T lookup(final ResourceLocation id) {
        return this.registry.m_7745_(id);
    }

    @Override
    public  V register(final ResourceLocation id, final V v) {
        ((WritableRegistry) this.registry).m_203505_(ResourceKey.m_135785_(this.registry.m_123023_(), id), v, Lifecycle.stable());
        return v;
    }

    @Override
    public void forEach(final BiConsumer f) {
        for (final Map.Entry, T> entry : new HashSet<>(this.registry.m_6579_())) {
            f.accept(entry.getKey().m_135782_(), entry.getValue());
        }
    }

    @Override
    public boolean isRegistered(final ResourceLocation id) {
        return this.registry.m_7804_(id);
    }

    @Override
    public @Nullable Holder getHolder(final ResourceLocation id) {
        return this.registry.m_203636_(ResourceKey.m_135785_(this.registry.m_123023_(), id)).orElse(null);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map, HolderSet.Named> getTags() {
        if (this.registry instanceof MappedRegistry mapped) {
            return ((MappedRegistryAccessor) mapped).getTagsDirectly();
        }
        throw new UnsupportedOperationException("Unsupported registry in handle: " + this.registry.getClass());
    }

    @Override
    public ResourceKey> key() {
        return this.registry.m_123023_();
    }

    @Override
    public Set keySet() {
        return this.registry.m_6566_();
    }

    @Override
    public Set, T>> entrySet() {
        return this.registry.m_6579_();
    }

    @Override
    public @NotNull Iterator iterator() {
        return this.registry.iterator();
    }

    @Override
    public Stream stream() {
        return this.registry.m_123024_();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy