package net.minecraft.core;

import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.mojang.serialization.Lifecycle;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

public class DefaultedRegistry<T> extends MappedRegistry<T> {
    private final ResourceLocation defaultKey;
    private T defaultValue;
    
    public DefaultedRegistry(final String string, final ResourceKey<Registry<T>> ug, final Lifecycle lifecycle) {
        super(ug, lifecycle);
        this.defaultKey = new ResourceLocation(string);
    }
    
    @Override
    public <V extends T> V registerMapping(final int integer, final ResourceKey<T> ug, final V object) {
        if (this.defaultKey.equals(ug.location())) {
            this.defaultValue = object;
        }
        return super.<V>registerMapping(integer, ug, object);
    }
    
    @Override
    public int getId(@Nullable final T object) {
        final int integer3 = super.getId(object);
        return (integer3 == -1) ? super.getId(this.defaultValue) : integer3;
    }
    
    @Nonnull
    @Override
    public ResourceLocation getKey(final T object) {
        final ResourceLocation uh3 = super.getKey(object);
        return (uh3 == null) ? this.defaultKey : uh3;
    }
    
    @Nonnull
    @Override
    public T get(@Nullable final ResourceLocation uh) {
        final T object3 = super.get(uh);
        return (object3 == null) ? this.defaultValue : object3;
    }
    
    @Nonnull
    @Override
    public T byId(final int integer) {
        final T object3 = super.byId(integer);
        return (object3 == null) ? this.defaultValue : object3;
    }
    
    @Nonnull
    @Override
    public T getRandom(final Random random) {
        final T object3 = super.getRandom(random);
        return (object3 == null) ? this.defaultValue : object3;
    }
    
    public ResourceLocation getDefaultKey() {
        return this.defaultKey;
    }
}
