Need help with vanish

Discussion in 'Spigot Help' started by flimosch, Jun 9, 2017.

  1. This is my code, but it's just vanishing players for new / joining players
    Code (Text):
    package Ghost.Commands.Server;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    import Ghost.main;
    import Ghost.Commands.Command;
    import Ghost.Commands.CommandCategory;

    public class Command_VANISH extends Command implements Listener{
        private ArrayList<Player> vanished = new ArrayList<Player>();

        public Command_VANISH() {
            super("Vanish", CommandCategory.SERVER);
            Bukkit.getServer().getPluginManager().registerEvents(this, main.instance);
        }

        @Override
        public void onCommand(Player p, ArrayList<String> args) {
            if(args.size() == 0){
                if (!vanished.contains(p)) {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.hidePlayer(p);
                    }
                    vanished.add(p);
                    p.sendMessage(main.instance.prefix+ "Du bist nun vanish!");
                }
                else {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        Bukkit.getPlayer(pl.getName()).showPlayer(p);
                    }
                    vanished.remove(p);
                    p.sendMessage(main.instance.prefix+ "Du bist nun nicht mehr vanish!");
                }
            }
            else{
                p.sendMessage(main.instance.prefix+"ยง4Error 503: Under Constructing");
            }
        }

        @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. Make a PlayerJoinEvent to handle the joining players.

    Also Bukkit.getPlayer() is not needed as you are already looping through the players.