Skip to content

Commit

Permalink
Fix display name not syncing again (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>
  • Loading branch information
Lyfts authored Dec 10, 2024
1 parent 82e3b28 commit 8c6e21b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 73 deletions.
8 changes: 0 additions & 8 deletions src/main/java/serverutils/command/CmdNick.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package serverutils.command;

import java.util.Collections;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.EnumChatFormatting;

import serverutils.ServerUtilities;
import serverutils.ServerUtilitiesConfig;
import serverutils.ServerUtilitiesPermissions;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.data.ForgePlayer;
import serverutils.lib.util.StringUtils;
import serverutils.net.MessageUpdateTabName;

public class CmdNick extends CmdBase {

Expand Down Expand Up @@ -48,9 +44,5 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
player.getPlayer().addChatMessage(
ServerUtilities.lang(player.getPlayer(), "serverutilities.lang.nickname_changed", name));
}

if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(Collections.singleton(player)).sendToAll();
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/serverutils/command/CmdNickFor.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package serverutils.command;

import java.util.Collections;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.EnumChatFormatting;

import serverutils.ServerUtilities;
import serverutils.ServerUtilitiesConfig;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.data.ForgePlayer;
import serverutils.lib.util.StringUtils;
import serverutils.net.MessageUpdateTabName;

public class CmdNickFor extends CmdBase {

Expand All @@ -40,9 +36,5 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE

sender.addChatMessage(ServerUtilities.lang(sender, "serverutilities.lang.nickname_changed", name));
}

if (ServerUtilitiesConfig.chat.replace_tab_names) {
new MessageUpdateTabName(Collections.singleton(player)).sendToAll();
}
}
}
48 changes: 33 additions & 15 deletions src/main/java/serverutils/data/ServerUtilitiesPlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import serverutils.lib.util.StringUtils;
import serverutils.lib.util.text_components.Notification;
import serverutils.lib.util.text_components.TextComponentParser;
import serverutils.net.MessageUpdateTabName;
import serverutils.ranks.Ranks;
import serverutils.task.Task;
import serverutils.task.TeleportTask;

Expand Down Expand Up @@ -215,13 +217,20 @@ public void clearCache() {
cachedNameForChat = null;
if (player.isFake()) return;
EntityPlayerMP p = player.getNullablePlayer();
if (p == null) return;

if (p != null) {
p.refreshDisplayName();
p.refreshDisplayName();
if (ServerUtilitiesConfig.chat.replace_tab_names) {
getNameForChat();
}
}

public IChatComponent getNameForChat(EntityPlayerMP playerMP) {
public IChatComponent getNameForChat() {
EntityPlayerMP playerMP = player.getNullablePlayer();
if (playerMP == null) {
return new ChatComponentText("");
}

if (ServerUtils.isFake(playerMP)) {
return new ChatComponentText(player.getName());
}
Expand All @@ -230,18 +239,22 @@ public IChatComponent getNameForChat(EntityPlayerMP playerMP) {
return cachedNameForChat.createCopy();
}

String text = player.getRankConfig(ServerUtilitiesPermissions.CHAT_NAME_FORMAT).getString();

try {
cachedNameForChat = TextComponentParser
.parse(text, ServerUtilitiesCommon.chatFormattingSubstituteFunction(player));
} catch (Exception ex) {
String s = "Error parsing " + text + ": " + ex.getLocalizedMessage();
ServerUtilities.LOGGER.error(s);
cachedNameForChat = new ChatComponentText("BrokenFormatting");
cachedNameForChat.getChatStyle().setColor(EnumChatFormatting.RED);
cachedNameForChat.getChatStyle()
.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(s)));
if (Ranks.isActive() && ServerUtilitiesConfig.ranks.override_chat) {
String text = player.getRankConfig(ServerUtilitiesPermissions.CHAT_NAME_FORMAT).getString();

try {
cachedNameForChat = TextComponentParser
.parse(text, ServerUtilitiesCommon.chatFormattingSubstituteFunction(player));
} catch (Exception ex) {
String s = "Error parsing " + text + ": " + ex.getLocalizedMessage();
ServerUtilities.LOGGER.error(s);
cachedNameForChat = new ChatComponentText("BrokenFormatting");
cachedNameForChat.getChatStyle().setColor(EnumChatFormatting.RED);
cachedNameForChat.getChatStyle()
.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(s)));
}
} else {
cachedNameForChat = new ChatComponentText(player.getDisplayNameString());
}

if (ServerUtilitiesConfig.teams.force_team_prefix || showTeamPrefix) {
Expand All @@ -256,6 +269,11 @@ public IChatComponent getNameForChat(EntityPlayerMP playerMP) {
cachedNameForChat = new ChatComponentText("").appendSibling(rec).appendSibling(cachedNameForChat);
}

if (ServerUtilitiesConfig.chat.replace_tab_names
&& !cachedNameForChat.getUnformattedText().equals(player.getName())) {
new MessageUpdateTabName(player, cachedNameForChat).sendToAll();
}

cachedNameForChat.appendText(" ");
return cachedNameForChat.createCopy();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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 @@ -93,7 +91,6 @@ public void onPlayerLoggedIn(ForgePlayerLoggedInEvent event) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onServerChatEvent(ServerChatEvent event) {

IChatComponent main = new ChatComponentText("");
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(Universe.get().getPlayer(player));
main.appendSibling(data.getNameForChat(player));
main.appendSibling(data.getNameForChat());

String message = event.message.trim();

Expand Down
25 changes: 0 additions & 25 deletions src/main/java/serverutils/net/MessageRankModify.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@

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;

import serverutils.ServerUtilitiesConfig;
import serverutils.ServerUtilitiesPermissions;
import serverutils.client.gui.ranks.RankInst;
import serverutils.lib.config.ConfigValueInstance;
import serverutils.lib.data.ForgePlayer;
import serverutils.lib.data.Universe;
import serverutils.lib.io.DataIn;
import serverutils.lib.io.DataOut;
import serverutils.lib.net.MessageToServer;
import serverutils.lib.net.NetworkWrapper;
import serverutils.lib.util.permission.PermissionAPI;
import serverutils.ranks.PlayerRank;
import serverutils.ranks.Rank;
import serverutils.ranks.Ranks;

Expand Down Expand Up @@ -62,40 +55,22 @@ public void onMessage(EntityPlayerMP player) {
return;
}

boolean updateNames = false;
boolean shouldSave = false;
for (ConfigValueInstance value : inst.group.getValues()) {
Rank.Entry entry = rank.setPermission(value.getId(), value.getValue());
if (entry == null) continue;
if (entry.node.equals(ServerUtilitiesPermissions.CHAT_NAME_FORMAT)) {
updateNames = true;
}
shouldSave = true;
}

for (String removed : removedEntries) {
Rank.Entry entry = rank.setPermission(removed, "");
if (entry == null) continue;
if (entry.node.equals(ServerUtilitiesPermissions.CHAT_NAME_FORMAT)) {
updateNames = true;
}
shouldSave = true;

}

if (shouldSave) {
Ranks.INSTANCE.save();
}

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;
toUpdate.add(fp);
}
new MessageUpdateTabName(toUpdate).sendToAll();
}
}
}
20 changes: 7 additions & 13 deletions src/main/java/serverutils/net/MessageUpdateTabName.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiPlayerInfo;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;

Expand All @@ -25,7 +23,6 @@
import serverutils.lib.io.DataOut;
import serverutils.lib.net.MessageToClient;
import serverutils.lib.net.NetworkWrapper;
import serverutils.ranks.Ranks;

public class MessageUpdateTabName extends MessageToClient {

Expand All @@ -35,21 +32,18 @@ public MessageUpdateTabName() {}

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));
entries.add(new TabNameEntry(player.getName(), data.getNameForChat(), afk));
}
}

public MessageUpdateTabName(ForgePlayer player, IChatComponent displayName) {
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(player);
boolean afk = data.afkTime >= ServerUtilitiesConfig.afk.getNotificationTimer();
entries.add(new TabNameEntry(player.getName(), displayName, afk));
}

@Override
public NetworkWrapper getWrapper() {
return ServerUtilitiesNetHandler.GENERAL;
Expand Down

0 comments on commit 8c6e21b

Please sign in to comment.