package com.crossge.necessities;

import com.crossge.necessities.RankManager.Rank;
import com.crossge.necessities.RankManager.RankManager;
import com.crossge.necessities.RankManager.User;
import org.bukkit.Bukkit;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;

public class ScoreBoards {
    private static ScoreboardManager man;
    private static Scoreboard b;
    RankManager rm = new RankManager();

    public void createScoreboard() {
        man = Bukkit.getScoreboardManager();
        b = man.getMainScoreboard();//Use main scoreboard instead of a new one for better compatability with other plugins
        for (Rank r : rm.getOrder())
            if (b.getTeam((rm.getOrder().size() - rm.getOrder().indexOf(r)) + "") == null) {
                Team t = b.registerNewTeam((rm.getOrder().size() - rm.getOrder().indexOf(r)) + "");
                t.setPrefix(r.getColor());
            }
    }

    public void addPlayer(User u) {
        if (u.getRank() == null || u.getPlayer() == null)
            return;
        Team t = b.getTeam((rm.getOrder().size() - rm.getOrder().indexOf(u.getRank())) + "");
        if (t == null)
            return;
        for (Team team : b.getTeams())
            if (team.hasEntry(u.getPlayer().getName()))
                team.removeEntry(u.getPlayer().getName());
        t.addEntry(u.getPlayer().getName());
        u.getPlayer().setScoreboard(b);
    }

    public void delPlayer(User u) {
        if (u.getRank() == null || u.getPlayer() == null)
            return;
        Team t = b.getTeam(u.getPlayer().getName());
        if (t == null)
            t = b.getTeam((rm.getOrder().size() - rm.getOrder().indexOf(u.getRank())) + "");
        if (t == null)
            return;
        t.removeEntry(u.getPlayer().getName());
        u.getPlayer().setScoreboard(man.getNewScoreboard());
    }
}
@@ -359,16 +359,199 @@
 oards %7B%0A
+    private static String%5B%5D ALPHABET = new String%5B%5D%7B%22a%22, %22b%22, %22c%22, %22d%22, %22e%22, %22f%22, %22g%22, %22h%22, %22i%22, %22j%22, %22k%22, %22l%22, %22m%22, %22n%22, %22o%22, %22p%22, %22q%22, %22r%22, %22s%22, %22t%22, %22u%22, %22v%22, %22w%22, %22x%22, %22y%22, %22z%22%7D;%0A
     priv
@@ -918,32 +918,39 @@
   if (b.getTeam(
+fromInt
 (rm.getOrder().s
@@ -982,21 +982,16 @@
 exOf(r))
- + %22%22
 ) == nul
@@ -1034,24 +1034,31 @@
 sterNewTeam(
+fromInt
 (rm.getOrder
@@ -1094,21 +1094,16 @@
 exOf(r))
- + %22%22
 );%0A     
@@ -1294,32 +1294,39 @@
 m t = b.getTeam(
+fromInt
 (rm.getOrder().s
@@ -1360,37 +1360,32 @@
 Of(u.getRank()))
- + %22%22
 );%0A        if (t
@@ -1876,16 +1876,23 @@
 getTeam(
+fromInt
 (rm.getO
@@ -1946,13 +1946,8 @@
 ()))
- + %22%22
 );%0A 
@@ -2102,10 +2102,191 @@
 );%0A    %7D
+%0A%0A    private String fromInt(int toTranslate) %7B%0A        toTranslate -= 1;%0A        return (toTranslate %3C 0 %7C%7C ALPHABET.length %3C= toTranslate) ? %22ERROR%22 : ALPHABET%5BtoTranslate%5D;%0A    %7D
 %0A%7D

Increase number of ranks that scoreboard supports sorting on tablist