22
33import java .util .Set ;
44
5+ import net .citizensnpcs .Citizens ;
6+ import net .citizensnpcs .trait .scoreboard .AbstractScoreboard ;
7+ import net .citizensnpcs .trait .scoreboard .AbstractTeam ;
8+ import net .citizensnpcs .trait .scoreboard .BukkitScoreboardImpl ;
9+ import net .citizensnpcs .trait .scoreboard .FoliaScoreboardImpl ;
10+ import net .megavex .scoreboardlibrary .api .team .TeamManager ;
511import org .bukkit .Bukkit ;
612import org .bukkit .ChatColor ;
713import org .bukkit .entity .Entity ;
814import org .bukkit .entity .LivingEntity ;
915import org .bukkit .entity .Player ;
10- import org .bukkit .scoreboard .Scoreboard ;
1116import org .bukkit .scoreboard .Team ;
12- import org .bukkit .scoreboard .Team .Option ;
1317import org .bukkit .scoreboard .Team .OptionStatus ;
1418
1519import com .google .common .collect .Iterables ;
2529import net .citizensnpcs .api .trait .TraitName ;
2630import net .citizensnpcs .api .util .DataKey ;
2731import net .citizensnpcs .api .util .SpigotUtil ;
28- import net .citizensnpcs .util .NMS ;
2932import net .citizensnpcs .util .Util ;
3033
3134@ TraitName ("scoreboardtrait" )
@@ -39,6 +42,8 @@ public class ScoreboardTrait extends Trait {
3942 @ Persist
4043 private Set <String > tags = Sets .newHashSet ("CITIZENS_NPC" );
4144
45+ private final AbstractScoreboard scoreboard ;
46+
4247 public ScoreboardTrait () {
4348 super ("scoreboardtrait" );
4449 metadata = CitizensAPI .getLocationLookup ().<Boolean > registerMetadata ("scoreboard" , (meta , event ) -> {
@@ -47,34 +52,36 @@ public ScoreboardTrait() {
4752 if (trait == null )
4853 continue ;
4954
50- Team team = trait .getTeam ();
55+ AbstractTeam team = trait .getTeam ();
5156 if (team == null || meta .has (event .getPlayer ().getUniqueId (), team .getName ()))
5257 continue ;
5358
54- NMS .sendTeamPacket (event .getPlayer (), team , 0 );
59+ //NMS.sendTeamPacket(event.getPlayer(), team, 0);
60+ team .sendToPlayer (event .getPlayer (), AbstractTeam .SendMode .ADD_OR_MODIFY );
5561
5662 meta .set (event .getPlayer ().getUniqueId (), team .getName (), true );
5763 }
5864 });
65+
66+ TeamManager teamManager = ((Citizens ) CitizensAPI .getPlugin ()).getTeamManager ();
67+ this .scoreboard = SpigotUtil .isFoliaServer () ? new FoliaScoreboardImpl (teamManager ) : new BukkitScoreboardImpl ();
5968 }
6069
61- private void clearClientTeams (Team team ) {
70+ private void clearClientTeams (AbstractTeam team ) {
6271 for (Player player : Bukkit .getOnlinePlayers ()) {
6372 if (metadata .remove (player .getUniqueId (), team .getName ())) {
64- NMS .sendTeamPacket (player , team , 1 );
73+ team .sendToPlayer (player , AbstractTeam .SendMode .REMOVE );
74+ //NMS.sendTeamPacket(player, team, 1);
6575 }
6676 }
6777 }
6878
6979 public void createTeam (String entityName ) {
70- if (SpigotUtil .isFoliaServer ())
71- return ; // not supported on Folia
7280 String teamName = Util .getTeamName (npc .getUniqueId ());
7381 npc .data ().set (NPC .Metadata .SCOREBOARD_FAKE_TEAM_NAME , teamName );
74- Scoreboard scoreboard = Util .getDummyScoreboard ();
75- Team team = scoreboard .getTeam (teamName );
82+ AbstractTeam team = scoreboard .getTeam (teamName );
7683 if (team == null ) {
77- team = scoreboard .registerNewTeam (teamName );
84+ team = scoreboard .createTeam (teamName );
7885 }
7986 if (!team .hasEntry (entityName )) {
8087 clearClientTeams (team );
@@ -86,11 +93,11 @@ public ChatColor getColor() {
8693 return color ;
8794 }
8895
89- private Team getTeam () {
96+ private AbstractTeam getTeam () {
9097 String teamName = npc .data ().get (NPC .Metadata .SCOREBOARD_FAKE_TEAM_NAME , "" );
9198 if (teamName .isEmpty ())
9299 return null ;
93- return Util . getDummyScoreboard () .getTeam (teamName );
100+ return scoreboard .getTeam (teamName );
94101 }
95102
96103 @ Override
@@ -102,20 +109,18 @@ public void load(DataKey key) {
102109
103110 @ Override
104111 public void onDespawn (DespawnReason reason ) {
105- if (SpigotUtil .isFoliaServer ())
106- return ; // Not Supported on Folia
107112 previousGlowingColor = null ;
108113 String name = lastName ;
109114 String teamName = npc .data ().get (NPC .Metadata .SCOREBOARD_FAKE_TEAM_NAME , "" );
110115 if (teamName .isEmpty ())
111116 return ;
112- Team team = Util . getDummyScoreboard () .getTeam (teamName );
117+ AbstractTeam team = scoreboard .getTeam (teamName );
113118 npc .data ().remove (NPC .Metadata .SCOREBOARD_FAKE_TEAM_NAME );
114119 if (team == null || name == null || !team .hasEntry (name )) {
115120 try {
116121 if (team != null && team .getSize () == 0 ) {
117122 clearClientTeams (team );
118- team . unregister ( );
123+ scoreboard . removeTeam ( teamName );
119124 }
120125 } catch (IllegalStateException ex ) {
121126 }
@@ -131,7 +136,7 @@ public void onDespawn(DespawnReason reason) {
131136 }
132137 if (team .getSize () <= 1 ) {
133138 clearClientTeams (team );
134- team . unregister ( );
139+ scoreboard . removeTeam ( teamName );
135140 } else {
136141 team .removeEntry (name );
137142 }
@@ -177,16 +182,18 @@ public void update() {
177182 String forceVisible = npc .data ().<Object > get (NPC .Metadata .NAMEPLATE_VISIBLE , true ).toString ();
178183 boolean nameVisibility = !npc .requiresNameHologram ()
179184 && (forceVisible .equals ("true" ) || forceVisible .equals ("hover" ));
180- Team team = getTeam ();
185+ AbstractTeam team = getTeam ();
181186 if (team == null )
182187 return ;
183188
184189 if (!Setting .USE_SCOREBOARD_TEAMS .asBoolean ()) {
185190 for (Player player : Bukkit .getOnlinePlayers ()) {
186191 metadata .remove (player .getUniqueId (), team .getName ());
187- NMS .sendTeamPacket (player , team , 1 );
192+ //NMS.sendTeamPacket(player, team, 1);
193+ team .sendToPlayer (player , AbstractTeam .SendMode .REMOVE );
188194 }
189- team .unregister ();
195+ //team.unregister();
196+ scoreboard .removeTeam (team .getName ());
190197 npc .data ().remove (NPC .Metadata .SCOREBOARD_FAKE_TEAM_NAME );
191198 return ;
192199 }
@@ -196,40 +203,35 @@ public void update() {
196203 : npc .getUniqueId ().toString ();
197204 }
198205 if (SUPPORT_TEAM_SETOPTION ) {
199- OptionStatus visibility = nameVisibility ? OptionStatus . ALWAYS : OptionStatus . NEVER ;
200- if (visibility != team .getOption ( Option . NAME_TAG_VISIBILITY )) {
206+ AbstractTeam . NameTags visibility = nameVisibility ? AbstractTeam . NameTags . ALWAYS_SHOW : AbstractTeam . NameTags . NEVER_SHOW ;
207+ if (visibility != team .getNameTagVisibility ( )) {
201208 changed = true ;
202209 }
203- team .setOption (Option .NAME_TAG_VISIBILITY , visibility );
204- } else {
205- NMS .setTeamNameTagVisible (team , nameVisibility );
210+ team .setNameTagVisibility (visibility );
206211 }
212+ // else { // TODO
213+ // NMS.setTeamNameTagVisible(team, nameVisibility);
214+ // }
215+
207216 if (SUPPORT_COLLIDABLE_SETOPTION ) {
208217 try {
209- OptionStatus collide = npc .data ().<Boolean > get (NPC .Metadata .COLLIDABLE , !npc .isProtected ())
210- ? OptionStatus .ALWAYS
211- : OptionStatus .NEVER ;
212- if (collide != team .getOption ( Option . COLLISION_RULE )) {
218+ AbstractTeam . CollisionRule collide = npc .data ().<Boolean > get (NPC .Metadata .COLLIDABLE , !npc .isProtected ())
219+ ? AbstractTeam . CollisionRule .ALWAYS
220+ : AbstractTeam . CollisionRule .NEVER ;
221+ if (collide != team .getCollisionRule ( )) {
213222 changed = true ;
214223 }
215- team .setOption (Option .COLLISION_RULE , collide );
224+ team .setCollisionRule (collide );
225+ //team.setOption(Option.COLLISION_RULE, collide);
216226 } catch (NoSuchMethodError e ) {
217227 SUPPORT_COLLIDABLE_SETOPTION = false ;
218228 } catch (NoClassDefFoundError e ) {
219229 SUPPORT_COLLIDABLE_SETOPTION = false ;
220230 }
221231 }
222- if (color != null ) {
223- if (SUPPORT_GLOWING_COLOR ) {
224- if (team .getColor () == null || previousGlowingColor == null
225- || previousGlowingColor != null && color != previousGlowingColor ) {
226- team .setColor (color );
227- previousGlowingColor = color ;
228- changed = true ;
229- }
230- } else if (team .getPrefix () == null || team .getPrefix ().length () == 0 || previousGlowingColor == null
231- || previousGlowingColor != null && !team .getPrefix ().equals (previousGlowingColor .toString ())) {
232- team .setPrefix (color .toString ());
232+ if (color != null && SUPPORT_GLOWING_COLOR ) {
233+ if (team .getColor () == null || previousGlowingColor == null || color != previousGlowingColor ) {
234+ team .setColor (color );
233235 previousGlowingColor = color ;
234236 changed = true ;
235237 }
@@ -241,9 +243,11 @@ public void update() {
241243 continue ;
242244
243245 if (metadata .has (player .getUniqueId (), team .getName ())) {
244- NMS .sendTeamPacket (player , team , 2 );
246+ //NMS.sendTeamPacket(player, team, 2);
247+ team .sendToPlayer (player , AbstractTeam .SendMode .ADD_OR_MODIFY );
245248 } else {
246- NMS .sendTeamPacket (player , team , 0 );
249+ //NMS.sendTeamPacket(player, team, 0);
250+ team .sendToPlayer (player , AbstractTeam .SendMode .ADD_OR_MODIFY );
247251
248252 metadata .set (player .getUniqueId (), team .getName (), true );
249253 }
0 commit comments