NPCs created via packet not getting removed using ClientboundRemoveEntitiesPacket

    • Type: Bug
    • Resolution: Invalid
    • Priority: Minor
    • None
    • Affects Version/s: None
    • Environment:

      Windows 10 64bit

      jdk-16.0.1

      IDE configured to use Java 16

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

      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.

       

            Assignee:
            Unassigned
            Reporter:
            maximus widmiaer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: