WickedProduction
9/27/2019 - 11:18 PM

datahandler

package me.kodysimpson.quartermaster.utils;

import me.kodysimpson.quartermaster.QuarterMaster;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;

import java.io.File;
import java.util.UUID;

public class DataHandler {

    //An easy way to access and manage player config files

    QuarterMaster plugin;
    Player p;
    UUID uuid;
    File userFile;
    FileConfiguration userConfig;

    public DataHandler(Player p){
        this.p = p;
        this.plugin = QuarterMaster.getPlugin();
        this.uuid = p.getUniqueId();
        userFile = new File(plugin.getDataFolder(), uuid.toString() + ".yml");
        userConfig = YamlConfiguration.loadConfiguration(userFile);
    }

    public void createPlayerFile(){
        if ( !(userFile.exists()) ) { //If file does not exist already, create one
            try {
                YamlConfiguration UserConfig = YamlConfiguration.loadConfiguration(userFile);

                UserConfig.set("Player.Info.name", p.getName());
                UserConfig.set("Player.Info.uuid", p.getUniqueId().toString());
                UserConfig.set("Player.Info.ip", p.getAddress().getAddress().getHostAddress());

                UserConfig.set("Player.LockData.locks-count", 0);

                UserConfig.save(userFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public FileConfiguration getUserFile(){
        return userConfig;
    }


    public void saveUserFile() {
        try {
            getUserFile().save(userFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}