From 8c0fa5e45b085a585a32cdc9d634484649245144 Mon Sep 17 00:00:00 2001 From: LOOHP Date: Tue, 18 Aug 2020 13:45:19 +0800 Subject: [PATCH] 2.7.1 Added Proper formatting from command actions --- pom.xml | 2 +- .../loohp/interactivechat/ConfigManager.java | 7 ++- .../interactivechat/InteractiveChat.java | 3 + .../Modules/CommandsDisplay.java | 58 ++++++++++++++++++- src/config.yml | 10 +++- src/config_legacy.yml | 10 +++- src/config_old.yml | 10 +++- 7 files changed, 88 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index fc0ffaa7..9ccd666e 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 InteractiveChat InteractiveChat - 2.7.0 + 2.7.1 src diff --git a/src/com/loohp/interactivechat/ConfigManager.java b/src/com/loohp/interactivechat/ConfigManager.java index 0977b614..3dfad69a 100644 --- a/src/com/loohp/interactivechat/ConfigManager.java +++ b/src/com/loohp/interactivechat/ConfigManager.java @@ -180,8 +180,11 @@ public static void loadConfig() { InteractiveChat.clickableCommands = getConfig().getBoolean("Commands.Enabled"); String[] commandsFormat = getConfig().getString("Commands.Format").split("\\{Command\\}"); - InteractiveChat.clickableCommandsPrefix = commandsFormat[0]; - InteractiveChat.clickableCommandsSuffix = commandsFormat[commandsFormat.length - 1]; + InteractiveChat.clickableCommandsPrefix = ChatColorUtils.translateAlternateColorCodes('&', commandsFormat[0]); + InteractiveChat.clickableCommandsSuffix = ChatColorUtils.translateAlternateColorCodes('&', commandsFormat[commandsFormat.length - 1]); InteractiveChat.clickableCommandsAction = ClickEvent.Action.valueOf(getConfig().getString("Commands.Action")); + InteractiveChat.clickableCommandsFormat = ChatColorUtils.translateAlternateColorCodes('&', getConfig().getString("Commands.Text")); + InteractiveChat.clickableCommandsHoverText = ChatColorUtils.translateAlternateColorCodes('&', String.join("\n", getConfig().getStringList("Commands.HoverMessage"))); + InteractiveChat.clickableCommandsEnforceColors = getConfig().getBoolean("Commands.EnforceReplaceTextColor"); } } \ No newline at end of file diff --git a/src/com/loohp/interactivechat/InteractiveChat.java b/src/com/loohp/interactivechat/InteractiveChat.java index 4a2ceed5..ee6a0c14 100644 --- a/src/com/loohp/interactivechat/InteractiveChat.java +++ b/src/com/loohp/interactivechat/InteractiveChat.java @@ -105,6 +105,9 @@ public class InteractiveChat extends JavaPlugin { public static String clickableCommandsPrefix = "["; public static String clickableCommandsSuffix = "]"; public static ClickEvent.Action clickableCommandsAction = ClickEvent.Action.SUGGEST_COMMAND; + public static String clickableCommandsFormat = ""; + public static String clickableCommandsHoverText = null; + public static boolean clickableCommandsEnforceColors = true; public static String NoPermission = "&cYou do not have permission to use that command!"; public static String InvExpired = "&cThis inventory view has expired!"; diff --git a/src/com/loohp/interactivechat/Modules/CommandsDisplay.java b/src/com/loohp/interactivechat/Modules/CommandsDisplay.java index f8148d08..696c8fdf 100644 --- a/src/com/loohp/interactivechat/Modules/CommandsDisplay.java +++ b/src/com/loohp/interactivechat/Modules/CommandsDisplay.java @@ -11,10 +11,12 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; public class CommandsDisplay { + @SuppressWarnings("deprecation") public static BaseComponent process(BaseComponent basecomponent) { List basecomponentlist = CustomStringUtils.loadExtras(basecomponent); List newlist = new ArrayList(); @@ -37,16 +39,44 @@ public static BaseComponent process(BaseComponent basecomponent) { before.setText(before.getText().substring(0, end)); StringBuilder cmd = new StringBuilder(); List cmdCompList = new ArrayList(); + + String[] formmat = InteractiveChat.clickableCommandsFormat.split("\\{Command\\}"); + String prepend = formmat[0]; + String color = ChatColorUtils.getLastColors(prepend); + String append = formmat[formmat.length - 1]; + for (int u = indexOfParsingStart; u < i; u++) { BaseComponent part = basecomponentlist.get(u); Bukkit.getConsoleSender().sendMessage(ChatColorUtils.stripColor(part.toLegacyText())); + if (InteractiveChat.clickableCommandsEnforceColors) { + if (part instanceof TextComponent) { + ((TextComponent) part).setText(color + ChatColorUtils.stripColor(((TextComponent) part).getText())); + } else { + part = ChatColorUtils.applyColor(part, color); + } + } cmdCompList.add(part); cmd.append(ChatColorUtils.stripColor(part.toLegacyText())); } + if (InteractiveChat.clickableCommandsEnforceColors) { + before.setText(color + ChatColorUtils.stripColor(before.getText())); + } cmdCompList.add(before); + //Bukkit.getConsoleSender().sendMessage(((TextComponent) before).getText().replace("§", "&")); cmd.append(ChatColorUtils.stripColor(before.toLegacyText())); + + HoverEvent hover = null; + if (InteractiveChat.clickableCommandsHoverText != null) { + hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {new TextComponent(InteractiveChat.clickableCommandsHoverText)}); + } ClickEvent click = new ClickEvent(InteractiveChat.clickableCommandsAction, cmd.toString()); + cmdCompList.add(0, new TextComponent(prepend)); + cmdCompList.add(new TextComponent(append)); + for (BaseComponent each : cmdCompList) { + if (hover != null) { + each.setHoverEvent(hover); + } each.setClickEvent(click); newlist.add(each); } @@ -81,15 +111,37 @@ public static BaseComponent process(BaseComponent basecomponent) { if (i + 1 == basecomponentlist.size()) { StringBuilder cmd = new StringBuilder(); List cmdCompList = new ArrayList(); - for (int u = indexOfParsingStart; u < i; u++) { + String[] formmat = InteractiveChat.clickableCommandsFormat.split("\\{Command\\}"); + String prepend = formmat[0]; + String color = ChatColorUtils.getLastColors(prepend); + String append = formmat[formmat.length - 1]; + + for (int u = indexOfParsingStart; u <= i; u++) { BaseComponent part = basecomponentlist.get(u); + Bukkit.getConsoleSender().sendMessage(ChatColorUtils.stripColor(part.toLegacyText())); + if (InteractiveChat.clickableCommandsEnforceColors) { + if (part instanceof TextComponent) { + ((TextComponent) part).setText(color + ChatColorUtils.stripColor(((TextComponent) part).getText())); + } else { + part = ChatColorUtils.applyColor(part, color); + } + } cmdCompList.add(part); cmd.append(ChatColorUtils.stripColor(part.toLegacyText())); } - cmdCompList.add(base); - cmd.append(ChatColorUtils.stripColor(base.toLegacyText())); + + HoverEvent hover = null; + if (InteractiveChat.clickableCommandsHoverText != null) { + hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {new TextComponent(InteractiveChat.clickableCommandsHoverText)}); + } ClickEvent click = new ClickEvent(InteractiveChat.clickableCommandsAction, cmd.toString()); + cmdCompList.add(0, new TextComponent(prepend)); + cmdCompList.add(new TextComponent(append)); + for (BaseComponent each : cmdCompList) { + if (hover != null) { + each.setHoverEvent(hover); + } each.setClickEvent(click); newlist.add(each); } diff --git a/src/config.yml b/src/config.yml index 8f1bd83f..d84b2979 100644 --- a/src/config.yml +++ b/src/config.yml @@ -77,15 +77,21 @@ Settings: Commands: #Whether or not to make commands displayed in chat clickable Enabled: true - + #The formatting to trigger the command display function Format: "[{Command}]" - + #The text to replace the matched section of the message + #Use "{Command}" for the command + Text: "&b[&e{Command}&b]" + EnforceReplaceTextColor: true #What happens when the player clicks the command #List of actions: #RUN_COMMAND #SUGGEST_COMMAND Action: SUGGEST_COMMAND + HoverMessage: + - "&eClick to use command!" + Chat: #Convert alternate color code character #Only one character is allowed, leave blank to disable this feature diff --git a/src/config_legacy.yml b/src/config_legacy.yml index 54d82b62..0cacba7c 100644 --- a/src/config_legacy.yml +++ b/src/config_legacy.yml @@ -72,15 +72,21 @@ Settings: Commands: #Whether or not to make commands displayed in chat clickable Enabled: true - + #The formatting to trigger the command display function Format: "[{Command}]" - + #The text to replace the matched section of the message + #Use "{Command}" for the command + Text: "&b[&e{Command}&b]" + EnforceReplaceTextColor: true #What happens when the player clicks the command #List of actions: #RUN_COMMAND #SUGGEST_COMMAND Action: SUGGEST_COMMAND + HoverMessage: + - "&eClick to use command!" + Chat: #Enable the mention title and sound AllowMention: true diff --git a/src/config_old.yml b/src/config_old.yml index 26b65e37..daa6c3fe 100644 --- a/src/config_old.yml +++ b/src/config_old.yml @@ -72,15 +72,21 @@ Settings: Commands: #Whether or not to make commands displayed in chat clickable Enabled: true - + #The formatting to trigger the command display function Format: "[{Command}]" - + #The text to replace the matched section of the message + #Use "{Command}" for the command + Text: "&b[&e{Command}&b]" + EnforceReplaceTextColor: true #What happens when the player clicks the command #List of actions: #RUN_COMMAND #SUGGEST_COMMAND Action: SUGGEST_COMMAND + HoverMessage: + - "&eClick to use command!" + Chat: #Enable the mention title and sound AllowMention: true