Solved Vanish all players (fairly simple)

Discussion in 'Plugin Development' started by michaelkrauty, Sep 8, 2013.

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

    michaelkrauty

    Hello! I'm working on a plugin that makes all players invisible to the command sender (exactly like MagicClock). So far I have this:

    Code:java
    1. if(args[0].equalsIgnoreCase("vanishall")){
    2. if(sender.hasPermission("e.use") || sender.hasPermission("e.vanishall")){
    3. if(args.length == 1){
    4.  
    5. for(Player players : sender.getServer().getOnlinePlayers()){
    6. player.hidePlayer(players);
    7. }
    8.  
    9. }else{
    10. sender.sendMessage("Usage: /e vanishall");
    11. return true;
    12. }
    13. }else{
    14. sender.sendMessage("You don't have permission to do that!");
    15. return true;
    16. }
    17. }


    This works fine, but I need a way to hide players that join after the command is issued. As it is, only players that are online when the command is issued are hidden. Help?

    Thanks.
     
  2. Offline

    Chinwe

    You need to add a player's name to a List when they vanish everyone. Then listen to PlayerJoinEvent, and loop through all players in the List: if they're in it, you can get your original player from the list (Bukkit.getPlayerExact(stringFromList)) and hide them :)
     
    michaelkrauty likes this.
  3. Offline

    michaelkrauty

    Alright

    Code:java
    1. //Main class (E.class):
    2.  
    3. public static ArrayList<String> hidden = new ArrayList<String>();
    4. public static ArrayList<String> online = new ArrayList<String>();
    5.  
    6.  
    7. @Override
    8. public void onEnable(){
    9. PluginManager plm = this.getServer().getPluginManager();
    10. plm.registerEvents(new JoinListener(), this);
    11.  
    12.  
    13. //...unimportant code...
    14.  
    15.  
    16. if(args[0].equalsIgnoreCase("vanishall")){
    17. if(sender.hasPermission("e.use") || sender.hasPermission("e.vanishall")){
    18. if(args.length == 1){
    19. if(!hidden.contains(playerName)){
    20. hidden.add(playerName);
    21. for(Player online : sender.getServer().getOnlinePlayers()){
    22. player.hidePlayer((Player) online);
    23. }
    24. }else{
    25. hidden.remove(playerName);
    26. for(Player online : sender.getServer().getOnlinePlayers()){
    27. player.showPlayer((Player) online);
    28. }
    29. }
    30. }else{
    31. sender.sendMessage("Usage: /e vanishall");
    32. return true;
    33. }
    34. }else{
    35. sender.sendMessage("You don't have permission to do that!");
    36. return true;
    37. }
    38. }
    39.  
    40.  
    41. //JoinListener.class:
    42.  
    43. public class JoinListener implements Listener{
    44.  
    45. public E plugin;
    46. @EventHandler
    47. public void onPlayerJoin(PlayerJoinEvent event){
    48. Player player = event.getPlayer();
    49. String playerName = player.getName();
    50. player.sendMessage("sup bro! :D");
    51. E.online.add(playerName);
    52.  
    53. }
    54.  
    55. }
    56.  



    Players who are online when the command is issued vanish, but people who weren't online when the command was issued aren't hidden when they log in..

    So what am I missing?
     
  4. Offline

    EdenCampo

    michaelkrauty

    You gotta loop again, not just add him to the list.
     
  5. Offline

    michaelkrauty

    How would I do that? I can't seem to figure it out
     
  6. Offline

    1Rogue

    Why not just hide the player in an onJoin method?

    Code:java
    1. public void onPlayerJoin (PlayerJoinEvent event) {
    2. event.getPlayer().hidePlayer();
    3. // Other handling code / add to lists / etc....
    4. }


    Also, you don't need to cast Player to an object that is already Player
     
  7. Offline

    michaelkrauty

    The point isn't to vanish all players on the entire server, it's to hide the players from the command sender
     
  8. Offline

    1Rogue


    In that case I would store a List (as previously mentioned) of vanished players in a custom player class. You could use a player manager class as well for getting the appropriate player

    PlayerManager
    Code:java
    1. public class PlayerManager {
    2.  
    3. private final Map<String, CustomPlayer> players = new ConcurrentHashMap();
    4.  
    5. public CustomPlayer getPlayer(String name) {
    6. return this.players.get(name);
    7. }
    8.  
    9. public void addPlayer(/* ... */) {
    10. /* ... */
    11. }
    12.  
    13. public void remPlayer(String name) {
    14. this.players.remove(name);
    15. }
    16.  
    17. }


    CustomPlayer
    Code:java
    1. public class CustomPlayer {
    2.  
    3. private final List<Player> hidden = new ArrayList();
    4.  
    5. public void hidePlayer(Player add) {
    6. // Hiding code
    7. this.hidden.add(add);
    8. }
    9.  
    10. public void unhidePlayer(Player rem) {
    11. // Unhiding code
    12. this.hidden.remove(rem);
    13. }
    14.  
    15. }
     
  9. Offline

    michaelkrauty

    It looks like all I need to do is loop through the (*the command sender*).hidePlayer(*the person who joined*) when a player joins. That's all I need to do from here. I just can't figure out how
     
  10. Offline

    michaelkrauty

  11. Offline

    Quantix

    The code below is what I think you're looking for. It's just an example so you can change the way the command works etc.
    Code:java
    1. import java.util.HashSet;
    2. import java.util.Set;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class VanishAll extends JavaPlugin implements Listener {
    15.  
    16. public static Set<String> hidingPlayers = new HashSet<String>();
    17.  
    18. @Override
    19. public void onEnable() {
    20. getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. @Override
    24. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    25. if (sender instanceof Player) {
    26. Player player = (Player) sender;
    27. if (command.getName().equalsIgnoreCase("vanishall")) {
    28. if (player.hasPermission("e.use") || player.hasPermission("e.vanish")) {
    29. if (!hidingPlayers.contains(player.getName())) {
    30. hidingPlayers.add(player.getName());
    31. for (Player online : getServer().getOnlinePlayers()) {
    32. player.hidePlayer(online);
    33. }
    34. player.sendMessage(ChatColor.GRAY + "All other players have been hidden from you!");
    35. } else {
    36. hidingPlayers.remove(player.getName());
    37. for (Player online : getServer().getOnlinePlayers()) {
    38. if (!player.canSee(online)) {
    39. player.showPlayer(online);
    40. }
    41. }
    42. player.sendMessage(ChatColor.GRAY + "All other players have been revealed to you!");}
    43. } else {
    44. player.sendMessage(ChatColor.RED + "You don't have permission to use that command!");
    45. }
    46. }
    47. }
    48. return false;
    49. }
    50.  
    51. @EventHandler (priority = EventPriority.NORMAL)
    52. public void onPlayerJoin (PlayerJoinEvent event) {
    53. if (!hidingPlayers.isEmpty()) {
    54. for (String name : hidingPlayers) {
    55. Player hidingPlayer = getServer().getPlayerExact(name);
    56. if (hidingPlayer != null) {
    57. hidingPlayer.hidePlayer(event.getPlayer());
    58. } else {
    59. hidingPlayers.remove(name);
    60. }
    61. }
    62. }
    63. }
    64. }
    Hope this helps!
     
    michaelkrauty likes this.
  12. Offline

    michaelkrauty

    Thank you, that worked :)
     
Thread Status:
Not open for further replies.

Share This Page