Solved Hiding Nametags

Discussion in 'Plugin Development' started by Grauly, Apr 23, 2019.

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

    Grauly

    I have been looking for a way to hide players nametags (the one over the head) but found no post that was in anyway up-to-date (latest one I found was from 2016). So what I want to know is, if it is possible to do that in current versions and if possible how.

    Thanks in advance!
     
  2. Offline

    KarimAKL

    @Grauly From a quick google search, it seems you can set an armor stand as a passenger for the player to hide the name tag.
    You just have to make the armorstand invisible and then hide it's name tag if not already hidden.
     
  3. Offline

    Grauly

    @KarimAKL Thanks!
    Thats the first step... but as far as i tested it , it hides it for all Players. I am looking for a possiblity to hide the name tag of Player A from Player B but not from Player C (So basicly hiding the nametag individualy), without using scoreboard-nonsene.
    But again. BIG Thanks for that first step... maybe ill find a good use for that.
     
    KarimAKL likes this.
  4. Offline

    KarimAKL

    @Grauly I haven't used NMS much but maybe if you could make it so the armorstand is only seen by 1 (or more) player(s) and then attach (set passenger) it to the player?
    EDIT: Now that i think about it, you could probably just hide the name tag with NMS without the armorstand.
     
  5. Offline

    Shqep

    You can also put players in separate scoreboard teams.
    There's, uh, a method to hide name tags for 1.8 and above.
    Code:Java
    1. void setNameTagVisibility(final NameTagVisibility p0) throws IllegalArgumentException;

    Maybe you can play around with it.
     
  6. Offline

    Grauly

    @KarimAKL I tested around with the armorstand and found a fatal flaw to it. If everything is set up as you sad, a player cant interact with anything (Hitbox of Armorstand blocking Everything) so it is a bit impractical for that use.
    Regarding NMS: what is that exactly?

    EDIT: I took a look at what it is, sounds promising, yet i have no idea how to archieve what i need.

    @Shqep Thanks for your Reply! Scoreboard teams makes stuff realy complicated (and as far as i know it is a big compatiblity issue). Scoreboards will be the last resort, if everything else fails.
     
    Last edited: Apr 24, 2019
  7. Offline

    KarimAKL

    @Grauly You can set the ArmorStand as a marker and then you won't have that problem. (Use the setMarker(boolean) method)
     
  8. Offline

    Grauly

    @KarimAKL Well that works.
    Before you mentioned NMS as solution too... do you have any sources i could look into to figure out what to do?
    The Armorstand hides for everyone, i am looking for the possibility described above - Still Thanks!
     
  9. Offline

    KarimAKL

  10. Offline

    Grauly

    @KarimAKL I checked the List of the Packets linked in the Page you linked but couldnt find any Packet relevant for nametags (except in scoreboard teams). What i found was that ProtocolLib seems to come up relativly often around this topic (nametag stuff) , but it seems like just another way of handeling/modifying packets (but i couldnt find the packet responsible for nametags).
     
  11. Offline

    KarimAKL

    @Grauly To make an armorstand that is only viewable by a specific player you can do something like this:
    Code:Java
    1. // Create NMS ArmorStand
    2. EntityArmorStand stand = new EntityArmorStand(((CraftWorld) location.getWorld()).getHandle(), location.getX(), location.getY(), location.getZ());
    3. // Set invisible
    4. stand.setInvisible(true);
    5. // Hide name tag
    6. stand.setCustomNameVisible(false);
    7. // Create spawn 'stand' packet
    8. PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
    9. // Send packet to player
    10. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);

    Of course you can change the code to fit your need.
     
  12. Offline

    Grauly

    So i did some digging with other searches and came across this post:
    https://www.spigotmc.org/threads/protocollib-and-nametag.176934/
    wich basicly is intercepting / changing the name with packets.
    I tried it, but it gave a few complications. (The Problem is i can set the Namestring to "" but it still shows the black box. Also it was not ressettable due to the Player having no name i could set it back to)
    Ill now try your code @KarimAKL

    EDIT: I tried it. i can create the armorstands for just one player but found no way to put it as a passenger of the other player. .addPassenger(Entity passenger) cant take an EntityArmorStand, and so far i found no other way

    EDIT2: I took another sweep around and came by the idea of "simulating" a scoreboard using Packets by sending Player B packets to tell him that Player A is in a team with tag Visiblity set to invisble (without creating scoreboards for the Server). Problem is, that i have no idea how to do that.
     
    Last edited: Apr 25, 2019
  13. Offline

    KarimAKL

    @Grauly Try doing something like this:
    Code:Java
    1. Player player; // Your player
    2. EntityPlayer p = ((CraftPlayer) player).getHandle(); // Your NMS player
    3. p.setPassenger(stand); // Set NMS armorstand as passenger for NMS player


    EDIT: For your other problem about not getting the player's old name you could save the name before changing it.
     
  14. Offline

    Grauly

    @KarimAKL It would in Theory work but i coulndt remove it if i try to "unhide" the Player again because there are no realy "registerd" Armorstands i could remove later (as far as i could find it).

    I tried using Packets and Scoreboard and ended up with this for hiding:
    Code:
            ScoreboardTeam team = new ScoreboardTeam(new Scoreboard(), "name");
            team.setNameTagVisibility(EnumNameTagVisibility.NEVER);
            ArrayList<String> coll = new ArrayList<String>();
            coll.add(player.getName());
            PacketPlayOutScoreboardTeam packet = new PacketPlayOutScoreboardTeam(team,coll,3);
            p2.getHandle().playerConnection.sendPacket(packet);
    
    p2 ist the Player that is hidden from - thing is it dose not work (no errors).
     
    Last edited: Apr 25, 2019
  15. Offline

    KarimAKL

    @Grauly For that you could save the entity like this:
    Code:Java
    1. // Create map for saving of armorstands
    2. Map<UUID, EntityArmorStand> stands = new HashMap<>();
    3.  
    4. // Create armorstand
    5. Player player; // Your player
    6. EntityArmorStand stand; // Your armorstand
    7. PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
    8. this.stands.put(player.getUniqueId(), stand);
    9. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    10.  
    11. // Remove armorstand
    12. Player player; // Your player
    13. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(this.stands.get(player.getUnique()).getId());
    14. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);


    I haven't worked with scoreboards, so i can't help you with those.
     
  16. Offline

    Grauly

    I got it working using Armorstands and the Map thing (I am unsure why i didnt think about that in the first Place, but i think my brain was fixated on that Armorstand not existing Serverside and therefor not existing at all).
    This "mystery" is now solved - and thats thanks to you @KarimAKL - that saved my project. Thank you.

    Code as Follows:
    Code:
    public Map<ArrayList<UUID>,EntityArmorStand> hiddenmap = new HashMap<ArrayList<UUID>,EntityArmorStand>();
        //First Player in the List is the hidden one
    
        @Override
        public void hide(Player player, Player hideFrom) {
            if(player == hideFrom)
                return;
            //Add Players to the List
            ArrayList<UUID> list = new ArrayList<UUID>();
            list.add(player.getUniqueId());
            list.add(hideFrom.getUniqueId());
            if(hiddenmap.containsKey(list)) {
                return;
            }
            //spawning the Armorstand & setting it up
            Location pos = player.getLocation();
            EntityArmorStand stand = new EntityArmorStand(((CraftWorld)pos.getWorld()).getHandle(),pos.getX(),pos.getY(),pos.getZ());
            stand.setInvisible(true);
            stand.setInvulnerable(true);
            stand.setMarker(true);
            stand.setCustomNameVisible(false);
            CraftPlayer p = (CraftPlayer) player;
            p.getHandle().passengers.add(stand);
            //Packet packing
            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(stand);
            //"Registering" the Stand and the Players
            hiddenmap.put(list, stand);
            //Packet sending
            CraftPlayer ply = (CraftPlayer) hideFrom;
            ply.getHandle().playerConnection.sendPacket(packet);
         
        }
    
        @Override
        public void unhide(Player player, Player hiddenFrom) {
            if(player == hiddenFrom)
                return;
            ArrayList<UUID> list = new ArrayList<UUID>();
            list.add(player.getUniqueId());
            list.add(hiddenFrom.getUniqueId());
            if(!(hiddenmap.containsKey(list))) {
                return;
            } 
            EntityArmorStand stand = hiddenmap.get(list);
            PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(stand.getId());
            CraftPlayer ply = (CraftPlayer) hiddenFrom;
            ply.getHandle().playerConnection.sendPacket(packet);
            hiddenmap.remove(list);
        }
    
    if this is considerd "spoonfeeding" ill be ok with removing it. I am just trying to spare any person after me starting all of this again. Also - i know that it is probably VERY unefficient. Please spare me.
     
    Last edited: Apr 26, 2019
    KarimAKL likes this.
  17. Offline

    KarimAKL

    @Grauly Glad to know you got it working. :)
     
Thread Status:
Not open for further replies.

Share This Page