Advertisement
Guest User

GhostFactory.java

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