player.canSee code not working ?

Discussion in 'Plugin Development' started by Musicguy, Sep 11, 2012.

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

    Musicguy

    Hey, I'm trying to create a command (/hide in my case) that turns the player invisible.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player player = (Player) sender;
        if(player.hasPermission("invisibleme.hide") == true){
            if(sender instanceof Player) {
                if(cmd.getName().equalsIgnoreCase("hide")){
                    if(player.canSee(player) == false){
                        player.sendMessage(ChatColor.DARK_GREEN + "You are now invisible!");
                        for(Player ply : Bukkit.getOnlinePlayers()){
                            ply.hidePlayer(player);
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "You are already invisible!");
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must be a player!");
            }
        } else {
            player.sendMessage(ChatColor.RED + "You do not have permission.");
        }
       
        return false;
    }
    So what I want to do is check if the player is already invisible then if that is true then it will say "You are already invisible" and if false then it will make the player invisible. But, when I try it on my server, when it's set to false it always says "You are already invisible" (when I'm not) and if I set it to true it always says "You are invisible" when it's supposed to say "You are already invisible".

    Anyone? Anyone ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  2. Offline

    APlusMalware

    So what you're saying is that it's doing the opposite of what it's supposed to do? If so, get rid of the " == false" in your "if" statement.
     
  3. Offline

    Sabersamus

    Also you're saying if(!player.canSee(player)),

    meaning, if i cant see myself{ }
     
  4. Offline

    Musicguy

    When I do that, It just ignores the else to that if.

    Yeah, what should I put instead then ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  5. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Store a player's visibility status as a boolean separately for tracking that stuff.
     
  6. Offline

    Sabersamus

    Code:java
    1.  
    2. for(Player other: Bukkit.getOnlinePlayers()){
    3. if(other.canSee(player)){
    4. other.hidePlayer(player);
    5. player.sendMessage("You are now invisible");
    6. }else{
    7. other.showPlayer(player);
    8. player.sendMessage("You have re-appeared");
    9. }
    10. }


    Although, like mbaxter said you might wanna store if a player is invisible or not somewhere else.

    Like a file, or database or something of that nature.

    that way you can say,

    Code:java
    1. if(getConfig().getBoolean(player.getName() + ".vanished")){
    2. // become invisible
    3. }else{
    4. //become visible
    5. }


    that way, it persists after logging out.
     
  7. Offline

    Musicguy

    Sabersamus When i use your code, and do the command "/hide" it makes me invisible and says "you are invisible blah blah" but, when i do it a second time is it supposed to make me visible? Cause it just says "you are invisible blah blah" and nothing happens. Here is my code for the command:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player player = (Player) sender;
    3. if(player.hasPermission("invisibleme.invisible") == true){
    4. if(sender instanceof Player){
    5. if(cmd.getName().equalsIgnoreCase("hide")){
    6. for(Player other: Bukkit.getOnlinePlayers()){
    7. if(other.canSee(player)){
    8. other.hidePlayer(player);
    9. player.sendMessage("invisible");
    10. } else {
    11. other.showPlayer(player);
    12. player.sendMessage("visible");
    13. }
    14. }
    15. }
    16. } else {
    17. sender.sendMessage("Must be player");
    18. }
    19. } else {
    20. player.sendMessage("You do not have permission.");
    21. }
    22. return false;
    23. }
    24.  
     
  8. Offline

    Sabersamus

    Code:java
    1.  
    2. private boolean vanished = false;
    3.  
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    5. if(cmd.getName().equalsIgnoreCase("hide"){
    6. if(sender instanceof Player){
    7. Player player = (Player)sender;
    8. if(player.hasPermission("blah")){
    9. if(vanished == false){
    10. for(Player other: Bukkit.getOnlinePlayers()){
    11. if(!other.hasPermission("Make a permission so that admins or ops or something can still see everyone"){
    12. other.hidePlayer(player);
    13. }
    14. }
    15. vanished = true;
    16. player.sendMessage(ChatColor.WHAT_EVER + "You are now vanished");
    17. }else{
    18. for(Player other: Bukkit.getOnlinePlayers()){
    19. other.showPlayer(other);
    20. }
    21. player.sendMessage(ChatColor.WHAT_EVER + "You are no longer invisible");
    22. vanished = false;
    23. }
    24. }
    25. }
    26. }
    27. }



    Another way you could do it, is make a hashmap

    Code:java
    1. public static HashMap<String, Boolean> vanished = Maps.newHashMap();


    and where if(vanished == false){ replace that with, if(vanished.contains(player.getName()){


    EDIT: Or a list, instead of a hashmap, that would work better
     
  9. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Sabersamus you just wrote something that would fall apart as soon as two people try using it at once
     
  10. Offline

    Sabersamus

    Thats why i edited it and said use a hashmap or a list
     
Thread Status:
Not open for further replies.

Share This Page