Creating Keys within a Configuration YAML

Discussion in 'Plugin Development' started by CMonster95, Aug 12, 2013.

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

    CMonster95

    Hello,

    I have no experience whatsoever in creating configuration files, however with research I was able to successfully compile the following.

    Code:
    package tk.craft95.lostwoods;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LostWoods extends JavaPlugin{
     
        File usersFile;
        FileConfiguration users;
     
        @Override
        public void onDisable() {
            saveYamls();
        }
     
        @Override
        public void onEnable() {
            usersFile = new File(getDataFolder(), "users.yml");
     
            try {
                firstRun();
            } catch (Exception e) {
                e.printStackTrace();
            }
            users = new YamlConfiguration();
     
            loadYamls();
        }
     
        private void firstRun() throws Exception {
            if(!usersFile.exists()){
                usersFile.getParentFile().mkdirs();
                copy(getResource("users.yml"), usersFile);
            }
        }
     
        private void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void loadYamls() {
            try {
                users.load(usersFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public void saveYamls() {
            try {
                users.save(usersFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("test")) {
                p.sendMessage("Test successful.");
                return true;
            }
            return false;
        }
     
    }
    Using references such as this and this and this I now know how to read and write to keys that already exist. My question is this: how do I create non-existent keys? A working example would be most appreciated. Thank you!

    18.5 hour bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    CMonster95

    26.5 hour bump
     
  3. Offline

    CMonster95

    Daily bump.

     
  4. Offline

    Croug

    The same way you set already existing keys
    Code:
    config.set("path.to.string", "value");
    config being your YamlConfiguration()
     
Thread Status:
Not open for further replies.

Share This Page