Plugin sends all 3 messages.

Discussion in 'Plugin Development' started by BurnerDiamond, Mar 25, 2015.

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

    BurnerDiamond

    It sends all three message instead of just one. How to fix?

    Code:
    package aurora.staffchat;
    
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    
    import java.util.HashMap;
    
    /**
    * Created by maxmigliorini on 24/03/15.
    */
    public class Command implements CommandExecutor {
    
        private HashMap<Player, Boolean> chat = new HashMap<Player, Boolean>();
    
        public HashMap<Player, Boolean> getChat() {
            return chat;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String s, String[] strings) {
    
            if(cmd.getName().equalsIgnoreCase("staffchat")) {
    
                Player p = (Player) sender;
    
                if(!p.hasPermission("expertised.staff.chat")) {
                    p.sendMessage("You do not have permission");
                }
    
                if(p.hasPermission("expertised.staff.chat")) {
    
                    if (chat.get(p) == null) {
                        chat.put(p, true);
                        p.sendMessage("You are now in staff chat");
                        return true;
                    }
    
                    if(chat.get(p) == true) {
                        chat.put(p, false);
                        p.sendMessage("You have left the staff chat");
                        return true;
    
                    }
    
                    if(chat.get(p) == false) {
                        chat.put(p, true);
                        p.sendMessage("You have left the staff chat");
                        return true;
    
                    }
                }
    
            }
            return false;
        }
    }
    
     
  2. @BurnerDiamond You can do that much different and it will work better.

    if(){ }else{
    if(){ }else{
     
  3. Offline

    BurnerDiamond

    I used 2 else if{

    statements but it doesn't seem to work.

    By what I understand it will make check true, then go to the next if statement and it will alays return true.
     
  4. Offline

    RainoBoy97

    First, use a List<String/UUID>.
    Second, it's not possible that it sends all three messages :p
     
  5. Offline

    mine-care

    Oh no!
    Please read my signature to see the issue with this^
    Secondly concerning your problem, you use normal if statements, You should use nested if statement as sugested above.
    (See https://howtoprogramwithjava.com/nested-if-statements/ )
    Thirdly Dont compare booleans as (boolean == true/false), instead to check for true do if(boolean) and for false do if(!boolean)
    Hope that helps
     
  6. Offline

    guitargun

    @BurnerDiamond why a hasmap and not a Set? you can simply check if the player is in the set. if so remove him if not add him. less complicated than what you have
     
  7. Offline

    VortexGmer

    If the p!permission you should return false. Not going to fix probably but looks nice
     
  8. Offline

    nverdier

    @VortexGmer Why would sending the user the usage if they don't have permissions look nice?
     
Thread Status:
Not open for further replies.

Share This Page