How to setup config checks?

Discussion in 'Spigot Plugin Development' started by Karatechimp29, May 28, 2015.

  1. ok so i have a few toggles in my config to toggle join messages and remove potion effects all on the event of the player joining:

    Code (Text):
        @EventHandler
        public void joinMessageCheck(PlayerJoinEvent event){
            Player player = event.getPlayer();
           
            if(this.getConfig().getString("Options.RemoveOnJoin").equals("true"))
            {
                 for (PotionEffect effect : player.getActivePotionEffects())
                        player.removePotionEffect(effect.getType());
            }
            else if(this.getConfig().getString("Options.RemoveOnJoin").equals("false"))
            {
                return;
            }
            if(this.getConfig().getString("Toggles.JoinMessage").equals("true"))
            {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Chat-Formatting.Prefix")+this.getConfig().getString("Chat-Formatting.OnJoin")));
            }
            else if(this.getConfig().getString("Toggles.JoinMessage").equals("false"))
            {
                return;
            }
        }
    when i have toggle them say, join message true and potion remove false it sets both to false and i dont see a join message. their has to be a better way to do this?
     
  2. Inkzzz

    Resource Staff

    Get booleans from config's by using getConfig()#.getBoolean("string");
     
  3. @Karatechimp29 read your code again. It's pretty obvious what's going on here.

    On a side note, rather than getting booleans as Strings, get them as booleans. Then you don't need the equals either.
     
    • Like Like x 1
  4. Right, I get the Boolean part and I'm thinking that all the other if statements after the first should be else if?
     
  5. More like, assuming Options.RemoveOnJoin is false, what happens? (I know what happens, this is for you to get a clear view of the code - something like rubber duck debugging :p)
     
  6. Oh, I knew that aha cheers buddy
     
  7. Code (Text):
        @EventHandler
        public void onJoinEvent(PlayerJoinEvent event){
            Player player = event.getPlayer();

            //TODO: Fix Toggle Remove Add-ons
            if(this.getConfig().getBoolean("Toggles.RemoveOnJoin")==true)
            {
                for (PotionEffect effect : player.getActivePotionEffects())
                    player.removePotionEffect(effect.getType());
            }
            else if(this.getConfig().getBoolean("Toggles.RemoveOnJoin")==false)
            {
                return;
            }
            else
            {
                if(player.isOp())
                {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Chat-Formatting.Prefix")+ChatColor.RED+"Config Is Improperly Defined!"));
                    return;
                }
            }

            //TODO: Fix Toggle JoinMessage
            if(this.getConfig().getBoolean("Toggles.JoinMessage")==true)
            {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Chat-Formatting.Prefix")+this.getConfig().getString("Chat-Formatting.OnJoin")));
            }
            else if(this.getConfig().getBoolean("Toggles.JoinMessage")==false)
            {
                return;
            }
            else
            {
                if(player.isOp())
                {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Chat-Formatting.Prefix")+ChatColor.RED+"Config Is Improperly Defined!"));
                }
            }
        }
    here is my join method, why do the toggles not work properly?
     
  8. Code (Java):
     if something is true
    {
        do this
    }
    else if something is false
    {
        do that
    }
    else
    {
        How much do you know about logic, really O.o...
    }
     
    • Winner Winner x 4
  9. konsolas

    Supporter

    GG
     
  10. how much do i really know about logic? my thinking of logic says that if i have one toggle for join messages with a true/false being the
    Code (Text):
    {
        do this
    }
    else if something is false
    {
        do that
    }
    but i have two toggles. one to remove potion effects and another to stop the join messages. logic says that whatever toggle i put first will be read and pass over my second check and get into the plugin. Please stay on topic, if i knew what to do here i would not have made the thread. This is probably so simple yet it's doing my head in. I'm here to look for a solution, not sarcastic remarks...

    I have been tweaking with this all day...

    Config looks something like this:
    Code (Text):
    Toggles:
        RemoveOnJoin: false
        JoinMessage: true
    i need the onPlayerJoin() event to be able to acurateley check both and do the correct actions. at the moment one is overpowering the other and only one is working. if i toggle one it like reverses both?

    Please don't think i'm ranting, i'm just a little wired -_-
     
  11. konsolas

    Supporter

    Now....
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

    There we are.
     
  12. @Karatechimp29 let me clarify it a bit more:
    Code (Java):
    if (remove on join)
        remove them
    else
        return
    if (join message)
        do join message
    else
        return;
    I'm sure that if you know what return does, you know why it doesn't work.
     
    #13 DarkSeraphim, May 28, 2015
    Last edited: May 28, 2015
  13. joehot200

    Supporter

  14. I think you're in the right area. Just one little error I find with all my plugins that I need to fix. When you are checking the config, it is apparently opposite. Therefore:
    Code (Text):
    if (this.getConfig().getBoolean("String.whatever", false) {
    // Put code here for if you DO want them to clear the potion effect.
    } else {
    // Add potion effect or whatever.
    }
     
    So, at least for me, everything is in reverse. Try it that way.
     
  15. i understand this but i dont understand how i do this with my 2 toggles, i think the previous code would be best for me...
     
  16. ok
     
    • Optimistic Optimistic x 1
  17. First off, I have no idea what you're asking. After giving info on how to use if statements, you ask how to create a player object? Trying to make sense of your question though, I'd suggest making sure you know Java before trying to use the Spigot API. It'll make your life a lot easier, trust me.