Posted January 14, 20214 yr 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!
January 14, 20214 yr That grabs the name from the NetworkPlayerInfo which is separate. You would need to set it from the ClientPlayerEntity in a sided-safe manner.
January 14, 20214 yr Author 47 minutes ago, diesieben07 said: by sending a SPlayerListItemPacket with Action.UPDATE_DISPLAY_NAME How would I do this? I've never worked with packets before.
January 14, 20214 yr Author 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.")); } } } }
January 14, 20214 yr Author 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 January 14, 20214 yr by Babatunde
January 15, 20214 yr 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.
January 15, 20214 yr Author 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?
January 15, 20214 yr Author 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?
January 16, 20214 yr 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 January 16, 20214 yr by coalbricks
January 16, 20214 yr Author 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 January 16, 20214 yr by Babatunde
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.