Skip to content

Commit

Permalink
Fix display names not syncing when player logs in (#148)
Browse files Browse the repository at this point in the history
Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>
Co-authored-by: boubou19 <miisterunknown@gmail.com>
  • Loading branch information
Lyfts and boubou19 authored Dec 7, 2024
1 parent 14e2268 commit ff219c8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/main/java/serverutils/command/CmdNick.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package serverutils.command;

import java.util.Collections;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.EnumChatFormatting;
Expand Down Expand Up @@ -48,7 +50,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
}

if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(player).sendToAll();
new MessageUpdateTabName(Collections.singleton(player)).sendToAll();
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/serverutils/command/CmdNickFor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package serverutils.command;

import java.util.Collections;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.EnumChatFormatting;
Expand Down Expand Up @@ -40,7 +42,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
}

if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(player).sendToAll();
new MessageUpdateTabName(Collections.singleton(player)).sendToAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package serverutils.handlers;

import java.util.Collections;

import net.minecraft.block.Block;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -91,7 +93,8 @@ public void onPlayerLoggedIn(ForgePlayerLoggedInEvent event) {
}

if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(event.getPlayer()).sendToAll();
new MessageUpdateTabName(Collections.singleton(event.getPlayer())).sendToAll();
new MessageUpdateTabName(Universe.get().getOnlinePlayers()).sendTo(player);
}

BackupTask.hadPlayer = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package serverutils.handlers;

import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -190,7 +191,7 @@ public void onServerTick(TickEvent.ServerTickEvent event) {

if (prevIsAfk != isAFK) {
if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(forgePlayer).sendToAll();
new MessageUpdateTabName(Collections.singleton(forgePlayer)).sendToAll();
}

for (EntityPlayerMP player1 : universe.server.getConfigurationManager().playerEntityList) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/serverutils/net/MessageRankModify.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static serverutils.ServerUtilitiesPermissions.RANK_EDIT;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChatComponentText;
Expand Down Expand Up @@ -86,12 +88,14 @@ public void onMessage(EntityPlayerMP player) {
}

if (ServerUtilitiesConfig.chat.replace_tab_names && updateNames) {
List<ForgePlayer> toUpdate = new ArrayList<>();
for (PlayerRank playerRank : Ranks.INSTANCE.playerRanks.values()) {
if (!playerRank.getParents().contains(rank)) continue;
ForgePlayer fp = Universe.get().getPlayer(playerRank.profile);
if (fp == null || !fp.isOnline()) continue;
new MessageUpdateTabName(fp).sendToAll();
toUpdate.add(fp);
}
new MessageUpdateTabName(toUpdate).sendToAll();
}
}
}
93 changes: 62 additions & 31 deletions src/main/java/serverutils/net/MessageUpdateTabName.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package serverutils.net;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiPlayerInfo;
import net.minecraft.client.network.NetHandlerPlayClient;
Expand All @@ -25,21 +29,24 @@

public class MessageUpdateTabName extends MessageToClient {

private String name;
private IChatComponent displayComponent;
private boolean afk;
private Collection<TabNameEntry> entries = new ArrayList<>();

public MessageUpdateTabName() {}

public MessageUpdateTabName(ForgePlayer player) {
EntityPlayerMP playerMP = player.getPlayer();
name = playerMP.getCommandSenderName();
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(player);
afk = data.afkTime >= ServerUtilitiesConfig.afk.getNotificationTimer();
if (Ranks.isActive()) {
displayComponent = data.getNameForChat(playerMP);
} else {
displayComponent = new ChatComponentText(playerMP.getDisplayName());
public MessageUpdateTabName(Collection<ForgePlayer> players) {
for (ForgePlayer player : players) {
EntityPlayerMP playerMP = player.getPlayer();
String name = playerMP.getCommandSenderName();
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(player);
boolean afk = data.afkTime >= ServerUtilitiesConfig.afk.getNotificationTimer();
IChatComponent displayComponent;
if (Ranks.isActive()) {
displayComponent = data.getNameForChat(playerMP);
} else {
displayComponent = new ChatComponentText(playerMP.getDisplayName());
}

entries.add(new TabNameEntry(name, displayComponent, afk));
}
}

Expand All @@ -50,16 +57,12 @@ public NetworkWrapper getWrapper() {

@Override
public void writeData(DataOut data) {
data.writeString(name);
data.writeTextComponent(displayComponent);
data.writeBoolean(afk);
data.writeCollection(entries, TabNameEntry.SERIALIZER);
}

@Override
public void readData(DataIn data) {
name = data.readString();
displayComponent = data.readTextComponent();
afk = data.readBoolean();
entries = data.readCollection(TabNameEntry.DESERIALIZER);
}

@Override
Expand All @@ -71,23 +74,51 @@ public void onMessage() {
// noinspection unchecked
Map<String, GuiPlayerInfo> infoMap = (Map<String, GuiPlayerInfo>) handler.playerInfoMap;
List<GuiPlayerInfo> infoList = handler.playerInfoList;
GuiPlayerInfo info = infoMap.get(name);
if (info == null) return;

String displayName = displayComponent.getFormattedText().replaceAll("[<>]", "");
for (TabNameEntry entry : entries) {
String name = entry.name;
GuiPlayerInfo info = infoMap.get(name);
if (info == null || entry.displayComponent == null) return;

String displayName = entry.displayComponent.getFormattedText().replaceAll("[<>]", "");

if (afk) {
displayName = EnumChatFormatting.GRAY + "[AFK] " + EnumChatFormatting.RESET + displayName;
if (entry.afk) {
displayName = EnumChatFormatting.GRAY + "[AFK] " + EnumChatFormatting.RESET + displayName;
}

if (info instanceof GuiPlayerInfoWrapper wrapper) {
wrapper.displayName = displayName;
} else {
infoMap.remove(name);
infoList.remove(info);
GuiPlayerInfoWrapper newInfo = new GuiPlayerInfoWrapper(info, displayName);
infoMap.put(name, newInfo);
infoList.add(newInfo);
}
}
}

if (info instanceof GuiPlayerInfoWrapper wrapper) {
wrapper.displayName = displayName;
} else {
infoMap.remove(name);
infoList.remove(info);
GuiPlayerInfoWrapper newInfo = new GuiPlayerInfoWrapper(info, displayName);
infoMap.put(name, newInfo);
infoList.add(newInfo);
private static class TabNameEntry {

private final String name;
private final IChatComponent displayComponent;
private final boolean afk;

public TabNameEntry(String name, @Nullable IChatComponent displayComponent, boolean afk) {
this.name = name;
this.displayComponent = displayComponent;
this.afk = afk;
}

public static final DataOut.Serializer<TabNameEntry> SERIALIZER = (data, entry) -> {
data.writeString(entry.name);
data.writeTextComponent(entry.displayComponent);
data.writeBoolean(entry.afk);
};

public static final DataIn.Deserializer<TabNameEntry> DESERIALIZER = data -> new TabNameEntry(
data.readString(),
data.readTextComponent(),
data.readBoolean());
}
}

0 comments on commit ff219c8

Please sign in to comment.