[TUT] Bukkit's new FileConfiguration API! Create a YAML configuration!

Discussion in 'Resources' started by Windwaker, Oct 23, 2011.

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

    Sagacious_Zed Bukkit Docs

    Did you update your server? Should be running at least 1337
     
  2. Offline

    Icelaunche

    yup I am
     
  3. Offline

    Windwaker

    Have you updated your Java Build path to include the latest Bukkit API?
     
  4. Offline

    feildmaster

    Reloading config (open)
    To reload config while storing the variable:
    Code:java
    1. public void reloadConfiguration() {
    2. reloadConfig();
    3. config = getConfig();
    4. }



    ... When teaching a bad practice, teach fully. Do not store the config inside a variable. plugin.getConfig() every time.
     
  5. Offline

    Windwaker

    1. Does it matter if you store getConfig() every time?
    2. This is for beginners starting out with bukkit configuration and they can take this knowledge and do what they will with it.
    3. Thats rude ^^
     
    isodnerf likes this.
  6. Offline

    feildmaster

    1- Yes. It will NEVER reload it if you store it in your own variable. (use plugin.getConfig()) Actually. Why did I think that? It stores the variable which will load always... But... Then why does reloading error out for some people. Odd. Oh wait. I thought it because it's TRUE.
    2- Stop giving beginners bad practice, so they don't bug me later.
    3- I don't care.


    EDIT: By the way, to check this:
    Code:java
    1. package org.bukkit.plugin.java;
    2.  
    3. import org.bukkit.configuration.file.FileConfiguration;
    4. import org.junit.Test;
    5. import static org.junit.Assert.*;
    6.  
    7. public class Plugin {
    8. public JavaPlugin getPlugin() {
    9. JavaPlugin plugin = new JavaPlugin() { public void onDisable() {} public void onEnable() {} };
    10. plugin.initialize(null, null, null, null, null, this.getClass().getClassLoader());
    11. return plugin;
    12. }
    13.  
    14. @ test
    15. public void test() {
    16. JavaPlugin plugin = getPlugin();
    17. FileConfiguration config = plugin.getConfig();
    18.  
    19. plugin.reloadConfig();
    20.  
    21. assertEquals(config, plugin.getConfig()); // This is false
    22. }
    23.  
    24. @ test
    25. public void test2() {
    26. JavaPlugin plugin = getPlugin();
    27. FileConfiguration config = plugin.getConfig();
    28.  
    29. plugin.reloadConfig();
    30.  
    31. config = plugin.getConfig();
    32.  
    33. assertEquals(config, plugin.getConfig()); // This is true.
    34. }
    35. }
    36.  


    remove the space between @ test

    I'm going to kill who ever made a username with test.

    Another test to try:

    Code:java
    1. @ Test
    2. public void test3() {
    3. JavaPlugin plugin = getPlugin();
    4. FileConfiguration config = plugin.getConfig();
    5.  
    6. assertEquals(config, plugin.getConfig()); // This is true.
    7. }
    8.  
    9. @ Test
    10. public void test4() {
    11. JavaPlugin plugin = getPlugin();
    12. FileConfiguration config = plugin.getConfig();
    13.  
    14. config.set("test.path", "teststring");
    15.  
    16. assertEquals(config, plugin.getConfig()); // This is true
    17. }


    Going to test some more later... These don't seem to be completely conclusive.. I dunno. I think I'm doing something wrong too. :)

    Code:Java
    1. @ Test
    2. public void fullTest() {
    3. JavaPlugin plugin = getPlugin();
    4. FileConfiguration config = plugin.getConfig();
    5.  
    6. config.set("path", "teststring");
    7. config.set("test", "teststring");
    8. config.set("string", "teststring");
    9.  
    10. plugin.saveConfig();
    11. plugin.reloadConfig();
    12.  
    13. config.set("reloaded", "Does this save to plugin?");
    14.  
    15. assertEquals(config.getValues(true), plugin.getConfig().getValues(true)); // Answer is... NO!
    16. // <{string=teststring, path=teststring, test=teststring, reloaded=Does this save to plugin?}> but was:<{string=teststring, path=teststring, test=teststring}>
    17. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    Windwaker

    @feildmaster

    Thank you :) Your input was helpful and added to the OP
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    @feildmaster
    Yay, test results!now everyone can be closer to the wiki!
     
  9. Offline

    feildmaster

    Ah... You're the dude that's always on the wiki. I was wondering where I'd seen your name before.
     
  10. Offline

    Hotshot

    what object does the getList method return?
     
  11. Offline

    DomovoiButler

    thats why you should go to the apidocs and figure it out yourself
     
  12. Offline

    cheese5505

    Is there a way to make multiple files?
     
  13. Offline

    Sagacious_Zed Bukkit Docs

  14. Offline

    DomovoiButler

  15. Offline

    iffa

    That's why you should use your eyes or start using an IDE.
     
    thehutch and WalkerCrouse like this.
  16. Offline

    Windwaker

  17. Offline

    DomovoiButler

    doing what?
     
  18. Offline

    Windwaker

    Linking to your tutorial after help has already been given :/
     
  19. Offline

    Hotshot

    Whenever I try to inticialize FileConfiguration congif; it gives me red sqigally line underneath even though ive imported
    import org.bukkit.configuration.file.FileConfiguration;
     
  20. Offline

    Windwaker

    Don't initialize it just use getConfig()
     
  21. Offline

    Father Of Time

    Excellent, I've been looking for a simple yet informative tutorial regarding the new configuration, this post has met both needs.

    Thank you for the Tutorial Walker Crouse!
     
    Walker Crouse likes this.
  22. Offline

    TopGear93

    i cant believe they changed the config in the first place lol. anyways, im confused on how to do this new config. this is as far as i got with your tutorial. how would i do int? id like to make the type ID in my block listener configurable..
    Code:
    package us.topgear93.pressurechange;
    
    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    
    @SuppressWarnings("deprecation")
    public class PressureChange extends JavaPlugin{
        public Configuration config;
        protected static String BLOCK_PLACE = "STONE_PLATE";
        protected static String BLOCK_AGAINST = "DIRT";
        protected static int BLOCK_CHANGEDTO = 46;
        private final BL BlockListener = new BL(this);
        public PressureChange plugin;
        public static Logger log;
    
        @Override
        public void onDisable() {
            plugin.saveConfig();
            log.info(getDescription().getName() + " version " + getDescription().getVersion() + " unloaded.");
        }
        @Override
        public void onEnable() {
            loadConfiguration();
            PluginManager pm = getServer().getPluginManager();
            log = Logger.getLogger("Minecraft");
            log.info(getDescription().getName() + " version " + getDescription().getVersion() + " loaded.");
            pm.registerEvent(Event.Type.BLOCK_PLACE, this.BlockListener, Event.Priority.Normal, this);
        }
        public void loadConfiguration(){
            String path1 = "BlockPlaced";
            String path2 = "BlockAgainst";
                    plugin.getConfig().addDefault(path1, "STONE_PLATE");
                    plugin.getConfig().addDefault(path2, "DIRT");
                    plugin.getConfig().options().copyDefaults(true);
                    plugin.saveConfig();
        }
    }
    how do i make my block listener take in the config?

    Code:
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class BL extends BlockListener{
        public static PressureChange plugin;
        public BL(PressureChange instance){
            plugin = instance;
        }
        public void onBlockPlace(BlockPlaceEvent event){
            Block new1 = event.getBlock();
            if (event.getBlockPlaced().getType() == Material.STONE_PLATE){
                if (new1.getType() == Material.DIRT);
                Block block = new1.getLocation().subtract(1,1,0).getBlock();
                ((Block) block).setTypeId(46);
            }
        }
    }
     
  23. Offline

    Windwaker

    First of all, you don't need "plugin.getConfig()" to get the config in your main class. You can simply do "getConfig()" or "this.getConfig()" to get the config in the BlockListener you would do "plugin.getConfig()"
     
  24. Offline

    thehutch

    I'm still confused with @feildmaster 's config thing because getPlugin() doesnt exist :( are you using craftbukkit or something other than the API or wrote it down without an IDE?
    For my plugin i'm trying to reload the config but it isnt working or atleast when I do reload my plugin isnt updating and also there are no errors and the message "Reload complete" is reached
    This is my reload command so far:
    Code:
            if(cmd.getName().equalsIgnoreCase("iskin") && args.length !=0) {
    
            if (args[0].equalsIgnoreCase("reload") && args.length == 1) {
    
                        if (!cs.hasPermission("iskin.reload")) {
                            cs.sendMessage(ChatColor.RED + "You do not have permission");
                            return false;
                        }
                            Plugin plug = plugin.getServer().getPluginManager().getPlugin("iSkin");
                            plug.reloadConfig();
                            for(Player p : plugin.getServer().getOnlinePlayers()) {
                                plugin.updatePlayerSkin(p.getName());
                            }
                            plugin.updateGroupSkin();
                            cs.sendMessage(ChatColor.GREEN + "Reload complete");
                            return true;
                        }
    The config itself doesnt change and it does save but the skins do not update however I know the methods for them work because there are other commands which use it and they work perfectly
     
  25. Offline

    feildmaster

    @thehutch : it's using test files in the bukkit API. It was written in the API. getPlugin() is a function I invented.
     
  26. Offline

    thehutch

    Ah ok well what should I do for my plugin then any suggestions?
     
  27. Offline

    Unlucky4ever

    This may seem like a dumb question, but I'm new to this kind of thing.

    How would I make it so that it generates a config file with default values as
    Code:
    messages:
        username: "Testing"
    I got that part of course, but how about if I wanted to add my own things into it manually

    Code:
    messages:
        username: "Testing"
        blah: "Test"
        har: "har"
    So that it looks like that, but still reads all of them.
     
  28. Offline

    Windwaker

    What do you mean by "add my own things into it manually"?

    If you wanted to generate the above you could create 3 paths as defaults so...
    Code:java
    1. plugin.getConfig().addDefault("messages.username", "Testing");
    2. plugin.getConfig().addDefault("messages.blah", "Test");
    3. plugin.getConfig().addDefault("messages.har", "har");
     
  29. Offline

    Unlucky4ever

    I guess it requires no special stuff but I'll explain it if it does.

    I want it to generate the first one on its own, which I already got it to do.

    But once it's generated I want users to be able to edit the yml and add in the stuff that is in my second part.

    So it starts as:
    Code:
    messages:
        username: "Testing"
    Then when you edit it, it shows:
    Code:
    messages:
        username: "Testing"
        blah: "Test"
        har: "har"
    But it will still read each one as it's own thing.
     
  30. Offline

    Windwaker

    You don't have to do anything then for "setting" they can just get the field you are looking for when you need it, if it returns null, it doesn't exist, else, it does and it returns the String.
     
Thread Status:
Not open for further replies.

Share This Page