Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. package de.evilchicken132.SurvivalGames.Manager;
  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 Ghost_Manager {
  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<String>();
  34.  
  35. public Ghost_Manager(Plugin plugin) {
  36. // Initialize
  37. createTask(plugin);
  38. createGetTeam();
  39. }
  40.  
  41. private void createGetTeam() {
  42. Scoreboard board = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
  43.  
  44. ghostTeam = board.getTeam(GHOST_TEAM_NAME);
  45.  
  46. // Create a new ghost team if needed
  47. if (ghostTeam == null) {
  48. ghostTeam = board.registerNewTeam(GHOST_TEAM_NAME);
  49. }
  50. // Thanks to Rprrr for noticing a bug here
  51. ghostTeam.setCanSeeFriendlyInvisibles(true);
  52. }
  53.  
  54. private void createTask(Plugin plugin) {
  55. task = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
  56. @Override
  57. public void run() {
  58. for (OfflinePlayer member : getMembers()) {
  59. Player player = member.getPlayer();
  60.  
  61. if (player != null) {
  62. // Update invisibility effect
  63. setGhost(player, isGhost(player));
  64. } else {
  65. ghosts.remove(member.getName());
  66. ghostTeam.removePlayer(member);
  67. }
  68. }
  69. }
  70. }, UPDATE_DELAY, UPDATE_DELAY);
  71. }
  72.  
  73. /**
  74. * Remove all existing player members and ghosts.
  75. */
  76. public void clearMembers() {
  77. if (ghostTeam != null) {
  78. for (OfflinePlayer player : getMembers()) {
  79. ghostTeam.removePlayer(player);
  80. }
  81. }
  82. }
  83.  
  84. /**
  85. * Add the given player to this ghost manager. This ensures that it can see ghosts, and later become one.
  86. * @param player - the player to add to the ghost manager.
  87. */
  88. public void addPlayer(Player player) {
  89. validateState();
  90. if (!ghostTeam.hasPlayer(player)) {
  91. ghostTeam.addPlayer(player);
  92. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
  93. }
  94. }
  95.  
  96. /**
  97. * Determine if the given player is tracked by this ghost manager and is a ghost.
  98. * @param player - the player to test.
  99. * @return TRUE if it is, FALSE otherwise.
  100. */
  101. public boolean isGhost(Player player) {
  102. return player != null && hasPlayer(player) && ghosts.contains(player.getName());
  103. }
  104.  
  105. /**
  106. * Determine if the current player is tracked by this ghost manager, or is a ghost.
  107. * @param player - the player to check.
  108. * @return TRUE if it is, FALSE otherwise.
  109. */
  110. public boolean hasPlayer(Player player) {
  111. validateState();
  112. return ghostTeam.hasPlayer(player);
  113. }
  114.  
  115. /**
  116. * Set wheter or not a given player is a ghost.
  117. * @param player - the player to set as a ghost.
  118. * @param isGhost - TRUE to make the given player into a ghost, FALSE otherwise.
  119. */
  120. public void setGhost(Player player, boolean isGhost) {
  121. // Make sure the player is tracked by this manager
  122. if (!hasPlayer(player))
  123. addPlayer(player);
  124.  
  125. if (isGhost) {
  126. ghosts.add(player.getName());
  127. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
  128. } else if (!isGhost) {
  129. ghosts.remove(player.getName());
  130. player.removePotionEffect(PotionEffectType.INVISIBILITY);
  131. }
  132. }
  133.  
  134. /**
  135. * Remove the given player from the manager, turning it back into the living and making it unable to see ghosts.
  136. * @param player - the player to remove from the ghost manager.
  137. */
  138. public void removePlayer(Player player) {
  139. validateState();
  140. if (ghostTeam.removePlayer(player)) {
  141. player.removePotionEffect(PotionEffectType.INVISIBILITY);
  142. }
  143. }
  144.  
  145. /**
  146. * Retrieve every ghost currently tracked by this manager.
  147. * @return Every tracked ghost.
  148. */
  149. public OfflinePlayer[] getGhosts() {
  150. validateState();
  151. Set<OfflinePlayer> players = new HashSet<OfflinePlayer>(ghostTeam.getPlayers());
  152.  
  153. // Remove all non-ghost players
  154. for (Iterator<OfflinePlayer> it = players.iterator(); it.hasNext(); ) {
  155. if (!ghosts.contains(it.next().getName())) {
  156. it.remove();
  157. }
  158. }
  159. return toArray(players);
  160. }
  161.  
  162. /**
  163. * Retrieve every ghost and every player that can see ghosts.
  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("Ghost factory has closed. Cannot reuse instances.");
  194. }
  195. }
  196. }
  197.  
  198. In der Hauptklasse:
  199. public Ghost_Manager ghostFactory;
  200.  
  201. im Onenable:
  202. this.ghostFactory = new Ghost_Manager(this);
  203.  
  204. Wenn er verreckt oder so :
  205. Scoreboard sb = Bukkit.getScoreboardManager().getMainScoreboard();
  206. sb.clearSlot(DisplaySlot.SIDEBAR);
  207. p.setScoreboard(sb);
  208. SurvivalGames.getInstance().ghostFactory.addPlayer(p);
  209. SurvivalGames.getInstance().ghostFactory.setGhost(p, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement