[SPIGOT-6796] NPCs created via packet not getting removed using ClientboundRemoveEntitiesPacket Created: 18/Nov/21  Updated: 18/Nov/21  Resolved: 18/Nov/21

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: maximus widmiaer Assignee: Unassigned
Resolution: Invalid Votes: 0
Labels: 1.17.1
Environment:

Windows 10 64bit

jdk-16.0.1

IDE configured to use Java 16


Version: This server is running Paper version git-Paper-384 (MC: 1.17.1) (Implementing API version 1.17.1-R0.1-SNAPSHOT) (Git: 51b7b60)
Plugin: N/A
Guidelines Read: Yes

 Description   

Note that I do use a paper server for testing but the same bug occurs on a spigot server.

 

This does require an IDE for java development, a server you can test on, and a new java project with spigot and craftbukkit as dependencies to use nms.

 

Main class
 

public class Main extends JavaPlugin
    @Override
    public void onEnable()
    {
        //Registers commands
        getCommand("npc-create").setExecuter(new NPCCreateCommand());
        getCommand("npc-remove").setExecuter(new NPCRemoveCommand());
    }

NPCCreateCommand class

public class NPCCreateCommand implements CommandExecuter
{
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) 
	{
		if(sender instanceof Player p)
		{
			CraftPlayer a = (CraftPlayer) p;
			EntityPlayer b = a.getHandle();
			
			MinecraftServer server = b.getMinecraftServer();
			WorldServer world = b.getWorldServer();
			GameProfile profile = new GameProfile(UUID.nameUUIDFromBytes(("NPC: test").getBytes(Charsets.UTF_8)), "test");

			EntityPlayer npc = new EntityPlayer(server, world, profile);
				
			NPCCommand.handlePlayerInfo(b, npc);
			PlayerConnection connection = player.b;
		
			connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a, npc));

			npc.setPosition(player.getBukkitEntity().getLocation().getX(), player.getBukkitEntity().getLocation().getY(), player.getBukkitEntity().getLocation().getZ());
		
		DataWatcher data = npc.getDataWatcher();
		data.set(new DataWatcherObject<>(17, DataWatcherRegistry.a), (byte) 0xFF);
		
		connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
		connection.sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), data, true));
		}

}

NPCRemoveCommand class

public class NPCRemoveCommand implements CommandExecuter
{
    @Override
    public void run(CommandSender sender, Command command, String label, String[] args) {
    		CraftPlayer a = (CraftPlayer) p;
		EntityPlayer b = a.getHandle();
			
		MinecraftServer server = b.getMinecraftServer();
		WorldServer world = b.getWorldServer();
			
			
		EntityPlayer npc = new EntityPlayer(server, world, new GameProfile(UUID.nameUUIDFromBytes(("NPC: test").getBytes(Charsets.UTF_8)), "test"));

		Player p = player.getBukkitEntity();
		
		//Debug message
		p.sendMessage("Deleted npc " + npc.getName().toString());
		
		
		connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, npc));
		connection.sendPacket(new PacketPlayOutEntityDestroy(npc.getId()));
    }
}

plugin.yml

name: test
version: 1
author: whoever
main: (whatever_package_main.class_is_in).Main
api-version: 1.17
description: test

commands:     npc-create:         description: creates test npc
        usage: /npc-create
    npc-remove:         description: removes test npc
        usage: /npc-destroy

Once this is done compile the jar and put it your server's plugin folder reload/start the server then run /npc-create. It will create an npc with a steve or alex skin and put a name into tab. Then run /npc-remove and it should remove the npc from tab and not remove the npc itself.

If you have any problems with the code please contact me.

 



 Comments   
Comment by md_5 [ 18/Nov/21 ]

Packets are not API or supported. Clearly removing entities works generally.

Generated at Tue Apr 22 04:54:50 UTC 2025 using Jira 10.3.5#10030005-sha1:190c783f2bd6c69cd5accdb70f97e48812a78d14.