[UNSOLVED]How to save yml files

Discussion in 'Plugin Development' started by plarsootje, Apr 12, 2012.

Thread Status:
Not open for further replies.
  1. how can i save a yml file
    NOT THE CONFIG FILE
    i have a new file created warps.yml
    but when i want to set the coords in the file i doesn't get it in the file
    if think i need to save the file when i set the coords here is my code
    Code:
    private static final String PREFIX = ChatColor.GREEN + "[test] " + ChatColor.WHITE;
        File playerInvConfigFile = new File(plugin.getDataFolder() + File.separator, "homes.yml");
        FileConfiguration pInv = YamlConfiguration.loadConfiguration(playerInvConfigFile);
       
       
       
     
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(PREFIX + "Player only command");
                return true;
            }
            Player p = (Player) sender;
            if (command.getName().equalsIgnoreCase("home") && args.length == 0 && p.hasPermission("test.home")) {
                int x = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX());
                int y = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getWorld().getSpawnLocation().getBlockY());
                int z = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getWorld().getSpawnLocation().getBlockZ());
                p.teleport(new Location(p.getWorld(), x, y, z));
                p.sendMessage(PREFIX + "teleported to home");
                return true;
            }
            if (command.getName().equalsIgnoreCase("sethome") && args.length == 0 && p.hasPermission("test.set")) {
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getLocation().getBlockX());
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockY());
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getLocation().getBlockZ());
     
     
                p.sendMessage(PREFIX + "Set your home in this world to [" + p.getLocation().getBlockX() + "|" + p.getLocation().getBlockY() + "|" + p.getLocation().getBlockZ() + "]");
                return true;
               
               
            }
            return false;
        }
     
  2. Offline

    Njol

    To save the config use pInv.save(playerInvConfigFile);
    I recommend to save it everytime a player sets their home to prevent data loss if the server crashes.
     
  3. i get an error in eclipse under the line you give me it says surroud with try/catch
     
  4. Offline

    Njol

    Then surround it with try/catch. The catch clause will likely be an IOException, which means that the file could not be saved due to some error.
     
  5. and how i can do that?
    because when i just add the surround try it again it doesn't save in homes.yml
     
  6. Offline

    Njol

    Post the changed code please, because from your description I can only guess what you're doing.
     
  7. Code:
       
        private static final String PREFIX = ChatColor.GREEN + "[test] " + ChatColor.WHITE;
        File playerInvConfigFile = new File(plugin.getDataFolder() + File.separator, "homes.yml");
        FileConfiguration pInv = YamlConfiguration.loadConfiguration(playerInvConfigFile);
       
       
       
     
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(PREFIX + "Player only command");
                return true;
            }
            Player p = (Player) sender;
            if (command.getName().equalsIgnoreCase("home") && args.length == 0 && p.hasPermission("test.home")) {
                int x = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX());
                int y = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getWorld().getSpawnLocation().getBlockY());
                int z = pInv.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getWorld().getSpawnLocation().getBlockZ());
                p.teleport(new Location(p.getWorld(), x, y, z));
                p.sendMessage(PREFIX + "teleported to home");
                return true;
            }
            if (command.getName().equalsIgnoreCase("sethome") && args.length == 0 && p.hasPermission("test.set")) {
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getLocation().getBlockX());
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockY());
                pInv.set(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getLocation().getBlockZ());
                try {
                    pInv.save(playerInvConfigFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
     
     
                p.sendMessage(PREFIX + "Set your home in this world to [" + p.getLocation().getBlockX() + "|" + p.getLocation().getBlockY() + "|" + p.getLocation().getBlockZ() + "]");
                return true;
               
               
            }
            return false;
        }
     
  8. Offline

    Njol

    The code looks correct. Do you get an exception when trying to save, or does it simply not save at all?
     
  9. yeah it doens't save at all
    but when i try to save it in my config file then it works but i want to have it in my homes.yml
     
  10. Offline

    Njol

    Can you try saving it in 'homes2.yml'? If that works it's likely that the file 'homes.yml' is still open somewhere, though I have no idea where.
    Also, if you're viewing the file in an editor, did you try to close the editor and open it again? It might be the case that your editor doesn't reload the changed file automatically.
     
  11. nope just the same
    but i think that there is something wrong with the save
    because when i do it in the config file everything is the same only saveConfig() is different
     
  12. Offline

    Njol

    I can't help you anymore, as I can't see anything wrong with what you do. You might want to add [unsolved] at the beginning of the thread's title so other devs will read it, especially some that have more experience with Bukkit's configuration API.
     
  13. Offline

    messageofdeath

    PHP:
    File file = new File(this.getDataFolder(), "warps.yml");
    if(!
    file.exists()) {
        try{
            
    file.createNewFile();
            
    //Created a file
        
    } catch(IOException e) {
            
    e.printStackTrace();
            
    //Error creating file
        
    }
    }
     
  14. I have done that i think that there is a problem with the saving of the file when i try to put the coords inn the file
     
  15. Offline

    Eistee²

    PHP:
    private BufferedWriter bw;
     
    public 
    void writeToFile(String toWrite){
                try{
                
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(plugins/Name of the folder/warps.yml)));
                
    bw.append(toWrite);
                
    bw.newLine();
                
    bw.close();
                }
                catch(
    Exception e){
                 
                }
                 
                }
     
    File file= new File(this.getDataFolder(),"warps.yml");
    if(!
    file.exists()) {
        try{
          
    file.createNewFile();
           
    //Created a file
       
    } catch(IOException e) {
            
    e.printStackTrace();
          
    //Error creating file
       
    }
    }
    this.writeToFile(Here the corodinates that you want to write (always over write the file);
     
     
     
    [/CODE]
    May this work :)
     
  16. so and can i paste this in my listener or must be it by onenable?
     
  17. Offline

    Eistee²

    The buffered write must be outside the command and privatee bufferedwirter bw declared
    and in the comand:
    File file= new File(this.getDataFolder(),"warps.yml");
    if(!file.exists()) {
    try{
    file.createNewFile();
    //Created a file
    } catch(IOException e) {
    e.printStackTrace();
    //Error creating file
    }
    }this.writeToFile(Here the corodinates that you want to write (always over write the file);

    Or wherever you have the cordiantes you must only say that class.writeToFile([Text]) :)
     
  18. so i can paste every thing in me executor and nothing in my onEnable
     
  19. Offline

    Eistee²

    yep you only need to declared buffered writer in the same file where the void is :) and change the comment to the file path :D

    If you done it right it should work fine (use it in my plugin and works without problems):)
     
  20. i get an error under
    Code:
    public void writeToFile(String toWrite){
                    try{
                    bw = new BufferedWriter([COLOR=#ff0000]new OutputStreamWriter(new FileOutputStream(plugins/dracinis/warps.yml)));[/COLOR]
                    bw.append(toWrite);
                    bw.newLine();
                    bw.close();
                    }
                    catch(Exception e){
                 
                    }
                 
                    }
       
        File file= new File(plugin.getDataFolder(),"warps.yml")[COLOR=#ff0000]; <--- this[/COLOR]
        if(!file.exists()) {
            try{
              file.createNewFile();
              //Created a file
          } catch(IOException e) {
                e.printStackTrace();
              //Error creating file
          }
        }
        this.writeToFile(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX());
        
     
  21. Offline

    Eistee²

    Hm i hope the color options
    PHP:
    [COLOR=#ff0000]new OutputStreamWriter(new FileOutputStream(plugins/dracinis/warps.yml)));[/COLOR]
    and here
    PHP:
    plugin.getDataFolder(),"warps.yml")[COLOR=#ff0000]; <--- this[/COLOR]
    only in the copy not in the orginal programm ... when yes delete it ... xD else try to get every thing in one string like
    String cords = p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX();
    this.writeToFile(cords) :)
     
  22. yeah it was the colors but it doesn't get red :p
    but how i need to fix the new fileOutputStream(Plugins/dracinis..
     
  23. Offline

    Eistee²

    what do you mean with fix? :)
    where get the warps.yml created :D?
     
  24. under the string i get an error but my warps.yml got saved in the plugins/dracinis
     
  25. Offline

    Eistee²

    May you write Plugins not plugins? :p
     
  26. Nope Still doesn't work
     
  27. Offline

    Eistee²

    Then try this :)
     
  28. Offline

    Neodork

    If you have a look at these YAML tutorials I'm sure you'll never encounter any problems with YAML again. Read them trough if you want to learn you'll have to spend time on it.

    I use this guys tutorial in combination with the information provided here.
     
  29. Offline

    Abigail111

    Hi there,this is the code :
    public static void SaveDocumentFile(REDocument document, string filePath);
    public static void SaveDocumentFile(BaseImage image, string filePath, BaseEncoder enc);
    public static void SaveDocumentFile(List<BaseImage> images, string filePath, BaseEncoder enc);
    public static void SaveDocumentFile(REDocument document, string filePath, BaseEncoder enc);
     
Thread Status:
Not open for further replies.

Share This Page