From 1bfa91a98b0225a356b0c4de0c6d692644dd3b0b Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 30 May 2022 16:17:22 +0200 Subject: [PATCH] Prevents editing of dangerous and unimplemented config values --- README.md | 26 +++++++----- .../stargatecommand/StargateCommand.java | 42 ++++++++++++++++++- .../command/CommandConfig.java | 17 ++++++-- .../command/CommandStarGateCommand.java | 16 ++++++- .../command/ConfigTabCompleter.java | 17 +++++++- .../command/StargateCommandTabCompleter.java | 17 +++++++- src/main/resources/plugin.yml | 4 +- 7 files changed, 117 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index bdc304e..35e362c 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,37 @@ > Please visit [our discord](https://discord.gg/mTaHuK6BVa) for all updates and support! # Description + Stargate-Command is an addon for Stargate which adds additional useful commands to the vanilla Stargate experience. #### Features: + * The ability to edit the config file through commands, and automated reloading of changed values ## Dependencies + [The most recent version of Stargate (> 1.0.0.4)](https://www.spigotmc.org/resources/stargate.87978/) # Permissions ### Nodes + ``` stargate.command.config - Gives access to the `/sgc config` command ``` # Changes + [Version 0.1.0] + - Full takeover removing old functionality, and, for now, replacing it with config editing -[Version 0.0.4] - - Fix for Bukkit's direction fix -[Version 0.0.3] - - Added /sgc owner command - - Now requires at least Stargate v0.7.8.0 -[Version 0.0.2] - - Fix an issue with dialing on a specific network - - Change permission checks to use Stargate, this allows proper permission debugging -[Version 0.0.1] - - Initial Release + [Version 0.0.4] +- Fix for Bukkit's direction fix + [Version 0.0.3] +- Added /sgc owner command +- Now requires at least Stargate v0.7.8.0 + [Version 0.0.2] +- Fix an issue with dialing on a specific network +- Change permission checks to use Stargate, this allows proper permission debugging + [Version 0.0.1] +- Initial Release diff --git a/src/main/java/net/knarcraft/stargatecommand/StargateCommand.java b/src/main/java/net/knarcraft/stargatecommand/StargateCommand.java index 0e9c76c..cd282d4 100644 --- a/src/main/java/net/knarcraft/stargatecommand/StargateCommand.java +++ b/src/main/java/net/knarcraft/stargatecommand/StargateCommand.java @@ -1,6 +1,7 @@ package net.knarcraft.stargatecommand; import net.TheDgtl.Stargate.api.StargateAPI; +import net.TheDgtl.Stargate.config.ConfigurationOption; import net.knarcraft.stargatecommand.command.CommandStarGateCommand; import net.knarcraft.stargatecommand.command.StargateCommandTabCompleter; import org.bukkit.command.PluginCommand; @@ -8,14 +9,23 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; +import java.util.ArrayList; +import java.util.List; + /** * The main class for the Stargate-Command add-on */ @SuppressWarnings("unused") public class StargateCommand extends JavaPlugin { + private List bannedConfigOptions; + @Override public void onEnable() { + //Initialize the list of banned configuration options + if (bannedConfigOptions == null) { + initializeBannedConfigOptions(); + } //Get the Stargate API ServicesManager servicesManager = this.getServer().getServicesManager(); RegisteredServiceProvider stargateProvider = servicesManager.getRegistration(StargateAPI.class); @@ -25,8 +35,8 @@ public class StargateCommand extends JavaPlugin { //Register commands PluginCommand stargateCommand = this.getCommand("stargateCommand"); if (stargateCommand != null) { - stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI)); - stargateCommand.setTabCompleter(new StargateCommandTabCompleter()); + stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI, bannedConfigOptions)); + stargateCommand.setTabCompleter(new StargateCommandTabCompleter(bannedConfigOptions)); } } else { throw new IllegalStateException("Unable to hook into Stargate. Make sure the Stargate plugin is installed " + @@ -39,4 +49,32 @@ public class StargateCommand extends JavaPlugin { //Currently, nothing needs to be disabled } + /** + * Initializes the list of banned configuration options + * + *

The banned configuration options are those options that should never be updated manually, and the ones deemed + * too risky to be updated on a running Stargate instance.

+ */ + private void initializeBannedConfigOptions() { + bannedConfigOptions = new ArrayList<>(); + bannedConfigOptions.add(ConfigurationOption.CONFIG_VERSION); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_DATABASE); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_USERNAME); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_ADDRESS); + bannedConfigOptions.add(ConfigurationOption.DATABASE_NAME); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_INSTANCE_NAME); + bannedConfigOptions.add(ConfigurationOption.USING_REMOTE_DATABASE); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_DRIVER); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_PASSWORD); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_PORT); + bannedConfigOptions.add(ConfigurationOption.BUNGEE_USE_SSL); + bannedConfigOptions.add(ConfigurationOption.SHOW_HIKARI_CONFIG); + for (ConfigurationOption option : ConfigurationOption.values()) { + //Configuration options with a null description are unimplemented and should not be used + if (option.getDescription() == null) { + bannedConfigOptions.add(option); + } + } + } + } diff --git a/src/main/java/net/knarcraft/stargatecommand/command/CommandConfig.java b/src/main/java/net/knarcraft/stargatecommand/command/CommandConfig.java index 1d09d04..6adb8db 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/CommandConfig.java @@ -12,6 +12,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.Arrays; +import java.util.List; /** * This command represents the config command for changing config values @@ -19,16 +20,19 @@ import java.util.Arrays; public class CommandConfig implements CommandExecutor { private final ConfigurationAPI configurationAPI; + private final List bannedConfigOptions; /** * Instantiates a new instance of the config command * - * @param configurationAPI

A reference to the Stargate API

+ * @param configurationAPI

A reference to the Stargate API

+ * @param bannedConfigOptions

A list of config options that shouldn't be available

*/ - public CommandConfig(ConfigurationAPI configurationAPI) { + public CommandConfig(ConfigurationAPI configurationAPI, List bannedConfigOptions) { super(); this.configurationAPI = configurationAPI; + this.bannedConfigOptions = bannedConfigOptions; } @Override @@ -45,6 +49,9 @@ public class CommandConfig implements CommandExecutor { ConfigurationOption selectedOption; try { selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase()); + if (bannedConfigOptions.contains(selectedOption)) { + return true; + } } catch (IllegalArgumentException exception) { commandSender.sendMessage("Invalid configuration option specified"); return true; @@ -215,10 +222,12 @@ public class CommandConfig implements CommandExecutor { * @param sender

The command sender to display the config list to

*/ private void displayConfigValues(CommandSender sender) { - sender.sendMessage(ChatColor.GREEN + "Stargate" + ChatColor.GOLD + "Config values:"); + sender.sendMessage(ChatColor.GREEN + "Stargate " + ChatColor.GOLD + "Config values:"); for (ConfigurationOption option : ConfigurationOption.values()) { - sender.sendMessage(getOptionDescription(option)); + if (!bannedConfigOptions.contains(option)) { + sender.sendMessage(getOptionDescription(option)); + } } } diff --git a/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java b/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java index 1839eb5..a7631dd 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java @@ -1,21 +1,32 @@ package net.knarcraft.stargatecommand.command; import net.TheDgtl.Stargate.api.StargateAPI; +import net.TheDgtl.Stargate.config.ConfigurationOption; import org.apache.commons.lang.ArrayUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.List; + /** * This command represents any command which starts with stargate-command (sgc) */ public class CommandStarGateCommand implements CommandExecutor { private final StargateAPI stargateAPI; + private final List bannedConfigOptions; - public CommandStarGateCommand(StargateAPI stargateAPI) { + /** + * Instantiates a new Stargate-command command + * + * @param stargateAPI

A reference to the Stargate API

+ * @param bannedConfigOptions

A list of config options that shouldn't be available

+ */ + public CommandStarGateCommand(StargateAPI stargateAPI, List bannedConfigOptions) { this.stargateAPI = stargateAPI; + this.bannedConfigOptions = bannedConfigOptions; } @Override @@ -24,7 +35,8 @@ public class CommandStarGateCommand implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("config")) { String[] subArgs = (String[]) ArrayUtils.remove(args, 0); - return new CommandConfig(stargateAPI.getConfigurationAPI()).onCommand(commandSender, command, s, subArgs); + return new CommandConfig(stargateAPI.getConfigurationAPI(), bannedConfigOptions).onCommand( + commandSender, command, s, subArgs); } } return false; diff --git a/src/main/java/net/knarcraft/stargatecommand/command/ConfigTabCompleter.java b/src/main/java/net/knarcraft/stargatecommand/command/ConfigTabCompleter.java index 6e86d04..1efe482 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/ConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/ConfigTabCompleter.java @@ -16,11 +16,21 @@ import java.util.List; */ public class ConfigTabCompleter implements TabCompleter { + private final List bannedConfigOptions; private List booleans; private List integers; private List chatColors; private List doubles; + /** + * Instantiates a new config tab completer + * + * @param bannedConfigOptions

A list of config options that shouldn't be available

+ */ + public ConfigTabCompleter(List bannedConfigOptions) { + this.bannedConfigOptions = bannedConfigOptions; + } + @Nullable @Override public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @@ -32,6 +42,9 @@ public class ConfigTabCompleter implements TabCompleter { ConfigurationOption selectedOption; try { selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase()); + if (bannedConfigOptions.contains(selectedOption)) { + return new ArrayList<>(); + } } catch (IllegalArgumentException exception) { return new ArrayList<>(); } @@ -39,7 +52,9 @@ public class ConfigTabCompleter implements TabCompleter { } else { List configOptionNames = new ArrayList<>(); for (ConfigurationOption option : ConfigurationOption.values()) { - configOptionNames.add(option.name()); + if (!bannedConfigOptions.contains(option)) { + configOptionNames.add(option.name()); + } } return filterMatching(configOptionNames, args[0]); } diff --git a/src/main/java/net/knarcraft/stargatecommand/command/StargateCommandTabCompleter.java b/src/main/java/net/knarcraft/stargatecommand/command/StargateCommandTabCompleter.java index a558737..48ec062 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/StargateCommandTabCompleter.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/StargateCommandTabCompleter.java @@ -1,5 +1,6 @@ package net.knarcraft.stargatecommand.command; +import net.TheDgtl.Stargate.config.ConfigurationOption; import org.apache.commons.lang.ArrayUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -11,8 +12,22 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +/** + * A tab completer for the main /sgc command + */ public class StargateCommandTabCompleter implements TabCompleter { + private final List bannedConfigOptions; + + /** + * Instantiates a new Stargate-command tab completer + * + * @param bannedConfigOptions

A list of config options that shouldn't be available

+ */ + public StargateCommandTabCompleter(List bannedConfigOptions) { + this.bannedConfigOptions = bannedConfigOptions; + } + @Override public @Nullable List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] args) { @@ -27,7 +42,7 @@ public class StargateCommandTabCompleter implements TabCompleter { return matchingCommands; } else if (args.length > 1 && args[0].equalsIgnoreCase("config")) { String[] subArgs = (String[]) ArrayUtils.remove(args, 0); - return new ConfigTabCompleter().onTabComplete(commandSender, command, s, subArgs); + return new ConfigTabCompleter(bannedConfigOptions).onTabComplete(commandSender, command, s, subArgs); } else { return new ArrayList<>(); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 46d8af6..580e28d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,11 +3,11 @@ main: net.knarcraft.stargatecommand.StargateCommand version: 0.1.0 description: Command addon for the Stargate plugin for Bukkit author: EpicKnarvik97 -depend: [Stargate] +depend: [ Stargate ] api-version: 1.18 commands: stargatecommand: - aliases: + aliases: - sgc description: The root command for all added commands usage: |