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

personthecat.catlib.registry.DynamicRegistryHandle 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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.minecraft.class_2378;
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 DynamicRegistryHandle implements RegistryHandle {

    private final Map>> synchronizer = new WeakHashMap<>();
    private volatile RegistryHandle wrapped;

    private DynamicRegistryHandle(final RegistryHandle wrapped) {
        this.wrapped = wrapped;
    }

    public static  DynamicRegistryHandle createHandle(final class_5321> key) {
        return new DynamicRegistryHandle<>(RegistryUtils.tryGetHandle(key).orElse(DummyRegistryHandle.getInstance()));
    }

    public synchronized void updateRegistry(final RegistryHandle updated) {
        this.wrapped = updated;
        this.synchronizer.forEach((mutex, listener) -> listener.accept(mutex, updated));
    }

    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
    public synchronized void listen(final Object mutex, final Consumer> listener) {
        this.synchronizer.put(mutex, (m, handle) -> {
            // Prevent this lambda from strongly referencing the mutex.
            synchronized (m) {
                listener.accept(handle);
            }
        });
    }

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

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

    @Override
    public  V register(final class_2960 id, final V v) {
        return this.wrapped.register(id, v);
    }

    @Override
    public void forEach(final BiConsumer f) {
        this.wrapped.forEach(f);
    }

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

    @Override
    public @Nullable class_6880 getHolder(final class_2960 id) {
        return this.wrapped.getHolder(id);
    }

    @Override
    public Map, class_6885.class_6888> getTags() {
        return this.wrapped.getTags();
    }

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

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

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

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

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

    @Override
    public int getId() {
        if (this.wrapped != null) return this.wrapped.getId();
        return RegistryHandle.super.getId();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy