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

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

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
package personthecat.catlib.registry;

import com.mojang.serialization.Lifecycle;
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;
import net.minecraft.class_2370;
import net.minecraft.class_2378;
import net.minecraft.class_2385;
import net.minecraft.class_2960;
import net.minecraft.class_5321;
import net.minecraft.class_6862;
import net.minecraft.class_6880;
import net.minecraft.class_6885;

public class MojangRegistryHandle implements RegistryHandle {

    private final class_2378 registry;

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

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

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

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

    @Override
    public  V register(final class_2960 id, final V v) {
        ((class_2385) this.registry).method_10272(class_5321.method_29179(this.registry.method_30517(), id), v, Lifecycle.stable());
        return v;
    }

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

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

    @Override
    public @Nullable class_6880 getHolder(final class_2960 id) {
        return this.registry.method_40264(class_5321.method_29179(this.registry.method_30517(), id)).orElse(null);
    }

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

    @Override
    public class_5321> key() {
        return this.registry.method_30517();
    }

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy