Creating Yaml

Discussion in 'Spigot Plugin Development' started by Dantesudante, Jun 7, 2017.

  1. How I can make a Method that create a Yaml (with Player who Join) in a new Folder?
    Code (Text):
    public class YamlInventory {
       
       
        public void yamlCreate(Player player) throws IOException {
           
            File playerFile = new File ("plugins/ChestPet/PlayersInventory/", player.getUniqueId().toString()+".yml/"); //"plugins/ChestPet/PlayersInventory/"
            FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
           
            if (!(playerFile.exists())) {
                playerFile.createNewFile();
            }
            else {
                player.sendMessage("vuoto");
            }
           
        }
       
    }
    Code (Text):
     @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent e) throws IOException {

            YamlInventory yaml = new YamlInventory() ;
            yaml.yamlCreate(e.getPlayer());
        }
     
  2. Code (Text):

    public FileConfiguration yamlCreate(Player player) throws IOException {
           
            File playerFile = new File ("plugins/ChestPet/PlayersInventory/", player.getUniqueId().toString()+".yml/"); //"plugins/ChestPet/PlayersInventory/"
            FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
           
            if (!(playerFile.exists())) {
                playerFile.createNewFile();
            }
            else {
                player.sendMessage("vuoto");
            }
        return playerData;
        }

    @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent e) throws IOException {

            YamlInventory yaml = new YamlInventory() ;
            yaml.yamlCreate(e.getPlayer().getUniqueId());
        }
     
     
    • Friendly Friendly x 1
  3. It's a bad idea to create a separate file for each player.
     
  4. You should definitely use JavaPlugin#getDataFolder-method to get the plugin folder.
    Also hardcoding slashes ("/") is bad practice in terms of OS support, you should instead use File.separator
     
    • Agree Agree x 2
  5. I tried with that:

    Code (Text):
    public class YamlInventory {
       
       
        public void yamlCreate(Player player) {
           
            File directoryinv = new File(Bukkit.getServer().getPluginManager().getPlugin("ChestPet").getDataFolder(), File.separator + "PlayersInventory");
            File playerFile = new File (directoryinv + File.separator , player.getUniqueId().toString()+".yml"); //"plugins/ChestPet/PlayersInventory/"
            FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
           
            if (!(playerFile.exists())) {
                try {
                    playerFile.createNewFile();
                } catch (IOException e) {
                }
               
            }
           
        }
    }