Idk how to word this

Discussion in 'Spigot Plugin Development' started by GeeGeeTom, May 17, 2015.

  1. Here's part of my code
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("mc")) {
               
                if (chatMuted == false) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been muted by §9" + sender.getName() + "§e.");
                    chatMuted = true;
                   
                }
                if (chatMuted == true) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been unmuted by §9" + sender.getName() + "§e.");
                    chatMuted = false;
                   
                   
                }
               
            }
    When I do /mc it mutes it but then unmutes it straight away, how can I stop it from doing this?
     
  2. Switch the second if to else if

    Code (Text):
    if(cmd.getName().equalsIgnoreCase("mc")) {
             
                if (chatMuted == false) {
                 
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been muted by §9" + sender.getName() + "§e.");
                    chatMuted = true;
                 
                }
              else if (chatMuted == true) {
                 
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been unmuted by §9" + sender.getName() + "§e.");
                    chatMuted = false;
                 
                 
                }
             
            }
     
  3. Because you check after again, you have two options. First use else if:
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("mc")) {
               
                if (chatMuted == false) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been muted by §9" + sender.getName() + "§e.");
                    chatMuted = true;
                   
                }
                else if (chatMuted == true) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been unmuted by §9" + sender.getName() + "§e.");
                    chatMuted = false;
                   
                   
                }
               
            }
    Second is to use return true in the first if
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("mc")) {
               
                if (chatMuted == false) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been muted by §9" + sender.getName() + "§e.");
                    chatMuted = true;
                    return true;
                }
                else if (chatMuted == true) {
                   
                    Bukkit.broadcastMessage("§c§lMC-Network §8:: §eChat has been unmuted by §9" + sender.getName() + "§e.");
                    chatMuted = false;
                   
                   
                }
               
            }
     
  4. else if is not necessary, use else
     
  5. Please use instead of
    if (sth == false)
    this:
    if (!sth)

    and instead of
    if (sth == true)
    this:
    if (sth)

    Makes your code much cleaner.
     
    • Agree Agree x 2
  6. I came here to teach them code, but you taught me something. Thanks for that :p

    Like everyone else said, use
    Code (Text):
    else if {
    }
    .