Solved Help with create a new yaml file

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

  1. Code (Text):
    public class YamlInventory {
     
     
        public void yamlCreate(Player player) {
         
            File directoryinv = new File(Bukkit.getServer().getPluginManager().getPlugin("MyPlugin").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) {
                }
             
            }
         
        }
    }
    Code (Text):
    @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent e) throws IOException {

            YamlInventory yaml = new YamlInventory() ;
            yaml.yamlCreate(e.getPlayer().getUniqueId());
        }
    I'm trying to create a new yaml with UUID of Players, It should be created when a Players join in the Server but It doesn't work
     
    #1 Dantesudante, Jun 7, 2017
    Last edited: Jun 7, 2017
  2. Mas

    Mas

    You are trying to declare your listener to make it throw and Exception, which won't work.
    Use. A try-catch block around the code which code throw the exception.
     
  3. Done, but still not working
    Code (Text):
        @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent e)  {

            YamlInventory yaml = new YamlInventory() ;

            yaml.yamlCreate(e.getPlayer());

        }
     
  4. Code (Text):
    File directoryinv = new File(Bukkit.getServer().getPluginManager().getPlugin("MyPlugin").getDataFolder(), File.separator + "PlayersInventory");
    Could be the directory wrong?
     
  5. use slash, file.pathseperator didnt work good for me. And where is the .yml in the end of it???
     
  6. Mas

    Mas

    Why would you use a hardcoded char over a constant made for this exact purpose? :p

    Get rid of the File.separator completely, since you are already specifying the directory in the first arg in the constructor.
     
  7. Code (Text):
            File directoryinv = new File(Bukkit.getServer().getPluginManager().getPlugin("MyPlugin").getDataFolder(), File.separator + "PlayersInventory");
            File playerFile = new File (directoryinv + File.separator , player.getUniqueId().toString()+".yml");
     
  8. I don't know what to do, there is people who say "use File.separator" and other who say "use the /"
     
  9. You should always use File.separator. If a different operating system is running your program and doesn't use the forward slash, than using / won't work.

    However, ItsMas_ is right. get rid of File.separator completely, on both lines. You don't need it when you are specifying the directory already as the previous argument.

    You also need to create the parent directory first by calling mkdir or mkdirs:
    Code (Text):
    if(!directoryinv.exists()){
        directoryinv.mkdirs();
    }
     
  10. Check out this tutorial, I think it could be helpful for this. Also it's only 4 mins long :p
     
  11. Something like this could work with some tweaking:

    Code (Text):

        public void createPlayerFile(String name){
            File player = new File(getDataFolder(), name+".yml");
            if (!player.exists()) {
                player.getParentFile().mkdirs();
                saveResource(name+".yml", false);
            }
        }