From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Corey Shupe Date: Wed, 11 Jan 2023 16:40:39 -0500 Subject: [PATCH] Add Listing API for Player diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundPlayerInfoUpdatePacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundPlayerInfoUpdatePacket.java index d43106eb89b14667e85cd6e8fa047d64f2e8ec87..56eddd28429cf42c02d88b8bf79f8b616fa45289 100644 --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundPlayerInfoUpdatePacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundPlayerInfoUpdatePacket.java @@ -29,12 +29,46 @@ public class ClientboundPlayerInfoUpdatePacket implements Packet actions, List entries) { + this.actions = actions; + this.entries = entries; + } + + public ClientboundPlayerInfoUpdatePacket(EnumSet actions, ClientboundPlayerInfoUpdatePacket.Entry entry) { + this.actions = actions; + this.entries = List.of(entry); + } + // Paper end - Add Listing API for Player public static ClientboundPlayerInfoUpdatePacket createPlayerInitializing(Collection players) { EnumSet enumSet = EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME); return new ClientboundPlayerInfoUpdatePacket(enumSet, players); } + // Paper start - Add Listing API for Player + public static ClientboundPlayerInfoUpdatePacket createPlayerInitializing(Collection players, ServerPlayer forPlayer) { + final EnumSet enumSet = EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME); + final List entries = new java.util.ArrayList<>(players.size()); + final org.bukkit.craftbukkit.entity.CraftPlayer bukkitEntity = forPlayer.getBukkitEntity(); + for (final ServerPlayer player : players) { + entries.add(new ClientboundPlayerInfoUpdatePacket.Entry(player, bukkitEntity.isListed(player.getBukkitEntity()))); + } + return new ClientboundPlayerInfoUpdatePacket(enumSet, entries); + } + + public static ClientboundPlayerInfoUpdatePacket createSinglePlayerInitializing(ServerPlayer player, boolean listed) { + final EnumSet enumSet = EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY, ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME); + final List entries = List.of(new Entry(player, listed)); + return new ClientboundPlayerInfoUpdatePacket(enumSet, entries); + } + + public static ClientboundPlayerInfoUpdatePacket updateListed(UUID playerInfoId, boolean listed) { + EnumSet enumSet = EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED); + return new ClientboundPlayerInfoUpdatePacket(enumSet, new ClientboundPlayerInfoUpdatePacket.Entry(playerInfoId, listed)); + } + // Paper end - Add Listing API for Player + public ClientboundPlayerInfoUpdatePacket(FriendlyByteBuf buf) { this.actions = buf.readEnumSet(ClientboundPlayerInfoUpdatePacket.Action.class); this.entries = buf.readList((buf2) -> { @@ -144,8 +178,16 @@ public class ClientboundPlayerInfoUpdatePacket implements Packet onlinePlayers = Lists.newArrayListWithExpectedSize(this.players.size() - 1); // Paper - Use single player info update packet on join for (int i = 0; i < this.players.size(); ++i) { ServerPlayer entityplayer1 = (ServerPlayer) this.players.get(i); if (entityplayer1.getBukkitEntity().canSee(bukkitPlayer)) { + // Paper start - Add Listing API for Player + if (entityplayer1.getBukkitEntity().isListed(bukkitPlayer)) { + // Paper end - Add Listing API for Player entityplayer1.connection.send(packet); + // Paper start - Add Listing API for Player + } else { + entityplayer1.connection.send(ClientboundPlayerInfoUpdatePacket.createSinglePlayerInitializing(player, false)); + } + // Paper end - Add Listing API for Player } if (entityplayer1 == player || !bukkitPlayer.canSee(entityplayer1.getBukkitEntity())) { // Paper - Use single player info update packet on join; Don't include joining player @@ -374,7 +382,7 @@ public abstract class PlayerList { } // Paper start - Use single player info update packet on join if (!onlinePlayers.isEmpty()) { - player.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(onlinePlayers)); + player.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(onlinePlayers, player)); // Paper - Add Listing API for Player } // Paper end - Use single player info update packet on join player.sentListPacket = true; diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index c6055020d279892d42c6869c71150849300ef34c..b0b18fcc2ec8769831860596823b362fe0fd9313 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -182,6 +182,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { private final ConversationTracker conversationTracker = new ConversationTracker(); private final Set channels = new HashSet(); private final Map>> invertedVisibilityEntities = new HashMap<>(); + private final Set unlistedEntities = new HashSet<>(); // Paper - Add Listing API for Player private static final WeakHashMap> pluginWeakReferences = new WeakHashMap<>(); private int hash = 0; private double health = 20; @@ -1992,7 +1993,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { otherPlayer.setUUID(uuidOverride); } // Paper end - this.getHandle().connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(otherPlayer))); + this.getHandle().connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(otherPlayer), this.getHandle())); // Paper - Add Listing API for Player if (original != null) otherPlayer.setUUID(original); // Paper - uuid override } @@ -2099,6 +2100,43 @@ public class CraftPlayer extends CraftHumanEntity implements Player { return (entity != null) ? this.canSee(entity) : false; // If we can't find it, we can't see it } + // Paper start - Add Listing API for Player + @Override + public boolean isListed(Player other) { + return !this.unlistedEntities.contains(other.getUniqueId()); + } + + @Override + public boolean unlistPlayer(@NotNull Player other) { + Preconditions.checkNotNull(other, "hidden entity cannot be null"); + if (this.getHandle().connection == null) return false; + if (this.equals(other)) return false; + if (!this.canSee(other)) return false; + + if (unlistedEntities.add(other.getUniqueId())) { + this.getHandle().connection.send(ClientboundPlayerInfoUpdatePacket.updateListed(other.getUniqueId(), false)); + return true; + } else { + return false; + } + } + + @Override + public boolean listPlayer(@NotNull Player other) { + Preconditions.checkNotNull(other, "hidden entity cannot be null"); + if (this.getHandle().connection == null) return false; + if (this.equals(other)) return false; + if (!this.canSee(other)) throw new IllegalStateException("Player cannot see other player"); + + if (this.unlistedEntities.remove(other.getUniqueId())) { + this.getHandle().connection.send(ClientboundPlayerInfoUpdatePacket.updateListed(other.getUniqueId(), true)); + return true; + } else { + return false; + } + } + // Paper end - Add Listing API for Player + @Override public Map serialize() { Map result = new LinkedHashMap();