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

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 java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.Registry;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.DynamicOps;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;

public class WeightedConfiguredFeature<FC extends FeatureConfiguration> {
    public final ConfiguredFeature<FC, ?> feature;
    public final float chance;
    
    public WeightedConfiguredFeature(final ConfiguredFeature<FC, ?> cdc, final float float2) {
        this.feature = cdc;
        this.chance = float2;
    }
    
    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.FEATURE.getKey(this.feature.feature).toString()), dynamicOps.createString("config"), this.feature.config.<T>serialize(dynamicOps).getValue(), dynamicOps.createString("chance"), dynamicOps.createFloat(this.chance))));
    }
    
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk) {
        return this.feature.place(bjx, caa, random, fk);
    }
    
    public static <T> WeightedConfiguredFeature<?> deserialize(final Dynamic<T> dynamic) {
        return ConfiguredFeature.<T>deserialize(dynamic).weighted(dynamic.get("chance").asFloat(0.0f));
    }
}
