Solved Boolean value not consistent when toggling?

Discussion in 'Plugin Development' started by oPryzeLP, Dec 2, 2014.

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

    oPryzeLP

    Hello all!
    I just finished coding my first plugin and I'm quite proud of it, however something strange is happening. I wanted to have the command '/dp toggle' switch a section of the plugin on or off. However when the command is entered, it does not ALWAYS toggle each time I enter the command. Most of the time my Boolean variable switches fine, but occasionally I can enter the command repeatedly and it won't switch. This goes on for around 30 seconds, until it magically starts being consistent again.

    My Boolean is declared under my main function:
    public Boolean enabled=true;

    Here's my code for toggling:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
        if(label.equalsIgnoreCase("dp"))
        {
            if(args.length == 1)
            {
                if(args[0].equalsIgnoreCase("toggle"))
                {
                    enabled = !enabled;
                 
                    if(enabled)
                        sender.sendMessage(ChatColor.RED + "DupePatcher has been disabled!");
                    else
                        sender.sendMessage(ChatColor.GREEN + "DupePatcher has been enabled!");
                    return true;
                }
            }
        }
        return false;
    }
    I have a feeling it has to do with the way java or the game stores variables? Again, this is happening at random. Any ideas?

    [​IMG] See how it's inconsistent?
     
  2. Offline

    Rocoty

    Would you mind showing us all the code?
     
    mine-care likes this.
  3. Offline

    jeussa

    You are forgetting the { and } around if(enable) and else
     
  4. Offline

    mine-care

    As said by Rocoty if we have the rest of the code we will be able to trace any problems, for instance the boolean beign changed by another piece of code.
    Thanks.
     
  5. This is valid - the braces are not required for single statements.
     
  6. Offline

    mine-care

    @jeusa They will work this way,

    EDIT: ninja'ed by @AdamQpzm (once more...)
     
    AdamQpzm likes this.
  7. Offline

    oPryzeLP

    Sorry, but yes I do mind with this specific plugin. I guess my real question was is this something that is common where variables in servers aren't consistent at times? Is the variable name "enabled" okay to use? Or should I make it more unique to this plugin? The only other time this variable is used is before the main code of my plugin:

    Code:
    if(enabled = false)
            return;
     
  8. Offline

    Skionz

    oPryzeLP 'enabled' is fine to use. You can use just about anything as a variable name as long as it is not a keyword and not already used. We need your entire class or at least where you declare enabled. Nobody will steal code that does not work.
     
  9. Offline

    Rocoty

    Take a good look at that if-statement. What exactly does it do? It does not do what you think it does.
     
    oPryzeLP and Skionz like this.
  10. Offline

    oPryzeLP

    Oh wow. lol. Simple mistake. And see, I didn't need to post my entire project. ;) Thanks for your help.
     
Thread Status:
Not open for further replies.

Share This Page