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 || option.isHidden()) { - 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 deleted file mode 100644 index 8d4e990..0000000 --- a/src/main/java/net/knarcraft/stargatecommand/command/CommandConfig.java +++ /dev/null @@ -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 ListA reference to the Stargate API
- * @param bannedConfigOptionsA list of config options that shouldn't be available
- */ - public CommandConfig(ConfigurationAPI configurationAPI, ListThe option which should be updated
- * @param commandSenderThe command sender that changed the value
- * @param valueThe new value of the config option
- */ - 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 dataTypeThe expected type for the value
- * @param valueThe value to check
- * @param commandSenderThe command sender to warn about invalid values
- * @returnTrue if the given value is valid for the option data type
- */ - 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 dataTypeThe expected type for the value
- * @param valueThe value to check
- * @returnTrue if the given value is valid for the option data type
- */ - private boolean matchesOptionDataType(OptionDataType dataType, String value) { - return Arrays.asList(dataType.getValues()).contains(value); - } - - /** - * Saves the configuration file and reloads - * - * @param commandSenderThe command sender that executed the config command
- */ - private void saveAndReload(CommandSender commandSender) { - configurationAPI.saveConfiguration(); - configurationAPI.reload(); - commandSender.sendMessage(getTranslatedInfoMessage(TranslatableMessage.CONFIG_UPDATED)); - } - - /** - * Gets an integer from a string - * - * @param commandSenderThe command sender that sent the config command
- * @param selectedOptionThe option the command sender is trying to change
- * @param valueThe value given
- * @returnAn integer, or null if it was invalid
- */ - 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 commandSenderThe command sender that sent the config command
- * @param selectedOptionThe option the command sender is trying to change
- * @param valueThe value given
- * @returnA double, or null if it was invalid
- */ - 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 senderThe command sender that sent the command
- * @param optionThe config option to print information about
- */ - 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 senderThe command sender to display the config list to
- */ - 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 optionThe option to describe
- * @returnA string describing the config option
- */ - 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}); - } - -} diff --git a/src/main/java/net/knarcraft/stargatecommand/command/CommandDial.java b/src/main/java/net/knarcraft/stargatecommand/command/CommandDial.java index 751dfe6..d8eeaf2 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/CommandDial.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/CommandDial.java @@ -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 stargateAPIA reference to the Stargate API
*/ - 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; diff --git a/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java b/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java index 92a0e56..bd9134b 100644 --- a/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java +++ b/src/main/java/net/knarcraft/stargatecommand/command/CommandStarGateCommand.java @@ -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 ListA reference to the Stargate API
- * @param bannedConfigOptionsA list of config options that shouldn't be available
- */ - public CommandStarGateCommand(StargateAPI stargateAPI, ListA reference to the registry API
- */ - 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]; + ListA list of config options that shouldn't be available
- */ - public ConfigTabCompleter(ListThe selected option
- * @param typedTextThe beginning of the typed text, for filtering matching results
- * @returnSome or all of the valid values for the option
- */ - private ListThe string to make into a list
- * @returnA list containing the string value
- */ - private ListA reference to the Stargate API
- */ - public DialTabCompleter(StargateAPI stargateAPI) { - this.stargateAPI = stargateAPI; - } @Nullable @Override @@ -58,8 +48,6 @@ public class DialTabCompleter implements TabCompleter { ListThe registry used for looking up portals
* @param livingEntityThe living entity to ray trace from
* @param rayLengthThe maximum length of the ray before giving up
* @returnThe portal the player is looking at, or null if no portal was found
*/ - 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)) {