11package de .timongcraft .veloboard ;
22
3+ import com .google .common .collect .ImmutableList ;
4+ import com .google .common .collect .ImmutableSet ;
35import com .velocitypowered .api .proxy .Player ;
46import de .timongcraft .veloboard .utils .ListUtils ;
57import de .timongcraft .velopacketimpl .network .protocol .packets .DisplayObjectivePacket ;
911import de .timongcraft .velopacketimpl .network .protocol .packets .UpdateTeamsPacket ;
1012import de .timongcraft .velopacketimpl .utils .ComponentUtils ;
1113import de .timongcraft .velopacketimpl .utils .annotations .Since ;
12- import net .kyori .adventure .text .Component ;
13- import net .kyori .adventure .text .format .NamedTextColor ;
14- import org .jetbrains .annotations .Nullable ;
15- import org .jetbrains .annotations .Unmodifiable ;
16-
1714import java .util .ArrayList ;
1815import java .util .Arrays ;
1916import java .util .Collection ;
20- import java .util .Collections ;
21- import java .util .EnumSet ;
2217import java .util .List ;
2318import java .util .Objects ;
19+ import net .kyori .adventure .text .Component ;
20+ import net .kyori .adventure .text .format .NamedTextColor ;
21+ import org .jetbrains .annotations .Nullable ;
22+ import org .jetbrains .annotations .Unmodifiable ;
2423
2524import static com .velocitypowered .api .network .ProtocolVersion .MINECRAFT_1_20_3 ;
2625
@@ -53,7 +52,7 @@ public VeloBoard(Player player, Component title, @Nullable ComponentUtils.Number
5352 public void initialize () {
5453 withLock (() -> {
5554 sendObjectivePacket (UpdateObjectivesPacket .Mode .CREATE_SCOREBOARD );
56- sendPacket (new DisplayObjectivePacket (1 , id ));
55+ sendPacket (DisplayObjectivePacket . of (1 , id ));
5756 });
5857 }
5958
@@ -64,7 +63,7 @@ public void resend() {
6463
6564 for (int i = 0 ; i < lines .size (); ++i ) {
6665 sendScorePacketUnchecked (i , UpdateScorePacket .Action .CREATE_OR_UPDATE_SCORE );
67- sendTeamPacketUnchecked (i , UpdateTeamsPacket . Mode . CREATE_TEAM , getLineByScore ( lines , i ));
66+ sendTeamPacketUnchecked (i , true , getLineByScoreUnsafe ( i ));
6867 }
6968 });
7069 }
@@ -73,7 +72,7 @@ public void resend() {
7372 public void clear () {
7473 withLock (() -> {
7574 for (int i = 0 ; i < this .lines .size (); ++i ) {
76- sendTeamPacketUnchecked ( i , UpdateTeamsPacket . Mode . REMOVE_TEAM );
75+ sendRemoveTeamUnchecked ( i );
7776 }
7877
7978 sendObjectivePacket (UpdateObjectivesPacket .Mode .REMOVE_SCOREBOARD );
@@ -169,19 +168,20 @@ public void updateLines(Collection<Component> lines) {
169168 for (int i = 0 ; i < this .lines .size (); i ++) {
170169 if (i >= oldLines .size ()) {
171170 sendScorePacketUnchecked (i , UpdateScorePacket .Action .CREATE_OR_UPDATE_SCORE );
172- sendTeamPacketUnchecked (i , UpdateTeamsPacket .Mode .CREATE_TEAM );
171+ sendTeamPacketUnchecked (i , true , getLineByScoreUnsafe (i ));
172+ } else {
173+ sendLineChangeUnsafe (i );
173174 }
174- sendLineChangeUnsafe (i );
175175 }
176176
177177 for (int i = this .lines .size (); i < oldLines .size (); i ++) {
178- sendTeamPacketUnchecked ( i , UpdateTeamsPacket . Mode . REMOVE_TEAM );
178+ sendRemoveTeamUnchecked ( i );
179179 sendScorePacketUnchecked (i , UpdateScorePacket .Action .REMOVE_SCORE );
180180 }
181181 }
182182
183183 for (int i = 0 ; i < this .lines .size (); ++i ) {
184- if (!Objects .equals (getLineByScore (oldLines , i ), getLineByScore ( this . lines , i ))) {
184+ if (!Objects .equals (getLineByScore (i , oldLines ), getLineByScoreUnsafe ( i ))) {
185185 sendLineChangeUnsafe (i );
186186 }
187187 }
@@ -264,26 +264,33 @@ private void checkLineIndexUnsafe(int lineIndex, boolean checkInRange, boolean c
264264 }
265265
266266 private int getScoreByLineUnsafe (int lineIndex ) {
267+ return getScoreByLine (lineIndex , lines );
268+ }
269+
270+ private int getScoreByLine (int lineIndex , List <Component > lines ) {
267271 return lines .size () - lineIndex - 1 ;
268272 }
269273
270- private static @ Nullable Component getLineByScore (List <Component > lines , int score ) {
271- if (score < lines .size ()) {
272- return lines .get (lines .size () - score - 1 );
273- } else {
274- return null ;
275- }
274+ private @ Nullable Component getLineByScoreUnsafe (int score ) {
275+ return getLineByScore (score , lines );
276+ }
277+
278+ private @ Nullable Component getLineByScore (int score , List <Component > lines ) {
279+ int index = lines .size () - score - 1 ;
280+ if (index < 0 || index >= lines .size ()) return null ;
281+ return lines .get (index );
276282 }
277283
278284 private void sendLineChangeUnsafe (int score ) {
279- sendTeamPacketUnchecked (score , UpdateTeamsPacket . Mode . UPDATE_TEAM_INFO , getLineByScore ( lines , score ));
285+ sendTeamPacketUnchecked (score , false , getLineByScoreUnsafe ( score ));
280286 }
281287
282288 private void sendObjectivePacket (UpdateObjectivesPacket .Mode mode ) {
283289 sendPacket (
284- new UpdateObjectivesPacket (
285- id ,
286- mode ,
290+ mode == UpdateObjectivesPacket .Mode .REMOVE_SCOREBOARD
291+ ? UpdateObjectivesPacket .ofRemove (id )
292+ : UpdateObjectivesPacket .of (id ,
293+ mode == UpdateObjectivesPacket .Mode .CREATE_SCOREBOARD ,
287294 player .translateMessage (title ),
288295 UpdateObjectivesPacket .Type .INTEGER ,
289296 defaultNumberFormat
@@ -293,40 +300,44 @@ private void sendObjectivePacket(UpdateObjectivesPacket.Mode mode) {
293300
294301 private void sendScorePacketUnchecked (int score , UpdateScorePacket .Action action ) {
295302 sendPacket (
296- action != UpdateScorePacket .Action .REMOVE_SCORE || player .getProtocolVersion ().getProtocol () < MINECRAFT_1_20_3 .getProtocol () ?
297- new UpdateScorePacket (
298- COLOR_CODES [score ],
299- action ,
300- id ,
301- score
302- )
303- :
304- new ResetScorePacket (
305- COLOR_CODES [score ],
306- id
307- )
303+ action == UpdateScorePacket .Action .CREATE_OR_UPDATE_SCORE
304+ ? UpdateScorePacket .of (COLOR_CODES [score ], id , score )
305+
306+ : player .getProtocolVersion ().lessThan (MINECRAFT_1_20_3 )
307+ ? UpdateScorePacket .ofLegacyRemove (COLOR_CODES [score ], id )
308+ : ResetScorePacket .of (COLOR_CODES [score ], id )
308309 );
309310 }
310311
311- private void sendTeamPacketUnchecked (int score , UpdateTeamsPacket . Mode mode ) {
312- sendTeamPacketUnchecked ( score , mode , Component . empty ( ));
312+ private void sendRemoveTeamUnchecked (int score ) {
313+ sendPacket ( UpdateTeamsPacket . ofRemove ( id + ':' + score ));
313314 }
314315
315- private void sendTeamPacketUnchecked (int score , UpdateTeamsPacket .Mode mode , Component teamPrefix ) {
316- sendPacket (
317- new UpdateTeamsPacket (
318- id + ':' + score ,
319- mode ,
320- Component .empty (),
321- EnumSet .noneOf (UpdateTeamsPacket .FriendlyFlag .class ),
322- UpdateTeamsPacket .NameTagVisibility .ALWAYS ,
323- UpdateTeamsPacket .CollisionRule .ALWAYS ,
324- NamedTextColor .BLACK ,
325- player .translateMessage (teamPrefix ),
326- Component .empty (),
327- Collections .singletonList (COLOR_CODES [score ])
328- )
329- );
316+ private void sendTeamPacketUnchecked (int score , boolean create , Component teamPrefix ) {
317+ if (create ) {
318+ sendPacket (UpdateTeamsPacket .ofCreate (
319+ id + ':' + score ,
320+ Component .empty (),
321+ ImmutableSet .of (),
322+ UpdateTeamsPacket .NameTagVisibility .ALWAYS ,
323+ UpdateTeamsPacket .CollisionRule .ALWAYS ,
324+ NamedTextColor .BLACK ,
325+ player .translateMessage (teamPrefix ),
326+ Component .empty (),
327+ ImmutableList .of (COLOR_CODES [score ])
328+ ));
329+ } else {
330+ sendPacket (UpdateTeamsPacket .ofUpdateInfo (
331+ id + ':' + score ,
332+ Component .empty (),
333+ ImmutableSet .of (),
334+ UpdateTeamsPacket .NameTagVisibility .ALWAYS ,
335+ UpdateTeamsPacket .CollisionRule .ALWAYS ,
336+ NamedTextColor .BLACK ,
337+ player .translateMessage (teamPrefix ),
338+ Component .empty ()
339+ ));
340+ }
330341 }
331342
332343}
0 commit comments