package net.minecraft.core;

import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;

public class DefaultedRegistry<T> extends MappedRegistry<T> {
    private final ResourceLocation defaultKey;
    private T defaultValue;
    
    public DefaultedRegistry(final String string) {
        this.defaultKey = new ResourceLocation(string);
    }
    
    @Override
    public <V extends T> V registerMapping(final int integer, final ResourceLocation sm, final V object) {
        if (this.defaultKey.equals(sm)) {
            this.defaultValue = object;
        }
        return super.<V>registerMapping(integer, sm, 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 sm3 = super.getKey(object);
        return (sm3 == null) ? this.defaultKey : sm3;
    }
    
    @Nonnull
    @Override
    public T get(@Nullable final ResourceLocation sm) {
        final T object3 = super.get(sm);
        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;
    }
}
