Checking if a value is true in a yml file

Discussion in 'Plugin Development' started by HeadGam3z, Jun 12, 2014.

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

    HeadGam3z

    So I'm making an online list for my guard plugin, and this is what I have so far:
    Code:java
    1. package com.gmail.mcheadgam3z.hgGuard.Commands;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.entity.Player;
    11.  
    12. import com.gmail.mcheadgam3z.hgGuard.Main;
    13.  
    14. public class HgGuards implements CommandExecutor {
    15. // vars
    16. static Main plugin;
    17. static File guards = new File(Main.plugin.getDataFolder(), "guards.yml");
    18. static YamlConfiguration guardData = YamlConfiguration
    19. .loadConfiguration(guards);
    20. // constructor
    21. public HgGuards(Main passedPlugin) {
    22. HgGuards.plugin = passedPlugin;
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String label,
    26. String[] args) {
    27. if (cmd.getName().equalsIgnoreCase("hgguards")) {
    28. if (sender.hasPermission("hgGuard.view")) { // change perm later
    29. if (sender instanceof Player) {
    30. Player p = (Player) sender;
    31. StringBuilder online = new StringBuilder();
    32. Player[] players = Bukkit.getOnlinePlayers();
    33. for (Player player : players) {
    34. if (!p.canSee(player))
    35. continue;
    36. if (online.length() > 0) {
    37. online.append(", ");
    38. }
    39. // check to see if online players have perm
    40. if (player.hasPermission("hgGuard.duty")) {
    41. for (String key : guardData
    42. .getConfigurationSection("Duty").getKeys(
    43. false)) {
    44. // error below
    45. if (key.contains("true")) {
    46. online.append(player.getDisplayName());
    47. }
    48. }
    49. }
    50. }
    51. // send sender list
    52. p.sendMessage("There are " + players.length + "/"
    53. + Bukkit.getMaxPlayers() + " guards online:\n"
    54. + online.toString());
    55. return true;
    56. }
    57. }
    58. }
    59. return false;
    60. }
    61. }
    62.  


    It was working fine, until I added
    Code:java
    1. for (String key : guardData
    2. .getConfigurationSection("Duty").getKeys(
    3. false)) {
    4. // error below
    5. if (key.contains("true")) {

    So there is something wrong there.
    What I am trying to do is search through my guards.yml file, and check to see if there is a UUID with the value of true (in this case, a string that is true 'cause contains() doesn't like booleans...) And if so, add them to the list and send the list to the player.

    My guards.yml file that it is searching through looks like this:
    Code:
    Duty:
      394a506f-a223-3770-a364-feeea5bcea01: true
    
    Any suggestions?
     
  2. The key would not contain the "true", it's the value that's true.

    Code:
                                for (Entry<String, Object> entrySet : guardData.getConfigurationSection("Duty").getValues(false).entrySet()) {
                                    // error below
                                    if (entrySet.getValue().toString().equalsIgnoreCase("true")) {
                                        online.append(player.getDisplayName());
                                    }
                                }
    
    Your new edited version that's slightly optimized (changed): http://pastie.org/private/sdkwhw3zfw9hxxrzihs1da
     
  3. Offline

    HeadGam3z

    KingFaris11
    It's still showing a player in the list even if their value is false. I think it's because even if it finds one value as true, it'll still add them to the list. So I guess I need to approach this from a different angle.
     
  4. Sorry, I misread your code completely, of course this won't work.
    Here's something I wrote quickly, not sure if it'll work:
    http://pastie.org/private/uzyezucdjgcd41awxoxw
    Although, I changed Bukkit.getMaxPlayers() to guardsOnDuty.size() since it makes sense for you to show the amount of guards on duty online out of the amount of guards on duty in total.
     
  5. Offline

    HeadGam3z

    KingFaris11
    Sorry about the long wait reply, somehow I didn't see this...
    Anyway, it gave me a NPE on this line:
    Code:java
    1. for (Entry<String, Object> entrySet : this.guardData.getConfigurationSection("Duty").getValues(false).entrySet()) {
     
  6. That means either your guardData is null OR Duty doesn't exist in the config OR guardData hasn't been loaded
     
  7. Offline

    HeadGam3z

    KingFaris11
    guardData exists, Duty exists in it's own guards.yml file, and it is getting loaded.
     
  8. Try this:
    Code:
    if (this.guardData != null && this.guardData.contains("Duty") && this.guardData.isConfigurationSection("Duty")) {
        for (Map.Entry<String, Object> entrySet : this.guardData.getConfigurationSection("Duty").getValues(false).entrySet()) {
            // Do stuff.
        }
    }
    
    If it no longer throws the error but doesn't pass this statement, it means either: guardData is null, guardData does not contain "Duty" (maybe you haven't loaded guardData properly) or Duty isn't correctly a configuration section (or a Map).

    I don't have time to format it, but this is how the final version should roughly look like: http://pastie.org/private/mpmhlkag03ezdlsbo8pdg

    I may have added an extra } at the end of Entry set clause accidentally.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page