Need to check true/false config

Discussion in 'Plugin Development' started by beeDevop, Jan 21, 2015.

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

    beeDevop

    I'm creating a very random plugin and need help on the devolpment, see the code to understand:

    Code:
    if (cmd.getName().equalsIgnoreCase("motd")) {}
                             p.sendMessage(this.getConfig().getString("motd"));
                             // Allright, and now, help me to create and option like: enable-motd: true or false
                             // If false, don't enable that command
     
  2. Offline

    Rufus5

    Code:
    if(this.getConfig().get("motdenabled") == "true"){
    
    }else{
    
    }
    Then make a string called motdenabled and set it to true.
     
  3. @Rufus5 Wouldn't it actually be:
    Code:
    if(this.getConfig().getBoolean("motdenabled") == true) {
    // If true, do this:
    } else {
    // If false, do this:
    }
     
  4. Offline

    Rufus5

    Code:
    this.getConfig().set("motdenabled", true);
    and
    this.getConfig().getBoolean("motdenabled");
     
    CodePlaysMinecraft likes this.
  5. Offline

    beeDevop

    I want to do with all of these codes: What i do now ? (I try to add your code but it give a eclipse error)
    Code:
    if (cmd.getName().equalsIgnoreCase("motd")) {}
                             p.sendMessage(this.getConfig().getString("motd"));
                    
                     if (cmd.getName().equalsIgnoreCase("info")) {}
                             p.sendMessage(this.getConfig().getString("info"));
                    
                     if (cmd.getName().equalsIgnoreCase("credits")) {}
                             p.sendMessage(this.getConfig().getString("credits"));
                    
                     if (cmd.getName().equalsIgnoreCase("staff")) {}
                             p.sendMessage(this.getConfig().getString("staff-team"));
                            
                     if (cmd.getName().equalsIgnoreCase("helpme")) {}
                             Bukkit.broadcastMessage(this.getConfig().getString("help-brodcast"));
                             
     
  6. Offline

    1Rogue

    For one, == compares for object instances, it doesn't compare for value:

    Code:java
    1. String one = "true";
    2. String two = new String("true");
    3. System.out.printf("%B\n", one == two);


    Would print "FALSE".

    Secondly, if/while/else/do statements take a boolean parameter, you don't need to compare a boolean to another boolean and then pass it:

    Code:java
    1. if (this.getConfig().getBoolean("example")) {
    2. // value is true
    3. } else {
    4. // value is false
    5. }
     
  7. Offline

    beeDevop

    It's like this? Eclipse continues with the error on te second else word!

    Code:
    if (this.getConfig().getBoolean("motdenabled")) {
                if (cmd.getName().equalsIgnoreCase("motd")) {}
                p.sendMessage(this.getConfig().getString("motd"));
                } else
                p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
              
                   
                if (this.getConfig().getBoolean("infoenabled"))
                if (cmd.getName().equalsIgnoreCase("info")) {}
                p.sendMessage(this.getConfig().getString("info"));
                } else //Error
                p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
     
  8. Offline

    dart2112

    Try this,

    Code:
    if (this.getConfig().getBoolean("motdenabled")) {
                if (cmd.getName().equalsIgnoreCase("motd")) {
                p.sendMessage(this.getConfig().getString("motd"));
                }
                } else {
                p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
                 {
                if (this.getConfig().getBoolean("infoenabled")){
                if (cmd.getName().equalsIgnoreCase("info")) {
                p.sendMessage(this.getConfig().getString("info"));
                } else{ //Error
                p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
                }
               }
    just basic formatting that was wrong.
     
  9. Offline

    beeDevop

    Ok and on my config, is like that (I didn't tried, let me have sure first)

    # beeBasic config
    # Do not remove '' of texts or you will make the plugin crash!

    # MOTD:

    # Enable the /motd command? Use true for yes and false for no
    enable-motd: true
    # Now, what will be the text of your MOTD? Do not remove the '', that will not appear, it's just Bukkit requiriments
    motd: 'This is the default beeBasic MOTD. You can modify this in plugins/beeBasic/config.yml'


    P.S: Yes, i put enable-motd and enabe-info on the code
     
  10. Offline

    dart2112

    don't see why it wouldn't work now, if it still doesn't work, make sure that if there is an error that you post the stack trace
     
  11. Code:java
    1.  
    2. if (getConfig().getBoolean("motdenabled")) {
    3. if (cmd.getName().equalsIgnoreCase("motd")) {
    4. p.sendMessage(getConfig().getString("motd"));
    5. return true;
    6. }
    7. } else {
    8. p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
    9. return true;
    10. }
    11. if (getConfig().getBoolean("infoenabled")){
    12. if (cmd.getName().equalsIgnoreCase("info")) {
    13. p.sendMessage(getConfig().getString("info"));
    14. return true;
    15. } else {
    16. p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
    17. return true;
    18. }
    19.  
     
  12. Offline

    beeDevop

    I used @MaTaMoR_ code and this work, but when i disable MOTD, info disables too. If i disable just info, motd keep enabled but when i do /info the plugin didn't reply that isn't enabled.
    Code:
    if (getConfig().getBoolean("enable-motd")) {
                    if (cmd.getName().equalsIgnoreCase("motd")) {               
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',this.getConfig().getString("motd")));
                            return true;
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
                        return true;
                            }
                if (getConfig().getBoolean("enable-info")){
                    if (cmd.getName().equalsIgnoreCase("info")) {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',this.getConfig().getString("info")));
                            return true;
                    } else {
                        p.sendMessage(ChatColor.RED + "This command is not enabled here! Please contact your server admin!");
                        return true;
     
  13. Offline

    1Rogue

    Format your code properly yourself, don't rely on other people to do it for you: https://google-styleguide.googlecode.com/svn/trunk/javaguide.html
     
  14. Offline

    beeDevop

  15. Offline

    JustThiemoo

    Dont know if it makes sence, you could try, instead of if, (second time) else if. (A), and you forget } at the end.
     
Thread Status:
Not open for further replies.

Share This Page