Setting a boolean

Discussion in 'Plugin Development' started by RoboticPlayer, Sep 9, 2015.

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

    RoboticPlayer

    I am creating a plugin which has a global mute feature, and when a command is run, it toggles the global mute. I have a boolean value created to store the whether or not the mute is enabled, but I don't know how to set it to true or false. Here is a snippet of the code:
    Code:
    boolean globalmute = false;
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            if (globalmute == true) {
                e.setCancelled(true);
                // Tell player the chat is muted
            }
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("chatmanager")) {
                if (args[0].equalsIgnoreCase("globalmute")) {
                    if (globalmute == true) {
                        // Broadcast that the mute is disabled
                        // Set globalmute to false
                    } else if (globalmute == false) {
                        // Broadcast that the mute is enabled
                        // Set globalmute to true
                    }
                }
            }
            return true;
        }
    I didn't put in the full code because it isn't really necessary, I just wanted to show what I'm talking about. And another, smaller thing with that, how is it possible to send messages that are bold and colored? Because it doesn't let you do
    Code:
    sender.sendMessage(ChatColor.RED + ChatColor.BOLD + "message");
    It gives the error:
    Code:
     The operator + is undefined for the argument type(s) org.bukkit.ChatColor, org.bukkit.ChatColor   
     
  2. Offline

    SuperOriginal

    You need to insert a string between ChatColor.RED and ChatColor.BOLD.

    Code:
    ChatColor.RED + "" + ChatColor.BOLD + "message"
    That should achieve the same effect.
     
  3. Offline

    RoboticPlayer

  4. Offline

    SuperOriginal

  5. Offline

    RoboticPlayer

  6. Offline

    rbrick

  7. Offline

    SuperSniper

    @henderry2019
    Do this for checking booleans:

    TRUE:
    Code:
    if(globalmute) {
    
    FALSE:
    Code:
    if(!globalmute) {
    
     
  8. Offline

    RoboticPlayer

    For some reason, it isn't setting the boolean. Here is a snippet of the code (note that I changed the boolean name from globalmute to isGloablMute):
    Code:
    if (isGlobalMute) {
                        Bukkit.getServer().broadcastMessage(
                                ChatColor.DARK_RED + "" + ChatColor.BOLD + "GlobalMute is no longer enabled!");
                        isGlobalMute = false;
                        return true;
                    } else if (!isGlobalMute) {
                        Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "GlobalMute has been enabled!");
                        isGlobalMute = true;
                        return true;
                    }
    The way I know it's not setting the boolean is because if I set my listener to if(!isGlobalMute), it won't let me chat, even if I run the command.

    EDIT: I was looking at my code, and I am now even more confused. The thing is, if it wasn't setting the boolean, then it would always print that GlobalMute has been enabled, but the message toggles, while the ability to chat does not. Any help?
     
  9. Offline

    mythbusterma

    @henderry2019

    "isGlobalMute" would be the name of a method. You had it correct before.

    The issue is you're using different instances of the same class for registering the command and the listener. Don't do that.
     
Thread Status:
Not open for further replies.

Share This Page