Skip to content

Commit

Permalink
bunch of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed Nov 27, 2024
1 parent 0c66a5d commit 4a6d468
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
57 changes: 20 additions & 37 deletions Essentials/src/main/java/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,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 @@ -598,42 +598,42 @@ public String getChatFormat(final String group) {

@Override
public String getChatFormat(final String group, final ChatType chatType) {
final String mFormat = chatFormats.getFormat(group, chatType, new ChatFormatConfigSupplier(config, group, 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;
}

// Idk where these classes should be
private static class ChatFormatConfigSupplier implements Supplier<String> {

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

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

@Override
public String get() {
//String configFormat = config.getString("chat.group-formats." + (group == null ? "Default" : group), config.getString("chat.format", "&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}"));
String configFormat = null;
CommentedConfigurationNode node = config.getSection("chat.group-formats." + (group == null ? "Default" : group));
if (node != null) {
configFormat = getFormat(node, chatType);
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);
}
// Group format is null, try default format

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

if (configFormat == null) {
configFormat = config.getString(formatPath, null);
}
// Group format is null, get the default one

if (configFormat == null) {
configFormat = "&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}";
}
Expand All @@ -655,21 +655,6 @@ public String get() {
configFormat = "§r".concat(configFormat);
return configFormat;
}

private String getFormat(CommentedConfigurationNode node, ChatType chatType) {
// Try to get the specified chat type
CommentedConfigurationNode child = node.node(chatType.key());
if (child.virtual()) {
// Try to get the default chat type
child = node.node("default");
}
if (child.virtual()) {
// Get the current node
child = node;
}
return child.getString();
}

}

private static class ChatFormats {
Expand Down Expand Up @@ -1929,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
7 changes: 4 additions & 3 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -937,17 +937,18 @@ chat:
#format: '&7[{GROUP}]&r {DISPLAYNAME}&7:&r {MESSAGE}'
#format: '&7{PREFIX}&r {DISPLAYNAME}&r &7{SUFFIX}&r: {MESSAGE}'

# With chat types
# 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}'

# With chat types for the group 'admins'
# 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}'
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,8 +91,8 @@ protected void handleChatFormat(AsyncPlayerChatEvent event) {
final String suffix = FormatUtil.replaceFormat(ess.getPermissionsHandler().getSuffix(player));
final Team team = player.getScoreboard().getPlayerTeam(player);

// chat.getType() can't return ChatType.LOCAL (which can leads to inconsistencies)
String format = ess.getSettings().getChatFormat(group, chat.getType());
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 @@ -105,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 @@ -143,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 @@ -289,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

0 comments on commit 4a6d468

Please sign in to comment.