package net.minecraft.world.level.storage;

import org.apache.logging.log4j.LogManager;
import javax.annotation.Nullable;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.util.datafix.DataFixTypes;
import java.io.InputStream;
import java.io.FileInputStream;
import net.minecraft.Util;
import java.io.OutputStream;
import net.minecraft.nbt.NbtIo;
import java.io.FileOutputStream;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import com.mojang.datafixers.DataFixer;
import java.io.File;
import org.apache.logging.log4j.Logger;

public class PlayerDataStorage {
    private static final Logger LOGGER;
    private final File playerDir;
    protected final DataFixer fixerUpper;
    
    public PlayerDataStorage(final LevelStorageSource.LevelStorageAccess a, final DataFixer dataFixer) {
        this.fixerUpper = dataFixer;
        (this.playerDir = a.getLevelPath(LevelResource.PLAYER_DATA_DIR).toFile()).mkdirs();
    }
    
    public void save(final Player bec) {
        try {
            final CompoundTag le3 = bec.saveWithoutId(new CompoundTag());
            final File file4 = File.createTempFile(bec.getStringUUID() + "-", ".dat", this.playerDir);
            NbtIo.writeCompressed(le3, new FileOutputStream(file4));
            final File file5 = new File(this.playerDir, bec.getStringUUID() + ".dat");
            final File file6 = new File(this.playerDir, bec.getStringUUID() + ".dat_old");
            Util.safeReplaceFile(file5, file4, file6);
        }
        catch (Exception exception3) {
            PlayerDataStorage.LOGGER.warn("Failed to save player data for {}", bec.getName().getString());
        }
    }
    
    @Nullable
    public CompoundTag load(final Player bec) {
        CompoundTag le3 = null;
        try {
            final File file4 = new File(this.playerDir, bec.getStringUUID() + ".dat");
            if (file4.exists() && file4.isFile()) {
                le3 = NbtIo.readCompressed(new FileInputStream(file4));
            }
        }
        catch (Exception exception4) {
            PlayerDataStorage.LOGGER.warn("Failed to load player data for {}", bec.getName().getString());
        }
        if (le3 != null) {
            final int integer4 = le3.contains("DataVersion", 3) ? le3.getInt("DataVersion") : -1;
            bec.load(NbtUtils.update(this.fixerUpper, DataFixTypes.PLAYER, le3, integer4));
        }
        return le3;
    }
    
    public String[] getSeenPlayers() {
        String[] arr2 = this.playerDir.list();
        if (arr2 == null) {
            arr2 = new String[0];
        }
        for (int integer3 = 0; integer3 < arr2.length; ++integer3) {
            if (arr2[integer3].endsWith(".dat")) {
                arr2[integer3] = arr2[integer3].substring(0, arr2[integer3].length() - 4);
            }
        }
        return arr2;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
