From 37c88685324165dad8692dae03014cbfeb119936 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Tue, 31 Jan 2012 13:54:15 -0600 Subject: [PATCH] [Bleeding] Implemented Vanish API. Remove players that cannot see a player from their EntityTrackerEntry and only send user list updates to players who can see the player they reference. --- .../net/minecraft/server/EntityTracker.java | 2 +- .../minecraft/server/EntityTrackerEntry.java | 8 +++ .../server/ServerConfigurationManager.java | 32 +++++++-- .../craftbukkit/entity/CraftPlayer.java | 65 ++++++++++++++----- 4 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/minecraft/server/EntityTracker.java b/src/main/java/net/minecraft/server/EntityTracker.java index 89eba263d5..93f9598395 100644 --- a/src/main/java/net/minecraft/server/EntityTracker.java +++ b/src/main/java/net/minecraft/server/EntityTracker.java @@ -8,7 +8,7 @@ import java.util.Set; public class EntityTracker { private Set a = new HashSet(); - private IntHashMap trackedEntities = new IntHashMap(); + public IntHashMap trackedEntities = new IntHashMap(); // CraftBukkit - private -> public private MinecraftServer c; private int d; private World world; // CraftBukkit - change type diff --git a/src/main/java/net/minecraft/server/EntityTrackerEntry.java b/src/main/java/net/minecraft/server/EntityTrackerEntry.java index caef4df747..6ccf018980 100644 --- a/src/main/java/net/minecraft/server/EntityTrackerEntry.java +++ b/src/main/java/net/minecraft/server/EntityTrackerEntry.java @@ -187,6 +187,14 @@ public class EntityTrackerEntry { if (d0 >= (double) (-this.b) && d0 <= (double) this.b && d1 >= (double) (-this.b) && d1 <= (double) this.b) { if (!this.trackedPlayers.contains(entityplayer)) { + // CraftBukkit start + if (tracker instanceof EntityPlayer) { + org.bukkit.entity.Player player = ((EntityPlayer) tracker).getBukkitEntity(); + if (!entityplayer.getBukkitEntity().canSee(player)) { + return; + } + } + // CraftBukkit end this.trackedPlayers.add(entityplayer); entityplayer.netServerHandler.sendPacket(this.b()); if (this.isMoving) { diff --git a/src/main/java/net/minecraft/server/ServerConfigurationManager.java b/src/main/java/net/minecraft/server/ServerConfigurationManager.java index 271fb03596..3cea43f1e2 100644 --- a/src/main/java/net/minecraft/server/ServerConfigurationManager.java +++ b/src/main/java/net/minecraft/server/ServerConfigurationManager.java @@ -111,10 +111,8 @@ public class ServerConfigurationManager { } public void c(EntityPlayer entityplayer) { - // CraftBukkit start - cserver.detectListNameConflict(entityplayer); - this.sendAll(new Packet201PlayerInfo(entityplayer.listName, true, 1000)); - // CraftBukkit end + cserver.detectListNameConflict(entityplayer); // CraftBukkit + //this.sendAll(new Packet201PlayerInfo(entityplayer.name, true, 1000)); // CraftBukkit - replaced with loop below this.players.add(entityplayer); WorldServer worldserver = this.server.getWorldServer(entityplayer.dimension); @@ -142,11 +140,24 @@ public class ServerConfigurationManager { worldserver.addEntity(entityplayer); this.getPlayerManager(entityplayer.dimension).addPlayer(entityplayer); + // CraftBukkit start - sendAll above replaced with this loop + Packet201PlayerInfo packet = new Packet201PlayerInfo(entityplayer.listName, true, 1000); + for (int i = 0; i < this.players.size(); ++i) { + EntityPlayer entityplayer1 = (EntityPlayer) this.players.get(i); + + if (entityplayer1.getBukkitEntity().canSee(entityplayer.getBukkitEntity())) { + entityplayer1.netServerHandler.sendPacket(packet); + } + } + // CraftBukkit end + for (int i = 0; i < this.players.size(); ++i) { EntityPlayer entityplayer1 = (EntityPlayer) this.players.get(i); // CraftBukkit start - .name -> .listName - entityplayer.netServerHandler.sendPacket(new Packet201PlayerInfo(entityplayer1.listName, true, entityplayer1.ping)); + if (entityplayer.getBukkitEntity().canSee(entityplayer1.getBukkitEntity())) { + entityplayer.netServerHandler.sendPacket(new Packet201PlayerInfo(entityplayer1.listName, true, entityplayer1.ping)); + } // CraftBukkit end } } @@ -169,8 +180,15 @@ public class ServerConfigurationManager { this.server.getWorldServer(entityplayer.dimension).kill(entityplayer); this.players.remove(entityplayer); this.getPlayerManager(entityplayer.dimension).removePlayer(entityplayer); - // CraftBukkit start - .name -> .listName - this.sendAll(new Packet201PlayerInfo(entityplayer.listName, false, 9999)); + // CraftBukkit start - .name -> .listName, replace sendAll with loop + Packet201PlayerInfo packet = new Packet201PlayerInfo(entityplayer.listName, false, 9999); + for (int i = 0; i < this.players.size(); ++i) { + EntityPlayer entityplayer1 = (EntityPlayer) this.players.get(i); + + if (entityplayer1.getBukkitEntity().canSee(entityplayer.getBukkitEntity())) { + entityplayer1.netServerHandler.sendPacket(packet); + } + } // CraftBukkit end return playerQuitEvent.getQuitMessage(); // CraftBukkit diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 6aa39bca69..e91726375d 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -11,22 +11,12 @@ import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import net.minecraft.server.ChunkCoordinates; -import net.minecraft.server.EntityPlayer; -import net.minecraft.server.NBTTagCompound; -import net.minecraft.server.Packet131ItemData; -import net.minecraft.server.Packet200Statistic; -import net.minecraft.server.Packet201PlayerInfo; -import net.minecraft.server.Packet250CustomPayload; -import net.minecraft.server.Packet3Chat; -import net.minecraft.server.Packet51MapChunk; -import net.minecraft.server.Packet53BlockChange; -import net.minecraft.server.Packet54PlayNoteBlock; -import net.minecraft.server.Packet61WorldEvent; -import net.minecraft.server.Packet6SpawnPosition; -import net.minecraft.server.Packet70Bed; -import net.minecraft.server.WorldServer; +import net.minecraft.server.*; import org.bukkit.*; +import org.bukkit.Achievement; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.World; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.CraftOfflinePlayer; import org.bukkit.craftbukkit.CraftServer; @@ -46,6 +36,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { private long lastPlayed = 0; private boolean hasPlayedBefore = false; private Set channels = new HashSet(); + private Set hiddenPlayers = new HashSet(); private int hash = 0; public CraftPlayer(CraftServer server, EntityPlayer entity) { @@ -162,8 +153,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().listName = name; // Change the name on the client side - server.getHandle().sendAll(new Packet201PlayerInfo(oldName, false, 9999)); - server.getHandle().sendAll(new Packet201PlayerInfo(name, true, getHandle().ping)); + Packet201PlayerInfo oldpacket = new Packet201PlayerInfo(oldName, false, 9999); + Packet201PlayerInfo packet = new Packet201PlayerInfo(name, true, getHandle().ping); + for (int i = 0; i < server.getHandle().players.size(); ++i) { + EntityPlayer entityplayer = (EntityPlayer) server.getHandle().players.get(i); + + if (entityplayer.getBukkitEntity().canSee(this)) { + entityplayer.netServerHandler.sendPacket(oldpacket); + entityplayer.netServerHandler.sendPacket(packet); + } + } } @Override @@ -564,6 +563,38 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().spawnWorld = location.getWorld().getName(); } + public void hidePlayer(Player player) { + hiddenPlayers.add(player); + + //remove this player from the hidden player's EntityTrackerEntry + EntityTracker tracker = ((WorldServer) entity.world).tracker; + EntityPlayer other = ((CraftPlayer) player).getHandle(); + EntityTrackerEntry entry = (EntityTrackerEntry) tracker.trackedEntities.a(other.id); + if (entry != null) { + entry.c(getHandle()); + } + + //remove the hidden player from this player user list + getHandle().netServerHandler.sendPacket(new Packet201PlayerInfo(player.getPlayerListName(), false, 9999)); + } + + public void showPlayer(Player player) { + hiddenPlayers.remove(player); + + EntityTracker tracker = ((WorldServer) entity.world).tracker; + EntityPlayer other = ((CraftPlayer) player).getHandle(); + EntityTrackerEntry entry = (EntityTrackerEntry) tracker.trackedEntities.a(other.id); + if (entry != null && !entry.trackedPlayers.contains(getHandle())) { + entry.b(getHandle()); + } + + getHandle().netServerHandler.sendPacket(new Packet201PlayerInfo(player.getPlayerListName(), true, getHandle().ping)); + } + + public boolean canSee(Player player) { + return !hiddenPlayers.contains(player); + } + public Map serialize() { Map result = new LinkedHashMap();