From 268b279fdfc5a4cf2ba78e67e2ea0d28e4afee48 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 18 Jun 2024 13:26:21 +0100 Subject: [PATCH] feat: add the ability to disable HuskSync commands --- .../william278/husksync/command/PluginCommand.java | 6 ++++-- .../net/william278/husksync/config/Settings.java | 12 +++++++++++- docs/Config-File.md | 4 +++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/command/PluginCommand.java b/common/src/main/java/net/william278/husksync/command/PluginCommand.java index 4419256d..00ad53c2 100644 --- a/common/src/main/java/net/william278/husksync/command/PluginCommand.java +++ b/common/src/main/java/net/william278/husksync/command/PluginCommand.java @@ -80,7 +80,7 @@ protected ArgumentElement user(@NotNull String name) { return new ArgumentElement<>(name, reader -> { final String username = reader.readString(); return plugin.getDatabase().getUserByName(username).orElseThrow( - () -> CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader) + () -> CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader) ); }, (context, builder) -> { plugin.getOnlineUsers().forEach(u -> builder.suggest(u.getUsername())); @@ -119,7 +119,9 @@ public PluginCommand supply(@NotNull HuskSync plugin) { @NotNull public static PluginCommand[] create(@NotNull HuskSync plugin) { - return Arrays.stream(values()).map(type -> type.supply(plugin)).toArray(PluginCommand[]::new); + return Arrays.stream(values()).map(type -> type.supply(plugin)) + .filter(command -> !plugin.getSettings().isCommandDisabled(command)) + .toArray(PluginCommand[]::new); } } diff --git a/common/src/main/java/net/william278/husksync/config/Settings.java b/common/src/main/java/net/william278/husksync/config/Settings.java index 66207032..67ab01e9 100644 --- a/common/src/main/java/net/william278/husksync/config/Settings.java +++ b/common/src/main/java/net/william278/husksync/config/Settings.java @@ -25,6 +25,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import net.william278.husksync.command.PluginCommand; import net.william278.husksync.data.DataSnapshot; import net.william278.husksync.data.Identifier; import net.william278.husksync.database.Database; @@ -75,6 +76,9 @@ public class Settings { @Comment("Whether to cancel game event packets directly when handling locked players if ProtocolLib or PacketEvents is installed") private boolean cancelPackets = true; + @Comment("Add HuskSync commands to this list to prevent them from being registered (e.g. ['userdata'])") + @Getter(AccessLevel.NONE) + private List disabledCommands = Lists.newArrayList(); // Database settings @Comment("Database settings") @@ -182,7 +186,7 @@ public static class RedisSentinel { } // Synchronization settings - @Comment("Redis settings") + @Comment("Data syncing settings") private SynchronizationSettings synchronization = new SynchronizationSettings(); @Getter @@ -294,4 +298,10 @@ public EventListener.Priority getEventPriority(@NotNull EventListener.ListenerTy } } + public boolean isCommandDisabled(@NotNull PluginCommand command) { + return disabledCommands.stream().map(c -> c.startsWith("/") ? c.substring(1) : c) + .anyMatch(c -> c.equalsIgnoreCase(command.getName()) || command.getAliases().contains(c)); + } + + } diff --git a/docs/Config-File.md b/docs/Config-File.md index 810eaad5..41f1d879 100644 --- a/docs/Config-File.md +++ b/docs/Config-File.md @@ -33,6 +33,8 @@ debug_logging: true enable_plan_hook: true # Whether to cancel game event packets directly when handling locked players if ProtocolLib or PacketEvents is installed cancel_packets: true +# Add HuskSync commands to this list to prevent them from being registered (e.g. ['userdata']) +disabled_commands: [] # Database settings database: # Type of database to use (MYSQL, MARIADB, POSTGRES, MONGO) @@ -76,7 +78,7 @@ redis: # List of host:port pairs nodes: [] password: '' -# Redis settings +# Data syncing settings synchronization: # The data synchronization mode to use (LOCKSTEP or DELAY). LOCKSTEP is recommended for most networks. # Docs: https://william278.net/docs/husksync/sync-modes