Yaml Config Files?

Discussion in 'Plugin Development' started by Nineza, Mar 11, 2011.

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

    Nineza

    Um, ok. I'm pretty new to java, and I know some of you are probably gonna tell me I need to learn more before I get into proper programming, but I thought this would help me learn.

    I'm making a plugin called OtherStuph, which is going to be a collection of commands which you may not find in Essentials, or things like that. Like /fire <username> which sets someone on fire, until the /unfire <username> command is used. Optional timeout parameter, so the fire disappears after x number of seconds.
    Well, there was going to be a command /forum for people who have a forum they want their server users to visit. It's a disable-able command (that's coming later), and the forum link is set in the config file.
    For example:
    config.yml (open)

    Code:
    forum:
        enabled: true
        link: http://myforum.com/
    


    And then I want my plugin to read the link bit. I know you use a yaml loader or something, and you'd use forum.link or something, but I'm not entirely sure on the details.
    I tried looking at other projects' source codes, but they're either too complicated for me to understand, or I have no idea where the yaml call is. :/

    So yeah, here's my source code for the main plugin bit... I'd like the variable forumlink to become what the link should be please.

    OtherStuph.java (open)

    Code:
    package com.Nineza.otherstuph;
    
    import java.io.File;
    import java.util.HashMap;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.util.config.Configuration;
    
    /**
    * Plugin for Bukkit
    *
    * @author Nineza
    */
    public class OtherStuph extends JavaPlugin {
        File f = new File(getDataFolder(), "");
        Configuration config = new Configuration(f);
        private final String forumlink = "ForumLinkHere"; //I want the forum link to be found over here
        private final OSPlayerListener playerListener = new OSPlayerListener(this);
        private final OSBlockListener blockListener = new OSBlockListener(this);
        private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
    
        // NOTE: There should be no need to define a constructor any more for more info on moving from
        // the old constructor see:
        // http://forums.bukkit.org/threads/too-long-constructor.5032/
        
        @Override
        public void onDisable() {
            // TODO: Place any custom disable code here
    
            // NOTE: All registered events are automatically unregistered when a plugin is disabled
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            System.out.println("Goodbye world!");
        }
    
        @Override
        public void onEnable() {
            // TODO: Place any custom enable code here including the registration of any events
    
            // Register our events
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_PHYSICS, blockListener, Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_CANBUILD, blockListener, Priority.Normal, this);
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            PluginDescriptionFile pdfFile = this.getDescription();
            getServer().getLogger().info("[" + pdfFile.getName() + "] Version " + pdfFile.getVersion() + " successfully loaded!");
        }
    
        public boolean isDebugging(final Player player) {
            if (debugees.containsKey(player)) {
                return debugees.get(player);
            } else {
                return false;
            }
        }
    
        public void setDebugging(final Player player, final boolean value) {
            debugees.put(player, value);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
        {
            String[] split = args;
            String commandName = command.getName().toLowerCase();
            if(sender instanceof Player)
            {
                Player player = (Player) sender;
                    
                if (commandName.equals("ostest")) {
                    player.sendMessage("Hi there, " + player.getDisplayName() + "! =D");
                    return true;
                } else if (commandName.equals("gettime")) {
                    player.sendMessage("The time is " + player.getWorld().getTime() % 24000 + "!");
                    return true;
                } else if (commandName.equals("forum")) {
                    player.sendMessage("The forums are at: " + this.forumlink + "");
                    return true;
                }
            }
            return false;
        }
    }
    

    And my plugin.yml, if you need it:
    plugin.yml (open)

    Code:
    name: OtherStuph
    main: com.Nineza.otherstuph.OtherStuph
    version: 0.01
    author: Nineza
    description: A bunch of useful stuff for Bukkit users.
    commands:
        ostest:
            description: A test for the OtherStuph plugin
            usage: /<command>
        gettime:
            description: Find out the raw time after dawn
            usage: /<command>
        forum:
            description: Displays the link to the forums
            usage: /<command>
    


    And sorry if I've made a real idiot of myself. :p

    Thanks in advance!
     
  2. Offline

    Nineza

    Um, please? Someone help? D:
     
  3. Offline

    Ziddia

    I want to know this too please
     
  4. Offline

    Sammy

    I made a Yaml file handler a few weeks ago, Im pretty new to java so my code is simple ^^

    Code:
    package com.SySammy.SyBot.UI;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.yaml.snakeyaml.Yaml;
    
    /**
    *
    * @author Sammy
    */
    public class yamlHandler {
    
        private String cmd;
        private Object objd;
        private Object obju;
        BufferedReader input;
    
        public yamlHandler(String command) {
            this.cmd = command;
            try {
    
                input = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("commands.yml")));
    
    
            } catch (Exception ex) {
                Logger.getLogger(yamlHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            Yaml yaml = new Yaml();
            Map<String, Map<String, Map<String, Object>>> data = (Map<String, Map<String, Map<String, Object>>>) yaml.load(input);
    
            if (command.equals(command)) {
                objd = data.get("commands").get(cmd).get("description");
                obju = data.get("commands").get(cmd).get("usage");
            }
        }
    
        public String getdescription() {
            return objd.toString();
        }
    
        public String getusage() {
            return obju.toString();
        }
    }
    My yml file is only for commands, but its a new one not the plugin.yml:

    Code:
    commands:
      help:
        description: Help menu
        usage: |
              /<command> - help menu.
    Hope it helps
     
  5. Offline

    Nineza

    I'm new to java too, and I think there's a built-in class to the Bukkit api. :S
    Anyone else heard of that?
    But in the meantime, I'll try yours.
    Thanks. :D
     
  6. Offline

    Sammy

    I don't think you can use the bukkit yml handler without recompiling bukkit
     
  7. Offline

    clash

    The Plugin interface (which JavaPlugin implements) provides a Configuration object via getConfiguration().
    You don't need to create your own if you're getting/setting values in the config.yml for your plugin.

    So you can get the URL like:

    Code:
    String url = getConfiguration().getString("forum.link");
    And you can construct separate Configuration objects and use them as well for additional files.
    No need to recompile Bukkit.
     
  8. Offline

    Nineza

    Ok, I just tried that thing. I can get stuff from the config, but I'm not sure how to save. Usually it's config.setString(), but when I tried that it said it didn't exist.
    Any ideas? :S
     
  9. Offline

    clash

    To set values, use setProperty():
    Code:
    String url = "http://myforum.com/";
    getConfiguration().setProperty("forum.link", url);
    getConfiguration().save();
    The save() method saves the changes you made to the file.
     
  10. Offline

    Nineza

    Yeah, I knew about save, but didn't quite work out about setProperty. It said object for the second argument, and didn't quite realise what that meant.
    Thanks. :D
     
  11. Offline

    Nohup

    the only other thing to add is that if you want to load something other than config.yml you have to load it explicitly:

    Code:
                    String path = wName + ".config.yml";
                    File f = new File(getDataFolder(), path);
    
                    Configuration c = null;
    
                    if (f.exists())
                    {
                        c = new Configuration(f);
                        c.load();
                    }
                    _configs.put(wName, c);
    
    That is how I load my world-specific configs.
     
  12. Offline

    Tim Visee

    Thanks! This works for me...
     
  13. Offline

    Canownueasy

    Bukkit uses a library called Snakeyaml :p
     
  14. Offline

    hmpf

Thread Status:
Not open for further replies.

Share This Page