From d20e3d6e1a2b6fdf21800eb541ac990d80f2cf7e Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Thu, 24 Oct 2024 16:44:45 +0800 Subject: [PATCH] =?UTF-8?q?misc:=20=E9=87=8D=E6=96=B0=E5=9C=A8=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E4=B8=96=E7=95=8C=E4=B8=AD=E5=AF=BB=E6=89=BE=E7=8E=A9?= =?UTF-8?q?=E5=AE=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../network/listeners/ProtocolListener.java | 25 +++++++---- .../morph/events/CommonEventProcessor.java | 5 --- .../nifeather/morph/misc/PlayerTracker.java | 43 ------------------- 3 files changed, 16 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/xyz/nifeather/morph/misc/PlayerTracker.java diff --git a/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/listeners/ProtocolListener.java b/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/listeners/ProtocolListener.java index 05daf7c8..54d5f57c 100644 --- a/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/listeners/ProtocolListener.java +++ b/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/listeners/ProtocolListener.java @@ -17,8 +17,6 @@ import xyz.nifeather.morph.backends.server.renderer.network.PacketFactory; import xyz.nifeather.morph.config.ConfigOption; import xyz.nifeather.morph.config.MorphConfigManager; -import xyz.nifeather.morph.misc.NmsRecord; -import xyz.nifeather.morph.misc.PlayerTracker; import xyz.nifeather.morph.utilities.NmsUtils; import xyz.nifeather.morph.utilities.ReflectionUtils; @@ -90,15 +88,24 @@ protected Player getNmsPlayerFrom(int id) //if (!TickThread.isTickThread()) // logger.warn("Not on a tick thread! Caution for exceptions!"); - var bukkitPlayer = PlayerTracker.instance().getPlayers() - .stream() - .filter(p -> p.getEntityId() == id) - .findFirst() - .orElse(null); + // Bukkit.getOnlinePlayers() 会将正前往不同维度的玩家从列表里移除 + // 因此我们需要在每个世界都手动查询一遍 + for (var world : Bukkit.getWorlds()) + { + // For performance, we use NMS instead of CraftWorld + var nmsWorld = NmsUtils.getNmsLevel(world); + var worldPlayers = nmsWorld.players(); + + var match = worldPlayers.stream() + .filter(p -> p.getId() == id) + .findFirst() + .orElse(null); - if (bukkitPlayer == null) return null; + if (match != null) + return match; + } - return NmsRecord.ofPlayer(bukkitPlayer); + return null; } @Nullable diff --git a/src/main/java/xyz/nifeather/morph/events/CommonEventProcessor.java b/src/main/java/xyz/nifeather/morph/events/CommonEventProcessor.java index e2fda3dc..a3d378ac 100644 --- a/src/main/java/xyz/nifeather/morph/events/CommonEventProcessor.java +++ b/src/main/java/xyz/nifeather/morph/events/CommonEventProcessor.java @@ -37,7 +37,6 @@ import xyz.nifeather.morph.messages.vanilla.VanillaMessageStore; import xyz.nifeather.morph.misc.DisguiseTypes; import xyz.nifeather.morph.misc.OfflineDisguiseResult; -import xyz.nifeather.morph.misc.PlayerTracker; import xyz.nifeather.morph.misc.gui.AnimSelectScreenWrapper; import xyz.nifeather.morph.misc.gui.DisguiseSelectScreenWrapper; import xyz.nifeather.morph.misc.permissions.CommonPermissions; @@ -362,8 +361,6 @@ public void onPlayerJoin(PlayerJoinEvent e) var player = e.getPlayer(); var state = morphs.getDisguiseStateFor(player); - PlayerTracker.instance().addPlayer(player); - clientHandler.markPlayerReady(player); //如果玩家是第一次用客户端连接,那么等待3秒向其发送提示 @@ -432,8 +429,6 @@ else if (result == OfflineDisguiseResult.LIMITED) @EventHandler public void onPlayerExit(PlayerQuitEvent e) { - PlayerTracker.instance().removePlayer(e.getPlayer()); - clientHandler.unInitializePlayer(e.getPlayer()); skillHandler.removeUnusedList(e.getPlayer()); diff --git a/src/main/java/xyz/nifeather/morph/misc/PlayerTracker.java b/src/main/java/xyz/nifeather/morph/misc/PlayerTracker.java deleted file mode 100644 index e0e038f6..00000000 --- a/src/main/java/xyz/nifeather/morph/misc/PlayerTracker.java +++ /dev/null @@ -1,43 +0,0 @@ -package xyz.nifeather.morph.misc; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import it.unimi.dsi.fastutil.objects.ObjectLists; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import java.util.List; - -public class PlayerTracker -{ - public static PlayerTracker instance() - { - return TracketLoader.trackerInstance; - } - - private static final class TracketLoader - { - public static final PlayerTracker trackerInstance = new PlayerTracker(); - } - - public PlayerTracker() - { - validPlayers.addAll(Bukkit.getOnlinePlayers()); - } - - private final List validPlayers = ObjectLists.synchronize(new ObjectArrayList<>()); - - public final List getPlayers() - { - return new ObjectArrayList<>(validPlayers); - } - - public void addPlayer(Player player) - { - validPlayers.add(player); - } - - public void removePlayer(Player player) - { - validPlayers.remove(player); - } -}