mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-08-02 20:45:28 +02:00
Fixed a bug where party members didn't have properly colored names, added some optimization and removed some unnecessary API
This commit is contained in:
@@ -1,64 +1,87 @@
|
||||
package com.gmail.nossr50.chat.author;
|
||||
|
||||
import com.gmail.nossr50.datatypes.chat.ChatChannel;
|
||||
import com.gmail.nossr50.util.text.TextUtils;
|
||||
import com.google.common.base.Objects;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class AbstractPlayerAuthor implements Author {
|
||||
private final @NotNull Player player;
|
||||
private @NotNull String displayName;
|
||||
private @Nullable TextComponent componentDisplayName;
|
||||
private @Nullable TextComponent componentUserName;
|
||||
private @NotNull String lastKnownDisplayName;
|
||||
private final @NotNull HashMap<ChatChannel, String> sanitizedNameCache;
|
||||
|
||||
public AbstractPlayerAuthor(@NotNull Player player) {
|
||||
this.player = player;
|
||||
this.displayName = player.getDisplayName();
|
||||
this.lastKnownDisplayName = player.getDisplayName();
|
||||
this.sanitizedNameCache = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the {@link TextComponent} version of a players display name
|
||||
* Cached and only processed as needed
|
||||
* Always checks if the player display name has changed, if it has it regenerates the output
|
||||
* Returns true if a players display name has changed
|
||||
*
|
||||
* @return the {@link TextComponent} version of a players display name
|
||||
* @return true if the players display name has changed
|
||||
*/
|
||||
public @NotNull TextComponent getComponentDisplayName() {
|
||||
//Not sure if this is expensive but it ensures always up to date names
|
||||
if(!player.getDisplayName().equals(displayName)) {
|
||||
displayName = player.getDisplayName();
|
||||
componentDisplayName = null;
|
||||
}
|
||||
|
||||
if(componentDisplayName != null) {
|
||||
return componentDisplayName;
|
||||
} else {
|
||||
//convert to adventure component
|
||||
componentDisplayName = TextUtils.ofLegacyTextRaw(displayName);
|
||||
}
|
||||
return componentDisplayName;
|
||||
private boolean hasPlayerDisplayNameChanged() {
|
||||
return !player.getDisplayName().equals(lastKnownDisplayName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the {@link TextComponent} version of a players current minecraft nickname
|
||||
* Cached and only processed as needed
|
||||
*
|
||||
* @return the {@link TextComponent} version of a players current minecraft nickname
|
||||
* Player display names can change and this method will update the last known display name of this player
|
||||
*/
|
||||
public @NotNull TextComponent getComponentUserName() {
|
||||
//Not sure if this is expensive but it ensures always up to date names
|
||||
if(componentUserName != null) {
|
||||
return componentUserName;
|
||||
private void updateLastKnownDisplayName() {
|
||||
lastKnownDisplayName = player.getDisplayName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a sanitized name for a channel
|
||||
* Sanitized names are names that are friendly to the {@link net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer}
|
||||
* Sanitized names for authors are cached by channel and are only created as needed
|
||||
* Sanitized names will update if a players display name has updated
|
||||
*
|
||||
* @param chatChannel target chat channel
|
||||
* @return the sanitized name for a player
|
||||
*/
|
||||
protected @NotNull String getSanitizedName(@NotNull ChatChannel chatChannel, boolean useDisplayName) {
|
||||
//Already in cache
|
||||
if(sanitizedNameCache.containsKey(chatChannel)) {
|
||||
//Update cache
|
||||
if(useDisplayName && hasPlayerDisplayNameChanged()) {
|
||||
updateLastKnownDisplayName();
|
||||
updateSanitizedNameCache(chatChannel, true);
|
||||
}
|
||||
} else {
|
||||
//convert to adventure component
|
||||
componentUserName = TextUtils.ofLegacyTextRaw(player.getName());
|
||||
//Update last known display name
|
||||
if(useDisplayName && hasPlayerDisplayNameChanged()) {
|
||||
updateLastKnownDisplayName();
|
||||
}
|
||||
|
||||
//Add cache entry
|
||||
updateSanitizedNameCache(chatChannel, useDisplayName);
|
||||
}
|
||||
|
||||
return sanitizedNameCache.get(chatChannel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the sanitized name cache
|
||||
* This will add entries if one didn't exit
|
||||
* Sanitized names are associated with a {@link ChatChannel} as different chat channels have different chat name settings
|
||||
*
|
||||
* @param chatChannel target chat channel
|
||||
* @param useDisplayName whether or not to use this authors display name
|
||||
*/
|
||||
private void updateSanitizedNameCache(@NotNull ChatChannel chatChannel, boolean useDisplayName) {
|
||||
if(useDisplayName) {
|
||||
sanitizedNameCache.put(chatChannel, TextUtils.sanitizeForSerializer(player.getDisplayName()));
|
||||
} else {
|
||||
//No need to sanitize a basic String
|
||||
sanitizedNameCache.put(chatChannel, player.getName());
|
||||
}
|
||||
return componentUserName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,11 +108,13 @@ public abstract class AbstractPlayerAuthor implements Author {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
AbstractPlayerAuthor that = (AbstractPlayerAuthor) o;
|
||||
return Objects.equal(player, that.player);
|
||||
return Objects.equal(player, that.player) &&
|
||||
Objects.equal(lastKnownDisplayName, that.lastKnownDisplayName) &&
|
||||
Objects.equal(sanitizedNameCache, that.sanitizedNameCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(player);
|
||||
return Objects.hashCode(player, lastKnownDisplayName, sanitizedNameCache);
|
||||
}
|
||||
}
|
||||
|
@@ -2,25 +2,10 @@ package com.gmail.nossr50.chat.author;
|
||||
|
||||
import com.gmail.nossr50.datatypes.chat.ChatChannel;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface Author extends Identity {
|
||||
|
||||
/**
|
||||
* The name of this author as used in mcMMO chat
|
||||
* This is the {@link TextComponent} representation of the users current chat username
|
||||
* This can either be the player's display name or the player's official registered nickname with Mojang it depends on the servers chat settings for mcMMO
|
||||
*
|
||||
* NOTE:
|
||||
* mcMMO doesn't transform a players name into a component when creating the chat message, instead it converts the whole string from raw legacy text (including md5 stuff) -> TextComponent via {@link net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer}
|
||||
* This method is just provided for convenience, it uses lazy initialization
|
||||
*
|
||||
* @param chatChannel which chat channel this is going to
|
||||
* @return The name of this author as used in mcMMO chat
|
||||
*/
|
||||
@NotNull TextComponent getAuthoredComponentName(@NotNull ChatChannel chatChannel);
|
||||
|
||||
/**
|
||||
* The name of this author as used in mcMMO chat
|
||||
* This is the {@link String} representation of the users current chat username
|
||||
|
@@ -2,7 +2,6 @@ package com.gmail.nossr50.chat.author;
|
||||
|
||||
import com.gmail.nossr50.datatypes.chat.ChatChannel;
|
||||
import com.gmail.nossr50.util.text.TextUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -11,20 +10,13 @@ import java.util.UUID;
|
||||
public class ConsoleAuthor implements Author {
|
||||
private final UUID uuid;
|
||||
private final @NotNull String name;
|
||||
private final @NotNull TextComponent componentName;
|
||||
|
||||
public ConsoleAuthor(@NotNull String name) {
|
||||
this.uuid = new UUID(0, 0);
|
||||
this.name = name;
|
||||
this.componentName = TextUtils.ofBungeeRawStrings(name);
|
||||
}
|
||||
|
||||
//TODO: Think of a better solution later
|
||||
@Override
|
||||
public @NotNull TextComponent getAuthoredComponentName(@NotNull ChatChannel chatChannel) {
|
||||
return componentName;
|
||||
this.name = TextUtils.sanitizeForSerializer(name);
|
||||
}
|
||||
|
||||
//TODO: Think of a less clunky solution later
|
||||
@Override
|
||||
public @NotNull String getAuthoredName(@NotNull ChatChannel chatChannel) {
|
||||
return name;
|
||||
|
@@ -2,7 +2,6 @@ package com.gmail.nossr50.chat.author;
|
||||
|
||||
import com.gmail.nossr50.config.ChatConfig;
|
||||
import com.gmail.nossr50.datatypes.chat.ChatChannel;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -12,22 +11,9 @@ public class PlayerAuthor extends AbstractPlayerAuthor {
|
||||
super(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull TextComponent getAuthoredComponentName(@NotNull ChatChannel chatChannel) {
|
||||
if(ChatConfig.getInstance().useDisplayNames(chatChannel)) {
|
||||
return getComponentDisplayName();
|
||||
} else {
|
||||
return getComponentUserName();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthoredName(@NotNull ChatChannel chatChannel) {
|
||||
if(ChatConfig.getInstance().useDisplayNames(chatChannel)) {
|
||||
return getPlayer().getDisplayName();
|
||||
} else {
|
||||
return getPlayer().getName();
|
||||
}
|
||||
return getSanitizedName(chatChannel, ChatConfig.getInstance().useDisplayNames(chatChannel));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user