Solved Checking if a arena in a config file exists

Discussion in 'Plugin Development' started by mine2012craft, Dec 30, 2016.

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

    mine2012craft

    Hello Coders,

    So basically what I am trying to accomplish here is checking if a arena name in a config file (config.yml because I'm too lazy currently to make a custom one) exists, and I am having trouble doing it, because I don't really know how to exactly check if the arena name exists in the config file. I could always use something like "if (args[0] == "Arena." + args[0]) but I don't really think I want to do that.

    If you're confused:

    I want to figure out how to check if this in the config:

    Code:
    Arena:
         Arena1: <---- This
               //Stuff
    exists, and I'm not putting in some random arena name that doesn't even exists.

    If anyone knows how to do so and can help me out, that would be greatly appreciated.

    Thank you
    WarlordWeaponry
     
  2. Offline

    Zombie_Striker

    @mine2012craft
    1. Use getConfig().getConfigurationSection("Arena") to get the arena configation (the path that holds all the arena names)
    2. Use ConfigurationSection#getKeys(false) to get all the subpaths for that section. That should return a List of all the arena names in the config.
    3. To check if an arena name is in the config, use the list from #2 and check if that list contains the name you are looking for.
     
  3. Offline

    mine2012craft

    @Zombie_Striker Wow thank you! I'll notify you if I still need help solving this.

    EDIT: Hmm.. Actually getKey returns a set, not a list.

    Code:
                            ConfigurationSection arenas = Core.getInstance().getConfig().getConfigurationSection("Arenas");
                            Set<String> arenalist = arenas.getKeys(true);
                            if (args[0] == arenalist){ <--  Cannot convert string to Set<String>
    
     
    Last edited: Dec 31, 2016
  4. I have same prob
     
  5. Offline

    Zombie_Striker

    @mine2012craft @danichef
    My mistake. Instead, check if the set contains the string.

    [Edit]
    This will never work. You are checking if a String is equal to a List/Set. That will never happen. What you want to use is the Set#contains method.
     
  6. Offline

    mine2012craft

    @Zombie_Striker Ahh, alright thank you! Now its working properly.

    EDIT: Oh, wait.
    @Zombie_Striker
    Internal Error. I'm getting it from the Set<String>

    Code:
                            ConfigurationSection arenas = Core.getInstance().getConfig().getConfigurationSection("Arenas");
                            Set<String> arenalist = arenas.getKeys(true); //<----- This Line
                            if (args[0] == "create"){
                                player.sendMessage(ChatColor.GOLD + "[Arena Manager] - " + ChatColor.RED + "Incorrect Usage: /arena create <name>");
                                return true;
                            }else if (arenalist.contains(args[0])){
    When it hits that line, it does not go any further
     
    Last edited: Jan 2, 2017
  7. Offline

    Jakeeeee

    Why not use #isSet?
    #isSet would return the same thing as looping through the keys then comparing, or am I missing something here?
     
  8. Offline

    mine2012craft

  9. Offline

    Zombie_Striker

    This is your problem. If the args[0] does not exists, as you are testting here, then args[0] should throw a ArrayOutOfBoundsExeption. Instead of doing this, check if args.length is == to 0.
     
  10. Offline

    mine2012craft

    @Zombie_Striker Oh, oops sorry. Actually that line is suppose to say

    Code:
    if (args[0] == "create"){
    Also the error I'm getting is not an Array. It is a Null Pointer:

    Code:
    [02:32:23] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'arena' in plugin AdvancedKitPVP v1.4
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-7d78b81-70bc70b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-7d78b81-70bc70b]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) ~[spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1344) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1179) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot.jar:git-Spigot-7d78b81-70bc70b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at testPackage.ArenaManagerSystem.onCommand(ArenaManagerSystem.java:32) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-7d78b81-70bc70b]
        ... 15 more
    
    Line 32 Error:

    Code:
                            ConfigurationSection arenas = Core.getInstance().getConfig().getConfigurationSection("Arenas");
                            Set<String> arenalist = arenas.getKeys(true); //<----- Line 32
                            if (args[0] == "create"){
                                player.sendMessage(ChatColor.GOLD + "[Arena Manager] - " + ChatColor.RED + "Incorrect Usage: /arena create <name>");
                                return true;
                            }else if (arenalist.contains(args[0])){
     
    Last edited: Jan 2, 2017
  11. Offline

    Jakeeeee

    Instead of == (use == only for enums) use .equals the reason has been said millions of times on this forums.
     
    Last edited: Jan 2, 2017
  12. Offline

    Zombie_Striker

    @mine2012craft
    The configuration section is null. Are you sure there is a configuration section called "Arenas"?
     
  13. Offline

    mine2012craft

    @Jakeeeee Ok I'll fix that up.

    @Zombie_Striker Yes it does, but I'm guessing it needs to actually contain an arena for it to actually work...

    Oh, yup. It does. I'm stupid... Thank you so much!
     
Thread Status:
Not open for further replies.

Share This Page