From 8856d2b071eb0f76e8e9380a80d2c1f5423444d8 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 29 Oct 2020 13:06:22 -0700 Subject: [PATCH] move chat config options from config.yml -> chat.yml --- Changelog.txt | 10 +++- .../com/gmail/nossr50/chat/ChatManager.java | 39 ++++++++++++++ .../nossr50/chat/author/AdminAuthor.java | 5 +- .../nossr50/chat/author/PartyAuthor.java | 5 +- .../nossr50/commands/CommandManager.java | 25 +++++++-- .../com/gmail/nossr50/config/ChatConfig.java | 51 +++++++++++++++++++ .../java/com/gmail/nossr50/config/Config.java | 7 --- .../gmail/nossr50/datatypes/party/Party.java | 4 +- .../runnables/party/PartyChatTask.java | 50 ------------------ src/main/resources/chat.yml | 20 ++++++++ src/main/resources/config.yml | 7 --- 11 files changed, 148 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/config/ChatConfig.java delete mode 100644 src/main/java/com/gmail/nossr50/runnables/party/PartyChatTask.java create mode 100644 src/main/resources/chat.yml diff --git a/Changelog.txt b/Changelog.txt index abc246fbb..3a323c230 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,5 +1,13 @@ Version 2.1.151 - Fixed a bug where players could chat to party chat without the party chat permission + Fixed a bug where players could use the party chat command without the party chat permission + Added new config 'chat.yml' + All chat settings that used to be in 'config.yml' are now in 'chat.yml' + + NOTES: + The new config file lets you disable the chat system (you can disable all of it, or just party chat, and or just admin chat) without permission nodes. + If you disable the party/admin chat, then the party/admin chat command never gets registered and attempting to use the command will result in a whole lot of nothing. + I hate adding more config files using the old .yml system, but the config update is a ways out and this works for now. + Reminder that the look/feel of party/admin chat is now determined by locale entries Version 2.1.150 Fixed an ArrayIndexOutOfBounds exception when using /skillreset diff --git a/src/main/java/com/gmail/nossr50/chat/ChatManager.java b/src/main/java/com/gmail/nossr50/chat/ChatManager.java index f993d8da0..a15f1cc9f 100644 --- a/src/main/java/com/gmail/nossr50/chat/ChatManager.java +++ b/src/main/java/com/gmail/nossr50/chat/ChatManager.java @@ -4,6 +4,7 @@ import com.gmail.nossr50.chat.author.Author; import com.gmail.nossr50.chat.author.ConsoleAuthor; import com.gmail.nossr50.chat.mailer.AdminChatMailer; import com.gmail.nossr50.chat.mailer.PartyChatMailer; +import com.gmail.nossr50.config.ChatConfig; import com.gmail.nossr50.datatypes.chat.ChatChannel; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; @@ -25,12 +26,15 @@ public class ChatManager { private final @NotNull ConsoleAuthor consoleAuthor; private final @NotNull Audience consoleAudience; + private final boolean isChatEnabled; + public ChatManager(@NotNull mcMMO pluginRef) { adminChatMailer = new AdminChatMailer(pluginRef); partyChatMailer = new PartyChatMailer(pluginRef); this.consoleAuthor = new ConsoleAuthor(LocaleLoader.getString("Chat.Identity.Console")); this.consoleAudience = mcMMO.getAudiences().filter((cs) -> cs instanceof ConsoleCommandSender); + this.isChatEnabled = ChatConfig.getInstance().isChatEnabled(); } /** @@ -179,5 +183,40 @@ public class ChatManager { public void sendConsoleMessage(@NotNull Author author, @NotNull TextComponent message) { consoleAudience.sendMessage(author, message); } + + /** + * Whether the mcMMO chat system which handles party and admin chat is enabled or disabled + * @return true if mcMMO chat processing (for party/admin chat) is enabled + */ + public boolean isChatEnabled() { + return isChatEnabled; + } + + /** + * Whether or not a specific chat channel is enabled + * ChatChannels are enabled/disabled via user config + * + * If chat is disabled, this always returns false + * If NONE is passed as a {@link ChatChannel} it will return true + * @param chatChannel target chat channel + * @return true if the chat channel is enabled + */ + public boolean isChatChannelEnabled(@NotNull ChatChannel chatChannel) { + if(!isChatEnabled) { + return false; + } else { + switch(chatChannel) { + + case ADMIN: + case PARTY: + case PARTY_OFFICER: + return ChatConfig.getInstance().isChatChannelEnabled(chatChannel); + case NONE: + return true; + default: + return false; + } + } + } } diff --git a/src/main/java/com/gmail/nossr50/chat/author/AdminAuthor.java b/src/main/java/com/gmail/nossr50/chat/author/AdminAuthor.java index f5680872c..e4b26f8c2 100644 --- a/src/main/java/com/gmail/nossr50/chat/author/AdminAuthor.java +++ b/src/main/java/com/gmail/nossr50/chat/author/AdminAuthor.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.chat.author; -import com.gmail.nossr50.config.Config; +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; @@ -23,7 +24,7 @@ public class AdminAuthor implements Author { if(overrideName != null) { return overrideName; } else { - if(Config.getInstance().getAdminDisplayNames()) { + if(ChatConfig.getInstance().useDisplayNames(ChatChannel.ADMIN)) { return player.getDisplayName(); } else { return player.getName(); diff --git a/src/main/java/com/gmail/nossr50/chat/author/PartyAuthor.java b/src/main/java/com/gmail/nossr50/chat/author/PartyAuthor.java index 3100d1f91..3a821018c 100644 --- a/src/main/java/com/gmail/nossr50/chat/author/PartyAuthor.java +++ b/src/main/java/com/gmail/nossr50/chat/author/PartyAuthor.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.chat.author; -import com.gmail.nossr50.config.Config; +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; @@ -23,7 +24,7 @@ public class PartyAuthor implements Author { if(overrideName != null) { return overrideName; } else { - if(Config.getInstance().getPartyDisplayNames()) { + if(ChatConfig.getInstance().useDisplayNames(ChatChannel.PARTY)) { return player.getDisplayName(); } else { return player.getName(); diff --git a/src/main/java/com/gmail/nossr50/commands/CommandManager.java b/src/main/java/com/gmail/nossr50/commands/CommandManager.java index 0b07e54c7..7182744b2 100644 --- a/src/main/java/com/gmail/nossr50/commands/CommandManager.java +++ b/src/main/java/com/gmail/nossr50/commands/CommandManager.java @@ -5,6 +5,8 @@ import co.aikar.commands.BukkitCommandManager; import co.aikar.commands.ConditionFailedException; import com.gmail.nossr50.commands.chat.AdminChatCommand; import com.gmail.nossr50.commands.chat.PartyChatCommand; +import com.gmail.nossr50.config.ChatConfig; +import com.gmail.nossr50.datatypes.chat.ChatChannel; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; @@ -18,9 +20,9 @@ import org.jetbrains.annotations.NotNull; * For now this class will only handle ACF converted commands, all other commands will be handled elsewhere */ public class CommandManager { - public static final String ADMIN_CONDITION = "adminCondition"; - public static final String PARTY_CONDITION = "partyCondition"; - public static final String MMO_DATA_LOADED = "mmoDataLoaded"; + public static final @NotNull String ADMIN_CONDITION = "adminCondition"; + public static final @NotNull String PARTY_CONDITION = "partyCondition"; + public static final @NotNull String MMO_DATA_LOADED = "mmoDataLoaded"; private final @NotNull mcMMO pluginRef; private final @NotNull BukkitCommandManager bukkitCommandManager; @@ -34,8 +36,21 @@ public class CommandManager { } private void registerCommands() { - bukkitCommandManager.registerCommand(new AdminChatCommand(pluginRef)); - bukkitCommandManager.registerCommand(new PartyChatCommand(pluginRef)); + registerChatCommands(); + } + + /** + * Registers chat commands if the chat system is enabled + */ + private void registerChatCommands() { + if(ChatConfig.getInstance().isChatEnabled()) { + if(ChatConfig.getInstance().isChatChannelEnabled(ChatChannel.ADMIN)) { + bukkitCommandManager.registerCommand(new AdminChatCommand(pluginRef)); + } + if(ChatConfig.getInstance().isChatChannelEnabled(ChatChannel.PARTY)) { + bukkitCommandManager.registerCommand(new PartyChatCommand(pluginRef)); + } + } } public void registerConditions() { diff --git a/src/main/java/com/gmail/nossr50/config/ChatConfig.java b/src/main/java/com/gmail/nossr50/config/ChatConfig.java new file mode 100644 index 000000000..f8d117288 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/ChatConfig.java @@ -0,0 +1,51 @@ +package com.gmail.nossr50.config; + +import com.gmail.nossr50.datatypes.chat.ChatChannel; +import com.gmail.nossr50.util.StringUtils; +import org.jetbrains.annotations.NotNull; + +public class ChatConfig extends AutoUpdateConfigLoader { + private static ChatConfig instance; + + private ChatConfig() { + super("chat.yml"); + validate(); + } + + public static ChatConfig getInstance() { + if (instance == null) { + instance = new ChatConfig(); + } + + return instance; + } + + @Override + protected void loadKeys() { + //Sigh this old config system... + } + + @Override + protected boolean validateKeys() { + return true; + } + + public boolean isChatEnabled() { + return config.getBoolean("Chat.Enable", true); + } + + public boolean isChatChannelEnabled(@NotNull ChatChannel chatChannel) { + String key = "Chat.Channels." + StringUtils.getCapitalized(chatChannel.toString()) + ".Enabled"; + return config.getBoolean(key, true); + } + + /** + * Whether or not to use display names for players in target {@link ChatChannel} + * @param chatChannel target chat channel + * @return true if display names should be used + */ + public boolean useDisplayNames(@NotNull ChatChannel chatChannel) { + String key = "Chat.Channels." + StringUtils.getCapitalized(chatChannel.toString()) + ".Use_Display_Names"; + return config.getBoolean(key, true); + } +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index fa8c875dc..1ab532744 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -259,13 +259,6 @@ public class Config extends AutoUpdateConfigLoader { public boolean getPreferBeta() { return config.getBoolean("General.Prefer_Beta", false); } public boolean getVerboseLoggingEnabled() { return config.getBoolean("General.Verbose_Logging", false); } - public String getPartyChatPrefix() { return config.getString("Commands.partychat.Chat_Prefix_Format", "[[GREEN]]([[WHITE]]{0}[[GREEN]])"); } - public boolean getPartyChatColorLeaderName() { return config.getBoolean("Commands.partychat.Gold_Leader_Name", true); } - public boolean getPartyDisplayNames() { return config.getBoolean("Commands.partychat.Use_Display_Names", true); } - public String getPartyChatPrefixAlly() { return config.getString("Commands.partychat.Chat_Prefix_Format_Ally", "[[GREEN]](A)[[RESET]]"); } - - public String getAdminChatPrefix() { return config.getString("Commands.adminchat.Chat_Prefix_Format", "[[AQUA]][[[WHITE]]{0}[[AQUA]]]"); } - public boolean getAdminDisplayNames() { return config.getBoolean("Commands.adminchat.Use_Display_Names", true); } public boolean getMatchOfflinePlayers() { return config.getBoolean("Commands.Generic.Match_OfflinePlayers", false); } public long getDatabasePlayerCooldown() { return config.getLong("Commands.Database.Player_Cooldown", 1750); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java index d15d525a9..9943d149e 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java +++ b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java @@ -1,8 +1,10 @@ package com.gmail.nossr50.datatypes.party; import com.gmail.nossr50.chat.SamePartyPredicate; +import com.gmail.nossr50.config.ChatConfig; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; +import com.gmail.nossr50.datatypes.chat.ChatChannel; import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.locale.LocaleLoader; @@ -403,7 +405,7 @@ public class Party { List nearbyPlayerList = getNearMembers(UserManager.getPlayer(player)); - boolean useDisplayNames = Config.getInstance().getPartyDisplayNames(); + boolean useDisplayNames = ChatConfig.getInstance().useDisplayNames(ChatChannel.PARTY); if(isPartyLeaderOfflineOrHidden) { diff --git a/src/main/java/com/gmail/nossr50/runnables/party/PartyChatTask.java b/src/main/java/com/gmail/nossr50/runnables/party/PartyChatTask.java deleted file mode 100644 index aca2d25e1..000000000 --- a/src/main/java/com/gmail/nossr50/runnables/party/PartyChatTask.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.gmail.nossr50.runnables.party; - -import com.gmail.nossr50.config.Config; -import com.gmail.nossr50.datatypes.party.Party; -import com.gmail.nossr50.locale.LocaleLoader; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class PartyChatTask extends BukkitRunnable { - private final Plugin plugin; - - private final Party party; - private final String senderName; - private final String displayName; - private String message; - - public PartyChatTask(Plugin plugin, Party party, String senderName, String displayName, String message) { - this.plugin = plugin; - - this.party = party; - this.senderName = senderName; - this.displayName = displayName; - this.message = message; - } - - @Override - public void run() { - if (Config.getInstance().getPartyChatColorLeaderName() && senderName.equalsIgnoreCase(party.getLeader().getPlayerName())) { - message = message.replaceFirst(Pattern.quote(displayName), ChatColor.GOLD + Matcher.quoteReplacement(displayName) + ChatColor.RESET); - } - - for (Player member : party.getOnlineMembers()) { - member.sendMessage(message); - } - - if (party.getAlly() != null) { - for (Player member : party.getAlly().getOnlineMembers()) { - String allyPrefix = LocaleLoader.formatString(Config.getInstance().getPartyChatPrefixAlly()); - member.sendMessage(allyPrefix + message); - } - } - - plugin.getServer().getConsoleSender().sendMessage(ChatColor.stripColor("[mcMMO] [P]<" + party.getName() + ">" + message)); - } -} diff --git a/src/main/resources/chat.yml b/src/main/resources/chat.yml new file mode 100644 index 000000000..0aa86c7b6 --- /dev/null +++ b/src/main/resources/chat.yml @@ -0,0 +1,20 @@ +# Settings for the chat channels in mcMMO +Chat: + # Turn this off if you don't want mcMMO to process any chat (such as party chat or admin chat) + Enable: true + Channels: + Party: + # Enable or disable party chat + Enable: true + # Whether or not to use the current display name of a player + Use_Display_Names: true + Admin: + # Enable or disable party chat + Enable: true + # Whether or not to use the current display name of a player + Use_Display_Names: true +# CUSTOMIZATION INFORMATION +# If you want to customize the look and feel of chat channels, that is handled through localization which is customizeable. +# You can find information on how to handle that in the link below +# https://mcmmo.org/wiki/Locale +# When editing the locale for chat, do a search in the master text file (your local locale override file is empty at first, read the wiki article above) for "chat" and that should lead you right to the locale entries related to chat \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5ac02a3c4..b9e299eeb 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -575,13 +575,6 @@ Commands: # If true, require players to have a mcmmo.commands.ptp.world.[WorldName] permission # to teleport to, from, or within any given world. World_Based_Permissions: false - partychat: - Chat_Prefix_Format: '[[GREEN]]([[WHITE]]{0}[[GREEN]])' - Use_Display_Names: true - Chat_Prefix_Format_Ally: '[[GREEN]](A)[[RESET]]' - adminchat: - Chat_Prefix_Format: '[[AQUA]][[[WHITE]]{0}[[AQUA]]]' - Use_Display_Names: true # # Settings for particles