(Deprecated) Bukkit's Standard Configuration Tutorial

Discussion in 'Resources' started by captainawesome7, Jul 11, 2011.

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

    captainawesome7

    if(pvpon){

    }
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Code:java
    1. if (pvpon) {
    2. // runs if the "if clause" is true
    3. // if pvpon is true it evaluates true
    4. } else {
    5. // runs if the "if clause" is false
    6. // runs if pvpon evaluates false
    7. }


    This is standard java
     
  3. Offline

    Butkicker12

    Yeah I get it now. I wasnt thinking before school. :p
     
  4. Offline

    Lolmewn Retired Staff

    He hates you.
    Sorry ;)
     
    tips48 likes this.
  5. Offline

    captainawesome7

    I hate it when all your stuff goes in a spoiler/code tag :/
     
  6. Offline

    Lolmewn Retired Staff

    lolwut?
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Your best bet if you just want the names
    Code:
    this.getConfiguration().getKeys("List");
    this will return a List of Strings [Toast, Euhcou]

    alternately there is a getNodes method
     
  8. Offline

    Alexis

  9. Offline

    DirtyStarfish

    Im trying to use the config to save a list of players, and also get the list when needed. I have no errors in the code or console, but the list isn't being written to the config file. Heres the code Im using:

    This is to set the config and get the List<String> users.
    Code:
    config = getConfiguration();
    config.load();
    users = config.getStringList("players", users);
    config.save();
    Then I use this to add the player to the list and save to the config.
    Code:
    plugin.users.add(player.getName());
    plugin.users = plugin.config.getStringList("players", plugin.users);
    plugin.config.save();
    Im not really sure how im supposed to do this.
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    When you try to add to the config, this is what you are actually doing.
    You add the player name to the list of users.
    you set the variable users to the "List of String" of Users from the config.
    you save the config state.
     
    DirtyStarfish likes this.
  11. Offline

    DirtyStarfish

    So instead of
    Code:
    plugin.users = plugin.config.getStringList("players", plugin.users);
    I guess I should be using
    Code:
    plugin.config.setProperty("players", plugin.users);
    EDIT: Just tested it, looks like it works :) Thanks for the help!
     
  12. Offline

    acuddlyheadcrab

    Does anyone know how to insert a comment into a hashmap of config defaults?

    I basically have a hashmap of the defaults, and then a for loop that writes out all the keys in the hashmap.

    This is the class i reference in onEnable and that writes the default config, as I last edited it!
    PHP:
    public class ChatWarnConfig {
    //
    //METHODS :)
        
    private Configuration config;
        private 
    HashMap<StringObjectconfigDefaults = new HashMap<String,
            public 
    ChatWarnConfig(File configFile){
            
    this.config = new Configuration(configFile);
    //
    //    CONFIG DEFAULTS HASHMAP
            
    this.configDefaults.put("# This will display as a key, and not a comment!!! :O"null);
            
    this.configDefaults.put("Key_one""object");
            
    this.configDefaults.put("Key_Two""object_two");
    //
    // A simple if check that writes the default config if its not there
            
    if(!(configFile.exists())){
    //
    // For loop that writes each key in the hashmap
                
    for (String key this.configDefaults.keySet()){
                    
    this.config.setProperty(keythis.configDefaults.get(key));
                }
                
    this.config.save();
            } else
                
    this.config.load();
        }
    //
    // unrelated return methods
        
    public String getString(String key){
            if (!(
    this.configDefaults.containsKey(key))){
                return 
    key;
            }
            return 
    this.config.getString(key, (String) this.configDefaults.get(key));
        }
    }



    This is more or less a desired default config file:
    Code:
    # Define the string of characters you want to censor
    Replace: fuck
    With: duck
    
    # Define warning message
    Warn_message:
    
    # Max number of warnings
    max_warns:
    
    # Penalty - what to do when a player gets too many warnings
    # CHOOSE EITHER 'kick' OR 'ban'
    penalty:
     
  13. Offline

    NeoSilky

    @captainawesome7 any idea why this doesn't work?

    Code:
            if (args[0].equalsIgnoreCase("create") && args.length == 1) {
                profiles.load();
                profiles.setProperty(player.getDisplayName() + ".Online", true);
                profiles.save();
                return true;
            } 
    i have
    Code:
            protected static Configuration profiles;
    and the error is :
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    It looks as if you never initialized profiles, and the NPE sorta confirms that, atleast if the NPE happened when you call load on profiles.
     
  15. Offline

    NeoSilky

    how would i initialise 'profiles' ive been told i cant use getConfiguration() because thats only for the config? :)


    EDIT: i just realised i used Configuration profiles = getConfiguration(); but then thats wrong :S
     
  16. Offline

    Sagacious_Zed Bukkit Docs

    Configuration profiles = new Configuration(new File("path/to/profiles.yml")):

    EDIT: oh parens
     
  17. Offline

    NeoSilky

    I figured this out, but thanks :D on a slightly different topic, how could i turn everything after the 1st argument in a command, into a string if its an infinte length? :)
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    I have been wanting to do this. But as far as I can tell, Not possible currently. At least not without some clever extensions to the base snakeyaml classes
     
  19. Offline

    acuddlyheadcrab

    I was wondering how i could have my code write a default list in the config? Not a bunch of keys, but a list like:


    Code:
    List:
        - word1
        - word2
        - word3
    
    
     
  20. Offline

    DirtyStarfish

    Couldn't you use a for loop?
    Code:
    for (int i = 1; i < args.length; i++) {
    
    
    Start int i at 1 so it misses the first argument of the command.
    Then append each argument to a String with a space inbetween and it will be like how it was typed.
     
  21. Offline

    Sagacious_Zed Bukkit Docs

    @NeoSilky
    Although if you expect to be doing a whole bunch of concats, a string builder would be much faster. If it happens for something that needs to happen all the time, it could help.
     
  22. Offline

    captainawesome7

    ^This. StringBuilders work fine, I can paste some code from old plugins if you want it.
     
  23. Offline

    feildmaster Retired Staff

    Code:java
    1. config.setHeader("# This is a multi-line comment","# This is the second line","# This is the third Line", "# do you see the pattern?");
     
  24. Offline

    captainawesome7

    oo very nice
     
  25. Offline

    TomShar

    how would I store and load Locations? or even better an Array of locations
     
  26. Offline

    acuddlyheadcrab


    Well locations are made up of a World, and the X, Y, and Z coords in double format. So if you follow the YML format/rules and you can get x,y,z and a String of the worldname, you can use that to make a new location.


    Code:java
    1. // Written in a hurry; May contain errors
    2. World world = (some how get to Server).Server.getWorld(String worldname);
    3. Location loc = new Location(world, x,y,z);
     
  27. Offline

    captainawesome7

    just what that other guy said. Just store the x, y, z, and world. You don't need pitch and yaw but you can store those as well if you want to.
     
Thread Status:
Not open for further replies.

Share This Page