diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index 411b9dac10d..908e2ca11f0 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -202,7 +202,7 @@ public int getHomeLimit(final User user) { final Set 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); } } @@ -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 { - - private final EssentialsConfiguration config; + private class ChatFormatConfigSupplier implements Supplier { 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}"; } @@ -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 { @@ -1929,9 +1914,7 @@ public boolean isWorldChangeSpeedResetEnabled() { private List _getDefaultEnabledConfirmCommands() { final List 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; } diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/ChatType.java b/Essentials/src/main/java/net/essentialsx/api/v2/ChatType.java index 763a7637824..3124a93fa52 100644 --- a/Essentials/src/main/java/net/essentialsx/api/v2/ChatType.java +++ b/Essentials/src/main/java/net/essentialsx/api/v2/ChatType.java @@ -31,7 +31,7 @@ public enum ChatType { * *

This type used when local/global chat features are disabled */ - UNKNOWN, + UNKNOWN("normal"), ; private final String key; @@ -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. */ diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index 060f0dd7939..96bcb82afe5 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -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}' diff --git a/EssentialsChat/src/main/java/com/earth2me/essentials/chat/processing/AbstractChatHandler.java b/EssentialsChat/src/main/java/com/earth2me/essentials/chat/processing/AbstractChatHandler.java index 275881cc384..6853d1bf14d 100644 --- a/EssentialsChat/src/main/java/com/earth2me/essentials/chat/processing/AbstractChatHandler.java +++ b/EssentialsChat/src/main/java/com/earth2me/essentials/chat/processing/AbstractChatHandler.java @@ -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; } @@ -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)); @@ -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)); } @@ -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"); @@ -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; }