Solved Enable/Disable Via Boolean

Discussion in 'Spigot Plugin Development' started by Minersof49ers, May 19, 2016.

Thread Status:
Not open for further replies.
  1. I have a plugin a made, and at the start of the class, it has a boolean set to true. This is what I'm using to regulate whether it's enabled or disabled. Well, without the extra "toggle" at the end of the core command, it gives me the "Internal error" message. Here's the code for the command ( /glassbreaker ) and with the toggle.

    Note the when it says "toggle == true", "toggle" is the boolean at the beginning of the code.
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]args){
         
         
            if (sender.isOp()){
         
                if (args[0].length() == 0){
                 
                    sender.sendMessage(ChatColor.GREEN + "GlassBreaker: Break glass in survival mode to get it's drop!");
                }
                else if (args[0] == "toggle"){
             
                    if (toggle == true){
                        sender.sendMessage(ChatColor.YELLOW + "GlassBreaker disabled!");
                        toggle = false;
                    }
                    else if (toggle == false){
                        sender.sendMessage(ChatColor.YELLOW + "GlassBreaker enabled!");
                        toggle = true;
                    }
                 
                 
                }
    I don't know what I'm doing wrong here. The original command doesn't work anymore, and the toggle feature doesn't do anything.
     
  2. Don't do
    Code (Text):
    if (toggle == true)
    Use
    Code (Text):
    if (toggle) {
    //true
    } else {
    //false
    }
     
    • Informative Informative x 1
  3. Ah, I forgot that works as well. It still doesn't fix the problem though. That same Internal Error message is still coming up.
     
  4. Choco

    Moderator

    Condense this into 2 lines (8 -> 2)
    Code (Java):
    toggle = !toggle;
    sender.sendMessage(ChatColor.YELLOW + "GlassBreaker " + (toggle ? "enabled!" : "disabled!"));
     
    #4 Choco, May 19, 2016
    Last edited: May 19, 2016
  5. Provide the stack trace that occurs in the console for us developers to help you
     
    • Agree Agree x 1
  6. Choco

    Moderator

    Wait wait wait...
    Anything about this line?
    args[0].length()?

    1. No check for length of arguments (I assume that's what you were attempting to do). Could be no arguments (NullPointerException)
    2. You're checking the length of the first index of "args", not the args array itself
    3.
    That should use .equalsIgnoreCase(), and not ==. Use .equals() to compare strings
     
    #6 Choco, May 19, 2016
    Last edited: May 19, 2016
  7. Wow.Okay, so what would I do for the args[0] if I wanted to detect if I put "toggle" after the command /GlassBreaker ?
     
  8. Choco

    Moderator

    You need to check the length of the argument array, and not the length of the first (potential non-existant; ArrayIndexOutOfBoundsException) index.
    Code (Java):
    args.length == 0
    That will check if there are no arguments. As for checking what the first argument is (Assuming there is an argument);
    Code (Java):
    args[0].equalsIgnoreCase("toggle")
     
    • Winner Winner x 1
  9. Oh my gosh, this helped. Thank you so much!
     
  10. JamesJ

    Supporter

    There is no need to do this if it's an internal value.
     
Thread Status:
Not open for further replies.