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

import net.minecraft.world.level.levelgen.feature.Feature;
import org.apache.logging.log4j.LogManager;
import javax.annotation.Nullable;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.chunk.ChunkGenerator;
import java.util.Locale;
import net.minecraft.core.Registry;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import org.apache.logging.log4j.Logger;

public class StructureFeatureIO {
    private static final Logger LOGGER;
    public static final StructureFeature<?> MINESHAFT;
    public static final StructureFeature<?> PILLAGER_OUTPOST;
    public static final StructureFeature<?> NETHER_FORTRESS;
    public static final StructureFeature<?> STRONGHOLD;
    public static final StructureFeature<?> JUNGLE_PYRAMID;
    public static final StructureFeature<?> OCEAN_RUIN;
    public static final StructureFeature<?> DESERT_PYRAMID;
    public static final StructureFeature<?> IGLOO;
    public static final StructureFeature<?> SWAMP_HUT;
    public static final StructureFeature<?> OCEAN_MONUMENT;
    public static final StructureFeature<?> END_CITY;
    public static final StructureFeature<?> WOODLAND_MANSION;
    public static final StructureFeature<?> BURIED_TREASURE;
    public static final StructureFeature<?> SHIPWRECK;
    public static final StructureFeature<?> VILLAGE;
    
    private static StructureFeature<?> register(final String string, final StructureFeature<?> cfl) {
        return Registry.<StructureFeature<?>>register(Registry.STRUCTURE_FEATURE, string.toLowerCase(Locale.ROOT), cfl);
    }
    
    public static void bootstrap() {
    }
    
    @Nullable
    public static StructureStart loadStaticStart(final ChunkGenerator<?> caa, final StructureManager cmo, final CompoundTag jt) {
        final String string4 = jt.getString("id");
        if ("INVALID".equals(string4)) {
            return StructureStart.INVALID_START;
        }
        final StructureFeature<?> cfl5 = Registry.STRUCTURE_FEATURE.get(new ResourceLocation(string4.toLowerCase(Locale.ROOT)));
        if (cfl5 == null) {
            StructureFeatureIO.LOGGER.error("Unknown feature id: {}", string4);
            return null;
        }
        final int integer6 = jt.getInt("ChunkX");
        final int integer7 = jt.getInt("ChunkZ");
        final int integer8 = jt.getInt("references");
        final BoundingBox clb9 = jt.contains("BB") ? new BoundingBox(jt.getIntArray("BB")) : BoundingBox.getUnknownBox();
        final ListTag jz10 = jt.getList("Children", 10);
        try {
            final StructureStart clv11 = cfl5.getStartFactory().create(cfl5, integer6, integer7, clb9, integer8, caa.getSeed());
            for (int integer9 = 0; integer9 < jz10.size(); ++integer9) {
                final CompoundTag jt2 = jz10.getCompound(integer9);
                final String string5 = jt2.getString("id");
                final StructurePieceType cfm15 = Registry.STRUCTURE_PIECE.get(new ResourceLocation(string5.toLowerCase(Locale.ROOT)));
                if (cfm15 == null) {
                    StructureFeatureIO.LOGGER.error("Unknown structure piece id: {}", string5);
                }
                else {
                    try {
                        final StructurePiece clu16 = cfm15.load(cmo, jt2);
                        clv11.pieces.add(clu16);
                    }
                    catch (Exception exception16) {
                        StructureFeatureIO.LOGGER.error("Exception loading structure piece with id {}", string5, exception16);
                    }
                }
            }
            return clv11;
        }
        catch (Exception exception17) {
            StructureFeatureIO.LOGGER.error("Failed Start with id {}", string4, exception17);
            return null;
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
        MINESHAFT = register("Mineshaft", Feature.MINESHAFT);
        PILLAGER_OUTPOST = register("Pillager_Outpost", Feature.PILLAGER_OUTPOST);
        NETHER_FORTRESS = register("Fortress", Feature.NETHER_BRIDGE);
        STRONGHOLD = register("Stronghold", Feature.STRONGHOLD);
        JUNGLE_PYRAMID = register("Jungle_Pyramid", Feature.JUNGLE_TEMPLE);
        OCEAN_RUIN = register("Ocean_Ruin", Feature.OCEAN_RUIN);
        DESERT_PYRAMID = register("Desert_Pyramid", Feature.DESERT_PYRAMID);
        IGLOO = register("Igloo", Feature.IGLOO);
        SWAMP_HUT = register("Swamp_Hut", Feature.SWAMP_HUT);
        OCEAN_MONUMENT = register("Monument", Feature.OCEAN_MONUMENT);
        END_CITY = register("EndCity", Feature.END_CITY);
        WOODLAND_MANSION = register("Mansion", Feature.WOODLAND_MANSION);
        BURIED_TREASURE = register("Buried_Treasure", Feature.BURIED_TREASURE);
        SHIPWRECK = register("Shipwreck", Feature.SHIPWRECK);
        VILLAGE = register("Village", Feature.VILLAGE);
    }
}
