Jump to content

[1.15.2] Change player name in Tab List (Player List)


Babatunde

Recommended Posts

Hi everyone,

 

I want to add a suffix to every players name, but I cant get it to work.

I'm doing it like this:

 

@SubscribeEvent
public void onNameFormat(PlayerEvent.NameFormat event) {
    if (event.getEntity() instanceof PlayerEntity) {
        event.setDisplayname(event.getUsername() + " test");
    }
}

 

If I use

event.getDisplayname()

it shows the name + test, but it doesn't change on the tab list. 

Do I need to somehow update the tab list for it to work or am I doing it completely wrong?

 

Thanks!

Link to comment
Share on other sites

 That doesn't seem to work for me 🤔

 

This is what I got rn:

public class ChangeTabListEvents {

    @SubscribeEvent
    public void onNameFormat(PlayerEvent.NameFormat event) {
        if (event.getEntity() instanceof PlayerEntity) {
            event.setDisplayname(event.getUsername() + " (" + event.getPlayer().getMaxHealth() + ")");
            event.getEntity().sendMessage(new KeybindTextComponent("NameFormat event fired."));
        }
    }

    @SubscribeEvent
    public void onPlayerLogin(EntityJoinWorldEvent event) {
        if (event.getEntity() instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) event.getEntity();
            if (player.getServer() != null) {
                ForgeEventFactory.getPlayerDisplayName(player, player.getGameProfile().getName());
                player.getServer().getPlayerList().sendPacketToAllPlayers(new SPlayerListItemPacket(SPlayerListItemPacket.Action.UPDATE_DISPLAY_NAME));
                player.sendMessage(new KeybindTextComponent("Display name updated."));
            }
        }
    }
}

 

image.png.9e57ad8165bcfa5a3b89acb8f04a8111.png

Link to comment
Share on other sites

So, I've looked at reflection together with a friend of mine, but we could get it to work. 

 

We now have this:
 

    @SubscribeEvent
    public void onPlayerLogin(EntityJoinWorldEvent event) {
        if (event.getEntity() instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) event.getEntity();
            if (player.getServer() != null) {
                ForgeEventFactory.getPlayerDisplayName(player, player.getGameProfile().getName());
                try {
                    Field field = ObfuscationReflectionHelper.findField(SPlayerListItemPacket.class, "field_179769_b");
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                player.getServer().getPlayerList().sendPacketToAllPlayers(new SPlayerListItemPacket(SPlayerListItemPacket.Action.UPDATE_DISPLAY_NAME, (ServerPlayerEntity) player));
                player.sendMessage(new KeybindTextComponent("Display name updated."));
            }
        }
    }

 

field_179769_b = players from:

private final List<SPlayerListItemPacket.AddPlayerData> players = Lists.newArrayList();
  

from the SPlayerListItemPacket class. That is the entries you were talking about right?

 

But we don't know wat to do now.

 

P.S. I'm still using the ForgeEventFactory because getDisplayname() wouldn't fire the even for some reason...

 

Help would be much apriciated, thanks.

Edited by Babatunde
Link to comment
Share on other sites

It looks like you will need to use reflection twice.

After you create the packet object, you will need to access SPlayerListItemPacket#players. SPlayerListItem#getEntries() only exists on the client, so you won't be able to use this.

The elements of the list (of type SPlayerListItemPacket.AddPlayerData) contain a private 'displayName' member, which you will also need access to. (I would advise creating the Field objects in a static context instead of every EntityJoinWorldEvent, to save performance).

Iterate through all the objects in the list and set their display names accordingly - although from your code it looks like there should only be one object in the list.

 

 

Link to comment
Share on other sites

20 hours ago, diesieben07 said:

You need to call refreshDisplayName if you want the event to be fired again when calling getDisplayName again. This is explained in the event's documentation.

refreshDisplayName is from 1.12.2. A lot is the documentation is outdated.

20 hours ago, diesieben07 said:

Probably better to use PlayerLoggedInEvent.

Thanks, I forgot that existed.

 

20 hours ago, diesieben07 said:

You need to get the entries from the packet you create and forcibly set the display name property inside there.

I'm sorry, I've never worked with packets before. is there a tutorial or documentation on how to create packets?

Link to comment
Share on other sites

17 hours ago, coalbricks said:

(I would advise creating the Field objects in a static context instead of every EntityJoinWorldEvent, to save performance)

Thanks for the tip.

 

17 hours ago, coalbricks said:

After you create the packet object

How do I do this tho?

Link to comment
Share on other sites

8 hours ago, Babatunde said:

How do I do this tho?

You've already done it, here:

new SPlayerListItemPacket(SPlayerListItemPacket.Action.UPDATE_DISPLAY_NAME, (ServerPlayerEntity) player)

A packet is just a bit of data that is sent between two or more computers over a network. Usually you don't have to manually send packets because Minecraft takes care of this for you for standard vanilla gameplay, but in this case I think it's the best solution. You can dig around the 'net.minecraft.network' or 'net.minecraft.network.play' packages to look at some example packet implementations; AFAIK there isn't any documentation on them. What IDE are you using?

Edited by coalbricks
Link to comment
Share on other sites

6 hours ago, coalbricks said:

What IDE are you using?

Intellij IDEA Ultimate.

 

6 hours ago, coalbricks said:

You've already done it, here:

Oh nice! completely forgot about that line lmao.

 

This is what I've got so far, I'm kinda stuck because I have no idea what to do:

public class ChangeTabListEvents {

    private final Field fieldPlayers = ObfuscationReflectionHelper.findField(SPlayerListItemPacket.class, "field_179769_b");
    private final Field fieldDisplayName = ObfuscationReflectionHelper.findField(SPlayerListItemPacket.AddPlayerData.class, "field_179965_e");

    @SubscribeEvent
    public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
        PlayerEntity player = event.getPlayer();
        if (player.getServer() != null) {
            try {
                
            } catch (Throwable e) {
                e.printStackTrace();
            }
            player.getServer().getPlayerList().sendPacketToAllPlayers(new SPlayerListItemPacket(SPlayerListItemPacket.Action.UPDATE_DISPLAY_NAME, (ServerPlayerEntity) player));
            player.sendMessage(new KeybindTextComponent("Display name updated."));
        }
    }
}

Do I need to get the list somehow, or do I need to make my own and set my list as the players list? 

And why do I need the 'displayName'?

 

Thanks for the help so far, I appreciate it!

Edited by Babatunde
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.