All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.utils.DefaultPlayerDataSerializer Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.utils;

import cn.nukkit.Server;
import cn.nukkit.api.Since;
import com.google.common.base.Preconditions;

import java.io.*;
import java.util.Optional;
import java.util.UUID;

public class DefaultPlayerDataSerializer implements PlayerDataSerializer {
    private String dataPath;

    @Since("1.4.0.0-PN")
    public DefaultPlayerDataSerializer(Server server) {
        this(server.getDataPath());
    }

    @Since("1.4.0.0-PN")
    public DefaultPlayerDataSerializer(String dataPath) {
        this.dataPath = dataPath;
    }

    @Override
    public Optional read(String name, UUID uuid) throws IOException {
        String path = dataPath + "players/" + name + ".dat";
        File file = new File(path);
        if (!file.exists()) {
            return Optional.empty();
        }
        return Optional.of(new FileInputStream(file));

    }

    @Override
    public OutputStream write(String name, UUID uuid) throws IOException {
        Preconditions.checkNotNull(name, "name");
        String path = dataPath + "players/" + name + ".dat";
        File file = new File(path);
        return new FileOutputStream(file);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy