Add command disabling

This commit is contained in:
William committed 2022-08-22 17:29:58 +01:00
1 parent 632d43d131
commit 6cd2a23770
7 files changed
+59 -12

No files matched your search

@@ -4,6 +4,8 @@ import io.papermc.lib.PaperLib;
import net.william278.annotaml.Annotaml; import net.william278.annotaml.Annotaml;
import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.BukkitCommand;
import net.william278.huskhomes.command.BukkitCommandType; import net.william278.huskhomes.command.BukkitCommandType;
import net.william278.huskhomes.command.ConsoleExecutable;
import net.william278.huskhomes.command.DisabledCommand;
import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.config.Spawn; import net.william278.huskhomes.config.Spawn;
@@ -172,12 +174,26 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}))); })));
// Register commands // Register commands
for (final BukkitCommandType bukkitCommandType : BukkitCommandType.values()) { Arrays.stream(BukkitCommandType.values()).forEach(commandType -> {
final PluginCommand pluginCommand = getCommand(bukkitCommandType.commandBase.command); final PluginCommand pluginCommand = getCommand(commandType.commandBase.command);
if (pluginCommand != null) { if (pluginCommand == null) {
new BukkitCommand(bukkitCommandType.commandBase, this).register(pluginCommand); return;
} }
}
// If the command is disabled, use the disabled CommandBase
if (settings.disabledCommands.stream().anyMatch(disabledCommand -> {
final String command = (disabledCommand.startsWith("/") ? disabledCommand.substring(1) : disabledCommand);
return command.equalsIgnoreCase(commandType.commandBase.command) ||
Arrays.stream(commandType.commandBase.aliases)
.anyMatch(alias -> alias.equalsIgnoreCase(command));
})) {
new BukkitCommand(new DisabledCommand(this), this).register(pluginCommand);
return;
}
// Otherwise, register the command
new BukkitCommand(commandType.commandBase, this).register(pluginCommand);
});
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands."); getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands.");
// Hook into bStats metrics // Hook into bStats metrics
@@ -41,7 +41,7 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
pluginCommand.setDescription(command.getDescription()); pluginCommand.setDescription(command.getDescription());
pluginCommand.setExecutor(this); pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this); pluginCommand.setTabCompleter(this);
} catch (Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -3,6 +3,10 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.BukkitHuskHomes; import net.william278.huskhomes.BukkitHuskHomes;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* Commands available on the Bukkit HuskHomes implementation * Commands available on the Bukkit HuskHomes implementation
*/ */
@@ -16,18 +16,18 @@ public abstract class CommandBase {
@NotNull @NotNull
public final String command; public final String command;
/**
* Alias input strings for this command
*/
@NotNull
public final String[] aliases;
/** /**
* The permission node required to use this command * The permission node required to use this command
*/ */
@NotNull @NotNull
protected final String permission; protected final String permission;
/**
* Alias input strings for this command
*/
@NotNull
protected final String[] aliases;
/** /**
* Instance of the implementing plugin * Instance of the implementing plugin
*/ */
@@ -0,0 +1,18 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class DisabledCommand extends CommandBase {
public DisabledCommand(@NotNull HuskHomes implementor) {
super("", Permission.COMMAND_DISABLED_MESSAGE, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
plugin.getLocales().getLocale("error_command_disabled").ifPresent(onlineUser::sendMessage);
}
}
@@ -5,6 +5,7 @@ import net.william278.annotaml.KeyPath;
import net.william278.annotaml.YamlFile; import net.william278.annotaml.YamlFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@@ -205,6 +206,10 @@ public class Settings {
return Optional.empty(); return Optional.empty();
} }
// Disabled commands
@KeyPath("disabled_commands")
public List<String> disabledCommands = Collections.emptyList();
@SuppressWarnings("unused") @SuppressWarnings("unused")
public Settings() { public Settings() {
} }
@@ -27,6 +27,10 @@ public enum Permission {
/* /*
* Command permission nodes * Command permission nodes
*/ */
/**
* Lets the user view the "disabled command" message for disabled commands
*/
COMMAND_DISABLED_MESSAGE("huskhomes.disabled_command_message", DefaultAccess.EVERYONE),
/** /**
* Lets the user teleport to their homes ({@code /home [home_name]}) * Lets the user teleport to their homes ({@code /home [home_name]})