From b42192bdc9f884bb4a9f324adf90ef02b6ef2f17 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 20 Nov 2024 20:20:44 +0100 Subject: [PATCH] Add configuration validation and unknown command handler Introduced a configuration validation system using `GsonFile` and added a new `PluginConfig` class. Enhanced the `ReloadCommand` to reload configuration and implemented a handler for unknown commands to display custom error messages based on the configuration. --- .../thenextlvl/commander/paper/CommanderPlugin.java | 8 ++++++++ .../commander/paper/command/ReloadCommand.java | 8 +++++++- .../commander/paper/config/PluginConfig.java | 10 ++++++++++ .../commander/paper/listener/CommandListener.java | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 paper/src/main/java/net/thenextlvl/commander/paper/config/PluginConfig.java diff --git a/paper/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java b/paper/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java index 7e55995..4fad3a6 100644 --- a/paper/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java +++ b/paper/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java @@ -1,6 +1,9 @@ package net.thenextlvl.commander.paper; +import core.file.FileIO; +import core.file.format.GsonFile; import core.i18n.file.ComponentBundle; +import core.io.IO; import lombok.Getter; import lombok.experimental.Accessors; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -8,6 +11,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.thenextlvl.commander.Commander; import net.thenextlvl.commander.paper.command.CommanderCommand; +import net.thenextlvl.commander.paper.config.PluginConfig; import net.thenextlvl.commander.paper.implementation.PaperCommandFinder; import net.thenextlvl.commander.paper.implementation.PaperCommandRegistry; import net.thenextlvl.commander.paper.implementation.PaperPermissionOverride; @@ -38,6 +42,10 @@ public class CommanderPlugin extends JavaPlugin implements Commander { TagResolver.standard(), Placeholder.component("prefix", bundle.component(Locale.US, "prefix")) )).build()); + private final FileIO config = new GsonFile<>( + IO.of(getDataFolder(), "config.json"), + new PluginConfig(true) + ).validate().save(); private final PaperCommandFinder commandFinder = new PaperCommandFinder(this); private final PaperCommandRegistry commandRegistry = new PaperCommandRegistry(this); diff --git a/paper/src/main/java/net/thenextlvl/commander/paper/command/ReloadCommand.java b/paper/src/main/java/net/thenextlvl/commander/paper/command/ReloadCommand.java index faa443b..b5aae96 100644 --- a/paper/src/main/java/net/thenextlvl/commander/paper/command/ReloadCommand.java +++ b/paper/src/main/java/net/thenextlvl/commander/paper/command/ReloadCommand.java @@ -24,9 +24,10 @@ class ReloadCommand { private int reload(CommandContext context) { var sender = context.getSource().getSender(); try { + var config = reloadConfig(); var commands = plugin.commandRegistry().reload(sender); var permissions = plugin.permissionOverride().reload(sender); - var success = commands || permissions; + var success = config || commands || permissions; if (success && sender instanceof Player player) player.updateCommands(); var message = success ? "command.reload.success" : "nothing.changed"; plugin.bundle().sendMessage(sender, message); @@ -37,4 +38,9 @@ private int reload(CommandContext context) { return 0; } } + + private boolean reloadConfig() { + var previous = plugin.config().getRoot(); + return !plugin.config().reload().getRoot().equals(previous); + } } diff --git a/paper/src/main/java/net/thenextlvl/commander/paper/config/PluginConfig.java b/paper/src/main/java/net/thenextlvl/commander/paper/config/PluginConfig.java new file mode 100644 index 0000000..0c05f2a --- /dev/null +++ b/paper/src/main/java/net/thenextlvl/commander/paper/config/PluginConfig.java @@ -0,0 +1,10 @@ +package net.thenextlvl.commander.paper.config; + +import com.google.gson.annotations.SerializedName; +import org.jspecify.annotations.NullMarked; + +@NullMarked +public record PluginConfig( + @SerializedName("unknown-command-message") boolean unknownCommandMessage +) { +} diff --git a/paper/src/main/java/net/thenextlvl/commander/paper/listener/CommandListener.java b/paper/src/main/java/net/thenextlvl/commander/paper/listener/CommandListener.java index aa59aa2..8ff6747 100644 --- a/paper/src/main/java/net/thenextlvl/commander/paper/listener/CommandListener.java +++ b/paper/src/main/java/net/thenextlvl/commander/paper/listener/CommandListener.java @@ -6,6 +6,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.player.PlayerCommandSendEvent; import org.jspecify.annotations.NullMarked; @@ -19,4 +20,16 @@ public void onCommandSend(PlayerCommandSendEvent event) { if (event.getPlayer().permissionValue("commander.bypass").equals(TriState.TRUE)) return; event.getCommands().removeAll(plugin.commandRegistry().hiddenCommands()); } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onUnknownCommand(UnknownCommandEvent event) { + if (!plugin.config().getRoot().unknownCommandMessage()) return; + var split = event.getCommandLine().split(" ", 2); + var command = plugin.getServer().getCommandMap().getCommand(split[0]); + if (command != null + && !event.getCommandLine().equals(split[0]) + && command.testPermissionSilent(event.getSender())) + return; + event.message(plugin.bundle().component(event.getSender(), "command.unknown")); + } }