Skip to content

Commit

Permalink
Fix incorrect onDespawn usage leaking teams in ScoreboardTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Sep 18, 2023
1 parent 3e7b465 commit 7b650a2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java
Expand Up @@ -22,7 +22,6 @@
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

Expand Down Expand Up @@ -73,7 +72,6 @@ public void createTeam(String entityName) {
clearClientTeams(team);
}
team.addEntry(entityName);
Messaging.debug("Created team", teamName, "with entity", entityName);
}

public ChatColor getColor() {
Expand All @@ -96,8 +94,16 @@ public void onDespawn(DespawnReason reason) {
return;
Team team = Util.getDummyScoreboard().getTeam(teamName);
npc.data().remove(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME);
if (team == null || name == null || !team.hasEntry(name))
if (team == null || name == null || !team.hasEntry(name)) {
try {
if (team != null && team.getSize() == 0) {
clearClientTeams(team);
team.unregister();
}
} catch (IllegalStateException ex) {
}
return;
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
if (npc.isSpawned())
return;
Expand All @@ -106,20 +112,18 @@ public void onDespawn(DespawnReason reason) {
} catch (IllegalStateException ex) {
return;
}
if (team.getSize() == 1) {
if (team.getSize() <= 1) {
clearClientTeams(team);
team.unregister();
Messaging.debug("Removed team", teamName);
} else {
team.removeEntry(name);
Messaging.debug("Removed team entry", name, "from", teamName);
}
}, reason == DespawnReason.DEATH && npc.getEntity() instanceof LivingEntity ? 20 : 2);
}

@Override
public void onRemove() {
onDespawn();
onDespawn(DespawnReason.REMOVAL);
}

@Override
Expand Down

0 comments on commit 7b650a2

Please sign in to comment.