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) { public void onPlayerJoin(PlayerJoinEvent event) {
skinUpdateTracker.updatePlayer(event.getPlayer(), Setting.INITIAL_PLAYER_JOIN_SKIN_PACKET_DELAY_TICKS.asInt(), skinUpdateTracker.updatePlayer(event.getPlayer(), Setting.INITIAL_PLAYER_JOIN_SKIN_PACKET_DELAY_TICKS.asInt(),
true); true);
ScoreboardTrait.onPlayerJoin(event);
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)

View File

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

View File

@ -75,7 +75,7 @@ public class PlayerUpdateTask extends BukkitRunnable {
PLAYERS_PENDING_REMOVE.add(entity); PLAYERS_PENDING_REMOVE.add(entity);
} }
public static Iterable<Player> getRegisteredPlayerNPCs() { public static Iterable<Player> getCurrentPlayerNPCs() {
return PLAYERS.values(); 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.npc.NPC;
import net.citizensnpcs.api.util.BoundingBox; import net.citizensnpcs.api.util.BoundingBox;
import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.npc.ai.NPCHolder;
public class Util { public class Util {
private Util() { private Util() {
@ -350,19 +349,6 @@ public class Util {
NMS.look(entity, yaw, pitch); 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 Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard(); private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
private static String MINECRAFT_REVISION; private static String MINECRAFT_REVISION;