How can players with a certain permission see players that are vanished?

Discussion in 'Plugin Development' started by 360_, Oct 18, 2017.

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

    360_

    So i got a Vanish plugin work and I'm trying to make it so players with the permission "staff.all" can see players in /v but normal players won't see the player in /v
    This is the code I got so far
    Code (open)
    Code:
    package me.Server.devtest.command;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class VanishCommand implements CommandExecutor {
    
        private ArrayList<Player> vanished = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You cannot vanish!");
                return true;
            }
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("vanish")) {
                // Check perms
               
                if (!vanished.contains(p)) {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.hidePlayer(p);
                    }
                    vanished.add(p);
                    p.sendMessage(ChatColor.BLUE + "Poof! You have vanished!");
                    Bukkit.broadcast(ChatColor.GOLD + p.getPlayer().getName() + ChatColor.YELLOW + "has vanished", "chat.mod");
                    if(!p.hasPermission("op")) return true;
                }
                else {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.showPlayer(p);
                    }
                    vanished.remove(p);
                    p.sendMessage(ChatColor.BLUE + "Poof! You have unvanished!");
                    Bukkit.broadcast(ChatColor.GOLD + p.getPlayer().getName() + ChatColor.YELLOW + "has unvanished", "chat.mod");
                    if(!p.hasPermission("op")) return true;
                }
            }
            return true;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            for (Player p : vanished) {
                e.getPlayer().hidePlayer(p);
            }
        }
       
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            vanished.remove(e.getPlayer());
        }
    }
    
     
  2. Offline

    RunsWithShovels

    When you are hiding the player, check if the other players have the permission 'staff.all'. Then showplayer to the ones that have the permission.

    Also this almost looks exact like some pogo code
     
  3. Cant you do when the player joins check if he has that permission. If not hide the players
     
  4. Offline

    Horsey

    Just don't hide the player from the people with the permission.
     
  5. Offline

    RunsWithShovels

    That's what I said.....

    Put the check in an if - else statement.

    And you will want to put a check for command usage permissions also.
     
    Last edited by a moderator: Oct 18, 2017
  6. Using what RunWithShovels said, You should be sending packets to all players on the server who have the correct permission. Let them know that "This player is here".
     
  7. Offline

    360_

    So i tried this but it didn't work
    Code:
    package me.Server.devtest.command;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class VanishCommand implements CommandExecutor {
    
        private ArrayList<Player> vanished = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You cannot vanish!");
                return true;
            }
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("vanish")) {
                // Check perms
               
                if (!vanished.contains(p)) {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.hidePlayer(p);
                    }
                    vanished.add(p);
                    p.sendMessage(ChatColor.BLUE + "Poof! You have vanished!");
                    Bukkit.broadcast(ChatColor.GOLD + p.getPlayer().getName() + ChatColor.YELLOW + "has vanished", "chat.mod");
                        if (p.isOp()) {
                            pl.hidePlayer(p);
                    }
                }
                else {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.showPlayer(p);
                    }
                    vanished.remove(p);
                    p.sendMessage(ChatColor.BLUE + "Poof! You have unvanished!");
                    Bukkit.broadcast(ChatColor.GOLD + p.getPlayer().getName() + ChatColor.YELLOW + "has unvanished", "chat.mod");
                    if(!p.hasPermission("op")) return true;
                }
            }
            return true;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            for (Player p : vanished) {
                e.getPlayer().hidePlayer(p);
            }
        }
       
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            vanished.remove(e.getPlayer());
        }
    }
    
     
  8. Where are you checking permissions? Also theres a method inside player to check if they're op. p.isOp() returns true or false
    Im talking about where you put
    p.hasPermission ("op")
     
  9. Offline

    RunsWithShovels

    First you don't need the check for if the player isOp , it needs a permissions check right after the player runs the command, since OPs have all perms it would do away with the need for this check.

    Second, you need to put the check for "staff.all" right before where you hide the player, if a certain player has "staff.all" showplayer, else hideplayer.
     
    Last edited: Oct 20, 2017
Thread Status:
Not open for further replies.

Share This Page