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

personthecat.catlib.mixin.forge.MojangRegistryMixin Maven / Gradle / Ivy

package personthecat.catlib.mixin.forge;

import com.mojang.serialization.Lifecycle;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import personthecat.catlib.event.LibEvent;
import personthecat.catlib.registry.MojangRegistryHandle;
import personthecat.catlib.event.registry.RegistryAddedCallback;
import personthecat.catlib.event.registry.forge.RegistryEventAccessor;

@Mixin(MappedRegistry.class)
public abstract class MojangRegistryMixin extends Registry implements RegistryEventAccessor {

    @Nullable
    private LibEvent> registryAddedEvent = null;

    protected MojangRegistryMixin(final ResourceKey> key, final Lifecycle lifecycle) {
        super(key, lifecycle);
    }

    @Inject(method = "register", at = @At("RETURN"))
    public  void onRegister(ResourceKey key, V v, Lifecycle lifecycle, CallbackInfoReturnable ci) {
        if (this.registryAddedEvent != null) {
            this.registryAddedEvent.invoker().onRegistryAdded(new MojangRegistryHandle<>(this), key.m_135782_(), v);
        }
    }

    @NotNull
    @Override
    public LibEvent> getRegistryAddedEvent() {
        if (this.registryAddedEvent == null) {
            this.registryAddedEvent = LibEvent.create(callbacks -> (handle, id, t) ->
                callbacks.forEach(c -> c.onRegistryAdded(handle, id, t)));
        }
        return this.registryAddedEvent;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy