Broken commit for supporting legacy Stargate
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
target/
|
||||
.idea/
|
||||
*.iml
|
||||
6
pom.xml
6
pom.xml
@@ -30,7 +30,7 @@
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.18.2-R0.1-SNAPSHOT</version>
|
||||
<version>1.21.8-R0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
@@ -39,9 +39,9 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.TheDgtl</groupId>
|
||||
<groupId>net.knarcraft</groupId>
|
||||
<artifactId>Stargate</artifactId>
|
||||
<version>1.0.0.4-ALPHA</version>
|
||||
<version>0.11.5.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.knarcraft.stargatecommand;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.config.ConfigurationOption;
|
||||
import net.knarcraft.stargate.Stargate;
|
||||
import net.knarcraft.stargatecommand.command.CommandStarGateCommand;
|
||||
import net.knarcraft.stargatecommand.command.StargateCommandTabCompleter;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormatter;
|
||||
@@ -16,9 +15,6 @@ 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
|
||||
*/
|
||||
@@ -26,14 +22,9 @@ import java.util.List;
|
||||
public class StargateCommand extends JavaPlugin {
|
||||
|
||||
private static StargateCommand instance;
|
||||
private List<ConfigurationOption> bannedConfigOptions;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
//Initialize the list of banned configuration options
|
||||
if (bannedConfigOptions == null) {
|
||||
initializeBannedConfigOptions();
|
||||
}
|
||||
instance = this;
|
||||
|
||||
FileConfiguration configuration = this.getConfig();
|
||||
@@ -44,15 +35,15 @@ public class StargateCommand extends JavaPlugin {
|
||||
|
||||
//Get the Stargate API
|
||||
ServicesManager servicesManager = this.getServer().getServicesManager();
|
||||
RegisteredServiceProvider<StargateAPI> stargateProvider = servicesManager.getRegistration(StargateAPI.class);
|
||||
RegisteredServiceProvider<Stargate> stargateProvider = servicesManager.getRegistration(Stargate.class);
|
||||
if (stargateProvider != null) {
|
||||
StargateAPI stargateAPI = stargateProvider.getProvider();
|
||||
Stargate stargate = stargateProvider.getProvider();
|
||||
|
||||
//Register commands
|
||||
PluginCommand stargateCommand = this.getCommand("stargateCommand");
|
||||
if (stargateCommand != null) {
|
||||
stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI, bannedConfigOptions));
|
||||
stargateCommand.setTabCompleter(new StargateCommandTabCompleter(stargateAPI, bannedConfigOptions));
|
||||
stargateCommand.setExecutor(new CommandStarGateCommand());
|
||||
stargateCommand.setTabCompleter(new StargateCommandTabCompleter());
|
||||
}
|
||||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
pluginManager.registerEvents(new StargateListener(), this);
|
||||
@@ -93,32 +84,4 @@ public class StargateCommand extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 || option.isHidden()) {
|
||||
bannedConfigOptions.add(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,273 +0,0 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.config.ConfigurationAPI;
|
||||
import net.TheDgtl.Stargate.config.ConfigurationOption;
|
||||
import net.TheDgtl.Stargate.config.OptionDataType;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormat;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormatter;
|
||||
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedErrorMessage;
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedInfoMessage;
|
||||
|
||||
/**
|
||||
* This command represents the config command for changing config values
|
||||
*/
|
||||
public class CommandConfig implements CommandExecutor {
|
||||
|
||||
private final ConfigurationAPI configurationAPI;
|
||||
private final List<ConfigurationOption> bannedConfigOptions;
|
||||
|
||||
/**
|
||||
* Instantiates a new instance of the config command
|
||||
*
|
||||
* @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, List<ConfigurationOption> bannedConfigOptions) {
|
||||
super();
|
||||
|
||||
this.configurationAPI = configurationAPI;
|
||||
this.bannedConfigOptions = bannedConfigOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (commandSender instanceof Player player) {
|
||||
if (!player.hasPermission(StargateCommandCommand.CONFIG.getPermissionNode())) {
|
||||
player.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PERMISSION_DENIED));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length > 0) {
|
||||
ConfigurationOption selectedOption;
|
||||
try {
|
||||
selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase());
|
||||
if (bannedConfigOptions.contains(selectedOption)) {
|
||||
return true;
|
||||
}
|
||||
} catch (IllegalArgumentException exception) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_CONFIGURATION_OPTION));
|
||||
return true;
|
||||
}
|
||||
if (args.length > 1) {
|
||||
updateConfigValue(selectedOption, commandSender, args[1]);
|
||||
} else {
|
||||
//Display info and the current value of the given config value
|
||||
printConfigOptionValue(commandSender, selectedOption);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
//Display all config options
|
||||
displayConfigValues(commandSender);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a config value
|
||||
*
|
||||
* @param selectedOption <p>The option which should be updated</p>
|
||||
* @param commandSender <p>The command sender that changed the value</p>
|
||||
* @param value <p>The new value of the config option</p>
|
||||
*/
|
||||
private void updateConfigValue(ConfigurationOption selectedOption, CommandSender commandSender, String value) {
|
||||
//Validate any sign colors
|
||||
if (selectedOption.getDataType() == OptionDataType.COLOR) {
|
||||
try {
|
||||
ChatColor.of(value.toUpperCase());
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
commandSender.sendMessage(StringFormatter.replacePlaceholder(getTranslatedErrorMessage(
|
||||
TranslatableMessage.INVALID_DATATYPE_GIVEN), "{datatype}", "color"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
OptionDataType optionDataType = selectedOption.getDataType();
|
||||
Object typeCastedValue;
|
||||
|
||||
//Validate the input based on the data type
|
||||
switch (optionDataType) {
|
||||
case INTEGER -> {
|
||||
Integer intValue = getInteger(commandSender, selectedOption, value);
|
||||
if (intValue == null) {
|
||||
return;
|
||||
} else {
|
||||
typeCastedValue = intValue;
|
||||
}
|
||||
}
|
||||
case DOUBLE -> {
|
||||
Double doubleValue = getDouble(commandSender, selectedOption, value);
|
||||
if (doubleValue == null) {
|
||||
return;
|
||||
} else {
|
||||
typeCastedValue = doubleValue;
|
||||
}
|
||||
}
|
||||
case BOOLEAN -> typeCastedValue = Boolean.parseBoolean(value);
|
||||
default -> typeCastedValue = value;
|
||||
}
|
||||
|
||||
/* Test any option data type with a defined set of values.
|
||||
* Color is excluded as it has a near-infinite number of valid values. */
|
||||
if (optionDataType != OptionDataType.COLOR && optionDataType.getValues() != null &&
|
||||
optionDataType.getValues().length > 0) {
|
||||
if (!checkIfValueMatchesDatatype(optionDataType, value, commandSender)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
configurationAPI.setConfigurationOptionValue(selectedOption, typeCastedValue);
|
||||
saveAndReload(commandSender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value is valid for the given option data type and warns if it isn't
|
||||
*
|
||||
* @param dataType <p>The expected type for the value</p>
|
||||
* @param value <p>The value to check</p>
|
||||
* @param commandSender <p>The command sender to warn about invalid values</p>
|
||||
* @return <p>True if the given value is valid for the option data type</p>
|
||||
*/
|
||||
private boolean checkIfValueMatchesDatatype(OptionDataType dataType, String value, CommandSender commandSender) {
|
||||
if (!matchesOptionDataType(dataType, value)) {
|
||||
commandSender.sendMessage(StringFormatter.replacePlaceholder(getTranslatedErrorMessage(
|
||||
TranslatableMessage.INVALID_DATATYPE_GIVEN), "{datatype}",
|
||||
dataType.name().toLowerCase().replace('_', ' ')));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value is valid for the given option data type
|
||||
*
|
||||
* @param dataType <p>The expected type for the value</p>
|
||||
* @param value <p>The value to check</p>
|
||||
* @return <p>True if the given value is valid for the option data type</p>
|
||||
*/
|
||||
private boolean matchesOptionDataType(OptionDataType dataType, String value) {
|
||||
return Arrays.asList(dataType.getValues()).contains(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the configuration file and reloads
|
||||
*
|
||||
* @param commandSender <p>The command sender that executed the config command</p>
|
||||
*/
|
||||
private void saveAndReload(CommandSender commandSender) {
|
||||
configurationAPI.saveConfiguration();
|
||||
configurationAPI.reload();
|
||||
commandSender.sendMessage(getTranslatedInfoMessage(TranslatableMessage.CONFIG_UPDATED));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an integer from a string
|
||||
*
|
||||
* @param commandSender <p>The command sender that sent the config command</p>
|
||||
* @param selectedOption <p>The option the command sender is trying to change</p>
|
||||
* @param value <p>The value given</p>
|
||||
* @return <p>An integer, or null if it was invalid</p>
|
||||
*/
|
||||
private Integer getInteger(CommandSender commandSender, ConfigurationOption selectedOption, String value) {
|
||||
try {
|
||||
int intValue = Integer.parseInt(value);
|
||||
|
||||
if ((selectedOption == ConfigurationOption.USE_COST || selectedOption == ConfigurationOption.CREATION_COST) && intValue < 0) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.POSITIVE_NUMBER_REQUIRED));
|
||||
return null;
|
||||
}
|
||||
|
||||
return intValue;
|
||||
} catch (NumberFormatException exception) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NUMBER_GIVEN));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a double from a string
|
||||
*
|
||||
* @param commandSender <p>The command sender that sent the config command</p>
|
||||
* @param selectedOption <p>The option the command sender is trying to change</p>
|
||||
* @param value <p>The value given</p>
|
||||
* @return <p>A double, or null if it was invalid</p>
|
||||
*/
|
||||
private Double getDouble(CommandSender commandSender, ConfigurationOption selectedOption, String value) {
|
||||
try {
|
||||
double doubleValue = Double.parseDouble(value);
|
||||
|
||||
if (selectedOption == ConfigurationOption.GATE_EXIT_SPEED_MULTIPLIER && doubleValue < 0) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.POSITIVE_NUMBER_REQUIRED));
|
||||
return null;
|
||||
}
|
||||
|
||||
return doubleValue;
|
||||
} catch (NumberFormatException exception) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NUMBER_GIVEN));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints information about a config option and its current value
|
||||
*
|
||||
* @param sender <p>The command sender that sent the command</p>
|
||||
* @param option <p>The config option to print information about</p>
|
||||
*/
|
||||
private void printConfigOptionValue(CommandSender sender, ConfigurationOption option) {
|
||||
Object value = configurationAPI.getConfigurationOptionValue(option);
|
||||
String description = getOptionDescription(option);
|
||||
String currentValue = StringFormatter.replacePlaceholder(StringFormatter.getStringFormat(
|
||||
StringFormat.CONFIG_OPTION_CURRENT_VALUE_FORMAT), "{value}", String.valueOf(value));
|
||||
sender.sendMessage(StringFormatter.formatInfoMessage(description + currentValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the name and a small description of every config value
|
||||
*
|
||||
* @param sender <p>The command sender to display the config list to</p>
|
||||
*/
|
||||
private void displayConfigValues(CommandSender sender) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append(StringFormatter.getStringFormat(StringFormat.CONFIG_VALUES_HEADER_FORMAT));
|
||||
|
||||
for (ConfigurationOption option : ConfigurationOption.values()) {
|
||||
if (!bannedConfigOptions.contains(option)) {
|
||||
stringBuilder.append(getOptionDescription(option));
|
||||
}
|
||||
}
|
||||
sender.sendMessage(stringBuilder.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the description of a single config option
|
||||
*
|
||||
* @param option <p>The option to describe</p>
|
||||
* @return <p>A string describing the config option</p>
|
||||
*/
|
||||
private String getOptionDescription(ConfigurationOption option) {
|
||||
Object defaultValue = option.getDefaultValue();
|
||||
String stringValue = String.valueOf(defaultValue);
|
||||
if (option.getDataType() == OptionDataType.STRING_LIST) {
|
||||
stringValue = "[" + StringUtils.join((String[]) defaultValue, ",") + "]";
|
||||
}
|
||||
return StringFormatter.replacePlaceholders(StringFormatter.getStringFormat(
|
||||
StringFormat.CONFIG_OPTION_DESCRIPTION_FORMAT), new String[]{"{name}", "{description}", "{value}"},
|
||||
new String[]{option.name(), option.getDescription(), stringValue});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,12 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.manager.PermissionManager;
|
||||
import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.RealPortal;
|
||||
import net.knarcraft.stargate.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.PortalHandler;
|
||||
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
|
||||
import net.knarcraft.stargatecommand.manager.IconManager;
|
||||
import net.knarcraft.stargatecommand.manager.OverrideManager;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import net.knarcraft.stargatecommand.util.NameHelper;
|
||||
import net.knarcraft.stargatecommand.util.PortalFinderHelper;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -27,17 +23,11 @@ import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTransl
|
||||
public class CommandDial implements CommandExecutor {
|
||||
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final StargateAPI stargateAPI;
|
||||
private final RegistryAPI registryAPI;
|
||||
|
||||
/**
|
||||
* Instantiates a new dial command
|
||||
*
|
||||
* @param stargateAPI <p>A reference to the Stargate API</p>
|
||||
*/
|
||||
public CommandDial(StargateAPI stargateAPI) {
|
||||
this.stargateAPI = stargateAPI;
|
||||
this.registryAPI = stargateAPI.getRegistry();
|
||||
public CommandDial() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,34 +47,34 @@ public class CommandDial implements CommandExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
PermissionManager permissionManager = stargateAPI.getPermissionManager(player);
|
||||
String portalName = args[1].replace(spaceReplacement, " ");
|
||||
|
||||
//Validate that the network and portal exists, and that the player can access the portal
|
||||
Network network = NameHelper.getNetworkFromName(registryAPI, args[0]);
|
||||
if (network == null) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NETWORK_GIVEN));
|
||||
return true;
|
||||
}
|
||||
RealPortal targetPortal = (RealPortal) network.getPortal(portalName);
|
||||
if (targetPortal == null) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_PORTAL_GIVEN));
|
||||
return true;
|
||||
}
|
||||
if (!permissionManager.hasAccessPermission(targetPortal)) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PORTAL_NO_ACCESS));
|
||||
|
||||
Portal portal = PortalHandler.getByName(portalName, args[0]);
|
||||
if (portal == null) {
|
||||
if (PortalHandler.getNetwork(args[0]) == null) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NETWORK_GIVEN));
|
||||
} else {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_PORTAL_GIVEN));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//Find any Stargate block in the player's line of sight
|
||||
RealPortal originPortal = (RealPortal) PortalFinderHelper.findPortalByRaytrace(registryAPI, player, 10);
|
||||
Portal originPortal = PortalFinderHelper.findPortalByRaytrace(player, 10);
|
||||
if (originPortal == null) {
|
||||
player.sendMessage(getTranslatedErrorMessage(TranslatableMessage.NO_PORTAL_IN_SIGHT));
|
||||
return true;
|
||||
}
|
||||
originPortal.overrideDestination(targetPortal);
|
||||
OverrideManager.storeOverriddenDestination(originPortal);
|
||||
originPortal.open(player);
|
||||
|
||||
if (PermissionHelper.cannotAccessPortal(player, originPortal, portal)) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PORTAL_NO_ACCESS));
|
||||
return true;
|
||||
}
|
||||
|
||||
originPortal.getPortalActivator().setDestination(portal);
|
||||
originPortal.getPortalOpener().openPortal(player, true);
|
||||
|
||||
player.sendMessage(getTranslatedInfoMessage(TranslatableMessage.DIAL_SUCCESSFUL));
|
||||
return true;
|
||||
|
||||
@@ -1,49 +1,29 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.config.ConfigurationOption;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
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;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* This command represents any command which starts with stargate-command (sgc)
|
||||
*/
|
||||
public class CommandStarGateCommand implements CommandExecutor {
|
||||
|
||||
private final StargateAPI stargateAPI;
|
||||
private final List<ConfigurationOption> bannedConfigOptions;
|
||||
|
||||
/**
|
||||
* 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.bannedConfigOptions = bannedConfigOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (args.length > 0) {
|
||||
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
||||
if (args[0].equalsIgnoreCase(StargateCommandCommand.CONFIG.getName())) {
|
||||
return new CommandConfig(stargateAPI.getConfigurationAPI(), bannedConfigOptions).onCommand(
|
||||
commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase(StargateCommandCommand.DIAL.getName())) {
|
||||
return new CommandDial(stargateAPI).onCommand(commandSender, command, s, subArgs);
|
||||
String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
|
||||
if (args[0].equalsIgnoreCase(StargateCommandCommand.DIAL.getName())) {
|
||||
return new CommandDial().onCommand(commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase(StargateCommandCommand.VISUALIZER.getName())) {
|
||||
return new CommandVisualizer(stargateAPI.getRegistry()).onCommand(commandSender, command, s, subArgs);
|
||||
return new CommandVisualizer().onCommand(commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase(StargateCommandCommand.INFO.getName())) {
|
||||
return new TabCommandInfo(stargateAPI.getRegistry()).onCommand(commandSender, command, s, subArgs);
|
||||
return new TabCommandInfo().onCommand(commandSender, command, s, subArgs);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.Portal;
|
||||
import net.TheDgtl.Stargate.network.portal.PortalFlag;
|
||||
import net.knarcraft.stargate.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.PortalHandler;
|
||||
import net.knarcraft.stargate.portal.property.PortalOptions;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormat;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormatter;
|
||||
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
|
||||
import net.knarcraft.stargatecommand.manager.IconManager;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import net.knarcraft.stargatecommand.util.NameHelper;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedErrorMessage;
|
||||
|
||||
/**
|
||||
@@ -23,33 +23,21 @@ import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTransl
|
||||
*/
|
||||
public class CommandVisualizer implements CommandExecutor {
|
||||
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final RegistryAPI registryAPI;
|
||||
|
||||
/**
|
||||
* Instantiates a visualizer command
|
||||
*
|
||||
* @param registryAPI <p>A reference to the registry API</p>
|
||||
*/
|
||||
public CommandVisualizer(RegistryAPI registryAPI) {
|
||||
this.registryAPI = registryAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
@NotNull String[] arguments) {
|
||||
if (!commandSender.hasPermission(StargateCommandCommand.VISUALIZER.getPermissionNode())) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PERMISSION_DENIED));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 1) {
|
||||
if (arguments.length < 1) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.COMMAND_VISUALIZER_ARGUMENTS));
|
||||
return true;
|
||||
}
|
||||
|
||||
Network network = NameHelper.getNetworkFromName(registryAPI, args[0]);
|
||||
|
||||
String networkName = arguments[0];
|
||||
List<String> network = PortalHandler.getNetwork(networkName);
|
||||
if (network == null) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NETWORK_GIVEN));
|
||||
return true;
|
||||
@@ -61,25 +49,28 @@ public class CommandVisualizer implements CommandExecutor {
|
||||
StringFormat.COMMAND_VISUALIZER_FORMAT)));
|
||||
|
||||
//Print info about all portals in the network
|
||||
for (Portal portal : network.getAllPortals()) {
|
||||
for (String portalName : network) {
|
||||
Portal portal = PortalHandler.getByName(portalName, networkName);
|
||||
|
||||
StringBuilder iconBuilder = new StringBuilder();
|
||||
if (portal.hasFlag(PortalFlag.HIDDEN)) {
|
||||
PortalOptions options = portal.getOptions();
|
||||
if (options.isHidden()) {
|
||||
iconBuilder.append(Icon.HIDDEN.getPlaceholder());
|
||||
} else {
|
||||
iconBuilder.append(Icon.NOT_HIDDEN.getPlaceholder());
|
||||
}
|
||||
if (portal.hasFlag(PortalFlag.ALWAYS_ON)) {
|
||||
if (options.isAlwaysOn()) {
|
||||
iconBuilder.append(Icon.ALWAYS_ON.getPlaceholder());
|
||||
} else {
|
||||
iconBuilder.append(Icon.NOT_ALWAYS_ON.getPlaceholder());
|
||||
}
|
||||
if (portal.hasFlag(PortalFlag.RANDOM)) {
|
||||
if (options.isRandom()) {
|
||||
iconBuilder.append(Icon.RANDOM.getPlaceholder());
|
||||
} else {
|
||||
iconBuilder.append(Icon.NOT_RANDOM.getPlaceholder());
|
||||
}
|
||||
String fixedString = "";
|
||||
if (portal.hasFlag(PortalFlag.FIXED)) {
|
||||
if (options.isFixed()) {
|
||||
fixedString = StringFormatter.replacePlaceholder(StringFormatter.getStringFormat(
|
||||
StringFormat.COMMAND_VISUALIZER_FIXED_FORMAT), "{portal}",
|
||||
portal.getDestinationName());
|
||||
@@ -91,7 +82,7 @@ public class CommandVisualizer implements CommandExecutor {
|
||||
}
|
||||
|
||||
commandSender.sendMessage(IconManager.replaceIconsInString(
|
||||
StringFormatter.replacePlaceholder(stringBuilder.toString(), "{network}", network.getName())));
|
||||
StringFormatter.replacePlaceholder(stringBuilder.toString(), "{network}", networkName)));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,165 +0,0 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.config.ConfigurationOption;
|
||||
import net.TheDgtl.Stargate.config.OptionDataType;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatching;
|
||||
|
||||
/**
|
||||
* This is the completer for stargates config sub-command (/sgc config)
|
||||
*/
|
||||
public class ConfigTabCompleter implements TabCompleter {
|
||||
|
||||
private final List<ConfigurationOption> bannedConfigOptions;
|
||||
private List<String> booleans;
|
||||
private List<String> integers;
|
||||
private List<String> chatColors;
|
||||
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
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
//Don't display any info to non-authorized users
|
||||
if (!commandSender.hasPermission(StargateCommandCommand.CONFIG.getPermissionNode())) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (booleans == null || integers == null || chatColors == null) {
|
||||
initializeAutoCompleteLists();
|
||||
}
|
||||
|
||||
if (args.length > 2) {
|
||||
return new ArrayList<>();
|
||||
} else if (args.length > 1) {
|
||||
ConfigurationOption selectedOption;
|
||||
try {
|
||||
selectedOption = ConfigurationOption.valueOf(args[0].toUpperCase());
|
||||
if (bannedConfigOptions.contains(selectedOption)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} catch (IllegalArgumentException exception) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return getPossibleOptionValues(selectedOption, args[1]);
|
||||
} else {
|
||||
List<String> configOptionNames = new ArrayList<>();
|
||||
for (ConfigurationOption option : ConfigurationOption.values()) {
|
||||
if (!bannedConfigOptions.contains(option)) {
|
||||
configOptionNames.add(option.name());
|
||||
}
|
||||
}
|
||||
return filterMatching(configOptionNames, args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get possible values for the selected option
|
||||
*
|
||||
* @param selectedOption <p>The selected option</p>
|
||||
* @param typedText <p>The beginning of the typed text, for filtering matching results</p>
|
||||
* @return <p>Some or all of the valid values for the option</p>
|
||||
*/
|
||||
private List<String> getPossibleOptionValues(ConfigurationOption selectedOption, String typedText) {
|
||||
String[] availableValues = selectedOption.getDataType().getValues();
|
||||
if (availableValues != null && availableValues.length > 0) {
|
||||
return filterMatching(List.of(availableValues), typedText);
|
||||
}
|
||||
|
||||
//Just return the default value as most values should be possible
|
||||
if (selectedOption == ConfigurationOption.DEFAULT_NETWORK) {
|
||||
if (typedText.trim().isEmpty()) {
|
||||
return putStringInList((String) selectedOption.getDefaultValue());
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedOption.getDataType() == OptionDataType.COLOR) {
|
||||
return filterMatching(chatColors, typedText);
|
||||
}
|
||||
|
||||
//If the config value is a boolean, show the two boolean values
|
||||
if (selectedOption.getDataType() == OptionDataType.BOOLEAN) {
|
||||
return filterMatching(booleans, typedText);
|
||||
}
|
||||
|
||||
//If the config value is an integer, display some valid numbers
|
||||
if (selectedOption.getDataType() == OptionDataType.INTEGER) {
|
||||
if (typedText.trim().isEmpty()) {
|
||||
return integers;
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
//If the config value is a double, display some valid numbers
|
||||
if (selectedOption.getDataType() == OptionDataType.DOUBLE) {
|
||||
if (typedText.trim().isEmpty()) {
|
||||
return doubles;
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts a single string value into a string list
|
||||
*
|
||||
* @param value <p>The string to make into a list</p>
|
||||
* @return <p>A list containing the string value</p>
|
||||
*/
|
||||
private List<String> putStringInList(String value) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add(value);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all lists of auto-completable values
|
||||
*/
|
||||
private void initializeAutoCompleteLists() {
|
||||
booleans = new ArrayList<>();
|
||||
booleans.add("true");
|
||||
booleans.add("false");
|
||||
|
||||
integers = new ArrayList<>();
|
||||
integers.add("0");
|
||||
integers.add("5");
|
||||
|
||||
getColors();
|
||||
|
||||
doubles = new ArrayList<>();
|
||||
doubles.add("5");
|
||||
doubles.add("1");
|
||||
doubles.add("0.5");
|
||||
doubles.add("0.1");
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the list of chat colors
|
||||
*/
|
||||
private void getColors() {
|
||||
chatColors = List.of(OptionDataType.COLOR.getValues());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,16 +32,6 @@ import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatchi
|
||||
public class DialTabCompleter implements TabCompleter {
|
||||
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final StargateAPI stargateAPI;
|
||||
|
||||
/**
|
||||
* Instantiates a new dial tab completer
|
||||
*
|
||||
* @param stargateAPI <p>A reference to the Stargate API</p>
|
||||
*/
|
||||
public DialTabCompleter(StargateAPI stargateAPI) {
|
||||
this.stargateAPI = stargateAPI;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@@ -58,8 +48,6 @@ public class DialTabCompleter implements TabCompleter {
|
||||
|
||||
List<String> availableNetworks = new ArrayList<>();
|
||||
Map<String, List<String>> availablePortals = new HashMap<>();
|
||||
RegistryAPI registryAPI = stargateAPI.getRegistry();
|
||||
PermissionManager permissionManager = stargateAPI.getPermissionManager(player);
|
||||
|
||||
//Populate the collections with available networks and portals
|
||||
populateNetworksAndPortals(permissionManager, availableNetworks, availablePortals);
|
||||
@@ -77,7 +65,7 @@ public class DialTabCompleter implements TabCompleter {
|
||||
} else {
|
||||
return filterMatching(availableNetworks, args[0].replace(spaceReplacement, " "));
|
||||
}
|
||||
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
@@ -49,9 +49,7 @@ public class StargateCommandTabCompleter implements TabCompleter {
|
||||
//Get the actual arguments for the specified sub-command
|
||||
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
||||
|
||||
if (args[0].equalsIgnoreCase(StargateCommandCommand.CONFIG.getName())) {
|
||||
return new ConfigTabCompleter(bannedConfigOptions).onTabComplete(commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase(StargateCommandCommand.DIAL.getName())) {
|
||||
if (args[0].equalsIgnoreCase(StargateCommandCommand.DIAL.getName())) {
|
||||
return new DialTabCompleter(stargateAPI).onTabComplete(commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase(StargateCommandCommand.VISUALIZER.getName())) {
|
||||
return new VisualizerTabCompleter(stargateAPI.getRegistry()).onTabComplete(commandSender, command, s, subArgs);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class TabCommandInfo implements TabExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
Portal portal = PortalFinderHelper.findPortalByRaytrace(registryAPI, player, 15);
|
||||
Portal portal = PortalFinderHelper.findPortalByRaytrace(player, 15);
|
||||
if (portal == null) {
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.NO_PORTAL_IN_SIGHT));
|
||||
return true;
|
||||
|
||||
@@ -5,11 +5,6 @@ package net.knarcraft.stargatecommand.property;
|
||||
*/
|
||||
public enum StargateCommandCommand {
|
||||
|
||||
/**
|
||||
* The config command
|
||||
*/
|
||||
CONFIG("config", "stargate.command.config", false),
|
||||
|
||||
/**
|
||||
* The dial command
|
||||
*/
|
||||
|
||||
@@ -13,9 +13,9 @@ import java.util.UUID;
|
||||
* A helper class for dealing with Portal and Network names
|
||||
*/
|
||||
public final class NameHelper {
|
||||
|
||||
|
||||
private NameHelper() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.knarcraft.stargatecommand.util;
|
||||
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.PortalHandler;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
@@ -19,12 +19,11 @@ public final class PortalFinderHelper {
|
||||
/**
|
||||
* Find the portal a living entity is looking at using a single traced ray
|
||||
*
|
||||
* @param registryAPI <p>The registry used for looking up portals</p>
|
||||
* @param livingEntity <p>The living entity to ray trace from</p>
|
||||
* @param rayLength <p>The maximum length of the ray before giving up</p>
|
||||
* @return <p>The portal the player is looking at, or null if no portal was found</p>
|
||||
*/
|
||||
public static Portal findPortalByRaytrace(RegistryAPI registryAPI, LivingEntity livingEntity, int rayLength) {
|
||||
public static Portal findPortalByRaytrace(LivingEntity livingEntity, int rayLength) {
|
||||
Location entityLocation = livingEntity.getLocation().add(0, livingEntity.getEyeHeight(), 0);
|
||||
Vector entityDirection = livingEntity.getLocation().getDirection();
|
||||
Portal foundPortal = null;
|
||||
@@ -32,7 +31,7 @@ public final class PortalFinderHelper {
|
||||
//Follow the ray outwards from the entity until a portal is found
|
||||
for (int i = 0; i < rayLength; i++) {
|
||||
entityLocation.add(entityDirection);
|
||||
foundPortal = registryAPI.getPortal(entityLocation);
|
||||
foundPortal = PortalHandler.getByEntrance(entityLocation);
|
||||
//Stop if a portal is found. Also, don't trace through solid blocks.
|
||||
Material traceMaterial = entityLocation.getBlock().getType();
|
||||
if (foundPortal != null || (!traceMaterial.isAir() && traceMaterial != Material.WATER)) {
|
||||
|
||||
Reference in New Issue
Block a user