From 780f52e24ccf38a0ac352e8dab2b177a502c6c63 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 7 Dec 2024 10:50:52 +0100 Subject: [PATCH 1/2] fix display names not syncing when player logs in Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com> --- .../java/serverutils/command/CmdNick.java | 4 +- .../java/serverutils/command/CmdNickFor.java | 4 +- .../ServerUtilitiesPlayerEventHandler.java | 5 +- .../ServerUtilitiesServerEventHandler.java | 3 +- .../serverutils/net/MessageRankModify.java | 6 +- .../serverutils/net/MessageUpdateTabName.java | 93 ++++++++++++------- 6 files changed, 79 insertions(+), 36 deletions(-) diff --git a/src/main/java/serverutils/command/CmdNick.java b/src/main/java/serverutils/command/CmdNick.java index ad3656e2..8096bb4e 100644 --- a/src/main/java/serverutils/command/CmdNick.java +++ b/src/main/java/serverutils/command/CmdNick.java @@ -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; @@ -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(); } } } diff --git a/src/main/java/serverutils/command/CmdNickFor.java b/src/main/java/serverutils/command/CmdNickFor.java index 8a3e5905..2265074f 100644 --- a/src/main/java/serverutils/command/CmdNickFor.java +++ b/src/main/java/serverutils/command/CmdNickFor.java @@ -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; @@ -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(); } } } diff --git a/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java b/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java index b7e85d0a..af537c28 100644 --- a/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java +++ b/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java @@ -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; @@ -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().getPlayers()).sendTo(player); } BackupTask.hadPlayer = true; diff --git a/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java b/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java index 2d945a76..cd655678 100644 --- a/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java +++ b/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java @@ -1,5 +1,6 @@ package serverutils.handlers; +import java.util.Collections; import java.util.Map; import java.util.regex.Pattern; @@ -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) { diff --git a/src/main/java/serverutils/net/MessageRankModify.java b/src/main/java/serverutils/net/MessageRankModify.java index 98cffc46..65c69a12 100644 --- a/src/main/java/serverutils/net/MessageRankModify.java +++ b/src/main/java/serverutils/net/MessageRankModify.java @@ -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; @@ -86,12 +88,14 @@ public void onMessage(EntityPlayerMP player) { } if (ServerUtilitiesConfig.chat.replace_tab_names && updateNames) { + List 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(); } } } diff --git a/src/main/java/serverutils/net/MessageUpdateTabName.java b/src/main/java/serverutils/net/MessageUpdateTabName.java index 0b61117f..16ef4d1b 100644 --- a/src/main/java/serverutils/net/MessageUpdateTabName.java +++ b/src/main/java/serverutils/net/MessageUpdateTabName.java @@ -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; @@ -25,21 +29,24 @@ public class MessageUpdateTabName extends MessageToClient { - private String name; - private IChatComponent displayComponent; - private boolean afk; + private Collection 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 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)); } } @@ -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 @@ -71,23 +74,51 @@ public void onMessage() { // noinspection unchecked Map infoMap = (Map) handler.playerInfoMap; List 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 SERIALIZER = (data, entry) -> { + data.writeString(entry.name); + data.writeTextComponent(entry.displayComponent); + data.writeBoolean(entry.afk); + }; + + public static final DataIn.Deserializer DESERIALIZER = data -> new TabNameEntry( + data.readString(), + data.readTextComponent(), + data.readBoolean()); } } From e901c205d983909adfefc6479d23e0e903f091a3 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 7 Dec 2024 10:55:24 +0100 Subject: [PATCH 2/2] wrong one Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com> --- .../serverutils/handlers/ServerUtilitiesPlayerEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java b/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java index af537c28..d2b0e62c 100644 --- a/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java +++ b/src/main/java/serverutils/handlers/ServerUtilitiesPlayerEventHandler.java @@ -94,7 +94,7 @@ public void onPlayerLoggedIn(ForgePlayerLoggedInEvent event) { if (ServerUtilitiesConfig.chat.replace_tab_names) { new MessageUpdateTabName(Collections.singleton(event.getPlayer())).sendToAll(); - new MessageUpdateTabName(Universe.get().getPlayers()).sendTo(player); + new MessageUpdateTabName(Universe.get().getOnlinePlayers()).sendTo(player); } BackupTask.hadPlayer = true;