Need help with individual .yml (Config coding)

Discussion in 'Plugin Development' started by Suni, Feb 8, 2015.

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

    Suni

    I was wondering if I may get some help with coding my config. The problem is that it creates the .yml folder, but nothing in there is written and none of my methods work. I believe I might be going at this code the wrong way. Would I be having to do something with instances instead of using static? The code works if I were to write these codings within the listener:

    Working Test Listener
    Code:
    package com.suni.listeners;
    
    import java.io.File;
    
    import com.suni.utils.Config;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import com.suni.bukkit.MainPlugin;
    
    public class NewPlayerJoin implements Listener{
     
        @EventHandler
        public void createNewPlayerConfig(PlayerJoinEvent event){
            try {
                if(!MainPlugin.getInstance().getDataFolder().exists()){
                    MainPlugin.getInstance().getDataFolder().mkdirs();
                }
             
                File playerDir = new File(MainPlugin.getInstance().getDataFolder(), "Players");
               File file = new File(playerDir, event.getPlayer().getUniqueId() + ".yml");
               FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file);
             
             
                if(!file.exists()){
                    MainPlugin.getInstance().getLogger().info("Player file not found, creating!");
                    playerConfig.set("Name", event.getPlayer().getName());
                    playerConfig.save(file);
                }
                else {
                    MainPlugin.getInstance().getLogger().info(event.getPlayer().getName() + " found, loading!");
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
         
        }
     
    }
    
    
    I have a couple of objectives:
    1. Create a separate config class that can be used for other classes of my plugin.
    2. Make it so every unique player has their own config file.

    My test Listener
    Code:
    package com.suni.listeners;
    
    import com.suni.utils.Config;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import com.suni.bukkit.MainPlugin;
    
    public class NewPlayerJoin implements Listener{
     
        @EventHandler
        public void createNewPlayerConfig(PlayerJoinEvent event){
            try {
                if(!MainPlugin.getInstance().getDataFolder().exists()){
                    MainPlugin.getInstance().getDataFolder().mkdirs();
                }
             
                if(!Config.fileDirectory().exists()){
                    MainPlugin.getInstance().getLogger().info("Player file not found, creating!");
                    Config.getPlayerConfig().set("Name", event.getPlayer().getName());
                    Config.saveFile();
                }
                else {
                    MainPlugin.getInstance().getLogger().info(event.getPlayer().getName() + " found, loading!");
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
         
        }
     
    }
    
    My Config
    Code:
    package com.suni.utils;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import com.suni.bukkit.MainPlugin;
    
    public class Config {
     
        private static String uUID = "";
     
        public static String getPlayerUUID(){
            List<World> theWorlds = Bukkit.getServer().getWorlds();
            for(World worlds : theWorlds){
                List<Player> thePlayers = Bukkit.getServer().getWorld(worlds.getUID()).getPlayers();
                for(Player players : thePlayers){
                 
                UUID playerUUID = players.getUniqueId();
                uUID = playerUUID.toString();
        }
            }
         
            return uUID;
        }
    
        public static File fileDirectory(){
         
            File playerDir = new File(MainPlugin.getInstance().getDataFolder(), "Players");
            File file = new File(playerDir, getPlayerUUID() + ".yml");
    
            return file;
        }
     
        public static FileConfiguration getPlayerConfig(){
    
            FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(fileDirectory());
         
            return playerConfig;
         
        }
    
        public static void saveFile(){
         
            try {
             
                getPlayerConfig().save(fileDirectory());
             
            } catch (IOException e) {
    
                e.printStackTrace();
            }
         
        }
     
     
    }
    
    Thank you.
     
  2. Offline

    Sheepii

    What does MainPlugin extend?

    Also, can you post the static method of getInstance()
     
  3. Offline

    caderape

    @Suni You need a copy method. Only the config.yml copy itself.

    Copy method it's a bit complex and hard to understand, so i will just give you one to use in yours plugins.

    Code:
    private void Copy(InputStream in, File file)
    {
    OutPutStream out = new Fileoutputstream(file);
    int len;
    byte[] b = new byte[1024];
    while((len = in.read(b)) > 0)
    {
    out.write(b, 0, len);
    }
    out.close; in .close
    }
    The inputstream it's your ressource file (plugin.getRessources("individual.yml"). The File, tit's the created file.
     
  4. Offline

    Suni

    EDIT: I ended up creating a constructor and use extends in my other classes. Thank you again for the responses.
     
    Last edited: Feb 9, 2015
Thread Status:
Not open for further replies.

Share This Page