package net.minecraft.core;

import java.util.function.Function;
import java.util.Objects;
import com.mojang.serialization.Lifecycle;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.dimension.DimensionType;
import java.util.Optional;
import net.minecraft.resources.ResourceKey;

public interface RegistryAccess {
     <E> Optional<WritableRegistry<E>> registry(final ResourceKey<Registry<E>> ug);
    
    Registry<DimensionType> dimensionTypes();
    
    default RegistryHolder builtin() {
        return DimensionType.registerBuiltin(new RegistryHolder());
    }
    
    public static final class RegistryHolder implements RegistryAccess {
        public static final Codec<RegistryHolder> CODEC;
        private final MappedRegistry<DimensionType> dimensionTypes;
        
        public RegistryHolder() {
            this(new MappedRegistry<DimensionType>(Registry.DIMENSION_TYPE_REGISTRY, Lifecycle.experimental()));
        }
        
        private RegistryHolder(final MappedRegistry<DimensionType> gh) {
            this.dimensionTypes = gh;
        }
        
        public void registerDimension(final ResourceKey<DimensionType> ug, final DimensionType cif) {
            this.dimensionTypes.<DimensionType>register(ug, cif);
        }
        
        @Override
        public <E> Optional<WritableRegistry<E>> registry(final ResourceKey<Registry<E>> ug) {
            if (Objects.equals(ug, Registry.DIMENSION_TYPE_REGISTRY)) {
                return Optional.<WritableRegistry<E>>of((WritableRegistry<E>)this.dimensionTypes);
            }
            return Optional.<WritableRegistry<E>>empty();
        }
        
        @Override
        public Registry<DimensionType> dimensionTypes() {
            return this.dimensionTypes;
        }
        
        static {
            CODEC = MappedRegistry.<DimensionType>networkCodec(Registry.DIMENSION_TYPE_REGISTRY, Lifecycle.experimental(), DimensionType.DIRECT_CODEC).xmap((Function)RegistryHolder::new, a -> a.dimensionTypes).fieldOf("dimension").codec();
        }
    }
}
