True or false in config

Discussion in 'Spigot Plugin Development' started by alessio2010, Jun 6, 2017.

  1. Code (Text):

    if (!getConfig().getBoolean("BandageRecipe")) {

    //run false code here, if needed

    } else {

    //run true code here
    {
    in your case !plugin.getConfig()


    EDIT,

    in config you're using ' ' on a Boolean, not needed. Also pretty sure that setup might not work.
     
  2. Here's what you will need:
    Code (Text):
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
           if(!plugin.getConfig().getBoolean("joinmessage")) {
               return;
           }
            if (plugin.getConfig().getBoolean("joinmessage")) {
                String msg = config.getString("joinmessagemessage");
                e.getPlayer().sendMessage(msg);
            }
         
        }
       
    I know people generally don't spoon-feed on here, but I like to do it with an explanation of what I did.

    Tips:
    1) Don't need "== true/false"s on if statements. Just use a "!" to check if its false and just leave it alone to check if its true.
    2) A return; can be used in anything but a boolean parameter. A "return;" will cancel anything below. In a boolean, you can use "return false;" to return nothing, even inside the brackets its in. Or, you can use "return true;" to return everything outside the brackets.
    3) I suggest you learn Java and read up on the Bukkit API. It's not bad being new, we all start somewhere.
     
    • Like Like x 2
  3. Alright, Thank you very much!
     
  4. It does not work.. If i set it to true it works but when it's set to false it still sends the event.
     
  5. Okay so i found the problem, you added another if which you're not supposed to do and just let the else be running the event. Thanks!
     
  6. Code (Text):

     @EventHandler
           public void onJoin(PlayerJoinEvent e) {
              if(!plugin.getConfig().getBoolean("joinmessage")) {
                  return;
              }
                   
                   String msg = config.getString("joinmessagemessage");
                   e.getPlayer().sendMessage(msg);
               }
             
           }
     
     
  7. Why not just use a setup like this below?

    Code (Text):
    JoinMessage:
      Enabled: true
      Message: "test"
    also you're not even pulling out a message, you need to do config.getstring("joinmessage.message");

    Then you basically do the same to check if its true..

    getBoolean("joinmessage.Enabled");
     
  8. I am pulling a string... look closer
     
  9. No, just... No.
    I suggest you do too.
     
  10. Gianluca

    Gianluca Retired Resource Staff
    Retired Patron

    Firstly, an if statement checks if a value is true and then runs it, so, the "== true" is redundant.

    For the message, you're pulling from "config.getString("joinmessagemessage")" whereas for the boolean, you're pulling from "
    plugin.getConfig().getBoolean("joinmessage")". Could it be that the config call for the message doesn't have anywhere to call from?