package net.minecraft.data.structures;

import net.minecraft.nbt.NbtUtils;
import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.util.datafix.DataFixers;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.nbt.CompoundTag;

public class StructureUpdater implements SnbtToNbt.Filter {
    @Override
    public CompoundTag apply(final String string, final CompoundTag jt) {
        if (string.startsWith("data/minecraft/structures/")) {
            return updateStructure(patchVersion(jt));
        }
        return jt;
    }
    
    private static CompoundTag patchVersion(final CompoundTag jt) {
        if (!jt.contains("DataVersion", 99)) {
            jt.putInt("DataVersion", 500);
        }
        return jt;
    }
    
    private static CompoundTag updateStructure(final CompoundTag jt) {
        final StructureTemplate cms2 = new StructureTemplate();
        cms2.load(NbtUtils.update(DataFixers.getDataFixer(), DataFixTypes.STRUCTURE, jt, jt.getInt("DataVersion")));
        return cms2.save(new CompoundTag());
    }
}
