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

@@ -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();
}
}
}