package net.minecraft.world.level.levelgen.placement;

import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.Registry;
import com.mojang.datafixers.types.DynamicOps;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.levelgen.ChunkGeneratorSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.datafixers.Dynamic;
import net.minecraft.world.level.levelgen.feature.configurations.DecoratorConfiguration;

public class ConfiguredDecorator<DC extends DecoratorConfiguration> {
    public final FeatureDecorator<DC> decorator;
    public final DC config;
    
    public ConfiguredDecorator(final FeatureDecorator<DC> cka, final Dynamic<?> dynamic) {
        this((FeatureDecorator<DecoratorConfiguration>)cka, cka.createSettings(dynamic));
    }
    
    public ConfiguredDecorator(final FeatureDecorator<DC> cka, final DC cgk) {
        this.decorator = cka;
        this.config = cgk;
    }
    
    public <FC extends FeatureConfiguration, F extends Feature<FC>> boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final ConfiguredFeature<FC, F> cdc) {
        return this.decorator.<FC, F>placeFeature(bjx, caa, random, fk, this.config, cdc);
    }
    
    public <T> Dynamic<T> serialize(final DynamicOps<T> dynamicOps) {
        return (Dynamic<T>)new Dynamic((DynamicOps)dynamicOps, dynamicOps.createMap((Map)ImmutableMap.of(dynamicOps.createString("name"), dynamicOps.createString(Registry.DECORATOR.getKey(this.decorator).toString()), dynamicOps.createString("config"), this.config.<T>serialize(dynamicOps).getValue())));
    }
    
    public static <T> ConfiguredDecorator<?> deserialize(final Dynamic<T> dynamic) {
        final FeatureDecorator<? extends DecoratorConfiguration> cka2 = Registry.DECORATOR.get(new ResourceLocation(dynamic.get("name").asString("")));
        return new ConfiguredDecorator<>(cka2, dynamic.get("config").orElseEmptyMap());
    }
}
