How can I save data in a YAML file?

Discussion in 'Plugin Development' started by Jakob de Guzman, Jan 19, 2019.

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

    Jakob de Guzman

    So I have plugins on my server for an example Bedwars. and to make an arena you must select a region and then create the arena. I was wondering how to save data in like a file named "arenas.yml" and if someone can help me hook my plugin with WorldEdit that would be much appreciated.

    Thanks in advance.

    EDIT: If this helps I am trying to create a minigame plugin based on the minigame Wool Wars on munchymc.com. I am also inexperienced so please explain how your solution works thanks!
     
  2. Offline

    MightyOne

    Are you looking for help with creating the yml file or how to save and load data from it?
     
  3. Offline

    Jakob de Guzman

    How to save and load data from it @MightyOne

    EDIT: What I'm trying to do is set positions for an "arena"
     
  4. Offline

    KarimAKL

    @Jakob de Guzman
    To create a YAML file:
    Code:Java
    1.  
    2. File file = new File(getDataFolder()+File.separator+"arenas.yml");
    3. FileConfiguration config = YamlConfiguration.load(file);
    4. if (!file.exists()) {
    5. file.createNewFile(); //This needs a try catch
    6. }

    To set something in the file:
    Code:Java
    1. config.set("Location.World", location.getWorld().getName());
    2. config.set("Location.X", location.getX());
    3. config.set("Location.Y", location.getY());
    4. config.set("Location.Z", location.getZ());
    5. //To remove something do 'config.set("Whatever.Path-Here", null)'
    6. config.save(file); //This needs a try catch

    To get something from the file:
    Code:Java
    1. config.getString("Whatever.Path-Here");
    2. config.getInt("Path-To.Some.Integer-Here");
    3. config.getLong(""); //etc
     
  5. Offline

    Jakob de Guzman

    @KarimAKL I've seen this post before, I don't understand it and I do not know where it goes. Sorry as I said I am new
     
  6. Offline

    timtower Administrator Administrator Moderator

    If you are new then I highly suggest to avoid making minigames, they are not easy.
    And you use that where you want to save the arena's file.
     
  7. Offline

    MightyOne

    Maybe you can start showing some code. Where are you trying to implement a custom yml file? How does your Arena class look like? At which point exactly can we help you?
     
    Last edited: Jan 20, 2019
  8. Offline

    Jakob de Guzman

    @MightyOne

    Create Command
    Code:
    package me.Proximitynow.WoolWars.Commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.Proximitynow.WoolWars.Main;
    import me.Proximitynow.WoolWars.utils.utils;
    
    public class CreateCommand implements CommandExecutor    {
    
        private Main plugin;
       
        public CreateCommand(Main plugin) {
           
            this.plugin = plugin;
            plugin.getCommand("wwcreate").setExecutor(this);;
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (!(sender instanceof Player)) {
               
                sender.sendMessage("Only players can execute this command!");
                return true;
               
            }
           
            Player p = (Player) sender;
           
            if (p.hasPermission("ww.create")) {
           
                if (args.length == 0) {
               
                    p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " " + plugin.getConfig().getString("missingargs")));
                   
                } else {
                   
                    p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " &aArena &e" + args[0] + " &ahas been created!"));
                   
                    // Create an arena here and save in arenas.yml
                   
                }
               
            return true;
           
            } else {
               
                p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " " + plugin.getConfig().getString("noperms")));
               
            }
           
            return false;
           
        }
       
    }
    
    Delete Command:
    Code:
    package me.Proximitynow.WoolWars.Commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.Proximitynow.WoolWars.Main;
    import me.Proximitynow.WoolWars.utils.utils;
    
    public class DeleteCommand implements CommandExecutor    {
    
        private Main plugin;
       
        public DeleteCommand(Main plugin) {
           
            this.plugin = plugin;
            plugin.getCommand("wwdelete").setExecutor(this);;
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (!(sender instanceof Player)) {
               
                sender.sendMessage("Only players can execute this command!");
                return true;
               
            }
           
            Player p = (Player) sender;
           
            if (p.hasPermission("ww.delete")) {
           
                if (args.length == 0) {
               
                    p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " " + plugin.getConfig().getString("missingargs")));
                   
                } else {
                   
                    p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " &aArena &e" + args[0] + " &ahas been deleted!"));
               
                    //Delete arena with ID/NAME
                   
                }
               
            return true;
           
            } else {
               
                p.sendMessage(utils.chat(plugin.getConfig().getString("prefix") + " " + plugin.getConfig().getString("noperms")));
               
            }
           
            return false;
           
        }
       
    }
    
     
  9. Offline

    KarimAKL

    @Jakob de Guzman
    In the CreateCommand class you can set the arena in the 'arenas.yml' file like this:
    Code:Java
    1. File file = new File(plugin.getDataFolder()+File.separator+"arenas.yml");
    2. FileConfiguration config = YamlConfiguration.load(file);
    3. config.set("Arenas."+args[0]+".World", p.getWorld().getName());
    4. //etc
    5. config.save(file); //Try catch for this line

    Put that code at the comment you made saying "// Create an arena here and save in arenas.yml"
    For the DeleteCommand just do this:
    Code:Java
    1. File file = new File(plugin.getDataFolder()+File.separator+"arenas.yml");
    2. FileConfiguration config = YamlConfiguration.load(file);
    3. config.set("Arenas."+args[0], null);
    4. config.save(file); //Try catch for this line

    That should set the arena to null (remove it)
    Again, that code goes where you said "//Delete arena with ID/NAME"

    EDIT: Also, create the 'arenas.yml' file in the onEnable() to create it on startup
    EDIT2: My bad, it's not 'YamlConfiguration.load(file)' but 'YamlConfiguration.loadConfiguration(file)'
     
    Last edited by a moderator: Jan 20, 2019
  10. Offline

    Jakob de Guzman

    @KarimAKL where do i put the arenas.yml in the onEnbale do i do it like the saveDefaultConfig or...

    EDIT: and with the create command when i use it, it says "The method load(File) is undefined for the type YamlConfiguration"
     
  11. Offline

    KarimAKL

    Yes, do it like the 'saveDefaultConfig()'

    Yeah that was my bad, it's 'YamlConfiguration.loadConfiguration(file)'
     
Thread Status:
Not open for further replies.

Share This Page