Remove team unregistering to ScoreboardTrait, add scoreboard-teams send ticks to assist with a possible scoreboard conflict

This commit is contained in:
fullwall 2022-07-13 20:09:21 +08:00
parent 3fcf8931f7
commit 4dfac9c62d
15 changed files with 40 additions and 57 deletions

View File

@ -150,6 +150,7 @@ public class Settings {
PLACEHOLDER_SKIN_UPDATE_FREQUENCY("npc.skins.placeholder-update-frequency-ticks", 5 * 60 * 20), PLACEHOLDER_SKIN_UPDATE_FREQUENCY("npc.skins.placeholder-update-frequency-ticks", 5 * 60 * 20),
REMOVE_PLAYERS_FROM_PLAYER_LIST("npc.player.remove-from-list", true), REMOVE_PLAYERS_FROM_PLAYER_LIST("npc.player.remove-from-list", true),
SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60), SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60),
SCOREBOARD_SEND_TICKS("npc.scoreboard-teams.packet-send-ticks", 1),
SELECTION_ITEM("npc.selection.item", "stick"), SELECTION_ITEM("npc.selection.item", "stick"),
SELECTION_MESSAGE("npc.selection.message", "Selected [[<npc>]] (ID <id>)."), SELECTION_MESSAGE("npc.selection.message", "Selected [[<npc>]] (ID <id>)."),
SERVER_OWNS_NPCS("npc.server-ownership", false), SERVER_OWNS_NPCS("npc.server-ownership", false),
@ -164,7 +165,7 @@ public class Settings {
TELEPORT_DELAY("npc.teleport-delay", -1), TELEPORT_DELAY("npc.teleport-delay", -1),
USE_BOAT_CONTROLS("npc.controllable.use-boat-controls", true), USE_BOAT_CONTROLS("npc.controllable.use-boat-controls", true),
USE_NEW_PATHFINDER("npc.pathfinding.use-new-finder", false), USE_NEW_PATHFINDER("npc.pathfinding.use-new-finder", false),
USE_SCOREBOARD_TEAMS("npc.player-scoreboard-teams.enable", true), USE_SCOREBOARD_TEAMS("npc.scoreboard-teams.enable", true),
WARN_ON_RELOAD("general.reload-warning-enabled", true); WARN_ON_RELOAD("general.reload-warning-enabled", true);
protected String path; protected String path;

View File

@ -15,7 +15,6 @@ import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Team;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -477,17 +476,9 @@ public class CitizensNPC extends AbstractNPC {
getEntity().setCustomName(getFullName()); getEntity().setCustomName(getFullName());
} }
String teamName = data().get(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME, ""); if (data().has(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME)) {
Team team = null; getOrAddTrait(ScoreboardTrait.class).apply(nameVisibility);
if (teamName.length() == 0 || (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
return;
if (!Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
team.unregister();
data().remove(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA);
return;
} }
getOrAddTrait(ScoreboardTrait.class).apply(team, nameVisibility);
} }
private void updateCustomNameVisibility() { private void updateCustomNameVisibility() {

View File

@ -5,10 +5,12 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option; import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus; import org.bukkit.scoreboard.Team.OptionStatus;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
@ -20,7 +22,7 @@ import net.citizensnpcs.util.Util;
public class ScoreboardTrait extends Trait { public class ScoreboardTrait extends Trait {
@Persist @Persist
private ChatColor color; private ChatColor color;
private boolean justSpawned; private int justSpawned;
private ChatColor previousGlowingColor; private ChatColor previousGlowingColor;
@Persist @Persist
private final Set<String> tags = new HashSet<String>(); private final Set<String> tags = new HashSet<String>();
@ -33,12 +35,20 @@ public class ScoreboardTrait extends Trait {
tags.add(tag); tags.add(tag);
} }
public void apply(Team team, boolean nameVisibility) { public void apply(boolean nameVisibility) {
Team team = getTeam();
if (!Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
team.unregister();
npc.data().remove(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA);
return;
}
Set<String> newTags = new HashSet<String>(tags); Set<String> newTags = new HashSet<String>(tags);
if (SUPPORT_TAGS) { if (SUPPORT_TAGS) {
try { try {
if (!npc.getEntity().getScoreboardTags().equals(tags)) { if (!npc.getEntity().getScoreboardTags().equals(tags)) {
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
for (Iterator<String> iterator = npc.getEntity().getScoreboardTags().iterator(); iterator for (Iterator<String> iterator = npc.getEntity().getScoreboardTags().iterator(); iterator
.hasNext();) { .hasNext();) {
String oldTag = iterator.next(); String oldTag = iterator.next();
@ -59,7 +69,7 @@ public class ScoreboardTrait extends Trait {
try { try {
OptionStatus visibility = nameVisibility ? OptionStatus.ALWAYS : OptionStatus.NEVER; OptionStatus visibility = nameVisibility ? OptionStatus.ALWAYS : OptionStatus.NEVER;
if (visibility != team.getOption(Option.NAME_TAG_VISIBILITY)) { if (visibility != team.getOption(Option.NAME_TAG_VISIBILITY)) {
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
} }
team.setOption(Option.NAME_TAG_VISIBILITY, visibility); team.setOption(Option.NAME_TAG_VISIBILITY, visibility);
} catch (NoSuchMethodError e) { } catch (NoSuchMethodError e) {
@ -74,7 +84,7 @@ public class ScoreboardTrait extends Trait {
OptionStatus collide = npc.data().<Boolean> get(NPC.COLLIDABLE_METADATA) ? OptionStatus.ALWAYS OptionStatus collide = npc.data().<Boolean> get(NPC.COLLIDABLE_METADATA) ? OptionStatus.ALWAYS
: OptionStatus.NEVER; : OptionStatus.NEVER;
if (collide != team.getOption(Option.COLLISION_RULE)) { if (collide != team.getOption(Option.COLLISION_RULE)) {
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
} }
team.setOption(Option.COLLISION_RULE, collide); team.setOption(Option.COLLISION_RULE, collide);
} catch (NoSuchMethodError e) { } catch (NoSuchMethodError e) {
@ -103,7 +113,7 @@ public class ScoreboardTrait extends Trait {
|| (previousGlowingColor != null && color != previousGlowingColor)) { || (previousGlowingColor != null && color != previousGlowingColor)) {
team.setColor(color); team.setColor(color);
previousGlowingColor = color; previousGlowingColor = color;
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
} }
} catch (NoSuchMethodError err) { } catch (NoSuchMethodError err) {
SUPPORT_GLOWING_COLOR = false; SUPPORT_GLOWING_COLOR = false;
@ -114,13 +124,13 @@ public class ScoreboardTrait extends Trait {
&& !team.getPrefix().equals(previousGlowingColor.toString()))) { && !team.getPrefix().equals(previousGlowingColor.toString()))) {
team.setPrefix(color.toString()); team.setPrefix(color.toString());
previousGlowingColor = color; previousGlowingColor = color;
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
} }
} }
} }
if (justSpawned) { if (justSpawned > 0) {
Util.sendTeamPacketToOnlinePlayers(team, 2); Util.sendTeamPacketToOnlinePlayers(team, 2);
justSpawned = false; justSpawned--;
} }
} }
@ -132,9 +142,24 @@ public class ScoreboardTrait extends Trait {
return tags; return tags;
} }
private Team getTeam() {
String teamName = npc.data().get(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME, "");
if (teamName.isEmpty())
return null;
return Util.getDummyScoreboard().getTeam(teamName);
}
@Override
public void onDespawn() {
if (npc.getEntity() == null)
return;
Util.removeTeamFor(npc,
npc.getEntity() instanceof Player ? npc.getEntity().getName() : npc.getUniqueId().toString());
}
@Override @Override
public void onSpawn() { public void onSpawn() {
justSpawned = true; justSpawned = Setting.SCOREBOARD_SEND_TICKS.asInt();
} }
public void removeTag(String tag) { public void removeTag(String tag) {
@ -143,7 +168,6 @@ public class ScoreboardTrait extends Trait {
public void setColor(ChatColor color) { public void setColor(ChatColor color) {
this.color = color; this.color = color;
justSpawned = true;
} }
private static boolean SUPPORT_COLLIDABLE_SETOPTION = true; private static boolean SUPPORT_COLLIDABLE_SETOPTION = true;

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -84,9 +84,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -82,9 +82,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -82,9 +82,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -81,9 +81,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();

View File

@ -85,9 +85,6 @@ public class HumanController extends AbstractEntityController {
public void remove() { public void remove() {
Player entity = getBukkitEntity(); Player entity = getBukkitEntity();
if (entity != null) { if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.removeTeamFor(NMS.getNPC(entity), entity.getName());
}
NMS.removeFromWorld(entity); NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC(); npc.getSkinTracker().onRemoveNPC();