Prevents editing of dangerous and unimplemented config values

This commit is contained in:
2022-05-30 16:17:22 +02:00
parent fb080c21c4
commit 1bfa91a98b
7 changed files with 117 additions and 22 deletions

View File

@@ -1,31 +1,37 @@
> Please visit [our discord](https://discord.gg/mTaHuK6BVa) for all updates and support! > Please visit [our discord](https://discord.gg/mTaHuK6BVa) for all updates and support!
# Description # Description
Stargate-Command is an addon for Stargate which adds additional useful commands to the vanilla Stargate experience. Stargate-Command is an addon for Stargate which adds additional useful commands to the vanilla Stargate experience.
#### Features: #### Features:
* The ability to edit the config file through commands, and automated reloading of changed values * The ability to edit the config file through commands, and automated reloading of changed values
## Dependencies ## Dependencies
[The most recent version of Stargate (> 1.0.0.4)](https://www.spigotmc.org/resources/stargate.87978/) [The most recent version of Stargate (> 1.0.0.4)](https://www.spigotmc.org/resources/stargate.87978/)
# Permissions # Permissions
### Nodes ### Nodes
``` ```
stargate.command.config - Gives access to the `/sgc config` command stargate.command.config - Gives access to the `/sgc config` command
``` ```
# Changes # Changes
[Version 0.1.0] [Version 0.1.0]
- Full takeover removing old functionality, and, for now, replacing it with config editing - Full takeover removing old functionality, and, for now, replacing it with config editing
[Version 0.0.4] [Version 0.0.4]
- Fix for Bukkit's direction fix - Fix for Bukkit's direction fix
[Version 0.0.3] [Version 0.0.3]
- Added /sgc owner <player> command - Added /sgc owner <player> command
- Now requires at least Stargate v0.7.8.0 - Now requires at least Stargate v0.7.8.0
[Version 0.0.2] [Version 0.0.2]
- Fix an issue with dialing on a specific network - Fix an issue with dialing on a specific network
- Change permission checks to use Stargate, this allows proper permission debugging - Change permission checks to use Stargate, this allows proper permission debugging
[Version 0.0.1] [Version 0.0.1]
- Initial Release - Initial Release

View File

@@ -1,6 +1,7 @@
package net.knarcraft.stargatecommand; package net.knarcraft.stargatecommand;
import net.TheDgtl.Stargate.api.StargateAPI; import net.TheDgtl.Stargate.api.StargateAPI;
import net.TheDgtl.Stargate.config.ConfigurationOption;
import net.knarcraft.stargatecommand.command.CommandStarGateCommand; import net.knarcraft.stargatecommand.command.CommandStarGateCommand;
import net.knarcraft.stargatecommand.command.StargateCommandTabCompleter; import net.knarcraft.stargatecommand.command.StargateCommandTabCompleter;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
@@ -8,14 +9,23 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
/** /**
* The main class for the Stargate-Command add-on * The main class for the Stargate-Command add-on
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class StargateCommand extends JavaPlugin { public class StargateCommand extends JavaPlugin {
private List<ConfigurationOption> bannedConfigOptions;
@Override @Override
public void onEnable() { public void onEnable() {
//Initialize the list of banned configuration options
if (bannedConfigOptions == null) {
initializeBannedConfigOptions();
}
//Get the Stargate API //Get the Stargate API
ServicesManager servicesManager = this.getServer().getServicesManager(); ServicesManager servicesManager = this.getServer().getServicesManager();
RegisteredServiceProvider<StargateAPI> stargateProvider = servicesManager.getRegistration(StargateAPI.class); RegisteredServiceProvider<StargateAPI> stargateProvider = servicesManager.getRegistration(StargateAPI.class);
@@ -25,8 +35,8 @@ public class StargateCommand extends JavaPlugin {
//Register commands //Register commands
PluginCommand stargateCommand = this.getCommand("stargateCommand"); PluginCommand stargateCommand = this.getCommand("stargateCommand");
if (stargateCommand != null) { if (stargateCommand != null) {
stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI)); stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI, bannedConfigOptions));
stargateCommand.setTabCompleter(new StargateCommandTabCompleter()); stargateCommand.setTabCompleter(new StargateCommandTabCompleter(bannedConfigOptions));
} }
} else { } else {
throw new IllegalStateException("Unable to hook into Stargate. Make sure the Stargate plugin is installed " + 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 //Currently, nothing needs to be disabled
} }
/**
* Initializes the list of banned configuration options
*
* <p>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.</p>
*/
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);
}
}
}
} }

View File

@@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* This command represents the config command for changing config values * This command represents the config command for changing config values
@@ -19,16 +20,19 @@ import java.util.Arrays;
public class CommandConfig implements CommandExecutor { public class CommandConfig implements CommandExecutor {
private final ConfigurationAPI configurationAPI; private final ConfigurationAPI configurationAPI;
private final List<ConfigurationOption> bannedConfigOptions;
/** /**
* Instantiates a new instance of the config command * Instantiates a new instance of the config command
* *
* @param configurationAPI <p>A reference to the Stargate API</p> * @param configurationAPI <p>A reference to the Stargate API</p>
* @param bannedConfigOptions <p>A list of config options that shouldn't be available</p>
*/ */
public CommandConfig(ConfigurationAPI configurationAPI) { public CommandConfig(ConfigurationAPI configurationAPI, List<ConfigurationOption> bannedConfigOptions) {
super(); super();
this.configurationAPI = configurationAPI; this.configurationAPI = configurationAPI;
this.bannedConfigOptions = bannedConfigOptions;
} }
@Override @Override
@@ -45,6 +49,9 @@ public class CommandConfig implements CommandExecutor {
ConfigurationOption selectedOption; ConfigurationOption selectedOption;
try { try {
selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase()); selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase());
if (bannedConfigOptions.contains(selectedOption)) {
return true;
}
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
commandSender.sendMessage("Invalid configuration option specified"); commandSender.sendMessage("Invalid configuration option specified");
return true; return true;
@@ -215,10 +222,12 @@ public class CommandConfig implements CommandExecutor {
* @param sender <p>The command sender to display the config list to</p> * @param sender <p>The command sender to display the config list to</p>
*/ */
private void displayConfigValues(CommandSender sender) { 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()) { for (ConfigurationOption option : ConfigurationOption.values()) {
sender.sendMessage(getOptionDescription(option)); if (!bannedConfigOptions.contains(option)) {
sender.sendMessage(getOptionDescription(option));
}
} }
} }

View File

@@ -1,21 +1,32 @@
package net.knarcraft.stargatecommand.command; package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.api.StargateAPI; import net.TheDgtl.Stargate.api.StargateAPI;
import net.TheDgtl.Stargate.config.ConfigurationOption;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* This command represents any command which starts with stargate-command (sgc) * This command represents any command which starts with stargate-command (sgc)
*/ */
public class CommandStarGateCommand implements CommandExecutor { public class CommandStarGateCommand implements CommandExecutor {
private final StargateAPI stargateAPI; private final StargateAPI stargateAPI;
private final List<ConfigurationOption> bannedConfigOptions;
public CommandStarGateCommand(StargateAPI stargateAPI) { /**
* Instantiates a new Stargate-command command
*
* @param stargateAPI <p>A reference to the Stargate API</p>
* @param bannedConfigOptions <p>A list of config options that shouldn't be available</p>
*/
public CommandStarGateCommand(StargateAPI stargateAPI, List<ConfigurationOption> bannedConfigOptions) {
this.stargateAPI = stargateAPI; this.stargateAPI = stargateAPI;
this.bannedConfigOptions = bannedConfigOptions;
} }
@Override @Override
@@ -24,7 +35,8 @@ public class CommandStarGateCommand implements CommandExecutor {
if (args.length > 0) { if (args.length > 0) {
if (args[0].equalsIgnoreCase("config")) { if (args[0].equalsIgnoreCase("config")) {
String[] subArgs = (String[]) ArrayUtils.remove(args, 0); 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; return false;

View File

@@ -16,11 +16,21 @@ import java.util.List;
*/ */
public class ConfigTabCompleter implements TabCompleter { public class ConfigTabCompleter implements TabCompleter {
private final List<ConfigurationOption> bannedConfigOptions;
private List<String> booleans; private List<String> booleans;
private List<String> integers; private List<String> integers;
private List<String> chatColors; private List<String> chatColors;
private List<String> doubles; private List<String> doubles;
/**
* Instantiates a new config tab completer
*
* @param bannedConfigOptions <p>A list of config options that shouldn't be available</p>
*/
public ConfigTabCompleter(List<ConfigurationOption> bannedConfigOptions) {
this.bannedConfigOptions = bannedConfigOptions;
}
@Nullable @Nullable
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@@ -32,6 +42,9 @@ public class ConfigTabCompleter implements TabCompleter {
ConfigurationOption selectedOption; ConfigurationOption selectedOption;
try { try {
selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase()); selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase());
if (bannedConfigOptions.contains(selectedOption)) {
return new ArrayList<>();
}
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
return new ArrayList<>(); return new ArrayList<>();
} }
@@ -39,7 +52,9 @@ public class ConfigTabCompleter implements TabCompleter {
} else { } else {
List<String> configOptionNames = new ArrayList<>(); List<String> configOptionNames = new ArrayList<>();
for (ConfigurationOption option : ConfigurationOption.values()) { for (ConfigurationOption option : ConfigurationOption.values()) {
configOptionNames.add(option.name()); if (!bannedConfigOptions.contains(option)) {
configOptionNames.add(option.name());
}
} }
return filterMatching(configOptionNames, args[0]); return filterMatching(configOptionNames, args[0]);
} }

View File

@@ -1,5 +1,6 @@
package net.knarcraft.stargatecommand.command; package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.config.ConfigurationOption;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -11,8 +12,22 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* A tab completer for the main /sgc command
*/
public class StargateCommandTabCompleter implements TabCompleter { public class StargateCommandTabCompleter implements TabCompleter {
private final List<ConfigurationOption> bannedConfigOptions;
/**
* Instantiates a new Stargate-command tab completer
*
* @param bannedConfigOptions <p>A list of config options that shouldn't be available</p>
*/
public StargateCommandTabCompleter(List<ConfigurationOption> bannedConfigOptions) {
this.bannedConfigOptions = bannedConfigOptions;
}
@Override @Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command,
@NotNull String s, @NotNull String[] args) { @NotNull String s, @NotNull String[] args) {
@@ -27,7 +42,7 @@ public class StargateCommandTabCompleter implements TabCompleter {
return matchingCommands; return matchingCommands;
} else if (args.length > 1 && args[0].equalsIgnoreCase("config")) { } else if (args.length > 1 && args[0].equalsIgnoreCase("config")) {
String[] subArgs = (String[]) ArrayUtils.remove(args, 0); 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 { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@@ -3,11 +3,11 @@ main: net.knarcraft.stargatecommand.StargateCommand
version: 0.1.0 version: 0.1.0
description: Command addon for the Stargate plugin for Bukkit description: Command addon for the Stargate plugin for Bukkit
author: EpicKnarvik97 author: EpicKnarvik97
depend: [Stargate] depend: [ Stargate ]
api-version: 1.18 api-version: 1.18
commands: commands:
stargatecommand: stargatecommand:
aliases: aliases:
- sgc - sgc
description: The root command for all added commands description: The root command for all added commands
usage: | usage: |