New config troubles

Discussion in 'Plugin Development' started by thehutch, Oct 14, 2011.

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

    thehutch

    ok so I don't quite understand how this config works but im having trouble correcting a small part of it.
    the old API use to use this:
    Code:
    List<String> a = config.getKeys("Skinlist");
    however im having trouble trying to convert this to the new config API, getKeys() now seems to be a boolean value and I need a string value.
    Any help is appreciated, thank you
     
  2. there is now something called sections. Read the javadocs for getKeys to know what the boolean means.
    for your code that would probably result in:
    Code:
    ConfigurationSection section = config.getConfigurationSection("Skinlist");
    Set<String> a = section.getKeys(false);
     
  3. Offline

    thehutch

    Ok i've got a new error now and to be honest a friend wrote this part of the code so I don't quite understand it but the error is to do with the new config.
    PHP:
        public String forceCape(Player p){

            
    ConfigurationSection section config.getConfigurationSection("Grouplist");
            
    Set<Stringsection.getKeys(false);

            
    // List<String> a = config.getKeys("Grouplist");
                
    if(== null || a.size() < 1){
                    return 
    null;
                    
    //Returns nothing if the player has not been forced.

            
    for(int i 0a.size(); i++){
                
    String groupname a.get(i);
                
    String url config.getString("Grouplist." groupname ".url"" INSERT CAPE URL HERE ");
                    if ((
    p.hasPermission("icape.group." groupname)) && (!p.hasPermission("*")))
                        return 
    url;
                
    //Gets the URL for that skinlist
            
    }
            return 
    null;
        }
    The commented out section is what use to be there and I replaced it with your code

    oops forgot sorry the error is where it is underlined where get() is
    String groupname = a.get(i);


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

    Sagacious_Zed Bukkit Docs

    Okay, that tells me where your error is...... My guess is that you are trying to implicitly cast an object to a string.

    If you want a string then use getString("path") which returns a string or null;
    if you want a list then use getList("path") which returns a List or null;

    using get("path") returns an object or null.
     
  5. Offline

    Taien

    Zed, I know you're a master since you wrote that wiki page...but I have to ask...how do you handle lists? I've had to use a scrap of code that supresses warnings and such that I found, but isn't there a better way?

    Also, I'm getting the following error:
    Code:
    2011-10-15 02:17:25 [SEVERE] Error occurred while enabling InfiniteRewards v0.1 (Is it up to date?): null
    java.lang.NullPointerException
        at me.taien.InfiniteRewards.loadPlayers(InfiniteRewards.java:65)
        at me.taien.InfiniteRewards.onEnable(InfiniteRewards.java:51)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:170)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:957)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:280)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:171)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:154)
        at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    It's because of this code, the second line is the line mentioned in the error...
    Code:
    players = YamlConfiguration.loadConfiguration(irPlayers);
            Set<String> ps = players.getConfigurationSection("players").getKeys(false);
            playerData = new HashMap<String,IRPlayer>();
            for (String s:ps)
            {
                int pb = players.getInt("Players." + s + ".Credits", 0);
                int pm = players.getInt("Players." + s + ".Minutes", 0);
                int ptm = players.getInt("Players." + s + ".Total", 0);
                playerData.put(s,new IRPlayer(s,pb,pm,ptm));
            }
    players is a FileConfiguration object, it is being set there for the first time by the first line.
     
  6. As you might have noticed, the return type was changed from List<...> to Set<...>. Sets don't store their contents via indexes, so such a method does not exist.
    You have to use an Iterator to loop through a Set (use google if you don't know how).

    Also, the relation of roots changes in a sub-section.
    So your new node is not like that:
    "Grouplist." + groupname + ".url"
    but rather like that:
    groupname + ".url"
    Since you are already inside of the "Grouplist" node.

    My best advice when having a complicated configuration thing to update: Completely recode that part!
     
  7. Offline

    thehutch

    Ok so I re-coded the whole method but now I have to supress this part:
    PHP:
    List<Stringgroup = (List<String>)section.getList("Grouplist");
    PHP:
        public String forceCape(Player p){

            
    ConfigurationSection section instance.config.getConfigurationSection("Grouplist");
            @
    SuppressWarnings("unchecked")
            List<
    Stringgroup = (List<String>)section.getList("Grouplist");

            if (
    group == null || group.size() < 1) {
                return 
    null;
                
    // return null if groups = empty
            
    }
            for(
    int i=i<group.size() ; i++) {
                
    //goes through the groups

                
    String currentgroup group.get(i);
                
    String url instance.config.getString(currentgroup ".url"," INSERT CAPE URL HERE ");

            if ((
    p.hasPermission("icape.group." currentgroup)) && (!p.hasPermission("*"))) {
                return 
    url;
                
    // Gets the URL for that group
                
    }
            }
            return 
    null;
    }
    I havent been able to test it because my Java thing is all messed up but should it look like this?
     
  8. getList is NOT the same as getKeys!
    getList("something") would apply to such a config:
    Code:
    something:
    - listitem
    - foo
    - bar
    - anything
    getKeys(true/false) on a ConfigurationSection that refers to the node called "something" would apply to such a config:
    Code:
    something:
        anything: true
        someValue: 26
        foo: bar
    You can't just mix that stuff up with each other!

    Also, when you get a ConfigurationSection, you only use that when you want to get something inside of that section, which you don't at String url = instance.config.getString...
     
  9. Offline

    thehutch

    hmm ok well i've given up on this part of the code but instead im going to ask you how do I get this:
    1. Find the ConfigSection "grouplist"
    2. Then sort through all of these groups to find the current players group
    3. Then set the cape to this group
    I would also like to say that I do understand points 1 and 3 but so you understand what is going on I put it all out.
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    @thehutch
    Actually, you probably want players as keys and groups as values. Since that is how you intend to use them. But In this case ill use groups as keys and players as values, it is just much more expensive to find the answer.

    Anyways, I am going to assume you have this type of setup in your yaml. Under groups it allows for arbitrary names for groups, but group need to have this set structure.
    Code:
    groups:
      red:
        cape: 'red cape'
        players:
        - bob
        - tom
      gold:
        cape: 'gold cape'
        players:
        - jerry
        - john
      blue:
        cape: 'blue cape'
        players:
        - lisa
        - apple
    !. Finding the groups, probably should not name the node grouplist, as this is not a list. it will help you make sense of this a little bit more. You can get the set of all possible groups, quite easily.
    Code:java
    1. Set<String> groups = config.getConfigurationSection("groups").getKeys(false);


    2. Going through these groups require an iterator as mentioned earlier or use a foreach loop. In this case It is the body of a method which returns the String Name of the cape, or null if the player is not found. It takes the Player object you are trying to find, but it would not be hard to change it to a string representation of a player.
    Code:java
    1. private String getPlayerCape(Player player) {
    2. Set<String> groups = config.getConfigurationSection("groups").getKeys(false);
    3. foreach (String groupName : groups) {
    4. ConfigurationSecion groupSection = config.getConfigurationSection("groups").getConfigurationSection(groupName);
    5. List<String> groupPlayers = groupSection.getList("players");
    6. if (groupPlayers.contains(player.getName()))) {
    7. return groupSection.getString("cape");
    8. }
    9. }
    10. return null;
    11. }


    3. What you do with the cape, is between you and the cape.
     
  11. Offline

    Aza24

    Code:
    FileConfiguration config;
    config = this.getConfig();
    getConfig().options().copyDefaults(true);
    saveConfig();
    
    ConfigurationSection section = config.getConfigurationSection("classes");
    Set<String> classes = section.getKeys(false);
    
    for(int i=0; i < classes.size(); i++){
        String className = classes.get(i);
        fightClasses.put(className, config.getString(className + ".items", null));
    }
    Im getting an error on the line
    Code:
    String className = classes.get(i);
    on the get bit.
     
  12. @Aza24 How about reading the thread? Hint: #7
     
Thread Status:
Not open for further replies.

Share This Page