Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. public class SimpleScoreboard
  2. {
  3.   private Objective obj;
  4.   private Scoreboard scoreboard;
  5.   private String title;
  6.   private Map<String, Integer> scores;
  7.   private List<Team> teams;
  8.  
  9.   public SimpleScoreboard(String title)
  10.   {
  11.     this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
  12.     this.title = title;
  13.     this.scores = Maps.newLinkedHashMap();
  14.     this.teams = Lists.newArrayList();
  15.   }
  16.  
  17.   public void blankLine()
  18.   {
  19.     add(" ");
  20.   }
  21.  
  22.   public void add(String text)
  23.   {
  24.     add(text, null);
  25.   }
  26.  
  27.   public void add(String text, Integer score)
  28.   {
  29.     Preconditions.checkArgument(text.length() < 48, "text cannot be over 48 characters in length");
  30.     text = fixDuplicates(text);
  31.     this.scores.put(text, score);
  32.   }
  33.  
  34.   private String fixDuplicates(String text)
  35.   {
  36.     while (this.scores.containsKey(text)) {
  37.       text = text + "ยงr";
  38.     }
  39.     if (text.length() > 48) {
  40.       text = text.substring(0, 47);
  41.     }
  42.     return text;
  43.   }
  44.  
  45.   private Map.Entry<Team, String> createTeam(String text)
  46.   {
  47.     String result = "";
  48.     if (text.length() <= 16) {
  49.       return new AbstractMap.SimpleEntry(null, text);
  50.     }
  51.     Team team = this.scoreboard.registerNewTeam("text-" + this.scoreboard.getTeams().size());
  52.     Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
  53.     team.setPrefix((String)iterator.next());
  54.     result = (String)iterator.next();
  55.     if (text.length() > 32) {
  56.       team.setSuffix((String)iterator.next());
  57.     }
  58.     this.teams.add(team);
  59.     return new AbstractMap.SimpleEntry(team, result);
  60.   }
  61.  
  62.   public void build()
  63.   {
  64.     obj = this.scoreboard.registerNewObjective(this.title.length() > 16 ? this.title.substring(0, 15) : this.title, "dummy");
  65.     obj.setDisplayName(this.title);
  66.     obj.setDisplaySlot(DisplaySlot.SIDEBAR);
  67.    
  68.     int index = this.scores.size();
  69.     for (Map.Entry<String, Integer> text : this.scores.entrySet())
  70.     {
  71.       Map.Entry<Team, String> team = createTeam((String)text.getKey());
  72.       Integer score = Integer.valueOf(text.getValue() != null ? ((Integer)text.getValue()).intValue() : index);
  73.       String player = (String)team.getValue();
  74.       if (team.getKey() != null) {
  75.         ((Team)team.getKey()).addPlayer(Bukkit.getOfflinePlayer(player));
  76.       }
  77.       obj.getScore(player).setScore(score.intValue());
  78.       index--;
  79.     }
  80.   }
  81.  
  82.   public void reset()
  83.   {
  84.     this.title = null;
  85.     this.scores.clear();
  86.     for (Team t : this.teams) {
  87.       t.unregister();
  88.     }
  89.     this.teams.clear();
  90.   }
  91.  
  92.   public Scoreboard getScoreboard()
  93.   {
  94.     return this.scoreboard;
  95.   }
  96.  
  97.   public void send(Player... players)
  98.   {
  99.     Player[] arrayOfPlayer;
  100.     int j = (arrayOfPlayer = players).length;
  101.     for (int i = 0; i < j; i++)
  102.     {
  103.       Player p = arrayOfPlayer[i];
  104.       p.setScoreboard(this.scoreboard);
  105.     }
  106.   }
  107.  
  108.   @SuppressWarnings("deprecation")
  109.   public void update(String text, Integer score) {
  110.       if (scores.containsKey(text)) {
  111.           scores.put(text, score);
  112.           for (Team t : teams) {
  113.               if (t.getName() == text) {
  114.                   t.unregister();
  115.               }
  116.           }
  117.  
  118.           Map.Entry<Team, String> team = createTeam(text);
  119.           OfflinePlayer player = Bukkit.getOfflinePlayer(team.getValue());
  120.           if (team.getKey() != null)
  121.               team.getKey().addPlayer(player);
  122.           obj.getScore(player).setScore(score);
  123.  
  124.       }
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement