NPC nms error 1.12.2

Discussion in 'Plugin Development' started by MrGG4ming, May 5, 2018.

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

    MrGG4ming

    Hello everyone, I was creating a NPCS plugin here, but in 1.8 and I decided to upgrade the jar version to 1.12 with this I realized that when trying to create an NPC nothing happens, simple message appears as if it had been spawned, however I do not see you.

    Code:
    public void spawn() {
            PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
    
            setValue(packet, "a", entityID);
            setValue(packet, "b", gameprofile.getId());
            setValue(packet, "c", getFixLocation(location.getX()));
            setValue(packet, "d", getFixLocation(location.getY()));
            setValue(packet, "e", getFixLocation(location.getZ()));
            setValue(packet, "f", getFixRotation(location.getYaw()));
            setValue(packet, "g", getFixRotation(location.getPitch()));
            DataWatcher w = new DataWatcher(null);
            w.register(new DataWatcherObject<>(13,DataWatcherRegistry.a),(byte)127);
            setValue(packet, "h", w);
            addToTablist();
            sendPacket(packet);
            headRotation(entityID, location.getYaw(), location.getPitch());
    
            Bukkit.getScheduler().runTaskLater(Main.plugin, new Runnable() {
                @Override
                public void run() {
                    rmvFromTablist();
                }
            }, 5L);
        }
    
        public static void teleport(int entityID, Location location) {
            PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
            setValue(packet, "a", entityID);
            setValue(packet, "b", getFixLocation(location.getX()));
            setValue(packet, "c", getFixLocation(location.getY()));
            setValue(packet, "d", getFixLocation(location.getZ()));
            setValue(packet, "e", getFixRotation(location.getYaw()));
            setValue(packet, "f", getFixRotation(location.getPitch()));
    
            sendPacket(packet);
            headRotation(entityID, location.getYaw(), location.getPitch());
        }
    
    
        public static void headRotation(int entityID, float yaw, float pitch) {
            PacketPlayOutEntityLook packet = new PacketPlayOutEntityLook(entityID, getFixRotation(yaw), getFixRotation(pitch), true);
            PacketPlayOutEntityHeadRotation packetHead = new PacketPlayOutEntityHeadRotation();
            setValue(packetHead, "a", entityID);
            setValue(packetHead, "b", getFixRotation(yaw));
    
            sendPacket(packet);
            sendPacket(packetHead);
        }
    
        public static void destroy(int entityID) {
            PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{entityID});
            rmvFromTablist();
            sendPacket(packet);
        }
    
        public static void addToTablist() {
            PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
            PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(gameprofile, 1, EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameprofile.getName())[0]);
            @SuppressWarnings("unchecked")
            List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) NPC.getValue(packet, "b");
            players.add(data);
    
            setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
            setValue(packet, "b", players);
    
            sendPacket(packet);
        }
    
        public static void rmvFromTablist() {
            PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
            PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(gameprofile, 1, EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameprofile.getName())[0]);
            @SuppressWarnings("unchecked")
            List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) NPC.getValue(packet, "b");
            players.add(data);
    
            setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
            setValue(packet, "b", players);
            sendPacket(packet);
        }
    It is worth noting that only difference in the code was the change of "DataWatcher" that changed between 1.8 to 1.12
     
    Last edited: May 5, 2018
  2. I see what you are trying to do, you are lucky because I saw the same video and I updated the code + extra features.

    I created an Util to create NPC in Bukkit-1.12. here
     
    Last edited: May 9, 2018
Thread Status:
Not open for further replies.

Share This Page