Solved Get the size of a list from a configuration file

Discussion in 'Plugin Development' started by Lionhard, Dec 28, 2013.

Thread Status:
Not open for further replies.
  1. Hello, It's me again, ;)

    Today I need to know when I use this:
    Code:java
    1. Set<String> allReviewers = reviewsConfig.getConfigurationSection("reviews").getKeys(false);

    How can I get the size of this Set? I used this:
    Code:java
    1. if (allReviewers.size() != 0) {
    2. // Code
    3. }

    I think this is also probably right, but the creation of the Set might do problems, as it maybe wants to insert something from the config what doesn't exist. I would appreciate any kind of help. ;))

    Thanks in advance, Lionhard98
     
  2. Offline

    CubieX

    Code:
    as it maybe wants to insert something from the config what doesn't exist.
    What do you mean with this?
     
  3. CubieX Well, I have a file reviews.yml. Inside is a section
    Code:
    reviews:
     
    
    So the section is empty, when someone reviews the server it will look like:
    Code:
    reviews:
      PlayersName:
        review: His Review
    I have a command for list those reviews. It works when there is atleast 1 player but if not it gives an internal error and I tried to fix it by looking if the Set is empty, but it doesnt work. Thats why I need help on how to check if the Set is empty. :/
     
  4. Offline

    CubieX

    Strange. Using
    Code:
    if(allReviewers.size() > 0)
    {
        // do stuff
    }
    should definitely work.

    You can also use
    Code:
    if(!allReviewers.isEmpty())
    {
        // do stuff
    }
    Or you could do:
    Code:
    for(String playerName : allReviewers)
    {
        // do stuff
    }
    The reason for the internal error (a stack trace woul d be helpful) could be the method you use to write those reviewers to file.
    I guess, if no player was ever added, you also do not add the "reviews" node to config.
    It probably get's created with he first player?
    So this call
    Code:
    Set<String> allReviewers = reviewsConfig.getConfigurationSection("reviews").getKeys(false);
    will not deliver anything useful, because the section "reviews" does not exist then.
     
  5. CubieX No, no. reviews is as default already in the file. I can show you the stacktrace, it points to:
    Code:java
    1. Set<String> allReviewers = reviewsConfig.getConfigurationSection("reviews").getKeys(false);


    Here is the stacktrace:
    Code:
    [00:35:58] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'urev' in plugin uReview v0.5.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
    Caused by: java.lang.NullPointerException
        at de.mineteria.Lionhard98.uReview.uReview.onCommand(uReview.java:226) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.7.2-R0.3-dev%232978.jar:git-Bukkit-1.7.2-R0.2-3-g530fcb7-b2978jnks]
        ... 13 more
    

    P.S.: I tried already:
    Code:java
    1. if (!allReviewers.isEmpty(){
    2. // Code
    3. }

    ,

    Code:java
    1. if (allReviewers.size() != 0{
    2. // Code
    3. }


    and also

    Code:java
    1. if (!allReviewers == null){
    2. // Code
    3. }


    nothing worked. The for loop you posted, I already use, but after the if statement. Here is the code if you want to take a look:
    Code:java
    1.  
    2. if (args[0].equalsIgnoreCase("list")) {
    3. if (s.hasPermission(new Permissions().uReviewList)) {
    4. Set<String> allReviewers = reviewsConfig.getConfigurationSection("reviews").getKeys(false);
    5. if (!allReviewers.isEmpty() || allReviewers == null) {
    6. s.sendMessage(ChatColor.translateAlternateColorCodes('&', getPluginPrefix() + ": " + this.getConfig().getString("customMessages.reviewListHeading")));
    7. for (String user : allReviewers) {
    8. String currentReview = reviewsConfig.getString("reviews." + user + ".review");
    9. if (reviewsConfig.contains("reviews." + user + ".rating")) {
    10. s.sendMessage(ChatColor.translateAlternateColorCodes('&', getPluginPrefix() + ": " + this.getConfig().getString("customMessages.reviewListWithRating").replace("%user%", user).replace("%review%", currentReview).replace("%rate%", Integer.toString(reviewsConfig.getInt("reviews." + user + ".rating"))).replace("%maxRate%", this.getConfig().getString("general.max-rating"))));
    11. } else {
    12. s.sendMessage(ChatColor.translateAlternateColorCodes('&', getPluginPrefix() + ": " + this.getConfig().getString("customMessages.reviewListWithoutRating").replace("%user%", user).replace("%review%", currentReview)));
    13.  
    14. }
    15. }
    16. } else {
    17. s.sendMessage(ChatColor.translateAlternateColorCodes('&', getPluginPrefix() + ": " + this.getConfig().getString("customMessages.reviewListNoReviewers")));
    18. }
    19. } else {
    20. s.sendMessage(ChatColor.translateAlternateColorCodes('&', getPluginPrefix() + ": " + this.getConfig().getString("customMessages.dontHaveRequiredPermissions")));
    21. }
    22. }
    23.  
     
  6. Offline

    CeramicTitan

    Lionhard

    Can you paste your config?
     
  7. Offline

    CeramicTitan

    yes
     
  8. CeramicTitan This is how it looks by default:
    Code:
    reviews:
     
     
    
    This is how it looks when someone reviews the server (then the list command works):
    I will for example use that I reviewed the server:
    Code:
    reviews:
      Lionhard98:
        review: This server is cool!
    When I then use another command to rate the server (give the server stars for example) then it looks like this:
    Code:
    reviews:
      Lionhard98:
        review: This server is cool!
        rating: 10
     
  9. Offline

    CeramicTitan

    why dont you store the review and rating by the player name. It will make things a lot easier.
     
  10. CeramicTitan so do String pReview = reviewsConfig.getString("reviews." + s.getName() + ".review");
    and then do if (pReview != null){
    // code
    } ?

    EDIT: That won't work. How should I get out the player without the set? I don't need just the commandSender, I need all reviewers.
     
  11. Offline

    CeramicTitan

    No. Make it so your config looks like:
    Code:
    Player:
      Review:
      Rating:
     
  12. CeramicTitan But I will get the same error, because I will have to get the ConfigurationSection "Player". Anyways, I can try.
     
  13. Offline

    CeramicTitan

    Nope, it will be easier to handle.

    You could always do: if(config.contains(p.getName())){
    config.getString(p.getName()+".review");
     
  14. CeramicTitan But I cant just use p, because I dont know p. I would need to make a for loop to go through the file to get p and then I would need again a set. I need to get all reviewers to show a list and not the sender or something else.

    Look:
    Code:
    Lionhard98:
      review: test
      rating: 1
    AnotherGuy:
      review: cool server
      rating: 6
    Noob:
      review: bad server
      rating: 1
    How can I get the review and rating of every single player here?
     
  15. Offline

    CeramicTitan

    Lionhard
    If your config is set to your example above. for(String name : config.getKeys(false)) will return all the player names that have made a review
     
  16. CeramicTitan Yes. And when there arent any players, it will throw the same error like the one I had.

    CeramicTitan Maybe I could use those try statements?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  17. Online

    timtower Administrator Administrator Moderator

    Lionhard An empty configurationsection looks like this:
    Code:
    reviews: {}
    So black
    Code:
    revieuws: 
    won't work
     
    Lionhard likes this.
  18. timtower Good idea, you are awesome! :D I'll try it.

    timtower WORKED!! YAYYY!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    timtower likes this.
  19. Online

    timtower Administrator Administrator Moderator

    Glad that I could help
     
    Lionhard likes this.
Thread Status:
Not open for further replies.

Share This Page