9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/patches/server/0090-Configurable-unknown-command-message.patch
2024-11-10 17:34:53 -05:00

183 lines
11 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Wed, 7 Aug 2024 18:54:01 +0800
Subject: [PATCH] Configurable unknown command message
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
index 91eea05a68d0ace8678fc5071e67cedb18a4386b..64d22c2be510ad759bcd6fe192d1ea8cf6bc9a6f 100644
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
@@ -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()));
- 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());
- 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, 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..d54e1aee7421129b25b7a4ce8027816d76e3c368
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/UnknownCommandMessage.java
@@ -0,0 +1,21 @@
+package org.dreeam.leaf.config.modules.misc;
+
+import org.dreeam.leaf.config.ConfigModules;
+import org.dreeam.leaf.config.EnumConfigCategory;
+
+public class UnknownCommandMessage extends ConfigModules {
+
+ public String getBasePath() {
+ return EnumConfigCategory.MISC.getBaseKeyName() + ".message";
+ }
+
+ 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,
+ placeholder: <detail>, shows detail of the unknown command information.
+ """);
+ }
+}
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index 4dbb109d0526afee99b9190fc256585121aac9b5..3de9968bc87b3dc024e423b382eb611135bf1b2c 100644
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
@@ -191,7 +191,6 @@ public class SpigotConfig
}
public static String whitelistMessage;
- public static String unknownCommandMessage;
public static String serverFullMessage;
public static String outdatedClientMessage = "Outdated client! Please use {0}";
public static String outdatedServerMessage = "Outdated server! I\'m still on {0}";
@@ -208,7 +207,6 @@ public class SpigotConfig
}
SpigotConfig.whitelistMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.whitelist", "You are not whitelisted on this server!" ) );
- SpigotConfig.unknownCommandMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.unknown-command", "Unknown command. Type \"/help\" for help." ) );
SpigotConfig.serverFullMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.server-full", "The server is full!" ) );
SpigotConfig.outdatedClientMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.outdated-client", SpigotConfig.outdatedClientMessage ) );
SpigotConfig.outdatedServerMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.outdated-server", SpigotConfig.outdatedServerMessage ) );