Solved Player.yml help

Discussion in 'Plugin Development' started by jakemaster2003, Oct 22, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    jakemaster2003

    Dear Bukkiters,

    i am making a custom plugin for my server but i have a problem.
    I am able to create .yml files for all players but when i try to edit it
    it doesn't wanna work :/

    FileManger:

    Code:
    package me.nl.YourPalJake.MagicalMC;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    
    import java.io.File;
    import java.util.UUID;
    
    public class FileManager implements Listener{
    
        File pdfolder;
        File pfile;
        FileConfiguration playerfile;
    
    
        public void Setup(Plugin p){
            if(!p.getDataFolder().exists()){
                p.getDataFolder().mkdir();
            }
            pdfolder = new File(p.getDataFolder(),  File.separator + "playerData");
            if(!pdfolder.exists()){
                try{
                    pdfolder.mkdir();
                }catch(Exception e){
                    e.printStackTrace();
                    Bukkit.getLogger().severe("Could not create playerData folder!");
                }
            }
    
    
        }
    
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            UUID uuid = p.getUniqueId();
            pdfolder = new File(Bukkit.getPluginManager().getPlugin("MagicalMC").getDataFolder(),  File.separator + "playerData");
            pfile = new File(pdfolder, File.separator + uuid + ".yml");
            FileConfiguration playerfile = new YamlConfiguration().loadConfiguration(pfile);
    
            if(!pfile.exists()){
                try{
                    pfile.createNewFile();
    
    
                }catch (Exception error){
                    error.printStackTrace();
                    Bukkit.getServer().getLogger().severe("Could not create a playerfile for name: " + p.getName() + " uuid: " + uuid);
    
                }
            }
            String name = "name";
            String balance = "magicalcoins-balance";
            getPlayerFile(uuid).addDefault(name, p.getName());
            savePlayerFile(uuid);
            reloadPlayerFile(uuid);
            if(!p.hasPlayedBefore()){
                getPlayerFile(uuid).addDefault(balance, 10);
                getPlayerFile(uuid).options().copyDefaults(true);
                savePlayerFile(uuid);
                reloadPlayerFile(uuid);
                return;
            }
    
    
    
        }
    
    
        public FileConfiguration getPlayerFile(UUID uuid){
            pdfolder = new File(Bukkit.getPluginManager().getPlugin("MagicalMC").getDataFolder(),  File.separator + "playerData");
            pfile = new File(pdfolder, File.separator + uuid + ".yml");
            FileConfiguration playerfile = new YamlConfiguration().loadConfiguration(pfile);
            return playerfile;
        }
    
        public void savePlayerFile(UUID uuid){
            pdfolder = new File(Bukkit.getPluginManager().getPlugin("MagicalMC").getDataFolder(),  File.separator + "playerData");
            pfile = new File(pdfolder, File.separator + uuid + ".yml");
            FileConfiguration playerfile = new YamlConfiguration().loadConfiguration(pfile);
            try{
                playerfile.save(pfile);
            }catch(Exception e){
                e.printStackTrace();
    
            }
    
        }
        public void reloadPlayerFile(UUID uuid){
            pdfolder = new File(Bukkit.getPluginManager().getPlugin("MagicalMC").getDataFolder(),  File.separator + "playerData");
            pfile = new File(pdfolder, File.separator + uuid + ".yml");
            FileConfiguration playerfile = new YamlConfiguration().loadConfiguration(pfile);
    
        }
    }

    And it also doesn't show any errors can someone help me>?
     
  2. Offline

    Caderape2

    @jakemaster2003
    - File.separator is used before the comma for delimit two folders in the path.
    - The datafolder is created when the plugin is enabled. No need to check if it exists.
    - You have a wrong usage of File constructor. It's 'new File(path, file)'
    For create a folder:
    For create a file in a new folder:
    QUOTE]File file = new File(getDataFolder() + File.Separator + "FolderName", "myFile.yml");[/QUOTE]

    the reason why the file doesn't save, it's because you are not saving the current instance of FileConfiguration where you did modifications
    .
    Here you have two differents instances of the configuration. You set in getPlayerFile() the value, and you create a new empty instance in savePlayerFile.
     
  3. Offline

    jakemaster2003

    Okay so i changed that path location how you said it but how do i load 1 instance and is it possible with multiple methods>? @Caderape2
     
    Last edited: Oct 23, 2017
  4. Offline

    Caderape2

    @jakemaster2003 You will have to pass the instance used for modification in the paramater of another method.
    Like 'savePlayerFile(UUID uuid, FileConfiguration config);'
    then create the file and use the config.save(File)
     
  5. Offline

    jakemaster2003

    @Caderape2 When i wanna use these methods in another class then i cannot define a file ;/
     
Thread Status:
Not open for further replies.

Share This Page