Advertisement
jezzye13

GhostManager

Apr 18th, 2015 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. public class GhostManager {
  2.     /**
  3.      * Team of ghosts and people who can see ghosts.
  4.      */
  5.     private static final String GHOST_TEAM_NAME = "Ghosts";
  6.     private static final long UPDATE_DELAY = 20L;
  7.     // No players in the ghost factory
  8.     private static final OfflinePlayer[] EMPTY_PLAYERS = new OfflinePlayer[0];
  9.     private Team ghostTeam;
  10.     // Task that must be cleaned up
  11.     private BukkitTask task;
  12.     private boolean closed;
  13.     // Players that are actually ghosts
  14.     private Set<String> ghosts = new HashSet<String>();
  15.  
  16.     public GhostManager(Plugin plugin) {
  17.         // Initialize
  18.         createTask(plugin);
  19.         createGetTeam();
  20.     }
  21.  
  22.     private void createGetTeam() {
  23.         Scoreboard board = Bukkit.getServer().getScoreboardManager()
  24.                 .getMainScoreboard();
  25.         ghostTeam = board.getTeam(GHOST_TEAM_NAME);
  26.         // Create a new ghost team if needed
  27.         if (ghostTeam == null) {
  28.             ghostTeam = board.registerNewTeam(GHOST_TEAM_NAME);
  29.         }
  30.         // Thanks to Rprrr for noticing a bug here
  31.         ghostTeam.setCanSeeFriendlyInvisibles(true);
  32.     }
  33.  
  34.     private void createTask(Plugin plugin) {
  35.         task = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
  36.             @Override
  37.             public void run() {
  38.                 for (OfflinePlayer member : getMembers()) {
  39.                     Player player = member.getPlayer();
  40.  
  41.                     if (player != null) {
  42.                         // Update invisibility effect
  43.                         setGhost(player, isGhost(player));
  44.                     } else {
  45.                         ghosts.remove(member.getName());
  46.                         ghostTeam.removePlayer(member);
  47.                     }
  48.                 }
  49.             }
  50.         }, UPDATE_DELAY, UPDATE_DELAY);
  51.     }
  52.  
  53.     /**
  54.      * Remove all existing player members and ghosts.
  55.      */
  56.     public void clearMembers() {
  57.         if (ghostTeam != null) {
  58.             for (OfflinePlayer player : getMembers()) {
  59.                 ghostTeam.removePlayer(player);
  60.             }
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Add the given player to this ghost manager. This ensures that it can see
  66.      * ghosts, and later become one.
  67.      *
  68.      * @param player
  69.      *            - the player to add to the ghost manager.
  70.      */
  71.     public void addPlayer(Player player) {
  72.         validateState();
  73.         if (!ghostTeam.hasPlayer(player)) {
  74.             ghostTeam.addPlayer(player);
  75.             player.addPotionEffect(new PotionEffect(
  76.                     PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Determine if the given player is tracked by this ghost manager and is a
  82.      * ghost.
  83.      *
  84.      * @param player
  85.      *            - the player to test.
  86.      * @return TRUE if it is, FALSE otherwise.
  87.      */
  88.     public boolean isGhost(Player player) {
  89.         return player != null && hasPlayer(player)
  90.                 && ghosts.contains(player.getName());
  91.     }
  92.  
  93.     /**
  94.      * Determine if the current player is tracked by this ghost manager, or is a
  95.      * ghost.
  96.      *
  97.      * @param player
  98.      *            - the player to check.
  99.      * @return TRUE if it is, FALSE otherwise.
  100.      */
  101.     public boolean hasPlayer(Player player) {
  102.         validateState();
  103.         return ghostTeam.hasPlayer(player);
  104.     }
  105.  
  106.     /**
  107.      * Set wheter or not a given player is a ghost.
  108.      *
  109.      * @param player
  110.      *            - the player to set as a ghost.
  111.      * @param isGhost
  112.      *            - TRUE to make the given player into a ghost, FALSE otherwise.
  113.      */
  114.     public void setGhost(Player player, boolean isGhost) {
  115.         // Make sure the player is tracked by this manager
  116.         if (!hasPlayer(player))
  117.             addPlayer(player);
  118.  
  119.         if (isGhost) {
  120.             ghosts.add(player.getName());
  121.             player.addPotionEffect(new PotionEffect(
  122.                     PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
  123.         } else if (!isGhost) {
  124.             ghosts.remove(player.getName());
  125.             player.removePotionEffect(PotionEffectType.INVISIBILITY);
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * Remove the given player from the manager, turning it back into the living
  131.      * and making it unable to see ghosts.
  132.      *
  133.      * @param player
  134.      *            - the player to remove from the ghost manager.
  135.      */
  136.     public void removePlayer(Player player) {
  137.         validateState();
  138.         if (ghostTeam.removePlayer(player)) {
  139.             player.removePotionEffect(PotionEffectType.INVISIBILITY);
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Retrieve every ghost currently tracked by this manager.
  145.      *
  146.      * @return Every tracked ghost.
  147.      */
  148.     public OfflinePlayer[] getGhosts() {
  149.         validateState();
  150.         Set<OfflinePlayer> players = new HashSet<OfflinePlayer>(
  151.                 ghostTeam.getPlayers());
  152.         // Remove all non-ghost players
  153.         for (Iterator<OfflinePlayer> it = players.iterator(); it.hasNext();) {
  154.             if (!ghosts.contains(it.next().getName())) {
  155.                 it.remove();
  156.             }
  157.         }
  158.         return toArray(players);
  159.     }
  160.  
  161.     /**
  162.      * Retrieve every ghost and every player that can see ghosts.
  163.      *
  164.      * @return Every ghost or every observer.
  165.      */
  166.     public OfflinePlayer[] getMembers() {
  167.         validateState();
  168.         return toArray(ghostTeam.getPlayers());
  169.     }
  170.  
  171.     private OfflinePlayer[] toArray(Set<OfflinePlayer> players) {
  172.         if (players != null) {
  173.             return players.toArray(new OfflinePlayer[0]);
  174.         } else {
  175.             return EMPTY_PLAYERS;
  176.         }
  177.     }
  178.  
  179.     public void close() {
  180.         if (!closed) {
  181.             task.cancel();
  182.             ghostTeam.unregister();
  183.             closed = true;
  184.         }
  185.     }
  186.  
  187.     public boolean isClosed() {
  188.         return closed;
  189.     }
  190.  
  191.     private void validateState() {
  192.         if (closed) {
  193.             throw new IllegalStateException(
  194.                     "Ghost factory has closed. Cannot reuse instances.");
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement