Try scanning the online players less

This commit is contained in:
fullwall 2022-07-25 15:53:34 +08:00
parent acca94d911
commit 71bc177298
4 changed files with 33 additions and 23 deletions

View File

@ -504,6 +504,8 @@ public class EventListen implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) {
skinUpdateTracker.updatePlayer(event.getPlayer(), Setting.INITIAL_PLAYER_JOIN_SKIN_PACKET_DELAY_TICKS.asInt(),
true);
ScoreboardTrait.onPlayerJoin(event);
}
@EventHandler(ignoreCancelled = true)

View File

@ -5,8 +5,10 @@ import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
@ -22,6 +24,7 @@ import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.PlayerUpdateTask;
import net.citizensnpcs.util.Util;
@TraitName("scoreboardtrait")
@ -138,16 +141,16 @@ public class ScoreboardTrait extends Trait {
}
}
for (Player player : npc.getEntity().getWorld().getPlayers()) {
if (player instanceof NPCHolder)
continue;
if (SENT_TEAMS.containsEntry(player.getUniqueId(), team.getName())) {
if (changed) {
if (changed) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player instanceof NPCHolder)
continue;
if (SENT_TEAMS.containsEntry(player.getUniqueId(), team.getName())) {
NMS.sendTeamPacket(player, team, 2);
} else {
NMS.sendTeamPacket(player, team, 0);
SENT_TEAMS.put(player.getUniqueId(), team.getName());
}
} else {
NMS.sendTeamPacket(player, team, 0);
SENT_TEAMS.put(player.getUniqueId(), team.getName());
}
}
}
@ -175,6 +178,11 @@ public class ScoreboardTrait extends Trait {
npc.getEntity() instanceof Player ? npc.getEntity().getName() : npc.getUniqueId().toString());
}
@Override
public void onSpawn() {
changed = true;
}
public void removeTag(String tag) {
tags.remove(tag);
}
@ -183,6 +191,20 @@ public class ScoreboardTrait extends Trait {
this.color = color;
}
public static void onPlayerJoin(PlayerJoinEvent event) {
for (Player npcPlayer : PlayerUpdateTask.getCurrentPlayerNPCs()) {
NPC npc = ((NPCHolder) npcPlayer).getNPC();
String teamName = npc.data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
Team team = null;
if (teamName.length() == 0 || (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
continue;
NMS.sendTeamPacket(event.getPlayer(), team, 0);
SENT_TEAMS.put(event.getPlayer().getUniqueId(), team.getName());
}
}
public static void onPlayerQuit(PlayerQuitEvent event) {
SENT_TEAMS.removeAll(event.getPlayer().getUniqueId());
}

View File

@ -75,7 +75,7 @@ public class PlayerUpdateTask extends BukkitRunnable {
PLAYERS_PENDING_REMOVE.add(entity);
}
public static Iterable<Player> getRegisteredPlayerNPCs() {
public static Iterable<Player> getCurrentPlayerNPCs() {
return PLAYERS.values();
}

View File

@ -28,7 +28,6 @@ import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.BoundingBox;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.npc.ai.NPCHolder;
public class Util {
private Util() {
@ -350,19 +349,6 @@ public class Util {
NMS.look(entity, yaw, pitch);
}
public static void updateNPCTeams(Player toUpdate, int mode) {
for (Player player : PlayerUpdateTask.getRegisteredPlayerNPCs()) {
NPC npc = ((NPCHolder) player).getNPC();
String teamName = npc.data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
Team team = null;
if (teamName.length() == 0 || (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
continue;
NMS.sendTeamPacket(toUpdate, team, mode);
}
}
private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
private static String MINECRAFT_REVISION;