Added to wrong arraylist?

Discussion in 'Plugin Development' started by Tommy Raids, Dec 10, 2014.

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

    Tommy Raids

    Hello everybody, I appreciate you trying to help me. Basically, I'm coding a /vanish command like McPvP's. When a youtuber vanishes, no players can see them except youtubers/mods/admins. When a mod vanishes, no one can see them besides mods/admins and when an admin vanishes, no one can see them unless they're an admin aswell.
    When ever I type /invis, it will always just "admin" vanish me. (The first instance) no matter if I have the youtuber or mod perms. Other than that, the see perms work so no need to stress about that. I suppose this is just a stupid error I've been looking over, but could someone please halp? (No error in console/ingame.)

    Code:
    ArrayList<UUID> hidden = new ArrayList<UUID>();
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
           
            if(!(sender instanceof Player))
                return false;
           
            Player p = (Player) sender;
           
            if(command.getName().equalsIgnoreCase("invis")){
                if(!p.hasPermission("litus.vanish")){
                    p.sendMessage("Unknown command. Type \"/help\" for help.");
                } else if(!hidden.contains(p.getUniqueId())){
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()){
                        //admin vanish
                        if(p.hasPermission("admin")){
                            if(!pl.hasPermission("admin")){
                                pl.hidePlayer(p);
                            }else{
                                if(p.hasPermission("admin")){
                                    pl.showPlayer(p);
                                }
                        }
                        }
                    }   hidden.add(p.getUniqueId());
                        p.sendMessage(ChatColor.LIGHT_PURPLE + "You are now invisible to (MOD) and below.");
                        p.setGameMode(GameMode.CREATIVE);
                        return true;
                       
                       
                } else if(!hidden.contains(p.getUniqueId())){
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()){
                        //mod vanish
                        if(p.hasPermission("mod")){
                            if(!pl.hasPermission("mod") || !pl.hasPermission("admin")){
                                pl.hidePlayer(p);
                            }else{
                                if(pl.hasPermission("mod") || pl.hasPermission("admin")){
                                    pl.showPlayer(p);
                                }
                            }
                        }
                       
                    }hidden.add(p.getUniqueId());
                    p.setGameMode(GameMode.CREATIVE);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + "You are now invisible to (YOUTUBER) and below.");
                    return true;
                   
                   
                   
                }else if(!hidden.contains(p.getUniqueId())){
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()){
                        if(p.hasPermission("youtube")){
                            if(!pl.hasPermission("youtube") || !pl.hasPermission("mod") || !pl.hasPermission("admin")){
                                pl.hidePlayer(p);
                            }else{
                                if(pl.hasPermission("youtube") || pl.hasPermission("mod") || pl.hasPermission("admin")){
                                    pl.showPlayer(p);
                                }
                            }
                        }
                    }hidden.add(p.getUniqueId());
                     p.setGameMode(GameMode.CREATIVE);
                     p.sendMessage(ChatColor.LIGHT_PURPLE + "You are now invisible to all (NON-STAFF) and below.");
                     return true;
                }
                   
                }  else{
                    if(hidden.contains(p.getUniqueId())){
                        if(command.getName().equalsIgnoreCase("vis")){
                            for (Player pl : Bukkit.getServer().getOnlinePlayers()){
                                pl.showPlayer(p);
                            }
                            hidden.remove(p.getUniqueId());
                            p.setGameMode(GameMode.SURVIVAL);
                            p.sendMessage(ChatColor.GREEN + "You are now visible to all players.");
                            return true;
                    }
                    }
                   
                } 
           
           
            return true;
     
  2. Offline

    Skionz

    @Tommy Raids Debug and tells us where it isn't working.
     
  3. Offline

    Tehmaker

    @Tommy Raids

    Quick way to debug is to send messages to the console after every if statement that you think isn't runnign right, and check to make sure they are running properly.

    Once you can tell us what isn't working properly, we can point you in the right direction.
     
  4. Offline

    Tommy Raids

    I added a statement under the adding to the arraylist of hidden, on perm admin and perm mod. When I do /invis as a mod it gives me the admin check if it's working.. nothing else
    for some reason the code isn't going through to the next line if it's the correct perm or something o-o

    @Skionz
     
  5. Offline

    Skionz

  6. Offline

    Tehmaker

    @Tommy Raids
    Sorry, I am having some trouble following what you are saying, could you please clarify?

    So what I am getting at is, when you are using the command as a moderator, the code stops when it checks if the player has the administrator permission node?
     
  7. Offline

    Tommy Raids

    Yes, I have the mod permission, and not the admin. Yet it still gives me the admin vanish.

    Haha, sorry. Basically I have a vanish command for 3 different groups of players. (Admin, Mod, Youtuber). Each group has specific/different perms that I wouldn't want other groups having. The error is that even if I don't have the perm to vanish as an admin, if I have the mod/youtuber perm, when vanishing it will vanish me as an admin. :p

    Guessing this is really confusing for you guys aswell? :/
    @Skionz @Tehmaker

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  8. Offline

    mythbusterma

    @Tommy Raids

    This isn't very confusing to me at all. The only thing that is confusing is what you're trying to accomplish here, you have three mutually exclusive statements in a row that are supposed to be checking for the different permissions, yet each one begins with if (!..contains) etc.
     
  9. Offline

    Tommy Raids

    What would you suggest?.. Yes, I used if (!..contains) because I found it easier to check that way.
     
  10. @Tommy Raids Indentation isn't just a pretty face :) It intended to show you the structure of your logic. Here's an approximation of what your code is doing, with correct indentation

    Code:
    if the player doesn't have the "litus.vanish" permission, then...
        send them a message
    Otherwise, then...
        if hidden doesn't contain the player's UUID, then...
            for every online player:
                if the player being hidden has the admin permission, then...
                    if the online player doesn't have the admin permission, then...
                        hide the player to hide from the online player
                    otherwise, then...
                        if the player has the admin permission, then...[1]
                            show the player to hide to the online player
            add the player's uuid to hidden, put them in creative, and send them a message
        otherwise, if hidden doesn't contain the player's UUID, then... [2]
            for every online player:
                // And so on. Not gonna rewrite the whole thing :)
    If you're still not able to see some logical errors there, here are a couple of things I'd like to point out

    [1] At this point, what's this check for? It's a bit redundant - you know that at this point the player must have permission, since this is in the else check to the "doesn't have permission" check... in other words, if they player didn't have permission, this bit of code wouldn't be running.

    [2] This check is "If the player isn't in the hidden field, then do something, otherwise if the player isn't in the hidden field then do something".... are you sure that's right?
     
    Tehmaker likes this.
  11. Offline

    Tommy Raids

    :3 I see your point, but what do you mean if I'm sure that the arraylist checking is right? Why wouldn't it be?..
     
  12. Offline

    Tommy Raids

    So I should remove it except for the first instance.. xD
     
  13. Offline

    Tommy Raids

    Alrighty, thanks. :)
     
Thread Status:
Not open for further replies.

Share This Page