Invisibility Plugin not working properly

Discussion in 'Plugin Development' started by ItsJerry, Oct 4, 2012.

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

    ItsJerry

    Hi, I made a simple plugin which hides you from other players when you type /hide and makes you visible when you type /unhide.

    The problem is that when players relog, invisible players are visible again.
    Source code:

    Code:
    public class jhHide extends JavaPlugin {
     
        Set<String> hiddens = new HashSet<String>();
     
        @Override
        public void onDisable() {
            hiddens.clear();
            for (String s : hiddens) {
                Player player = getServer().getPlayer(s);
                for (Player p : getServer().getOnlinePlayers())
                    p.showPlayer(player);
            }
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("[jhHide] Only players can use this command.");
                return true;
            }
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("hide")) {
                hiddens.add(player.getName());
                sender.sendMessage(ChatColor.YELLOW + "[jhHide] You are now hidden.");
                for (Player p : getServer().getOnlinePlayers())
                    if (!p.isOp())
                        p.hidePlayer(player);
            } else if (cmd.getName().equalsIgnoreCase("unhide")) {
                hiddens.remove(player.getName());
                sender.sendMessage(ChatColor.YELLOW + "[jhHide] You are now visible.");
                for (Player p : getServer().getOnlinePlayers())
                    p.showPlayer(player);
            }
            return true;
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player sender = event.getPlayer();
            if (!sender.isOp())
                for (String s : hiddens) {
                    Player player = getServer().getPlayer(s);
                    sender.hidePlayer(player);
                }
        }
     
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
            Player sender = event.getPlayer();
            hiddens.remove(sender.getName());
            for (String s : hiddens) {
                Player player = getServer().getPlayer(s);
                sender.showPlayer(player);
            }
        }
    }
    I'm not sure why the onPlayerJoin event is not working properly. Please give me some advice.

    Thanks, Jerry
     
  2. try waiting 1-20 ticks before hiding the players on the login event using the schedulars Sync task (NOT async tasks)
     
  3. Offline

    Zidkon

    I'm not sure but I think you don't need to show players again when a players quit.

    Anyway about the problem you can try waiting as ferrybig said already making a runable sync task.

    I'm not sure if this is affecting you now but don't use server.getPlayer() method, use better server.getPlayerExact() since you are getting the names by code there shouldn't be a problem with using getPlayerExact but with using getPlayer some weird times.
     
  4. Offline

    KollegahDerBoss

    Omg I love your Videos Jerry and Harry!!
     
  5. Offline

    ItsJerry

    lol. I found the problem already. I forgot to register the EventHandler using "getServer().getPluginManager().registerEvents(this, this);"
    Dumb mistake, thanks anyways guys.
     
Thread Status:
Not open for further replies.

Share This Page