11package net .swofty .type .bedwarsgame ;
22
3- import net .kyori .adventure .text .Component ;
43import net .minestom .server .MinecraftServer ;
54import net .minestom .server .entity .Player ;
6- import net .minestom .server .scoreboard .Sidebar ;
75import net .minestom .server .timer .Scheduler ;
86import net .minestom .server .timer .TaskSchedule ;
97import net .swofty .type .bedwarsgame .game .Game ;
108import net .swofty .type .bedwarsgame .game .GameStatus ;
11- import net .swofty .type .generic .data .handlers .BedWarsDataHandler ;
12- import net .swofty .commons .bedwars .map .BedWarsMapsConfig .MapTeam ;
139import net .swofty .commons .bedwars .map .BedWarsMapsConfig .TeamKey ;
1410import net .swofty .type .generic .HypixelConst ;
1511import net .swofty .type .generic .data .HypixelDataHandler ;
12+ import net .swofty .type .generic .data .handlers .BedWarsDataHandler ;
13+ import net .swofty .type .generic .scoreboard .HypixelScoreboard ;
1614import net .swofty .type .generic .user .HypixelPlayer ;
1715
1816import java .text .SimpleDateFormat ;
17+ import java .util .ArrayList ;
1918import java .util .Date ;
20- import java .util .HashMap ;
21- import java .util .Map ;
22- import java .util .UUID ;
19+ import java .util .List ;
2320
2421public class BedWarsGameScoreboard {
25- private static final Map < UUID , Sidebar > sidebarCache = new HashMap <> ();
22+ private static final HypixelScoreboard scoreboard = new HypixelScoreboard ();
2623 private static Integer prototypeName = 0 ;
2724
2825 public static void start (Game game ) {
@@ -38,7 +35,7 @@ public static void start(Game game) {
3835 }
3936
4037 for (HypixelPlayer player : game .getPlayers ()) {
41- if (player .joined - System .currentTimeMillis () > 5000 ) { // for now let's not show scoreboard too early
38+ if (player .joined - System .currentTimeMillis () > 5000 ) {
4239 continue ;
4340 }
4441 HypixelDataHandler dataHandler = player .getDataHandler ();
@@ -48,18 +45,18 @@ public static void start(Game game) {
4845 continue ;
4946 }
5047
51- Sidebar sidebar = new Sidebar (Component .text (getSidebarName (prototypeName )));
48+ List <String > lines = new ArrayList <>();
49+ lines .add ("§7" + new SimpleDateFormat ("MM/dd/yy" ).format (new Date ()) + " §8" + HypixelConst .getServerName ());
50+ lines .add ("§7 " );
5251
53- addLine ("§7" + new SimpleDateFormat ("MM/dd/yy" ).format (new Date ()) + " §8" + HypixelConst .getServerName (), sidebar );
54- addLine ("§7 " , sidebar );
5552 if (game .getGameStatus () == GameStatus .WAITING ) {
56- addLine ("§fMap: §a" + game .getMapEntry ().getName (), sidebar );
57- addLine ("§fPlayers: §a" + game .getPlayers ().size () + "/" + game .getMapEntry ().getConfiguration ().getTeams ().size (), sidebar );
58- addLine ("§7 " , sidebar );
59- addLine ("§fStarting in §a" + game .getCountdown ().getRemainingSeconds () + "s" , sidebar );
60- addLine ("§7 " , sidebar );
61- addLine ("§fMode: §a" + game .getBedwarsGameType ().getDisplayName (), sidebar );
62- addLine ("§fVersion: §7v1.9" , sidebar );
53+ lines . add ("§fMap: §a" + game .getMapEntry ().getName ());
54+ lines . add ("§fPlayers: §a" + game .getPlayers ().size () + "/" + game .getMapEntry ().getConfiguration ().getTeams ().size ());
55+ lines . add ("§7 " );
56+ lines . add ("§fStarting in §a" + game .getCountdown ().getRemainingSeconds () + "s" );
57+ lines . add ("§7 " );
58+ lines . add ("§fMode: §a" + game .getBedwarsGameType ().getDisplayName ());
59+ lines . add ("§fVersion: §7v1.9" );
6360 } else {
6461 String eventName = game .getEventManager ().getNextEvent () != null
6562 ? game .getEventManager ().getNextEvent ().getDisplayName ()
@@ -68,57 +65,33 @@ public static void start(Game game) {
6865 long minutesPart = seconds / 60 ;
6966 long secondsPart = seconds % 60 ;
7067 String timeLeft = String .format ("%d:%02d" , minutesPart , secondsPart );
71- addLine ("§f" + eventName + " in §a" + timeLeft , sidebar );
72- addLine ("§7 " , sidebar );
73- for (java .util .Map .Entry <TeamKey , MapTeam > entry : game .getMapEntry ().getConfiguration ().getTeams ().entrySet ()) {
68+ lines . add ("§f" + eventName + " in §a" + timeLeft );
69+ lines . add ("§7 " );
70+ for (java .util .Map .Entry <TeamKey , net . swofty . commons . bedwars . map . BedWarsMapsConfig . MapTeam > entry : game .getMapEntry ().getConfiguration ().getTeams ().entrySet ()) {
7471 TeamKey teamKey = entry .getKey ();
7572 String teamName = teamKey .getName ();
7673 String teamInitial = teamName .substring (0 , 1 ).toUpperCase ();
7774
7875 String bedStatus = game .getTeamManager ().isBedAlive (teamKey ) ? "§a✔" : "§c✖" ;
79- addLine (String .format ("%s%s §f%s %s" , teamKey .chatColor (), teamInitial , teamName , bedStatus ), sidebar );
76+ lines . add (String .format ("%s%s §f%s %s" , teamKey .chatColor (), teamInitial , teamName , bedStatus ));
8077 }
8178 }
82- addLine ("§7 " , sidebar );
83- addLine ("§ewww.hypixel.net" , sidebar );
84-
85- Sidebar oldSidebar = sidebarCache .get (player .getUuid ());
86-
87- sidebar .addViewer (player );
88-
89- sidebarCache .put (player .getUuid (), sidebar );
79+ lines .add ("§7 " );
80+ lines .add ("§ewww.hypixel.net" );
9081
91- if (oldSidebar != null && oldSidebar != sidebar ) {
92- final Sidebar finalOldSidebar = oldSidebar ;
93- scheduler .scheduleNextTick (() -> {
94- if (sidebarCache .get (player .getUuid ()) == sidebar ) {
95- try {
96- finalOldSidebar .removeViewer (player );
97- } catch (Exception e ) {
98-
99- }
100- }
101- });
82+ if (!scoreboard .hasScoreboard (player )) {
83+ scoreboard .createScoreboard (player , getSidebarName (prototypeName ));
10284 }
85+
86+ scoreboard .updateLines (player , lines );
87+ scoreboard .updateTitle (player , getSidebarName (prototypeName ));
10388 }
104- return TaskSchedule .tick (10 );
89+ return TaskSchedule .tick (10 );
10590 });
10691 }
10792
10893 public static void removeCache (Player player ) {
109- sidebarCache .remove (player .getUuid ());
110- }
111-
112- private static void addLine (String text , Sidebar sidebar ) {
113-
114- int score = sidebar .getLines ().size ();
115- sidebar .createLine (new Sidebar .ScoreboardLine (UUID .randomUUID ().toString (), Component .text (text ), score ));
116- }
117-
118- private static void addLine (Component text , Sidebar sidebar ) {
119-
120- int score = sidebar .getLines ().size ();
121- sidebar .createLine (new Sidebar .ScoreboardLine (UUID .randomUUID ().toString (), text , score ));
94+ scoreboard .removeScoreboard (player );
12295 }
12396
12497 private static String getSidebarName (int counter ) {
@@ -138,5 +111,4 @@ private static String getSidebarName(int counter) {
138111 return colors [2 ] + baseText + endColor ;
139112 }
140113 }
141-
142114}
0 commit comments