Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. package net.RevTut.Skywars.managers;
  2.  
  3. import net.RevTut.Skywars.SkyWars;
  4. import net.RevTut.Skywars.arena.Arena;
  5. import net.RevTut.Skywars.player.PlayerDat;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.scoreboard.*;
  9.  
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.UUID;
  13.  
  14. /**
  15.  * Score Board Manager.
  16.  *
  17.  * <P>Manages all the scoreboards in the server.</P>
  18.  *
  19.  * @author WaxCoder
  20.  * @version 1.0
  21.  */
  22. public class ScoreBoardManager {
  23.  
  24.     /**
  25.      * Main class
  26.      */
  27.     private final SkyWars plugin;
  28.  
  29.     /**
  30.      * Map with all the scoreboards of the players
  31.      */
  32.     private final Map<UUID, Scoreboard> scoreBoards = new HashMap<UUID, Scoreboard>();
  33.  
  34.     /**
  35.      * Constructor of ScoreBoardManager
  36.      *
  37.      * @param plugin main class
  38.      */
  39.     public ScoreBoardManager(final SkyWars plugin) {
  40.         this.plugin = plugin;
  41.     }
  42.  
  43.     /**
  44.      * Creates a new scoreboard to a player and saves it to a list of
  45.      * players scoreboards
  46.      *
  47.      * @param p player to create the scoreboard
  48.      */
  49.     public void createScoreBoard(final Player p) {
  50.         // Check if user already has Scoreboard
  51.         Scoreboard board = getScoreBoardByPlayer(p.getUniqueId());
  52.         if (board != null)
  53.             return;
  54.  
  55.         // Create scoreboard manager
  56.         Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  57.             @Override
  58.             public void run() {
  59.                 final ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
  60.                 Bukkit.getScheduler().runTask(plugin, new Runnable() {
  61.                     @Override
  62.                     public void run() {
  63.                         // New scoreboard
  64.                         final Scoreboard newBoard = scoreboardManager.getNewScoreboard();
  65.                         Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  66.                             @Override
  67.                             public void run() {
  68.                                 // Register new objective
  69.                                 final Objective objective = newBoard.registerNewObjective("test", "dummy");
  70.                                 // Display slot
  71.                                 objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  72.  
  73.                                 // Display name
  74.                                 objective.setDisplayName("§7► §3Sky Wars §7◄");
  75.  
  76.                                 // Alive
  77.                                 final Score scoreAlive = objective.getScore("§aAlive:");
  78.                                 scoreAlive.setScore(0);
  79.  
  80.                                 // Dead
  81.                                 final Score scoreDead = objective.getScore("§cDead:");
  82.                                 scoreDead.setScore(0);
  83.  
  84.                                 // Points
  85.                                 final Score scorePoints = objective.getScore("§7Points:");
  86.                                 scorePoints.setScore(0);
  87.  
  88.                                 // Scoreboard footer
  89.                                 final Score separador = objective.getScore("§3----------");
  90.                                 separador.setScore(-1);
  91.  
  92.                                 // Advertisement
  93.                                 final Score website = objective.getScore("§7Website:");
  94.                                 website.setScore(-2);
  95.                                 final Score site = objective.getScore("§3revtut.net");
  96.                                 site.setScore(-3);
  97.  
  98.                                 // Add to the map
  99.                                 scoreBoards.put(p.getUniqueId(), newBoard);
  100.                             }
  101.                         });
  102.                     }
  103.                 });
  104.             }
  105.         });
  106.     }
  107.  
  108.     /**
  109.      * Update alive players in the scoreboard of all the
  110.      * players which are on that arena
  111.      *
  112.      * @param arena arena to update alive players
  113.      */
  114.     public void updateAlive(Arena arena) {
  115.         int alive = arena.getAlivePlayers().size();
  116.         for (PlayerDat alvoDat : arena.getPlayers()) {
  117.             Player alvo = Bukkit.getPlayer(alvoDat.getUUID());
  118.             if (alvo == null)
  119.                 continue;
  120.             Scoreboard board = scoreBoards.get(alvo.getUniqueId());
  121.             if (board == null)
  122.                 continue;
  123.             Objective objective = board.getObjective(DisplaySlot.SIDEBAR);
  124.             objective.getScore("§aAlive:").setScore(alive);
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Update death players in the scoreboard of all the
  130.      * players which are on that arena
  131.      *
  132.      * @param arena arena to update death players
  133.      */
  134.     public void updateDeath(Arena arena) {
  135.         int dead = arena.getDeadPlayers().size();
  136.         for (PlayerDat alvoDat : arena.getPlayers()) {
  137.             Player alvo = Bukkit.getPlayer(alvoDat.getUUID());
  138.             if (alvo == null)
  139.                 continue;
  140.             Scoreboard board = scoreBoards.get(alvo.getUniqueId());
  141.             if (board == null)
  142.                 continue;
  143.             Objective objective = board.getObjective(DisplaySlot.SIDEBAR);
  144.             objective.getScore("§cDead:").setScore(dead);
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Update points of players in the scoreboard
  150.      *
  151.      * @param playerDat player to update the points
  152.      */
  153.     public void updatePoints(PlayerDat playerDat) {
  154.         Player player = Bukkit.getPlayer(playerDat.getUUID());
  155.         if (player == null)
  156.             return;
  157.         final int points = playerDat.getPoints();
  158.         final Scoreboard board = scoreBoards.get(player.getUniqueId());
  159.         if (board == null)
  160.             return;
  161.         Objective objective = board.getObjective(DisplaySlot.SIDEBAR);
  162.         objective.getScore("§7Points:").setScore(points);
  163.     }
  164.  
  165.     /**
  166.      * Get the scoreboard of a given player
  167.      *
  168.      * @param uuid uuid of the player
  169.      * @return scoreboard of the player
  170.      */
  171.     public Scoreboard getScoreBoardByPlayer(UUID uuid) {
  172.         if (scoreBoards.containsKey(uuid))
  173.             return scoreBoards.get(uuid);
  174.         return null;
  175.     }
  176.  
  177.     /**
  178.      * Remove the scoreboard of a player from the Map
  179.      *
  180.      * @param player player which scoreboard will be removed
  181.      * @return true if successfull
  182.      */
  183.     public boolean removePlayerScoreBoard(Player player) {
  184.         if (scoreBoards.containsKey(player.getUniqueId())) {
  185.             scoreBoards.remove(player.getUniqueId());
  186.             return true;
  187.         }
  188.         return false;
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement