Solved (Packets) How to disable NPC NameTag

Discussion in 'Plugin Development' started by Legendary_zotar, Jun 27, 2020.

  1. Offline

    Legendary_zotar

    Im trying to make an NPC using packets, Everything is going good but i cant seem to remove the NameTag, Ive tried adding it to a scoreboard, and adding a passenger ontop of it to remove the name tag, but either they dont work, or im doing it wrong

    (Please dont tell me to use Citizens API)

    Heres the code to create the npc:
    CreateNPC Method (open)
    Code:
    public static boolean spawnNPC(Location loc, String npcName, String npcSkin){
            String[] skinData = getSkin(npcSkin);
            if(skinData == null)
                return false;
    
            MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer nmsWorld = ((CraftWorld) loc.getWorld()).getHandle();
    
    
            GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Utils.Format(npcName));
    
            EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, new PlayerInteractManager(nmsWorld));
    
    
            gameProfile.getProperties().put("textures", new Property("textures", skinData[0], skinData[1]));
    
            npc.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            NPCs.put(NPCs.size(), npc);
            addNPCPacket(npc);
            return true;
        }


    and heres the code to send the packets
    SendPackets Method (open)
    Code:
    private static void addNPCPacket(EntityPlayer npc){
            for(Player p : Bukkit.getOnlinePlayers()) {
                DataWatcher watcher = npc.getDataWatcher();
                watcher.set(DataWatcherRegistry.a.a(16), (byte) -1);
                PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
                connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
                connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
                connection.sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), watcher, true));
                connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc));
                    }
                }.runTaskLater(WolfCraft.getInstance(), 10L);
            }
        }


    =================================================================
    Well i spent a few days trying to solve this, and 20 mins after posting this i found the solution, So i'll post for anyone in the future looking for the solution:

    After sending the packet to spawn your entity (PacketPlayOutNamedEntitySpawn)

    You remove the team, create a team, and add players to the team using PacketPlayOutScoreboardTeam(#ScoreboardTeam, Integer) (1 Remove, 0 Create, 3 Add List of players/npc)

    Show Spoiler
    Code:
    //Creating the team
    ScoreboardTeam team = new ScoreboardTeam(((CraftScoreboard) Bukkit.getScoreboardManager().getMainScoreboard()).getHandle(), npc.getName());
    
    //Setting name visibility
    team.setNameTagVisibility(ScoreboardTeamBase.EnumNameTagVisibility.NEVER);
    
    //Remove the Team (i assume so if it exists)
    connection.sendPacket(new PacketPlayOutScoreboardTeam(team, 1));
    //Creating the Team
    connection.sendPacket(new PacketPlayOutScoreboardTeam(team, 0));
    //Adding players to the team (You have to use the NPC's name, and add it to a list)
    connection.sendPacket(new PacketPlayOutScoreboardTeam(team, new ArrayList<String>(){{add(npc.getName());}}, 3));


    Source
     
    Last edited: Jun 28, 2020

Share This Page