Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package com.statiocraft.api.scoreboard;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.scoreboard.DisplaySlot;
  11. import org.bukkit.scoreboard.NameTagVisibility;
  12. import org.bukkit.scoreboard.Objective;
  13. import org.bukkit.scoreboard.Scoreboard;
  14. import org.bukkit.scoreboard.Team;
  15.  
  16. public class scBoard_DISABLED_ {
  17.    
  18.     private static final Map<Player, scBoard_DISABLED_> m = new HashMap<Player, scBoard_DISABLED_>();
  19.    
  20.     /**
  21.      * Creates a new scoreboard for the given player
  22.      * @param player
  23.      * @return
  24.      */
  25.     public static scBoard_DISABLED_ create(Player player, String displayName){
  26.         if(m.containsKey(player))return null;
  27.         return new scBoard_DISABLED_(player, displayName);
  28.     }
  29.    
  30.     /**
  31.      * Returns the scoreboard assigned to the given player
  32.      * @param player
  33.      * @return
  34.      */
  35.     public static scBoard_DISABLED_ get(Player player){
  36.         return m.get(player);
  37.     }
  38.    
  39.    
  40.    
  41.     private final Player p;
  42.     private final Scoreboard sb;
  43.     private final Objective o;
  44.     private final Team t;
  45.     private String dn, ntp, nts;
  46.     private NameTagVisibility ntv;
  47.     private final Content c;
  48.     private String[] oc;
  49.    
  50.    
  51.     private scBoard_DISABLED_(Player player, String displayName){
  52.         if(m.containsKey(player))m.get(player).destroy();
  53.        
  54.         p = player;
  55.         sb = Bukkit.getScoreboardManager().getNewScoreboard();
  56.         o = sb.registerNewObjective("Objective", "dummy");
  57.         t = sb.registerNewTeam("T_" + p.getName());
  58.         dn = displayName;
  59.        
  60.         p.setScoreboard(sb);
  61.         o.setDisplaySlot(DisplaySlot.SIDEBAR);
  62.         t.addPlayer(p);
  63.        
  64.         ntp = "";
  65.         nts = "";
  66.         ntv = NameTagVisibility.NEVER;
  67.        
  68.         c = new Content();
  69.         oc = new String[0];
  70.        
  71.         m.put(player, this);
  72.        
  73.         update();
  74.     }
  75.    
  76.     /**
  77.      * Destroys the scoreboard
  78.      */
  79.     public void destroy(){
  80.         m.remove(p);
  81.         p.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
  82.         o.unregister();
  83.         t.unregister();
  84.     }
  85.    
  86.     /**
  87.      * Returns the content
  88.      * @return
  89.      */
  90.     public Content getContent(){
  91.         return c;
  92.     }
  93.    
  94.     /**
  95.      * Returns the displayName
  96.      * @return
  97.      */
  98.     public String getDisplayName(){
  99.         return dn;
  100.     }
  101.    
  102.     /**
  103.      * Returns the nameTag prefix
  104.      * @return
  105.      */
  106.     public String getNametagPrefix(){
  107.         return ntp;
  108.     }
  109.    
  110.     /**
  111.      * Returns the nameTag suffix
  112.      * @return
  113.      */
  114.     public String getNametagSuffix(){
  115.         return nts;
  116.     }
  117.    
  118.     /**
  119.      * Returns the nameTag visibility
  120.      * @return
  121.      */
  122.     public NameTagVisibility getNametagVisibility(){
  123.         return ntv;
  124.     }
  125.    
  126.     /**
  127.      * Returns the bukkit-scoreboard
  128.      * @return
  129.      */
  130.     public Scoreboard getScoreboard(){
  131.         return sb;
  132.     }
  133.    
  134.     /**
  135.      * Returns the scoreboard size
  136.      * @return
  137.      */
  138.     public int getSize(){
  139.         return c.getSize();
  140.     }
  141.    
  142.    
  143.     /**
  144.      * Sets the displayName of the scoreboard
  145.      * @param displayName
  146.      */
  147.     public void setDisplayName(String displayName){
  148.         dn = displayName;
  149.     }
  150.    
  151.     /**
  152.      * Sets the nameTag prefix
  153.      * @param prefix
  154.      */
  155.     public void setNametagPrefix(String prefix){
  156.         ntp = prefix;
  157.     }
  158.    
  159.     /**
  160.      * Sets the nameTag suffix
  161.      * @param suffix
  162.      */
  163.     public void setNametagSuffix(String suffix){
  164.         nts = suffix;
  165.     }
  166.    
  167.     /**
  168.      * Sets the nameTag visibility
  169.      * @param visibility
  170.      */
  171.     public void setNametagVisibility(NameTagVisibility visibility){
  172.         ntv = visibility;
  173.     }
  174.    
  175.    
  176.     /**
  177.      * Updates the scoreboard
  178.      */
  179.     public void update(){      
  180.         //Update Sidebar
  181.         o.setDisplayName(dn);
  182.        
  183.         List<String> r = new ArrayList<String>();
  184.         String[] nc = c.getRaw();
  185.        
  186.         for(String os : oc){
  187.             boolean f = false;
  188.             for(String ns : nc){
  189.                 if(ns.equals(os)){
  190.                     f=true;
  191.                     break;
  192.                 }
  193.             }
  194.             if(!f)r.add(os);
  195.         }
  196.        
  197.         for(String s : r){
  198.             sb.resetScores(s);
  199.         }
  200.        
  201.         for(int n = 0; n < nc.length; n ++){
  202.             o.getScore(nc[n]).setScore(nc.length-n);
  203.         }
  204.        
  205.         oc = nc;
  206.        
  207.         //Update Team
  208.         if(ntp.isEmpty())ntp = "§f";
  209.         if(nts.isEmpty())nts = "§f";
  210.        
  211.         t.setNameTagVisibility(ntv);
  212.         t.setPrefix(ntp);
  213.         t.setSuffix(nts);
  214.     }
  215.    
  216.    
  217.    
  218.     public static class Content {
  219.        
  220.         private String[] c;
  221.        
  222.         public Content(int size){
  223.             c = new String[size];
  224.         }
  225.         public Content(){
  226.             this(0);
  227.         }
  228.        
  229.         /**
  230.          * Adds a line
  231.          * @param text
  232.          * @return
  233.          */
  234.         public int addLine(String text){
  235.             setLine(c.length, text);
  236.             return c.length-1;
  237.         }
  238.        
  239.         /**
  240.          * Returns the raw text
  241.          * @return
  242.          */
  243.         protected String[] getRaw(){
  244.             return c;
  245.         }
  246.        
  247.         /**
  248.          * Returns the size
  249.          * @return
  250.          */
  251.         public int getSize(){
  252.             return c.length;
  253.         }
  254.        
  255.         /**
  256.          * Inserts on the given lineNumber
  257.          * @param line
  258.          * @param text
  259.          * @return
  260.          */
  261.         public int insertLine(int line, String text){
  262.             if(line >= c.length){
  263.                 return setLine(line, text);
  264.             }else{
  265.                 String[] nc = new String[c.length+1];
  266.                 for(int n = 0; n < line; n ++){
  267.                     nc[n] = c[n];
  268.                 }
  269.                 nc[line] = text;
  270.                 for(int n = line; n < c.length; n ++){
  271.                     nc[n+1]=c[n];
  272.                 }
  273.                 return line;
  274.             }
  275.         }
  276.        
  277.         /**
  278.          * Removes the given lineNumber
  279.          * @param line
  280.          * @return
  281.          */
  282.         public boolean removeLine(int line){
  283.             if(line >= c.length)return false;
  284.             String[] nc = new String[c.length-1];
  285.            
  286.             if(line == 0){
  287.                 for(int n = 0; n < nc.length; n ++){
  288.                     nc[n]=c[n+1];
  289.                 }
  290.                 c=nc;
  291.                 return true;
  292.             }
  293.            
  294.             if(line == c.length-1){
  295.                 setSize(c.length-1);
  296.                 return true;
  297.             }
  298.            
  299.             for(int n = 0; n < line; n ++){
  300.                 nc[n]=c[n];
  301.             }
  302.             for(int n = line; n < nc.length; n ++){
  303.                 nc[n]=c[n+1];
  304.             }
  305.             return true;
  306.         }
  307.        
  308.         /**
  309.          * Sets the given lineNumber
  310.          * @param line
  311.          * @param text
  312.          * @return
  313.          */
  314.         public int setLine(int line, String text){
  315.             if(line >= c.length){
  316.                 setSize(c.length+1);
  317.                 c[c.length-1]=text;
  318.                 return c.length-1;
  319.             }else{
  320.                 c[line]=text;
  321.                 return line;
  322.             }
  323.         }
  324.        
  325.         /**
  326.          * Sets the size
  327.          * @param size
  328.          */
  329.         public void setSize(int size){
  330.             if(size == 0){
  331.                 c = new String[0];
  332.                 return;
  333.             }
  334.             else if(c.length == 0){
  335.                 c = new String[size];
  336.                 return;
  337.             }
  338.            
  339.             String[] nc = new String[size];
  340.             for(int n = 0; n < Math.min(nc.length, c.length); n ++){
  341.                 nc[n] = c[n];
  342.             }
  343.            
  344.             c = nc;
  345.         }
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement