Guest User

Hud.java

a guest
Apr 22nd, 2015
201
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.meccascoreboard.rpghud;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.scoreboard.DisplaySlot;
  7. import org.bukkit.scoreboard.Objective;
  8. import org.bukkit.scoreboard.Scoreboard;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Set;
  13.  
  14. /**
  15.  * Created by TheAwesomeGem on 4/5/2015.
  16.  */
  17. public class Hud
  18. {
  19.     public String ID;
  20.     public String Name;
  21.     public Player Pl;
  22.     public Scoreboard Board;
  23.     public Objective Object;
  24.     public Objective Buffer;
  25.     public Objective t;
  26.     public List<CustomScore> Scores;
  27.  
  28.     public Hud(Player player, String id, String name, DisplaySlot displaySlot)
  29.     {
  30.         Name = name;
  31.         ID = id;
  32.         Pl = player;
  33.         Board = Bukkit.getScoreboardManager().getNewScoreboard();
  34.  
  35.         Object = Board.registerNewObjective(id, "dummy");
  36.         Object.setDisplaySlot(displaySlot);
  37.         Object.setDisplayName(name);
  38.  
  39.         Buffer = Board.registerNewObjective(id + "buffer", "dummy");
  40.         Buffer.setDisplayName(name);
  41.  
  42.         Scores = new ArrayList<>();
  43.  
  44.         player.setScoreboard(Board);
  45.     }
  46.  
  47.     public CustomScore getScore(String name)
  48.     {
  49.         for(CustomScore customScore : Scores)
  50.             if (customScore.Name.equals(name)) return customScore;
  51.  
  52.         return null;
  53.     }
  54.  
  55.     public void addScore(String name, String value, String suffix, ChatColor nameColor, ChatColor valColor)
  56.     {
  57.         if(getScore(name) == null)
  58.         {
  59.             int slot = 0;
  60.  
  61.             if(Scores.size() > 0)
  62.                 slot = Scores.get(Scores.size() - 1).Slot + 1;
  63.  
  64.             CustomScore customScore = new CustomScore(name, value, suffix, nameColor, valColor, slot);
  65.             Scores.add(customScore);
  66.  
  67.             update();
  68.         }
  69.     }
  70.  
  71.     public void removeScore(String name)
  72.     {
  73.         Scores.remove(getScore(name));
  74.  
  75.         update();
  76.     }
  77.  
  78.     public void setScore(String name, String value)
  79.     {
  80.         CustomScore customScore = getScore(name);
  81.  
  82.         if (customScore != null)
  83.         {
  84.             customScore.Value = value;
  85.  
  86.             update();
  87.         }
  88.     }
  89.  
  90.     public void update()
  91.     {
  92.         resetScores();
  93.  
  94.         Buffer.setDisplaySlot(Object.getDisplaySlot());
  95.  
  96.         for(CustomScore customScore : Scores)
  97.         {
  98.             customScore.setScore(Buffer);
  99.         }
  100.  
  101.         t = Object;
  102.         Object = Buffer;
  103.         Buffer = t;
  104.     }
  105.  
  106.     public void resetScores()
  107.     {
  108.         Set<String> entries = Board.getEntries();
  109.         for (String entry : entries)
  110.         {
  111.             Board.resetScores(entry);
  112.         }
  113.     }
  114.  
  115.     public void destroy()
  116.     {
  117.         Object.unregister();
  118.         Object = null;
  119.         Buffer.unregister();
  120.         Buffer = null;
  121.     }
  122. }
RAW Paste Data