Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration option for specific chat type format #5337

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
- mc/*
- dev/*

permissions:
checks: write

jobs:
build:
name: Build and upload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.signs.EssentialsSign;
import com.earth2me.essentials.textreader.IText;
import net.essentialsx.api.v2.ChatType;
import net.kyori.adventure.text.minimessage.tag.Tag;
import org.bukkit.Material;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -37,6 +38,8 @@ public interface ISettings extends IConf {

String getChatFormat(String group);

String getChatFormat(String group, ChatType chatType);

String getWorldAlias(String world);

int getChatRadius();
Expand Down
151 changes: 126 additions & 25 deletions Essentials/src/main/java/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil;
import net.ess3.api.IEssentials;
import net.essentialsx.api.v2.ChatType;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.minimessage.tag.Tag;
Expand All @@ -35,6 +36,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand All @@ -45,6 +47,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand All @@ -59,7 +62,7 @@ public class Settings implements net.ess3.api.ISettings {
private final transient EssentialsConfiguration config;
private final transient IEssentials ess;
private final transient AtomicInteger reloadCount = new AtomicInteger(0);
private final Map<String, String> chatFormats = Collections.synchronizedMap(new HashMap<>());
private final ChatFormats chatFormats = new ChatFormats();
private int chatRadius = 0;
// #easteregg
private char chatShout = '!';
Expand Down Expand Up @@ -199,7 +202,7 @@ public int getHomeLimit(final User user) {
final Set<String> homeList = getMultipleHomes();
if (homeList != null) {
for (final String set : homeList) {
if (user.isAuthorized("essentials.sethome.multiple." + set) && (limit < getHomeLimit(set))) {
if (user.isAuthorized("essentials.sethome.multiple." + set) && limit < getHomeLimit(set)) {
limit = getHomeLimit(set);
}
}
Expand Down Expand Up @@ -590,32 +593,132 @@ public boolean isAlwaysRunBackup() {

@Override
public String getChatFormat(final String group) {
String mFormat = chatFormats.get(group);
if (mFormat == null) {
mFormat = config.getString("chat.group-formats." + (group == null ? "Default" : group), config.getString("chat.format", "&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}"));
mFormat = FormatUtil.replaceFormat(mFormat);
mFormat = mFormat.replace("{DISPLAYNAME}", "%1$s");
mFormat = mFormat.replace("{MESSAGE}", "%2$s");
mFormat = mFormat.replace("{GROUP}", "{0}");
mFormat = mFormat.replace("{WORLD}", "{1}");
mFormat = mFormat.replace("{WORLDNAME}", "{1}");
mFormat = mFormat.replace("{SHORTWORLDNAME}", "{2}");
mFormat = mFormat.replace("{TEAMPREFIX}", "{3}");
mFormat = mFormat.replace("{TEAMSUFFIX}", "{4}");
mFormat = mFormat.replace("{TEAMNAME}", "{5}");
mFormat = mFormat.replace("{PREFIX}", "{6}");
mFormat = mFormat.replace("{SUFFIX}", "{7}");
mFormat = mFormat.replace("{USERNAME}", "{8}");
mFormat = mFormat.replace("{NICKNAME}", "{9}");
mFormat = "§r".concat(mFormat);
chatFormats.put(group, mFormat);
}
return getChatFormat(group, null);
}

@Override
public String getChatFormat(final String group, final ChatType chatType) {
final String mFormat = chatFormats.getFormat(group, chatType, new ChatFormatConfigSupplier(group, chatType));
if (isDebug()) {
ess.getLogger().info(String.format("Found format '%s' for group '%s'", mFormat, group));
}
return mFormat;
}

private class ChatFormatConfigSupplier implements Supplier<String> {
private final String group;
private final ChatType chatType;

ChatFormatConfigSupplier(String group, ChatType chatType) {
this.group = group;
this.chatType = chatType;
}

@Override
public String get() {
final String chatKey = chatType.key();

final String groupPath = "chat.group-formats." + (group == null ? "Default" : group);
String configFormat = config.getString(groupPath + "." + chatKey, null);

if (configFormat == null) {
configFormat = config.getString(groupPath, null);
}

final String formatPath = "chat.format";
if (configFormat == null) {
configFormat = config.getString(formatPath + "." + chatKey, null);
}

if (configFormat == null) {
configFormat = config.getString(formatPath, null);
}

if (configFormat == null) {
configFormat = "&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}";
}

configFormat = FormatUtil.replaceFormat(configFormat);
configFormat = configFormat.replace("{DISPLAYNAME}", "%1$s");
configFormat = configFormat.replace("{MESSAGE}", "%2$s");
configFormat = configFormat.replace("{GROUP}", "{0}");
configFormat = configFormat.replace("{WORLD}", "{1}");
configFormat = configFormat.replace("{WORLDNAME}", "{1}");
configFormat = configFormat.replace("{SHORTWORLDNAME}", "{2}");
configFormat = configFormat.replace("{TEAMPREFIX}", "{3}");
configFormat = configFormat.replace("{TEAMSUFFIX}", "{4}");
configFormat = configFormat.replace("{TEAMNAME}", "{5}");
configFormat = configFormat.replace("{PREFIX}", "{6}");
configFormat = configFormat.replace("{SUFFIX}", "{7}");
configFormat = configFormat.replace("{USERNAME}", "{8}");
configFormat = configFormat.replace("{NICKNAME}", "{9}");
configFormat = "§r".concat(configFormat);
return configFormat;
}
}

private static class ChatFormats {

private final Map<String, TypedChatFormat> groupFormats;
private TypedChatFormat defaultFormat;

ChatFormats() {
defaultFormat = null;
groupFormats = new HashMap<>();
}

public String getFormat(String group, ChatType type, Supplier<String> configSupplier) {
// With such a large synchronize block, we synchronize a potential config deserialization
// It does not matter as it needs to be done. It's even better as we ensure to do it once
// TypedChatFormat is also synchronized
synchronized (this) {
final TypedChatFormat typedChatFormat;
if (group == null) {
if (defaultFormat == null) {
defaultFormat = new TypedChatFormat();
}
typedChatFormat = defaultFormat;
} else {
typedChatFormat = groupFormats.computeIfAbsent(group, s -> new TypedChatFormat());
}
return typedChatFormat.getFormat(type, configSupplier);
}
}

public void clear() {
synchronized (this) {
defaultFormat = null;
groupFormats.clear();
}
}

}

private static class TypedChatFormat {

private final Map<ChatType, String> typedFormats;
private String defaultFormat;

TypedChatFormat() {
defaultFormat = null;
typedFormats = new EnumMap<>(ChatType.class);
}

public String getFormat(ChatType type, Supplier<String> configSupplier) {
final String format;
if (type == null) {
if (defaultFormat == null) {
defaultFormat = configSupplier.get();
}
format = defaultFormat;
} else {
format = typedFormats.computeIfAbsent(type, c -> configSupplier.get());
}
return format;
}

}

@Override
public String getWorldAlias(String world) {
return worldAliases.getOrDefault(world.toLowerCase(), world);
Expand Down Expand Up @@ -1811,9 +1914,7 @@ public boolean isWorldChangeSpeedResetEnabled() {

private List<String> _getDefaultEnabledConfirmCommands() {
final List<String> commands = config.getList("default-enabled-confirm-commands", String.class);
for (int i = 0; i < commands.size(); i++) {
commands.set(i, commands.get(i).toLowerCase());
}
commands.replaceAll(String::toLowerCase);
return commands;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ChatType {
*
* <p>This type used when local/global chat features are disabled
*/
UNKNOWN,
UNKNOWN("normal"),
;

private final String key;
Expand All @@ -40,6 +40,10 @@ public enum ChatType {
this.key = name().toLowerCase(Locale.ENGLISH);
}

ChatType(final String key) {
this.key = key;
}

/**
* @return Lowercase name of the chat type.
*/
Expand Down
13 changes: 13 additions & 0 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ chat:

# Chat formatting can be done in two ways, you can either define a standard format for all chat.
# Or you can give a group specific chat format, to give some extra variation.
# For each of these formats, you can specify a sub format for each chat type.
# For more information of chat formatting, check out the wiki: http://wiki.ess3.net/wiki/Chat_Formatting
# Note: Using the {PREFIX} and {SUFFIX} placeholders along with {DISPLAYNAME} may cause double prefixes/suffixes to be shown in chat unless add-prefix-suffix is uncommented and set to false.

Expand All @@ -936,10 +937,22 @@ chat:
#format: '&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}'
#format: '&7{PREFIX}&r {DISPLAYNAME}&r &7{SUFFIX}&r: {MESSAGE}'

# You can also specify a format for each type of chat.
#format:
# normal: '{WORLDNAME} {DISPLAYNAME}&7:&r {MESSAGE}'
# question: '{WORLDNAME} &4{DISPLAYNAME}&7:&r {MESSAGE}'
# shout: '{WORLDNAME} &c[{GROUP}]&r &4{DISPLAYNAME}&7:&c {MESSAGE}'

# You can specify a format for each group.
group-formats:
# default: '{WORLDNAME} {DISPLAYNAME}&7:&r {MESSAGE}'
# admins: '{WORLDNAME} &c[{GROUP}]&r {DISPLAYNAME}&7:&c {MESSAGE}'

# You can also specify a format for each type of chat for each group.
# admins:
# question: '{WORLDNAME} &4{DISPLAYNAME}&7:&r {MESSAGE}'
# shout: '{WORLDNAME} &c[{GROUP}]&r &4{DISPLAYNAME}&7:&c {MESSAGE}'

# If you are using group formats make sure to remove the '#' to allow the setting to be read.
# Note: Group names are case-sensitive so you must match them up with your permission plugin.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void handleChatFormat(AsyncPlayerChatEvent event) {
// This listener should apply the general chat formatting only...then return control back the event handler
event.setMessage(FormatUtil.formatMessage(user, "essentials.chat", event.getMessage()));

if (ChatColor.stripColor(event.getMessage()).length() == 0) {
if (ChatColor.stripColor(event.getMessage()).isEmpty()) {
event.setCancelled(true);
return;
}
Expand All @@ -91,7 +91,8 @@ protected void handleChatFormat(AsyncPlayerChatEvent event) {
final String suffix = FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(player));
final Team team = player.getScoreboard().getPlayerTeam(player);

String format = ess.getSettings().getChatFormat(group);
final ChatType chatType = chat.getType();
String format = ess.getSettings().getChatFormat(group, chat.getRadius() > 0 && chatType == ChatType.UNKNOWN ? ChatType.LOCAL : chatType);
format = format.replace("{0}", group);
format = format.replace("{1}", ess.getSettings().getWorldAlias(world));
format = format.replace("{2}", world.substring(0, 1).toUpperCase(Locale.ENGLISH));
Expand All @@ -104,7 +105,7 @@ protected void handleChatFormat(AsyncPlayerChatEvent event) {
format = format.replace("{9}", nickname == null ? username : nickname);

// Local, shout and question chat types are only enabled when there's a valid radius
if (chat.getRadius() > 0 && event.getMessage().length() > 0) {
if (chat.getRadius() > 0 && !event.getMessage().isEmpty()) {
if (event.getMessage().length() > 1 && ((chat.getType() == ChatType.SHOUT && event.getMessage().charAt(0) == ess.getSettings().getChatShout()) || (chat.getType() == ChatType.QUESTION && event.getMessage().charAt(0) == ess.getSettings().getChatQuestion()))) {
event.setMessage(event.getMessage().substring(1));
}
Expand Down Expand Up @@ -142,7 +143,7 @@ protected void handleChatRecipients(AsyncPlayerChatEvent event) {

final User user = chat.getUser();

if (event.getMessage().length() > 0) {
if (!event.getMessage().isEmpty()) {
if (chat.getType() == ChatType.UNKNOWN) {
if (!user.isAuthorized("essentials.chat.local")) {
user.sendTl("notAllowedToLocal");
Expand Down Expand Up @@ -288,7 +289,7 @@ boolean isAborted(final AsyncPlayerChatEvent event) {
}

ChatType getChatType(final User user, final String message) {
if (message.length() == 0) {
if (message.isEmpty()) {
//Ignore empty chat events generated by plugins
return ChatType.UNKNOWN;
}
Expand Down
Loading