Solved NMS Hide "fake" Player Entity from tab and other plugins

Discussion in 'Plugin Development' started by Excel8392, Apr 5, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Excel8392

    Hello, I am trying to create a simple, much more lightweight version of the citizens plugin, which just allows you to create a simple non-moving player with a skin.
    Currently, I have this:
    Code:
    public class Npc {
    
        public EntityPlayer entityPlayer;
        private GameProfile gameProfile;
        private Location location;
    
        public Npc(Location location, String name) {
            gameProfile = new GameProfile(UUID.fromString("00000000-0000-0000-0000-000000000000"), "name");
            this.location = location;
    
            MinecraftServer minecraftServer = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer worldServer = ((CraftWorld) this.location.getWorld()).getHandle();
            this.entityPlayer = new EntityPlayer(minecraftServer, worldServer, this.gameProfile, new PlayerInteractManager(worldServer));
    
            this.entityPlayer.playerConnection = new PlayerConnection(minecraftServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND), this.entityPlayer);
            this.entityPlayer.setHealth(1f);
            worldServer.addEntity(this.entityPlayer);
            this. entityPlayer.setLocation(this.location.getX(), this.location.getY(), this.location.getZ(), this.location.getYaw(), this.location.getPitch());
        }
    
        public void spawnForPlayer(Player player) {
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, this.entityPlayer));
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutNamedEntitySpawn(entityPlayer));
        }
    
    }
    Which works 100% as intended. However, when opening tab, and when using things like Bukkit.getOnlinePlayers, my fake NPC player will still show up there. How can I make sure that the NPC isn't counted as a real player?
    Thanks in advance.

    EDIT: Also, I am using minecraft 1.15.
     
  2. Offline

    Reflxction

    MinecraftServer#getPlayerList() contains players, a mutable CopyOnWriteArrayList of EntityPlayers. Bukkit#getOnlinePlayers() is a concurrently-updated copy of MinecraftServer's player list, so modifying the NMS copy should also stop it from appearing in getOnlinePlayers().

    Basically:

    PHP:
            EntityPlayer entityPlayer = ...;
            ((
    CraftServerBukkit.getServer()).getServer().getPlayerList().players.remove(entityPlayer);
      
            
    // or, for better consistency
            
    ((CraftServerBukkit.getServer()).getServer().getPlayerList().players.removeIf(-> e.getUniqueID().equals(entityPlayer.getUniqueID()));
    Got my information from Minecraft 1.14.4. Shouldn't be different in 1.15.

    Disclaimer: I didn't test :p I'm merely tracking references kept in the Minecraft server
     
    Last edited: Apr 5, 2020
    Excel8392 likes this.
  3. Offline

    Excel8392

    Thanks! That hid it from Bukkit.getOnlinePlayers, but not from tab...
    But since the server I am working on is going to be using a custom tab list, and from what I can tell, that tab list uses Bukkit.getOnlinePlayers and such, this is perfect.
    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page