Solved Altering value in config and reloading

Discussion in 'Plugin Development' started by ohtwo, Mar 6, 2013.

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

    ohtwo

    Debug Method:
    Code:java
    1.  
    2.  
    3. private void debugMessage(String string) {
    4. if(this.plugin.getConfig().getBoolean("debug"))
    5. this.plugin.getServer().broadcastMessage(ChatColor.LIGHT_PURPLE + "[debug] " + string);
    6. }
    7.  


    (The following code works in practice, that is, the command itself works perfectly)
    Debug Mutator:
    Code:java
    1.  
    2. if(command.equals("debug")) {
    3. if(this.plugin.getConfig().get("debug").toString().equals("false")) {
    4. this.plugin.getConfig().set("debug", "true");
    5. this.plugin.saveConfig();
    6. this.plugin.reloadConfig();
    7. debugMessage("Switched debug message to true");
    8. this.plugin.debug = this.plugin.getConfig().getBoolean("debug");
    9. returntrue;
    10. }
    11. else if(this.plugin.getConfig().get("debug").toString().equals("true")) {
    12. debugMessage("Switching debug value to false");
    13. this.plugin.getConfig().set("debug", "false");
    14. this.plugin.saveConfig();
    15. this.plugin.reloadConfig();
    16. this.plugin.debug = this.plugin.getConfig().getBoolean("debug");
    17. returntrue;
    18. }
    19. else
    20. returnfalse;
    21.  


    I have an issue where when I mutate the value in the config to show debug messages, I still get debug messages when the value is false (I have confirmed that the config data does change). How do I make it so that I can "turn my debug off?"
     
  2. Offline

    ZeusAllMighty11

    Huh?


    Why are you cehcking for string values of a boolean instead of the actual boolean? That's why YAML api files have a getBoolean() method

    Code:
    getConfig().getBoolean("My.Path.To.Boolean");
    
    Code:
    boolean explosionEnabled(){
    return getConfig().getBoolean("All_Explosions.Allowed");
    }
    

    From there, I can do :
    Code:
    if(explosionEnabled()){
    // explosions are enabled , meaning 'true' in config file
    } else {
    // not enabled
    }
    
    I don't see what you are doing sorry



    Edit: Also I think you should compare your booleans in the config with " == " instead of using toString() methods and such, because it's a a waste of time, code space, and in this case errors


    And for what I thought the title of the thread was, you can refresh the file and retrieve the value almost instantaneously by using save() and reload() methods (I think reload())
     
  3. Offline

    ohtwo

    ZeusAllMighty11

    I wrote this a long time ago and I wasn't aware of the method. Even still I feel like I will get the same result. I will try it anyway.

    EDIT: Actually apparently I was aware of the method because I have used it elsewhere. However, I probably was overthinking it when I did the if-else checks.
     
  4. Offline

    ZeusAllMighty11

    ohtwo

    Not gonna lie your thread was pretty much a TL;DR for me but I looked at some and I see:

    Code:
    this.plugin.getConfig().set("debug", "true");
    
    Don't do that, you can literally do :

    Code:
    this.plugin.getConfig().set("debug", true);
    
    And retrieve the boolean with no silly parsing or errors! Just remember to use == or .equals()
    I think booleans use == though, and enums use .equals()
     
    ohtwo likes this.
  5. Offline

    ohtwo

    Just tested it and I still get the same result. I'm not exactly sure how to even approach this problem, and I have about a thousand lines of code spanning across 10 classes. Would it help to show my onEnable()?

    EDIT: Sent this before I got your last post. I'll try it. Sorry, I figure I'd give as much information as possible. I hate it when people don't give enough or refuse to give more and expect help.

    This worked perfectly. I didn't realize set() was an overloaded method. Good to know, thank you.

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

    ZeusAllMighty11

    I understand it's fine, no problem! Happy coding
     
Thread Status:
Not open for further replies.

Share This Page