From faa6373f4afdbd828e3978daf2e92cb64216d7d1 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Tue, 5 Oct 2021 13:52:03 -0700 Subject: [PATCH] Support components in command permission msgs (#6676) --- patches/api/Adventure.patch | 85 +++++++++++++++++++ ...ault-permission-message-configurable.patch | 15 ++-- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/patches/api/Adventure.patch b/patches/api/Adventure.patch index 4fd28bf841..81d400a1de 100644 --- a/patches/api/Adventure.patch +++ b/patches/api/Adventure.patch @@ -1220,6 +1220,78 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public void setLine(int index, @NotNull String line) throws IndexOutOfBoundsException; /** +diff --git a/src/main/java/org/bukkit/command/Command.java b/src/main/java/org/bukkit/command/Command.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/command/Command.java ++++ b/src/main/java/org/bukkit/command/Command.java +@@ -0,0 +0,0 @@ public abstract class Command { + protected String description; + protected String usageMessage; + private String permission; +- private String permissionMessage; ++ private net.kyori.adventure.text.Component permissionMessage; // Paper + public co.aikar.timings.Timing timings; // Paper + @NotNull public String getTimingName() {return getName();} // Paper + +@@ -0,0 +0,0 @@ public abstract class Command { + + if (permissionMessage == null) { + target.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is a mistake."); +- } else if (permissionMessage.length() != 0) { +- for (String line : permissionMessage.replace("", permission).split("\n")) { +- target.sendMessage(line); +- } ++ // Paper start - use components for permissionMessage ++ } else if (!permissionMessage.equals(net.kyori.adventure.text.Component.empty())) { ++ target.sendMessage(permissionMessage.replaceText(net.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("").replacement(permission).build())); ++ // Paper end + } + + return false; +@@ -0,0 +0,0 @@ public abstract class Command { + * command + * + * @return Permission check failed message ++ * @deprecated use {@link #permissionMessage()} + */ + @Nullable ++ @Deprecated // Paper + public String getPermissionMessage() { +- return permissionMessage; ++ return io.papermc.paper.text.PaperComponents.legacySectionSerializer().serialize(permissionMessage); // Paper + } + + /** +@@ -0,0 +0,0 @@ public abstract class Command { + * @param permissionMessage new permission message, null to indicate + * default message, or an empty string to indicate no message + * @return this command object, for chaining ++ * @deprecated use {@link #permissionMessage(net.kyori.adventure.text.Component)} + */ + @NotNull ++ @Deprecated // Paper + public Command setPermissionMessage(@Nullable String permissionMessage) { +- this.permissionMessage = permissionMessage; ++ this.permissionMessage = io.papermc.paper.text.PaperComponents.legacySectionSerializer().deserializeOrNull(permissionMessage); // Paper + return this; + } + +@@ -0,0 +0,0 @@ public abstract class Command { + this.usageMessage = (usage == null) ? "" : usage; + return this; + } ++ // Paper start ++ public @Nullable net.kyori.adventure.text.Component permissionMessage() { ++ return this.permissionMessage; ++ } ++ ++ public void permissionMessage(@Nullable net.kyori.adventure.text.Component permissionMessage) { ++ this.permissionMessage = permissionMessage; ++ } ++ // Paper end + + public static void broadcastCommandMessage(@NotNull CommandSender source, @NotNull String message) { + broadcastCommandMessage(source, message, true); diff --git a/src/main/java/org/bukkit/command/CommandSender.java b/src/main/java/org/bukkit/command/CommandSender.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/command/CommandSender.java @@ -1339,6 +1411,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @Override default boolean isOp() { +diff --git a/src/main/java/org/bukkit/command/PluginCommandYamlParser.java b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/command/PluginCommandYamlParser.java ++++ b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java +@@ -0,0 +0,0 @@ public class PluginCommandYamlParser { + } + + if (permissionMessage != null) { +- newCmd.setPermissionMessage(permissionMessage.toString()); ++ newCmd.permissionMessage(io.papermc.paper.text.PaperComponents.legacySectionSerializer().deserialize(permissionMessage.toString())); // Paper + } + + pluginCmds.add(newCmd); diff --git a/src/main/java/org/bukkit/command/ProxiedCommandSender.java b/src/main/java/org/bukkit/command/ProxiedCommandSender.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/command/ProxiedCommandSender.java diff --git a/patches/api/Make-the-default-permission-message-configurable.patch b/patches/api/Make-the-default-permission-message-configurable.patch index 308cb38ec9..45e468aee0 100644 --- a/patches/api/Make-the-default-permission-message-configurable.patch +++ b/patches/api/Make-the-default-permission-message-configurable.patch @@ -52,11 +52,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - if (permissionMessage == null) { - target.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is a mistake."); -- } else if (permissionMessage.length() != 0) { -+ // Paper start -+ String permissionMessage = this.permissionMessage != null ? this.permissionMessage : Bukkit.getPermissionMessage(); -+ if (!permissionMessage.isBlank()) { -+ // Paper end - for (String line : permissionMessage.replace("", permission).split("\n")) { - target.sendMessage(line); - } + // Paper start - use components for permissionMessage +- } else if (!permissionMessage.equals(net.kyori.adventure.text.Component.empty())) { ++ net.kyori.adventure.text.Component permissionMessage = this.permissionMessage != null ? this.permissionMessage : io.papermc.paper.text.PaperComponents.legacySectionSerializer().deserialize(Bukkit.getPermissionMessage()); ++ if (!permissionMessage.equals(net.kyori.adventure.text.Component.empty())) { + target.sendMessage(permissionMessage.replaceText(net.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("").replacement(permission).build())); + // Paper end + }