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?
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; } }
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; } }
Please use instead of if (sth == false) this: if (!sth) and instead of if (sth == true) this: if (sth) Makes your code much cleaner.
I came here to teach them code, but you taught me something. Thanks for that Like everyone else said, use Code (Text): else if { } .