Solved If config string = false or true

Discussion in 'Plugin Development' started by mactown21, May 15, 2014.

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

    mactown21

    How can i check a config string if it is true or false, else do something else.

    So mainly getConfig().getString("enabled");
    enable: true

    How can i check to see if that string is equals to true or false, if true do something, false do something else.
    Will this work?
    Code:
    if(getConfig().getString("enable").equals("true")){
    //Do something
    }else
    //do something
    {
    So this is what im thinking,
    Code:
            if(getConfig().getString("barapi").equals("true")){
                if (args[0].equalsIgnoreCase("website")) {
                    BarAPI.setMessage(player, getConfig().getString("website"), 10);
                }
            }else
                player.sendMessage(getConfig().getString("website"));
            return true;
    But is this the correct way to do it?
     
  2. Offline

    Drew1080

    Why not use a boolean instead of a string? If its only going to be true and false. Use the getBoolean("string") method to get it from the config.
     
  3. Offline

    mactown21

    Drew1080 How would i do that?

    And currently, i do have string[] args){
    but in this method im trying to get a true or false from a config string in config.yml
     
  4. Offline

    Bailey Payne

    That should work, only way to find out is testing it, you can use a Boolean method if you want, since its either true or false, but either way will work.
     
  5. Offline

    mactown21

    Bailey Payne Nope, that does not work for me, It crashes everytime. How would i do the boolean string with config?
     
  6. Offline

    Bailey Payne

    Whats the error? check the console.

    For the Boolean you replace getString with getBoolean, put your string name in so say mine is pvp, getBoolean("pvp")
    and then you need to return it either true or false, so I think .equals(true); would work but I am not 100% sure, you could just put a . and see what shows up in the drop down list (I have netbeans which does this, not sure if eclipse does :p)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Offline

    mactown21

    Bailey Payne Currently im not able to at the moment.
    This is what i did

    Code:
            if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("website")) {
                    BarAPI.setMessage(player, getConfig().getString("website"), 10);
                    return true;
                }
            }else
                player.sendMessage(getConfig().getString("website"));
            return true;
            {
            if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("ip")) {
                    BarAPI.setMessage(player, getConfig().getString("ip"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("ip"));
                return true;
            }
            }
          }
        }
        return false;
      }
    }

    But with getString("enable") will it for sure get that string enable: in config?

    I think i figured out what i did wrong, Everything seems clear now, im going to test it out, heres the results of now

    Code:
    if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("website")) {
                    BarAPI.setMessage(player, getConfig().getString("website"), 10);
                    return true;
                }
            }else{
                player.sendMessage(getConfig().getString("website"));
            return true;
            }
            if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("ip")) {
                    BarAPI.setMessage(player, getConfig().getString("ip"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("ip"));
                return true;
            }
            if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("donate")) {
                    BarAPI.setMessage(player, getConfig().getString("donate"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("donate"));
                return false;
            }   
            if(getConfig().getString("enable").equals("true")){
                if (args[0].equalsIgnoreCase("vote")) {
                    BarAPI.setMessage(player, getConfig().getString("vote"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("vote"));
                return true;
            }
            if(player.hasPermission("si.admin")){
                if(args[0].equalsIgnoreCase("reload")){
                    this.reloadConfig();
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD+"[MiniSiTool]" + ChatColor.RESET+""+ChatColor.DARK_AQUA+" || " + ChatColor.RESET+""+ChatColor.AQUA+""+ChatColor.BOLD+"Server Reloded!");
                    return true;
                }
            }else{
                player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD+"[MiniSiTool]" + ChatColor.RESET+""+ChatColor.DARK_AQUA+" || " + ChatColor.RESET+""+ChatColor.RED+""+ChatColor.BOLD+"You have no permissions to use this command!");
                return true;
            }
          }
        }
        return false;
      }
    }
    Bailey Payne Yea so currently i did get an error and crashed from the game

    Code:
    java.lang.NullPointerException
    Console    :    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:101) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:367) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.apache.logging.log4j.core.Logger.log(Logger.java:110) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:609) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at org.bukkit.craftbukkit.v1_7_R1.util.ForwardLogHandler.publish(ForwardLogHandler.java:33) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at java.util.logging.Logger.log(Unknown Source) ~[?:1.7.0_09]
    Console    :    at java.util.logging.Logger.doLog(Unknown Source) ~[?:1.7.0_09]
    Console    :    at java.util.logging.Logger.log(Unknown Source) ~[?:1.7.0_09]
    Console    :    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:957) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    :    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Console    : [01:02:47 INFO]: LegitSoulja lost connection: Internal server error
    Console    : [01:02:47 INFO]: LegitSoulja left the game.
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  8. Offline

    mactown21


    Im trying to figure out how to get a true of false value from a string

    enable: true
    enable: false

    If the string in the config equals true do something, else do something else..
    ive tried

    Code:
    if(getConfig().getString("enable").equals("true")){
      if(cmd.getName().equalsIgnoreCase("example")
          //If the string enable: in config = true do this
    }else{
    // if string enable: in config = false or null; do this
    } 
     
  9. mactown21 That looks like a boolean to me. Instead of using getString(path).equals() use getBoolean()
     
  10. Offline

    mactown21

    AdamQpzm
    so since im trying to get a value from a string in config do something like this?

    Code:
    if(getBoolean("enable").equals("true")){
    //do something
    }else{
    do this if false
    }
    I havnt made plugins in a while, so im kindof confused on the order things should go

    This is my boolean

    Code:
     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    and im not able to use getBoolean()
     
  11. Offline

    mazentheamazin

    mactown21
    getBoolean() returns a boolean value. So, it would look like this:
    Code:java
    1. if(getBoolean("enable")) {
    2. //Do something
    3. }else{
    4. //Do something else
    5. }
     
  12. Offline

    mactown21


    I have no string for this, when i use this i get The method getBoolean(String) is undefined for the type

    What if i do something like if(getConfig().getString("enable").contains("true")){
     
  13. mactown21 That's because you're copy and pasting rather than programming.
     
  14. Offline

    JeremyPark

    @mactown21

    Why can't you just use this?

    Code:java
    1. if(getConfig().getBoolean("enable") == true){
    2. if(cmd.getName().equalsIgnoreCase("example")
    3. //If the boolean enable: in config = true do this
    4. }else{
    5. // if boolean enable: in config = false or null; do this
    6. }


    That would be the correct way to get it from the config, (Also, the "== true" is optional, as it will do the same thing without it).

    Also, just something I do when testing is to print out strings to the config in specific parts, to see if they are triggered the way they should be. (Just an idea that might help you)
     
  15. Offline

    mactown21

    AdamQpzm
    -_- im actually righting the code not copying and pasting....!
     
  16. JeremyPark Less 'optional' more 'redundant'. Also, boolean can't be null. And it's generally better not to spoonfeed code. :)

    EDIT: mactown21 Okay, maybe you are writing it out but you're still just blindly doing what's said to you without actually thinking about what it means. Either you don't actually understand or you're not thinking:

    Of course you can't do getBoolean(path) - do you have that method in your class? No. It's not inherited either, so it doesn't exist. You were meant to call it on the config in the same way as getString(). And then you also tried getBoolean(path).equals()? getBoolean() - unsurprisingly - returns a boolean, which do not have an equals() method. Anyone who intends to make plugins should know this as it's basic Java knowledge. If you don't know basic Java, then you'll run into problems a lot. :)
     
  17. Offline

    mactown21


    I will try that


    Im not spoonfeeding, mostly the code that i put on here, i typed and not actually copy and pasted, I dont even copy and paste alot.

    And ive learned java for like a year, and im going back to learning because i quit for like 2 years

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  18. mactown21 I didn't say you were spoonfeeding. I implied you were being spoonfed. If you really did learn Java and have forgotten it, you still need to relearn it :) Also, see my edited post above.
     
  19. Offline

    mactown21

    So mainly, i have 5 strings in my config ip, website, donate, vote, & enable

    It works when the enable = true, but when its false i keep getting the website string from every command i use, even if i do /si reload i get the website string instead of it doing this.reloadConfig();

    Code:
            if(getConfig().getBoolean("barapi") == true){
                if (args[0].equalsIgnoreCase("website")) {
                    BarAPI.setMessage(player, getConfig().getString("website"), 10);
                    return true;
                }
            }else{
                player.sendMessage(getConfig().getString("website"));
                return true;
            }
            if(getConfig().getBoolean("barapi") == true){
                if (args[0].equalsIgnoreCase("ip")) {
                    BarAPI.setMessage(player, getConfig().getString("ip"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("ip"));
                return true;
            }
            if(getConfig().getBoolean("barapi") == true){
                if (args[0].equalsIgnoreCase("donate")) {
                    BarAPI.setMessage(player, getConfig().getString("donate"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("donate"));
                return false;
            } 
            if(getConfig().getBoolean("barapi") == true){
                if (args[0].equalsIgnoreCase("vote")) {
                    BarAPI.setMessage(player, getConfig().getString("vote"), 10);
                  return true;
                }
            }else{
                player.sendMessage(getConfig().getString("vote"));
                return true;
            }
            if(player.hasPermission("si.admin")){
                if(args[0].equalsIgnoreCase("reload")){
                    this.reloadConfig();
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD+"[MiniSiTool]" + ChatColor.RESET+""+ChatColor.DARK_AQUA+" || " + ChatColor.RESET+""+ChatColor.AQUA+""+ChatColor.BOLD+"Server Reloded!");
                    return true;
                }
            }else
                player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD+"[MiniSiTool]" + ChatColor.RESET+""+ChatColor.DARK_AQUA+" || " + ChatColor.RESET+""+ChatColor.RED+""+ChatColor.BOLD+"You have no permissions to use this command!");
                return true;
          }
        }
      return false;
    }
    }
    My label that is connected to args looks like this

    if (label.equalsIgnoreCase("si")) {
    if (args.length == 0) {
     
  20. Offline

    mazentheamazin

  21. Offline

    Drew1080

    mactown21
    Well if the key 'barapi' value is false in the config file then it will print out the string from the key 'website'.
     
  22. Offline

    mactown21


    Soo, should i change the name of barapi to something else?
     
  23. mactown21 What is barapi and please try to explain the logic behind your code? And no, changing won't make much of a difference.
     
  24. Offline

    mactown21

    BarApi is currently a Api for using the enderdragon bar to display text
    http://dev.bukkit.org/bukkit-plugins/bar-api/

    So mainly, im trying to figure out a way to make this a StandAlone plugin where, if you disable it using a string, it will ignore the barapi and go to else and display a message in chat instead
     
  25. Offline

    Drew1080

    mactown21
    No don't change the name it won't make much difference like AdamQpzm said. Just change it's value to true so it will only use the string from the key 'website' in your config when the first argument of the command equals 'website'.
     
  26. Offline

    mactown21


    But im trying to make an else statement so your able to disable / ignore the barapi. and just displace the message(string) in chat instead so mainly, your able to enable or disable it

    so this is barapi - dev.bukkit.org/bukkit-plugins/bar-api/

    if will show text above the enderdragon bar, if the string = false, it will go to the else statement and displace the message from the config string website or any other command in chat instead of showing the text on the bar

    This is really frustrating, and so confused on what to do​
    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  27. Offline

    Drew1080

    mactown21
    Ahh I get what you want todo now.
    To fix this just simply check the first argument of the command is equal to 'website'. Then check if the key 'barapi' is true or false using your if/else statement.
     
  28. Offline

    mactown21

    Drew1080 Ok, I hope this works. :oops:

    Drew1080 But wait, should i use this if(getConfig().getBoolean("bossbar") == true){ or
    if(getConfig().getString("barapi").equals("true")){

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  29. Offline

    TGRHavoc

    mactown21
    if (getConfig().getBoolean("bossbar")) will return true or false depending on the config. No need to check it with "==".
     
Thread Status:
Not open for further replies.

Share This Page