Skip to content

Commit

Permalink
Able to fully customize unknown command message
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Oct 13, 2024
1 parent 397c82b commit 1f7276d
Showing 1 changed file with 123 additions and 22 deletions.
145 changes: 123 additions & 22 deletions patches/server/0079-Configurable-unknown-command-message.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,142 @@ From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Wed, 7 Aug 2024 18:54:01 +0800
Subject: [PATCH] Configurable unknown command message

TODO - Dreeam: total configurable

diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
index 91eea05a68d0ace8678fc5071e67cedb18a4386b..7af159281a69510d2b0787d472f1af39e1225a47 100644
index 91eea05a68d0ace8678fc5071e67cedb18a4386b..64d22c2be510ad759bcd6fe192d1ea8cf6bc9a6f 100644
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
@@ -390,7 +390,14 @@ public class Commands {
@@ -390,33 +390,10 @@ public class Commands {
// Paper start - Add UnknownCommandEvent
final net.kyori.adventure.text.TextComponent.Builder builder = net.kyori.adventure.text.Component.text();
// commandlistenerwrapper.sendFailure(ComponentUtils.fromMessage(commandsyntaxexception.getRawMessage()));
+ // Leaf start - Configurable unknown command message
+ if (!"default".equals(org.dreeam.leaf.config.modules.misc.UnknownCommandMessage.unknownCommandMessage)) {
+ net.kyori.adventure.text.Component message = net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(org.dreeam.leaf.config.modules.misc.UnknownCommandMessage.unknownCommandMessage);
+ builder.append(message);
+ } else {
builder.color(net.kyori.adventure.text.format.NamedTextColor.RED).append(io.papermc.paper.brigadier.PaperBrigadier.componentFromMessage(commandsyntaxexception.getRawMessage()));
+ }
+ // Leaf end - Configurable unknown command message
- builder.color(net.kyori.adventure.text.format.NamedTextColor.RED).append(io.papermc.paper.brigadier.PaperBrigadier.componentFromMessage(commandsyntaxexception.getRawMessage()));
+ final net.kyori.adventure.text.TextComponent message = getUnknownCommandMessage(builder, commandsyntaxexception, label); // Leaf - Configurable unknown command message
// Paper end - Add UnknownCommandEvent
if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
@@ -416,7 +423,7 @@ public class Commands {
.append(net.kyori.adventure.text.Component.newline())
.append(io.papermc.paper.adventure.PaperAdventure.asAdventure(ichatmutablecomponent));
}
- if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
- int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
- MutableComponent ichatmutablecomponent = Component.empty().withStyle(ChatFormatting.GRAY).withStyle((chatmodifier) -> {
- return chatmodifier.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + label)); // CraftBukkit // Paper
- });
-
- if (i > 10) {
- ichatmutablecomponent.append(CommonComponents.ELLIPSIS);
- }
-
- ichatmutablecomponent.append(commandsyntaxexception.getInput().substring(Math.max(0, i - 10), i));
- if (i < commandsyntaxexception.getInput().length()) {
- MutableComponent ichatmutablecomponent1 = Component.literal(commandsyntaxexception.getInput().substring(i)).withStyle(ChatFormatting.RED, ChatFormatting.UNDERLINE);

- ichatmutablecomponent.append((Component) ichatmutablecomponent1);
- }
-
- ichatmutablecomponent.append((Component) Component.translatable("command.context.here").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC));
- // Paper start - Add UnknownCommandEvent
- // commandlistenerwrapper.sendFailure(ichatmutablecomponent);
- builder
- .append(net.kyori.adventure.text.Component.newline())
- .append(io.papermc.paper.adventure.PaperAdventure.asAdventure(ichatmutablecomponent));
- }
- org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(commandlistenerwrapper.getBukkitSender(), s, org.spigotmc.SpigotConfig.unknownCommandMessage.isEmpty() ? null : builder.build());
+ org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(commandlistenerwrapper.getBukkitSender(), s, builder.build()); // Leaf - Configurable unknown command message
+ org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(commandlistenerwrapper.getBukkitSender(), s, message); // Leaf - Configurable unknown command message
org.bukkit.Bukkit.getServer().getPluginManager().callEvent(event);
if (event.message() != null) {
commandlistenerwrapper.sendFailure(io.papermc.paper.adventure.PaperAdventure.asVanilla(event.message()), false);
@@ -691,6 +668,88 @@ public class Commands {
};
}

+ // Leaf start - Configurable unknown command message
+ private static net.kyori.adventure.text.TextComponent getUnknownCommandMessage(
+ net.kyori.adventure.text.TextComponent.Builder builder, CommandSyntaxException commandsyntaxexception, String label
+ ) {
+ String rawMessage = org.dreeam.leaf.config.modules.misc.UnknownCommandMessage.unknownCommandMessage;
+
+ if (!"default".equals(rawMessage)) {
+ final String input = commandsyntaxexception.getInput();
+ final int cursor = commandsyntaxexception.getCursor();
+
+ if (rawMessage.contains("<detail>") && input != null && cursor >= 0) {
+ final int i = Math.min(input.length(), cursor);
+ final net.kyori.adventure.text.TextComponent.Builder detail = net.kyori.adventure.text.Component.text();
+ final net.kyori.adventure.text.Component context = net.kyori.adventure.text.Component.translatable("command.context.here")
+ .color(net.kyori.adventure.text.format.NamedTextColor.RED)
+ .decorate(net.kyori.adventure.text.format.TextDecoration.ITALIC);
+ final net.kyori.adventure.text.event.ClickEvent event = net.kyori.adventure.text.event.ClickEvent.suggestCommand("/" + label);
+
+ detail.color(net.kyori.adventure.text.format.NamedTextColor.GRAY);
+
+ if (i > 10) {
+ detail.append(net.kyori.adventure.text.Component.text("..."));
+ }
+
+ detail.append(net.kyori.adventure.text.Component.text(input.substring(Math.max(0, i - 10), i)));
+ if (i < input.length()) {
+ net.kyori.adventure.text.Component commandInput = net.kyori.adventure.text.Component.text(input.substring(i))
+ .color(net.kyori.adventure.text.format.NamedTextColor.RED)
+ .decorate(net.kyori.adventure.text.format.TextDecoration.UNDERLINED);
+
+ detail.append(commandInput);
+ }
+
+ detail.append(context);
+ detail.clickEvent(event);
+
+ builder.append(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(rawMessage, net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.component("detail", detail.build())));
+ } else {
+ rawMessage = rawMessage.replace("<detail>", "");
+ builder.append(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(rawMessage));
+ }
+
+ return builder.build();
+ }
+
+ return getVanillaUnknownCommandMessage(builder, commandsyntaxexception, label);
+ }
+
+ private static net.kyori.adventure.text.TextComponent getVanillaUnknownCommandMessage(
+ net.kyori.adventure.text.TextComponent.Builder builder, CommandSyntaxException commandsyntaxexception, String label
+ ) {
+ builder.color(net.kyori.adventure.text.format.NamedTextColor.RED).append(io.papermc.paper.brigadier.PaperBrigadier.componentFromMessage(commandsyntaxexception.getRawMessage()));
+
+ if (commandsyntaxexception.getInput() != null && commandsyntaxexception.getCursor() >= 0) {
+ int i = Math.min(commandsyntaxexception.getInput().length(), commandsyntaxexception.getCursor());
+ MutableComponent ichatmutablecomponent = Component.empty().withStyle(ChatFormatting.GRAY).withStyle((chatmodifier) -> {
+ return chatmodifier.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + label)); // CraftBukkit // Paper
+ });
+
+ if (i > 10) {
+ ichatmutablecomponent.append(CommonComponents.ELLIPSIS);
+ }
+
+ ichatmutablecomponent.append(commandsyntaxexception.getInput().substring(Math.max(0, i - 10), i));
+ if (i < commandsyntaxexception.getInput().length()) {
+ MutableComponent ichatmutablecomponent1 = Component.literal(commandsyntaxexception.getInput().substring(i)).withStyle(ChatFormatting.RED, ChatFormatting.UNDERLINE);
+
+ ichatmutablecomponent.append(ichatmutablecomponent1);
+ }
+
+ ichatmutablecomponent.append(Component.translatable("command.context.here").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC));
+ // Paper start - Add UnknownCommandEvent
+ // commandlistenerwrapper.sendFailure(ichatmutablecomponent);
+ builder
+ .append(net.kyori.adventure.text.Component.newline())
+ .append(io.papermc.paper.adventure.PaperAdventure.asAdventure(ichatmutablecomponent));
+ }
+
+ return builder.build();
+ }
+ // Leaf end - Configurable unknown command message
+
public static void validate() {
CommandBuildContext commandbuildcontext = Commands.createValidationContext(VanillaRegistries.createLookup());
com.mojang.brigadier.CommandDispatcher<CommandSourceStack> com_mojang_brigadier_commanddispatcher = (new Commands(Commands.CommandSelection.ALL, commandbuildcontext)).getDispatcher();
diff --git a/src/main/java/org/dreeam/leaf/config/modules/misc/UnknownCommandMessage.java b/src/main/java/org/dreeam/leaf/config/modules/misc/UnknownCommandMessage.java
new file mode 100644
index 0000000000000000000000000000000000000000..cfa68915552899ce6ec0b202e1d6cf8a1ca0a777
index 0000000000000000000000000000000000000000..d54e1aee7421129b25b7a4ce8027816d76e3c368
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/UnknownCommandMessage.java
@@ -0,0 +1,20 @@
@@ -0,0 +1,21 @@
+package org.dreeam.leaf.config.modules.misc;
+
+import org.dreeam.leaf.config.ConfigModules;
Expand All @@ -50,12 +150,13 @@ index 0000000000000000000000000000000000000000..cfa68915552899ce6ec0b202e1d6cf8a
+ return EnumConfigCategory.MISC.getBaseKeyName() + ".message";
+ }
+
+ public static String unknownCommandMessage = "<red><lang:command.unknown.command>";
+ public static String unknownCommandMessage = "<red><lang:command.unknown.command><newline><detail>";
+
+ @Override
+ public void onLoaded() {
+ unknownCommandMessage = config.getString(getBasePath() + ".unknown-command", unknownCommandMessage, """
+ Unknown command message, using MiniMessage format, set to "default" to use vanilla message.
+ Unknown command message, using MiniMessage format, set to "default" to use vanilla message,
+ placeholder: <detail>, shows detail of the unknown command information.
+ """);
+ }
+}
Expand Down

0 comments on commit 1f7276d

Please sign in to comment.