Tablist set displayName

Hi I want to set a display name but I don’t know how.
This is my tablist so far.

@Listener
public void onPlayerJoinEvent(ClientConnectionEvent.Join event) {
	
	Player player = event.getTargetEntity();
	
	TabList tablist = player.getTabList();

	tablist.setHeader(Text.of(config.getNode("tablist", "text", "header").getString()));
	tablist.setFooter(Text.of(config.getNode("tablist", "text", "footer").getString()));

}

I hope you can help me!

How do you mean Display Name? Do you mean set the players display name on the tab list? Or do you mean set the header of the tab list?

The players display name on the tablist - something like ("[Prefix]" + PLAYER_NAME + “[Suffix]”)

TabList list = player.getTabList();
Sponge.getServer().getOnlinePlayers().stream().forEach(p -> {
    TabListEntry entry = list.getTabEntry(p.getUniqueId()).get();
    entry.setDisplayName(p.get(Keys.DISPLAY_NAME). get ());
}

Im doing this from memory so there maybe spelling mistakes.

Also here is more info on tab list entry

That does not work.
I found this in the Docs.

		TabListEntry entry = TabListEntry.builder()
    		    .list(tablist)
    		    .displayName(Text.of("Spongie"))
    		    .profile(gameProfile)
    		    .build();
    		tablist.addEntry(entry);

But I don’t know how to get the gameProfile

player.getProfile()

Okay thanks.

This is my code now:

@Listener
public void onPlayerJoinEvent(ClientConnectionEvent.Join event) {
	Player player = event.getTargetEntity();
	
	TabList tablist = player.getTabList();

	tablist.setHeader(Text.of(config.getNode("tablist", "text", "header").getString()));
	tablist.setFooter(Text.of(config.getNode("tablist", "text", "footer").getString()));
	
	String PLAYER_NAME = (player.getName());
	
	GameProfile gameProfile = player.getProfile();
	
	TabListEntry entry = TabListEntry.builder()
		    .list(tablist)
		    .gameMode(GameModes.SURVIVAL)
		    .displayName(Text.of("[Prefix]" + PLAYER_NAME + "[Suffix]"))
		    .profile(gameProfile)
		    .build();
	tablist.addEntry(entry);
}

But I get this error:

[22:23:11 INFO]: UUID of player Fridtjof_DE is 82ccbc07-d704-40ee-9d53-91309a342584
[22:23:11 INFO]: Fridtjof_DE[/127.0.0.1:56746] logged in with entity id 273 in world(0) at (418.42666319441554, 88.87605972864682, 261.8867820376216)
[22:23:12 ERROR] [Sponge]: Could not pass ClientConnectionEvent$Join$Impl to Plugin{id=simple_tablist, name=Simple TabList, version=1.0.0, description=Adds a Simple TabList, authors=[Fridtjof_DE], source=mods\Simple_Tablist.jar}
java.lang.IllegalArgumentException: cannot add duplicate entry
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) ~[minecraft_server.1.12.2.jar:?]
        at org.spongepowered.common.entity.player.tab.SpongeTabList.addEntry(SpongeTabList.java:155) ~[SpongeTabList.class:1.12.2-7.1.0-BETA-59]
        at org.spongepowered.common.entity.player.tab.SpongeTabList.addEntry(SpongeTabList.java:134) ~[SpongeTabList.class:1.12.2-7.1.0-BETA-59]
        at de.fridtjof_de.simple_tablist.Main.onPlayerJoinEvent(Main.java:114) ~[Main.class:?]
        at org.spongepowered.common.event.listener.JoinListener_Main_onPlayerJoinEvent8.handle(Unknown Source) ~[?:?]
        at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:95) ~[RegisteredListener.class:1.12.2-7.1.0-BETA-59]
        at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:423) [SpongeEventManager.class:1.12.2-7.1.0-BETA-59]
        at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:459) [SpongeEventManager.class:1.12.2-7.1.0-BETA-59]
        at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:446) [SpongeEventManager.class:1.12.2-7.1.0-BETA-59]
        at org.spongepowered.common.SpongeImpl.postEvent(SpongeImpl.java:217) [SpongeImpl.class:1.12.2-7.1.0-BETA-59]
        at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(SourceFile:1447) [pl.class:?]
        at net.minecraft.server.management.PlayerList.func_72355_a(SourceFile:1211) [pl.class:?]
        at net.minecraft.server.network.NetHandlerLoginServer.func_147326_c(SourceFile:119) [pc.class:?]
        at net.minecraft.server.network.NetHandlerLoginServer.func_73660_a(SourceFile:66) [pc.class:?]
        at net.minecraft.network.NetworkManager.func_74428_b(SourceFile:231) [gw.class:?]
        at net.minecraft.network.NetworkSystem.func_151269_c(SourceFile:187) [oz.class:?]
        at net.minecraft.server.MinecraftServer.func_71190_q(SourceFile:1657) [MinecraftServer.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(SourceFile:349) [nz.class:?]
        at net.minecraft.server.MinecraftServer.func_71217_p(SourceFile:560) [MinecraftServer.class:?]
        at net.minecraft.server.MinecraftServer.run(SourceFile:464) [MinecraftServer.class:?]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
[22:23:12 INFO]: Fridtjof_DE joined the game

So what its saying there is there is already a entry in the tab list like that.

This is probably to do with the fact you are using the players normal name as the display name. Try using the actual players display name or another display name

When i wake up tomorrow ill build a testing version and then hand you the code when its done

1 Like

This is the code that is working on my end.

Player player = event.getTargetEntity();
TabLis tabList = player.getTabList();
Sponge.getServer().getOnlinePlayers().stream().forEach(p -> {
    Optional<TabListEntry> opEntry = tabList.getEntry(p.getUniqueId());
    if(!opEntry.isPresent()){
        return;
    }
    opEntry.get().setDisplayName(Text.of("Test"));
});

Please note that I tried getting the display name from the player, however setting it with the /nick command from Nuclus did not work however setting it with my own custom plugin worked.
Please also note that I used TargetPlayerEvent for testing, simply because then I could effectively automatically update. If this code does not work on ClientConnectionEvent.Join then put a delay using the Scheduler of a single tick.

1 Like

Thank you!!!