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

personthecat.catlib.event.registry.forge.RegistryAddedEventImpl Maven / Gradle / Ivy

package personthecat.catlib.event.registry.forge;

import lombok.extern.log4j.Log4j2;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import personthecat.catlib.event.LibEvent;
import personthecat.catlib.event.registry.*;
import personthecat.catlib.registry.forge.ForgeRegistryHandle;
import personthecat.catlib.registry.RegistryHandle;
import personthecat.catlib.registry.RegistryUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Log4j2
public class RegistryAddedEventImpl {

    private static final Map, LibEvent>> DYNAMIC_EVENT_MAP = new ConcurrentHashMap<>();

    @SuppressWarnings("unchecked")
    public static  LibEvent> get(final ResourceKey> key) {
        final personthecat.catlib.registry.RegistryHandle handle = RegistryUtils.getHandle(key);
        final RegistryEventAccessor accessor = handle instanceof ForgeRegistryHandle
            ? (RegistryEventAccessor) ((ForgeRegistryHandle) handle).getRegistry()
            : (RegistryEventAccessor) ((personthecat.catlib.registry.MojangRegistryHandle) handle).getRegistry();
        return accessor.getRegistryAddedEvent();
    }

    public static  void withRetroactive(final ResourceKey> key, final RegistryAddedCallback f) {
        get(key).register(f);
        runRetroactively(key, f);
    }

    public static  void withDynamic(final ResourceKey> key, final RegistryAddedCallback f) {
        get(key).register(f);
        runDynamically(key, f);
    }

    public static  void exhaustive(final ResourceKey> key, final RegistryAddedCallback f) {
        get(key).register(f);
        runRetroactively(key, f);
        runDynamically(key, f);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void onRegistryAccess(final RegistryAccess registries) {
        DYNAMIC_EVENT_MAP.forEach((key, event) -> {
            final personthecat.catlib.registry.RegistryHandle registry = new personthecat.catlib.registry.MojangRegistryHandle(registries.m_175515_((ResourceKey) key));
            registry.forEach((id, t) -> ((LibEvent>) (Object) event).invoker()
                .onRegistryAdded(registry, id, t));
        });
    }

    private static  void runRetroactively(final ResourceKey> key, final RegistryAddedCallback f) {
        final RegistryHandle handle = RegistryUtils.getHandle(key);
        final boolean locked = handle instanceof ForgeRegistryHandle && ((ForgeRegistryHandle) handle).getRegistry().isLocked();
        if (locked) ((ForgeRegistryHandle) handle).getRegistry().unfreeze();
        handle.forEach((id, t) -> f.onRegistryAdded(handle, id, t));
        if (locked) ((ForgeRegistryHandle) handle).getRegistry().freeze();
    }

    @SuppressWarnings("unchecked")
    private static  void runDynamically(final ResourceKey> key, final RegistryAddedCallback f) {
        DYNAMIC_EVENT_MAP.computeIfAbsent(key, k -> (LibEvent>) (Object) newEvent()).register(f);
    }

    private static  LibEvent> newEvent() {
        return LibEvent.nonRecursive(callbacks -> (h, id, t) -> callbacks.forEach(c -> c.onRegistryAdded(h, id, t)));
    }
}