ProtocolLib

3M Downloads

Need help on fake NPC

poyrazinan opened this issue ยท 0 comments

commented

Question
I want to create fake NPC so i've tried in 1.8 and succeeded. After trying on 1.9+ (Current 1.16) it's not working.
I've tried tons of methods and i can't understand how it's work. Can you just help me =) I'm trying to learn this thing.

API method(s) used
List what API method(s) you're using
PacketContainer

PacketType.Play.Server.SPAWN_ENTITY_LIVING
PacketType.Play.Server.ENTITY_HEAD_ROTATION
PacketType.Play.Server.PLAYER_INFO

WrappedDataWatcher
WrappedDataWatcherObject

Expected behavior
I want to make npc spawned. When i remove WrappedDataWatcher it's not show but no error in console but there is no any npc :(

Code
It's last code i've tried. And print that error

FieldAccessException: No field with type net.minecraft.server.v1_16_R3.DataWatcher exists in class PacketPlayOutSpawnEntityLiving.

        PacketContainer spawn = npc.manager.createPacket(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
        
        spawn.getIntegers().write(0, npc.entityId);
        spawn.getUUIDs().write(0, npc.uuid);
        npc.setLocation(npc.location);
        
        String nms = Manager.getNMSVersion();
        
        if (nms.contains("1_8"))
        {
        	
        	spawn.getIntegers().write(1, CoordinateUtil.getFixedNumber(npc.location.getX()));
            spawn.getIntegers().write(2, CoordinateUtil.getFixedNumber(npc.location.getY()));
            spawn.getIntegers().write(3, CoordinateUtil.getFixedNumber(npc.location.getZ()));
        	
        }
        
        else
        {
        	
        	spawn.getDoubles().write(0, npc.getLocation().getX());
        	
        	spawn.getDoubles().write(1, npc.getLocation().getY());
        	
        	spawn.getDoubles().write(2, npc.getLocation().getZ());
        	
        }
        spawn.getBytes().write(0, CoordinateUtil.getByteAngle(npc.location.getYaw()));
        spawn.getBytes().write(1, CoordinateUtil.getByteAngle(npc.location.getPitch()));
        
        WrappedDataWatcher data = new WrappedDataWatcher();
        
        data.setObject(new WrappedDataWatcherObject(0, WrappedDataWatcher.Registry.get(Byte.class)), Byte.valueOf((byte)npc.entityId));
        
        spawn.getDataWatcherModifier().write(0, data);
        
        

        PacketContainer rotation = npc.manager.createPacket(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
        rotation.getIntegers().write(0, npc.entityId);
        rotation.getBytes().write(0, CoordinateUtil.getByteAngle(npc.location.getYaw()));	

        PacketContainer remove = npc.manager.createPacket(PacketType.Play.Server.PLAYER_INFO);
        remove.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.REMOVE_PLAYER);
        remove.getPlayerInfoDataLists().write(0, Collections.singletonList(npc.infoData));
        
        try {
            for(Player p : players) {
                npc.manager.sendServerPacket(p, spawn);
                npc.manager.sendServerPacket(p, rotation);
                npc.inventory.update();
                
            }
        } catch(InvocationTargetException e) {
            npc.plugin.getLogger().log(Level.WARNING, "Could not send npc packets", e);
            return;
        }
        new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    for(Player p : players) {
                        if(npc.rangePlayers.contains(p)) {
                            npc.manager.sendServerPacket(p, remove);
                        }
                    }
                } catch(InvocationTargetException e) {
                    npc.plugin.getLogger().log(Level.WARNING, "Could not send remove packet", e);
                }
            }
        }.runTaskLater(npc.plugin, 10);

Additional context
1.8 code which is works fine:

PacketContainer spawn = npc.manager.createPacket(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
        spawn.getIntegers().write(0, npc.entityId);
        spawn.getUUIDs().write(0, npc.uuid);
        npc.setLocation(npc.location);
        spawn.getIntegers().write(1, CoordinateUtil.getFixedNumber(npc.location.getX()));
        spawn.getIntegers().write(2, CoordinateUtil.getFixedNumber(npc.location.getY()));
        spawn.getIntegers().write(3, CoordinateUtil.getFixedNumber(npc.location.getZ()));
        spawn.getBytes().write(0, CoordinateUtil.getByteAngle(npc.location.getYaw()));
        spawn.getBytes().write(1, CoordinateUtil.getByteAngle(npc.location.getPitch()));
        spawn.getDataWatcherModifier().write(0, createWatcher(npc));

        PacketContainer rotation = npc.manager.createPacket(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
        rotation.getIntegers().write(0, npc.entityId);
        rotation.getBytes().write(0, CoordinateUtil.getByteAngle(npc.location.getYaw()));	

        PacketContainer remove = npc.manager.createPacket(PacketType.Play.Server.PLAYER_INFO);
        remove.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.REMOVE_PLAYER);
        remove.getPlayerInfoDataLists().write(0, Collections.singletonList(npc.infoData));
        
        try {
            for(Player p : players) {
                npc.manager.sendServerPacket(p, spawn);
                npc.manager.sendServerPacket(p, rotation);
                npc.inventory.update();
                
            }
        } catch(InvocationTargetException e) {
            npc.plugin.getLogger().log(Level.WARNING, "Could not send npc packets", e);
            return;
        }
        new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    for(Player p : players) {
                        if(npc.rangePlayers.contains(p)) {
                            npc.manager.sendServerPacket(p, remove);
                        }
                    }
                } catch(InvocationTargetException e) {
                    npc.plugin.getLogger().log(Level.WARNING, "Could not send remove packet", e);
                }
            }
        }.runTaskLater(npc.plugin, 10);

Watcher method

private static WrappedDataWatcher createWatcher(NPCImpl npc) {
       WrappedDataWatcher watcher = new WrappedDataWatcher();

        watcher.setObject(0, (byte) npc.state.getId());
        if(npc.particleEffectColor != -1) {
            watcher.setObject(7, npc.particleEffectColor);
        }
        watcher.setObject(10, npc.skinLayers.getFlags()); // Skin flags

        return watcher;
}