Fix Hex-Colored names in parties/admin chat

This commit is contained in:
nossr50
2020-11-06 14:01:07 -08:00
parent b2cdffe965
commit a4fd632d53
23 changed files with 300 additions and 220 deletions

View File

@ -13,6 +13,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.text.StringUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
@ -70,10 +71,10 @@ public class ChatManager {
private void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel chatChannel, @NotNull String rawMessage, boolean isAsync) {
switch (chatChannel) {
case ADMIN:
adminChatMailer.processChatMessage(mmoPlayer.getAdminAuthor(), rawMessage, isAsync, Permissions.colorChat(mmoPlayer.getPlayer()));
adminChatMailer.processChatMessage(mmoPlayer.getPlayerAuthor(), rawMessage, isAsync, Permissions.colorChat(mmoPlayer.getPlayer()));
break;
case PARTY:
partyChatMailer.processChatMessage(mmoPlayer.getPartyAuthor(), rawMessage, mmoPlayer.getParty(), isAsync, Permissions.colorChat(mmoPlayer.getPlayer()), isPartyLeader(mmoPlayer));
partyChatMailer.processChatMessage(mmoPlayer.getPlayerAuthor(), rawMessage, mmoPlayer.getParty(), isAsync, Permissions.colorChat(mmoPlayer.getPlayer()), isPartyLeader(mmoPlayer));
break;
case PARTY_OFFICER:
case NONE:

View File

@ -0,0 +1,95 @@
package com.gmail.nossr50.chat.author;
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.UUID;
public abstract class AbstractPlayerAuthor implements Author {
private final @NotNull Player player;
private @NotNull String displayName;
private @Nullable TextComponent componentDisplayName;
private @Nullable TextComponent componentUserName;
public AbstractPlayerAuthor(@NotNull Player player) {
this.player = player;
this.displayName = player.getDisplayName();
}
/**
* 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
*
* @return the {@link TextComponent} version of a players display name
*/
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.ofBungeeRawStrings(displayName);
}
return componentDisplayName;
}
/**
* 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
*/
public @NotNull TextComponent getComponentUserName() {
//Not sure if this is expensive but it ensures always up to date names
if(componentUserName != null) {
return componentUserName;
} else {
//convert to adventure component
componentUserName = TextUtils.ofBungeeRawStrings(player.getName());
}
return componentUserName;
}
@Override
public boolean isConsole() {
return false;
}
@Override
public boolean isPlayer() {
return true;
}
public @NotNull Player getPlayer() {
return player;
}
@Override
public @NonNull UUID uuid() {
return player.getUniqueId();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractPlayerAuthor that = (AbstractPlayerAuthor) o;
return Objects.equal(player, that.player);
}
@Override
public int hashCode() {
return Objects.hashCode(player);
}
}

View File

@ -1,79 +0,0 @@
package com.gmail.nossr50.chat.author;
import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.google.common.base.Objects;
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.UUID;
public class AdminAuthor implements Author {
private final @NotNull Player player;
private @Nullable String overrideName;
public AdminAuthor(@NotNull Player player) {
this.player = player;
}
@Override
public @NotNull String getAuthoredName() {
if(overrideName != null) {
return overrideName;
} else {
if(ChatConfig.getInstance().useDisplayNames(ChatChannel.ADMIN)) {
return player.getDisplayName();
} else {
return player.getName();
}
}
}
public @NotNull Player getPlayer() {
return player;
}
public @Nullable String getOverrideName() {
return overrideName;
}
/**
* Set the name of this author
* @param newName value of the new name
*/
public void setName(@NotNull String newName) {
overrideName = newName;
}
@Override
public boolean isConsole() {
return false;
}
@Override
public boolean isPlayer() {
return true;
}
@Override
public @NonNull UUID uuid() {
return player.getUniqueId();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AdminAuthor that = (AdminAuthor) o;
return Objects.equal(player, that.player) &&
Objects.equal(overrideName, that.overrideName);
}
@Override
public int hashCode() {
return Objects.hashCode(player, overrideName);
}
}

View File

@ -1,15 +1,35 @@
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
* @return the name of this author
* 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 use this method currently because we convert the whole chat message from raw Bungee-compatible strings to Bungee components before converting it to adventure components (hacky and will be changed later)
* So this method is provided for future use or if plugins want to make use of it
*
* @param chatChannel which chat channel this is going to
* @return The name of this author as used in mcMMO chat
*/
@NotNull String getAuthoredName();
@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
* 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
*
* @param chatChannel which chat channel this is going to
* @return The name of this author as used in mcMMO chat
*/
@NotNull String getAuthoredName(@NotNull ChatChannel chatChannel);
/**
* Whether or not this author is a {@link org.bukkit.command.ConsoleCommandSender}

View File

@ -1,5 +1,8 @@
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;
@ -8,14 +11,22 @@ 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.name = 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;
}
@Override
public @NotNull String getAuthoredName() {
public @NotNull String getAuthoredName(@NotNull ChatChannel chatChannel) {
return name;
}

View File

@ -1,75 +0,0 @@
package com.gmail.nossr50.chat.author;
import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.google.common.base.Objects;
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.UUID;
public class PartyAuthor implements Author {
private final @NotNull Player player;
private @Nullable String overrideName;
public PartyAuthor(@NotNull Player player) {
this.player = player;
}
@Override
public @NotNull String getAuthoredName() {
if(overrideName != null) {
return overrideName;
} else {
if(ChatConfig.getInstance().useDisplayNames(ChatChannel.PARTY)) {
return player.getDisplayName();
} else {
return player.getName();
}
}
}
/**
* Set the name of this author
* @param newName value of the new name
*/
public void setName(@NotNull String newName) {
overrideName = newName;
}
@Override
public boolean isConsole() {
return false;
}
@Override
public boolean isPlayer() {
return true;
}
public Player getPlayer() {
return player;
}
@Override
public @NonNull UUID uuid() {
return player.getUniqueId();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PartyAuthor that = (PartyAuthor) o;
return Objects.equal(player, that.player) &&
Objects.equal(overrideName, that.overrideName);
}
@Override
public int hashCode() {
return Objects.hashCode(player, overrideName);
}
}

View File

@ -0,0 +1,33 @@
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;
public class PlayerAuthor extends AbstractPlayerAuthor {
public PlayerAuthor(@NotNull Player player) {
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();
}
}
}

View File

@ -3,12 +3,13 @@ package com.gmail.nossr50.chat.mailer;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.chat.message.AdminChatMessage;
import com.gmail.nossr50.chat.message.ChatMessage;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.events.chat.McMMOChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.text.TextUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -53,9 +54,9 @@ public class AdminChatMailer extends AbstractChatMailer {
*/
public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message, boolean canColor) {
if(canColor) {
return Component.text(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(), LocaleLoader.addColors(message)));
return TextUtils.ofBungeeRawStrings(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), LocaleLoader.addColors(message)));
} else {
return Component.text(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(), message));
return TextUtils.ofBungeeRawStrings(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), message));
}
}

View File

@ -3,13 +3,14 @@ package com.gmail.nossr50.chat.mailer;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.chat.message.ChatMessage;
import com.gmail.nossr50.chat.message.PartyChatMessage;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.events.chat.McMMOChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.text.TextUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
@ -49,9 +50,9 @@ public class PartyChatMailer extends AbstractChatMailer {
}
if(isLeader) {
return Component.text(LocaleLoader.getString("Chat.Style.Party.Leader", author.getAuthoredName(), message));
return TextUtils.ofBungeeRawStrings(LocaleLoader.getString("Chat.Style.Party.Leader", author.getAuthoredName(ChatChannel.PARTY), message));
} else {
return Component.text(LocaleLoader.getString("Chat.Style.Party", author.getAuthoredName(), message));
return TextUtils.ofBungeeRawStrings(LocaleLoader.getString("Chat.Style.Party", author.getAuthoredName(ChatChannel.PARTY), message));
}
}

View File

@ -1,8 +1,10 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.google.common.base.Objects;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
@ -33,11 +35,6 @@ public abstract class AbstractChatMessage implements ChatMessage {
return author;
}
@Override
public @NotNull String getAuthorDisplayName() {
return author.getAuthoredName();
}
@Override
public @NotNull Audience getAudience() {
return audience;

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.plugin.Plugin;
@ -15,4 +16,9 @@ public class AdminChatMessage extends AbstractChatMessage {
public void sendMessage() {
audience.sendMessage(author, componentMessage);
}
@Override
public @NotNull String getAuthorDisplayName() {
return author.getAuthoredName(ChatChannel.ADMIN);
}
}

View File

@ -1,7 +1,9 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.jetbrains.annotations.NotNull;

View File

@ -1,14 +1,15 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.text.TextUtils;
import com.google.common.base.Objects;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -31,6 +32,11 @@ public class PartyChatMessage extends AbstractChatMessage {
return party;
}
@Override
public @NotNull String getAuthorDisplayName() {
return author.getAuthoredName(ChatChannel.PARTY);
}
@Override
public void sendMessage() {
/*
@ -40,7 +46,7 @@ public class PartyChatMessage extends AbstractChatMessage {
//Sends to everyone but console
audience.sendMessage(author, componentMessage);
TextComponent spyMessage = Component.text(LocaleLoader.getString("Chat.Spy.Party", author.getAuthoredName(), rawMessage, party.getName()));
TextComponent spyMessage = TextUtils.ofBungeeRawStrings(LocaleLoader.getString("Chat.Spy.Party", author.getAuthoredName(ChatChannel.PARTY), rawMessage, party.getName()));
//Relay to spies
messagePartyChatSpies(spyMessage);