diff --git a/README.md b/README.md index 83139b0..8692f37 100644 --- a/README.md +++ b/README.md @@ -541,7 +541,7 @@ portalInfoServer=Server: %server% #### \[Version 0.11.5.3] Unified Legacy Fork -- Hotfix for an issue wherein the gate folder failed to populate on fresh installs. +- Hotfix for an issue wherein the gate folder failed to populate on fresh installations. #### \[Version 0.11.5.2] Unified Legacy Fork @@ -757,7 +757,7 @@ found [here](https://github.com/stargate-rewritten/Stargate-ESR#version-01081-lc - Improves the stability of Stargate's load-time portal handling. - Highlights destination selector brackets on signs ("-") as to improve readability. - Uses dark red to mark portals which are inactive (missing destination or invalid gate type) -- Adds provisions to re-draw incorrect signs. +- Adds provision to re-draw incorrect signs. - Fixed a load of other miscellaneous bugs. #### \[Version 0.8.0.3] PseudoKnight fork diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index 3293978..86be6df 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -4,6 +4,7 @@ import net.knarcraft.knarlib.util.UpdateChecker; import net.knarcraft.stargate.command.CommandStarGate; import net.knarcraft.stargate.command.StarGateTabCompleter; import net.knarcraft.stargate.config.EconomyConfig; +import net.knarcraft.stargate.config.Message; import net.knarcraft.stargate.config.MessageSender; import net.knarcraft.stargate.config.StargateConfig; import net.knarcraft.stargate.config.StargateGateConfig; @@ -35,6 +36,8 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.scheduler.BukkitScheduler; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -112,7 +115,7 @@ public class Stargate extends JavaPlugin { * * @param version
The version of the new update available
*/ - public static void setUpdateAvailable(String version) { + public static void setUpdateAvailable(@NotNull String version) { updateAvailable = version; } @@ -121,6 +124,7 @@ public class Stargate extends JavaPlugin { * * @returnThe version number if an update is available. Null otherwise
*/ + @Nullable public static String getUpdateAvailable() { return updateAvailable; } @@ -130,6 +134,7 @@ public class Stargate extends JavaPlugin { * * @returnAn instance of this plugin, or null if not instantiated
*/ + @NotNull public static Stargate getInstance() { return stargate; } @@ -139,7 +144,7 @@ public class Stargate extends JavaPlugin { * * @param requestThe request to add
*/ - public static void addBlockChangeRequest(BlockChangeRequest request) { + public static void addBlockChangeRequest(@Nullable BlockChangeRequest request) { if (request != null) { blockChangeRequestQueue.add(request); } @@ -150,6 +155,7 @@ public class Stargate extends JavaPlugin { * * @returnA block change request queue
*/ + @NotNull public static QueueThe sender for sending messages to players
*/ + @NotNull public static MessageSender getMessageSender() { return stargateConfig.getMessageSender(); } @@ -168,6 +175,7 @@ public class Stargate extends JavaPlugin { * * @returnThe object containing gate configuration values
*/ + @NotNull public static StargateGateConfig getGateConfig() { return stargateConfig.getStargateGateConfig(); } @@ -177,6 +185,7 @@ public class Stargate extends JavaPlugin { * * @returnThis plugin's version
*/ + @NotNull public static String getPluginVersion() { return pluginVersion; } @@ -186,6 +195,7 @@ public class Stargate extends JavaPlugin { * * @returnThe logger
*/ + @NotNull public static Logger getConsoleLogger() { return logger; } @@ -206,7 +216,7 @@ public class Stargate extends JavaPlugin { * @param routeThe class name/route where something happened
* @param messageA message describing what happened
*/ - public static void debug(String route, String message) { + public static void debug(@NotNull String route, @NotNull String message) { if (stargateConfig == null || stargateConfig.isNotLoaded() || stargateConfig.isDebuggingEnabled()) { logger.info("[Stargate::" + route + "] " + message); } else { @@ -219,7 +229,7 @@ public class Stargate extends JavaPlugin { * * @param messageThe message to log
*/ - public static void logInfo(String message) { + public static void logInfo(@NotNull String message) { log(Level.INFO, message); } @@ -228,7 +238,7 @@ public class Stargate extends JavaPlugin { * * @param messageThe message to log
*/ - public static void logSevere(String message) { + public static void logSevere(@NotNull String message) { log(Level.SEVERE, message); } @@ -237,7 +247,7 @@ public class Stargate extends JavaPlugin { * * @param messageThe message to log
*/ - public static void logWarning(String message) { + public static void logWarning(@NotNull String message) { log(Level.WARNING, message); } @@ -247,7 +257,7 @@ public class Stargate extends JavaPlugin { * @param severityThe severity of the event triggering the message
* @param messageThe message to log
*/ - private static void log(Level severity, String message) { + private static void log(@NotNull Level severity, @NotNull String message) { if (logger == null) { logger = Bukkit.getLogger(); } @@ -261,6 +271,7 @@ public class Stargate extends JavaPlugin { * * @returnThe folder for storing the portal database
*/ + @NotNull public static String getPortalFolder() { return stargateConfig.getPortalFolder(); } @@ -272,6 +283,7 @@ public class Stargate extends JavaPlugin { * * @returnThe folder storing gate files
*/ + @NotNull public static String getGateFolder() { return stargateConfig.getGateFolder(); } @@ -281,31 +293,28 @@ public class Stargate extends JavaPlugin { * * @returnThe default network
*/ + @NotNull public static String getDefaultNetwork() { return stargateConfig.getStargateGateConfig().getDefaultPortalNetwork(); } /** - * Gets a translated string given its string key - * - *The name/key is the string before the equals sign in the language files
+ * Gets a translated string given its message key * * @param nameThe name/key of the string to get
* @returnThe full translated string
*/ - public static String getString(String name) { + public static @NotNull String getString(@NotNull Message name) { return stargateConfig.getLanguageLoader().getString(name); } /** - * Gets a backup string given its string key - * - *The name/key is the string before the equals sign in the language files
+ * Gets a backup string given its message key * * @param nameThe name/key of the string to get
* @returnThe full string in the backup language (English)
*/ - public static String getBackupString(String name) { + public static @NotNull String getBackupString(@NotNull Message name) { return stargateConfig.getLanguageLoader().getBackupString(name); } @@ -317,7 +326,8 @@ public class Stargate extends JavaPlugin { * @param valueThe replacement value
* @returnThe input string with the search replaced with value
*/ - public static String replaceVars(String input, String search, String value) { + @NotNull + public static String replacePlaceholders(@NotNull String input, @NotNull String search, @NotNull String value) { return input.replace(search, value); } @@ -326,6 +336,7 @@ public class Stargate extends JavaPlugin { * * @returnA plugin manager
*/ + @NotNull public static PluginManager getPluginManager() { return pluginManager; } @@ -335,6 +346,7 @@ public class Stargate extends JavaPlugin { * * @returnThe object containing economy config values
*/ + @NotNull public static EconomyConfig getEconomyConfig() { return stargateConfig.getEconomyConfig(); } @@ -344,6 +356,7 @@ public class Stargate extends JavaPlugin { * * @returnThe raw configuration
*/ + @NotNull public FileConfiguration getConfiguration() { return this.configuration; } @@ -354,8 +367,8 @@ public class Stargate extends JavaPlugin { this.configuration = new StargateYamlConfiguration(); try { this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME)); - } catch (IOException | InvalidConfigurationException e) { - logSevere("Unable to load the configuration! Message: " + e.getMessage()); + } catch (IOException | InvalidConfigurationException exception) { + logSevere("Unable to load the configuration! Message: " + exception.getMessage()); } } @@ -364,8 +377,8 @@ public class Stargate extends JavaPlugin { super.saveConfig(); try { this.configuration.save(new File(getDataFolder(), CONFIG_FILE_NAME)); - } catch (IOException e) { - logSevere("Unable to save the configuration! Message: " + e.getMessage()); + } catch (IOException exception) { + logSevere("Unable to save the configuration! Message: " + exception.getMessage()); } } @@ -390,8 +403,8 @@ public class Stargate extends JavaPlugin { this.configuration = new StargateYamlConfiguration(); try { this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME)); - } catch (IOException | InvalidConfigurationException e) { - getLogger().log(Level.SEVERE, e.getMessage()); + } catch (IOException | InvalidConfigurationException exception) { + getLogger().log(Level.SEVERE, exception.getMessage()); } this.configuration.options().copyDefaults(true); @@ -470,6 +483,7 @@ public class Stargate extends JavaPlugin { * * @returnThe chunk unload queue
*/ + @NotNull public static QueueThe new chunk unload request to add
*/ - public static void addChunkUnloadRequest(ChunkUnloadRequest request) { + public static void addChunkUnloadRequest(@NotNull ChunkUnloadRequest request) { chunkUnloadQueue.removeIf((item) -> item.getChunkToUnload().equals(request.getChunkToUnload())); chunkUnloadQueue.add(request); } @@ -489,6 +503,7 @@ public class Stargate extends JavaPlugin { * * @returnThe stargate configuration
*/ + @NotNull public static StargateConfig getStargateConfig() { return stargateConfig; } diff --git a/src/main/java/net/knarcraft/stargate/command/CommandAbout.java b/src/main/java/net/knarcraft/stargate/command/CommandAbout.java index 51a6725..e0e7217 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandAbout.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandAbout.java @@ -2,6 +2,7 @@ package net.knarcraft.stargate.command; import de.themoep.minedown.MineDown; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.config.Message; import net.knarcraft.stargate.utility.FileHelper; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; @@ -26,13 +27,15 @@ public class CommandAbout implements CommandExecutor { ChatColor highlightColor = ChatColor.GREEN; try (InputStream inputStream = Stargate.class.getResourceAsStream("/messages/about.md")) { - String aboutMessageString = FileHelper.readStreamToString(inputStream); - BaseComponent[] component = MineDown.parse(aboutMessageString); - commandSender.spigot().sendMessage(component); + if (inputStream != null) { + String aboutMessageString = FileHelper.readStreamToString(inputStream); + BaseComponent[] component = MineDown.parse(aboutMessageString); + commandSender.spigot().sendMessage(component); + } } catch (IOException ioException) { commandSender.sendMessage("Internal error"); } - String author = Stargate.getStargateConfig().getLanguageLoader().getString("author"); + String author = Stargate.getStargateConfig().getLanguageLoader().getString(Message.AUTHOR); if (!author.isEmpty()) { commandSender.sendMessage(textColor + "Language created by " + highlightColor + author); } diff --git a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java index 2ae0691..684393b 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java @@ -4,6 +4,7 @@ import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.ConfigOption; import net.knarcraft.stargate.config.ConfigTag; import net.knarcraft.stargate.config.DynmapManager; +import net.knarcraft.stargate.config.Message; import net.knarcraft.stargate.config.OptionDataType; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalRegistry; @@ -16,6 +17,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -65,7 +67,8 @@ public class CommandConfig implements CommandExecutor { * @param commandSenderThe command sender that changed the value
* @param valueThe new value of the config option
*/ - private void updateConfigValue(ConfigOption selectedOption, CommandSender commandSender, String value) { + private void updateConfigValue(@NotNull ConfigOption selectedOption, @NotNull CommandSender commandSender, + @NotNull String value) { FileConfiguration configuration = Stargate.getInstance().getConfiguration(); //Validate any sign colors @@ -119,7 +122,8 @@ public class CommandConfig implements CommandExecutor { * @param valueThe new value of the config option
* @param configurationThe configuration file to save to
*/ - private void updateBooleanConfigValue(ConfigOption selectedOption, String value, FileConfiguration configuration) { + private void updateBooleanConfigValue(@NotNull ConfigOption selectedOption, @NotNull String value, + @NotNull FileConfiguration configuration) { boolean newValue = Boolean.parseBoolean(value); if (selectedOption == ConfigOption.ENABLE_BUNGEE && newValue != Stargate.getGateConfig().enableBungee()) { Stargate.getStargateConfig().startStopBungeeListener(newValue); @@ -135,7 +139,8 @@ public class CommandConfig implements CommandExecutor { * @param commandSenderThe command sender that changed the value
* @param valueThe new value of the config option
*/ - private void updateStringConfigValue(ConfigOption selectedOption, CommandSender commandSender, String value) { + private void updateStringConfigValue(@NotNull ConfigOption selectedOption, @NotNull CommandSender commandSender, + @NotNull String value) { if (selectedOption == ConfigOption.GATE_FOLDER || selectedOption == ConfigOption.PORTAL_FOLDER || selectedOption == ConfigOption.DEFAULT_GATE_NETWORK) { if (value.contains("../") || value.contains("..\\")) { @@ -161,7 +166,8 @@ public class CommandConfig implements CommandExecutor { * @param commandSenderThe command sender that changed the value
* @param argumentsThe arguments for the new config option
*/ - private void updateListConfigValue(ConfigOption selectedOption, CommandSender commandSender, String[] arguments) { + private void updateListConfigValue(@NotNull ConfigOption selectedOption, @NotNull CommandSender commandSender, + @NotNull String[] arguments) { FileConfiguration configuration = Stargate.getInstance().getConfiguration(); if (selectedOption == ConfigOption.PER_SIGN_COLORS) { @@ -190,7 +196,8 @@ public class CommandConfig implements CommandExecutor { * @param argumentsThe arguments given by the user
* @returnThe per-sign color string to update with, or null if the input was invalid
*/ - private String parsePerSignColorInput(CommandSender commandSender, String[] arguments) { + @Nullable + private String parsePerSignColorInput(@NotNull CommandSender commandSender, @NotNull String[] arguments) { //Make sure the sign type is an actual sign if (Material.matchMaterial(arguments[1] + "_SIGN") == null) { Stargate.getMessageSender().sendErrorMessage(commandSender, "The given sign type is invalid"); @@ -199,7 +206,8 @@ public class CommandConfig implements CommandExecutor { String colorString = arguments[1] + ":"; //Validate the colors given by the user - String[] errorMessage = new String[]{"The given main sign color is invalid!", "The given highlight sign color is invalid!"}; + String[] errorMessage = new String[]{"The given main sign color is invalid!", "The given highlight sign color " + + "is invalid!"}; String[] newColors = new String[2]; for (int i = 0; i < 2; i++) { if (validatePerSignColor(arguments[i + 2])) { @@ -220,9 +228,11 @@ public class CommandConfig implements CommandExecutor { * @param colorStringThe new color string to replace any previous value with
* @param configurationThe file configuration to update with the new per-sign colors
*/ - private void updatePerSignColors(String signType, String colorString, FileConfiguration configuration) { + private void updatePerSignColors(@NotNull String signType, @NotNull String colorString, + @NotNull FileConfiguration configuration) { ListThe color chosen by the user
* @returnTrue if the given color is valid
*/ - private boolean validatePerSignColor(String color) { + private boolean validatePerSignColor(@NotNull String color) { ChatColor newHighlightColor = parseColor(color); return newHighlightColor != null || color.equalsIgnoreCase("default") || color.equalsIgnoreCase("inverted"); @@ -251,7 +261,7 @@ public class CommandConfig implements CommandExecutor { * @param selectedOptionThe config option that was changed
* @param commandSenderThe command sender that executed the config command
*/ - private void saveAndReload(ConfigOption selectedOption, CommandSender commandSender) { + private void saveAndReload(@NotNull ConfigOption selectedOption, @NotNull CommandSender commandSender) { //Save the config file and reload if necessary Stargate.getInstance().saveConfig(); @@ -268,7 +278,8 @@ public class CommandConfig implements CommandExecutor { * @param commandSenderThe command sender to alert if the color is invalid
* @param valueThe new option value
*/ - private boolean registerColor(ConfigOption selectedOption, String value, CommandSender commandSender) { + private boolean registerColor(@NotNull ConfigOption selectedOption, @NotNull String value, + @NotNull CommandSender commandSender) { ChatColor parsedColor = parseColor(value); if (parsedColor == null) { commandSender.sendMessage(ChatColor.RED + "Invalid color given"); @@ -291,7 +302,8 @@ public class CommandConfig implements CommandExecutor { * @param valueThe value to parse
* @returnThe parsed color or null
*/ - private ChatColor parseColor(String value) { + @Nullable + private ChatColor parseColor(@NotNull String value) { try { return ChatColor.of(value.toUpperCase()); } catch (IllegalArgumentException | NullPointerException ignored) { @@ -307,7 +319,9 @@ public class CommandConfig implements CommandExecutor { * @param valueThe value given
* @returnAn integer, or null if it was invalid
*/ - private Integer getInteger(CommandSender commandSender, ConfigOption selectedOption, String value) { + @Nullable + private Integer getInteger(@NotNull CommandSender commandSender, @NotNull ConfigOption selectedOption, + @NotNull String value) { try { int intValue = Integer.parseInt(value); @@ -331,7 +345,9 @@ public class CommandConfig implements CommandExecutor { * @param valueThe value given
* @returnA double, or null if it was invalid
*/ - private Double getDouble(CommandSender commandSender, ConfigOption selectedOption, String value) { + @Nullable + private Double getDouble(@NotNull CommandSender commandSender, @NotNull ConfigOption selectedOption, + @NotNull String value) { try { double doubleValue = Double.parseDouble(value); @@ -353,7 +369,7 @@ public class CommandConfig implements CommandExecutor { * @param commandSenderThe command sender initiating the reload
* @param configOptionThe changed config option
*/ - private void reloadIfNecessary(CommandSender commandSender, ConfigOption configOption) { + private void reloadIfNecessary(@NotNull CommandSender commandSender, @NotNull ConfigOption configOption) { if (ConfigTag.requiresFullReload(configOption)) { //Reload everything Stargate.getStargateConfig().reload(commandSender); @@ -391,7 +407,7 @@ public class CommandConfig implements CommandExecutor { * @param senderThe command sender that sent the command
* @param optionThe config option to print information about
*/ - private void printConfigOptionValue(CommandSender sender, ConfigOption option) { + private void printConfigOptionValue(@NotNull CommandSender sender, @NotNull ConfigOption option) { Object value = Stargate.getStargateConfig().getConfigOptions().get(option); sender.sendMessage(getOptionDescription(option)); sender.sendMessage(ChatColor.GREEN + "Current value: " + ChatColor.GOLD + value); @@ -402,8 +418,8 @@ public class CommandConfig implements CommandExecutor { * * @param senderThe command sender to display the config list to
*/ - private void displayConfigValues(CommandSender sender) { - sender.sendMessage(ChatColor.GREEN + Stargate.getBackupString("prefix") + ChatColor.GOLD + + private void displayConfigValues(@NotNull CommandSender sender) { + sender.sendMessage(ChatColor.GREEN + Stargate.getBackupString(Message.PREFIX) + ChatColor.GOLD + "Config values:"); for (ConfigOption option : ConfigOption.values()) { sender.sendMessage(getOptionDescription(option)); @@ -416,7 +432,8 @@ public class CommandConfig implements CommandExecutor { * @param optionThe option to describe
* @returnA string describing the config option
*/ - private String getOptionDescription(ConfigOption option) { + @NotNull + private String getOptionDescription(@NotNull ConfigOption option) { Object defaultValue = option.getDefaultValue(); String stringValue = String.valueOf(defaultValue); if (option.getDataType() == OptionDataType.STRING_LIST) { diff --git a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java index ff09aa9..c2319d2 100644 --- a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java @@ -61,7 +61,9 @@ public class ConfigTabCompleter implements TabCompleter { * @param typedTextThe beginning of the typed text, for filtering matching results
* @returnSome or all of the valid values for the option
*/ - private ListThe arguments given by the user
* @returnSome or all of the valid values for the option
*/ - private ListThe arguments given by the user
* @returnThe options to give the user
*/ - private ListThe string to make into a list
- * @returnA list containing the string value
- */ - private ListThe available chat colors
*/ + @NotNull private ListThe command sender to get available commands for
* @returnThe commands available to the command sender
*/ - private ListThe description of what this config option does
* @param defaultValueThe default value of this config option
*/ - ConfigOption(String configNode, String description, Object defaultValue) { + ConfigOption(@NotNull String configNode, @NotNull String description, @NotNull Object defaultValue) { this.configNode = configNode; this.description = description; this.defaultValue = defaultValue; @@ -235,7 +238,7 @@ public enum ConfigOption { * @param nameThe name of the config option to get
* @returnThe corresponding config option, or null if the name is invalid
*/ - public static ConfigOption getByName(String name) { + public static @Nullable ConfigOption getByName(@NotNull String name) { for (ConfigOption option : ConfigOption.values()) { if (option.getName().equalsIgnoreCase(name)) { return option; @@ -249,7 +252,7 @@ public enum ConfigOption { * * @returnThe name of this config option
*/ - public String getName() { + public @NotNull String getName() { if (!this.configNode.contains(".")) { return this.configNode; } @@ -262,7 +265,7 @@ public enum ConfigOption { * * @returnThe data type used
*/ - public OptionDataType getDataType() { + public @NotNull OptionDataType getDataType() { return this.dataType; } @@ -271,7 +274,7 @@ public enum ConfigOption { * * @returnThis config option's config node
*/ - public String getConfigNode() { + public @NotNull String getConfigNode() { return this.configNode; } @@ -280,7 +283,7 @@ public enum ConfigOption { * * @returnThe description of this config option
*/ - public String getDescription() { + public @NotNull String getDescription() { return this.description; } @@ -289,7 +292,7 @@ public enum ConfigOption { * * @returnThis config option's default value
*/ - public Object getDefaultValue() { + public @NotNull Object getDefaultValue() { return this.defaultValue; } diff --git a/src/main/java/net/knarcraft/stargate/config/ConfigTag.java b/src/main/java/net/knarcraft/stargate/config/ConfigTag.java index 024b205..a992d46 100644 --- a/src/main/java/net/knarcraft/stargate/config/ConfigTag.java +++ b/src/main/java/net/knarcraft/stargate/config/ConfigTag.java @@ -1,25 +1,38 @@ package net.knarcraft.stargate.config; -import java.util.Arrays; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; /** * A config tag groups config values by a property */ public enum ConfigTag { - COLOR(new ConfigOption[]{ConfigOption.FREE_GATES_COLOR, ConfigOption.MAIN_SIGN_COLOR, - ConfigOption.HIGHLIGHT_SIGN_COLOR, ConfigOption.PER_SIGN_COLORS}), - FOLDER(new ConfigOption[]{ConfigOption.GATE_FOLDER, ConfigOption.PORTAL_FOLDER}), - DYNMAP(new ConfigOption[]{ConfigOption.ENABLE_DYNMAP, ConfigOption.DYNMAP_ICONS_DEFAULT_HIDDEN}); + /** + * Color-related configuration options + */ + COLOR(Set.of(ConfigOption.FREE_GATES_COLOR, ConfigOption.MAIN_SIGN_COLOR, ConfigOption.HIGHLIGHT_SIGN_COLOR, + ConfigOption.PER_SIGN_COLORS)), - private final ConfigOption[] taggedOptions; + /** + * Folder-altering configuration options + */ + FOLDER(Set.of(ConfigOption.GATE_FOLDER, ConfigOption.PORTAL_FOLDER)), + + /** + * Dynmap-related configuration options + */ + DYNMAP(Set.of(ConfigOption.ENABLE_DYNMAP, ConfigOption.DYNMAP_ICONS_DEFAULT_HIDDEN)); + + private final SetThe config options included in this tag
*/ - ConfigTag(ConfigOption[] taggedOptions) { + ConfigTag(@NotNull SetThe config option to check
* @returnTrue of the config option is tagged
*/ - public boolean isTagged(ConfigOption option) { - return Arrays.stream(taggedOptions).anyMatch((item) -> item == option); + public boolean isTagged(@NotNull ConfigOption option) { + return taggedOptions.contains(option); } /** @@ -39,7 +52,7 @@ public enum ConfigTag { * @param configOptionThe config option to check
* @returnTrue if changing the config option requires a "reload of colors" to take effect
*/ - public static boolean requiresColorReload(ConfigOption configOption) { + public static boolean requiresColorReload(@NotNull ConfigOption configOption) { return (COLOR.isTagged(configOption) && configOption != ConfigOption.FREE_GATES_COLOR); } @@ -49,7 +62,7 @@ public enum ConfigTag { * @param optionThe config option to check
* @returnTrue if changing the config option requires a full reload to take effect
*/ - public static boolean requiresFullReload(ConfigOption option) { + public static boolean requiresFullReload(@NotNull ConfigOption option) { return FOLDER.isTagged(option); } @@ -59,7 +72,7 @@ public enum ConfigTag { * @param configOptionThe config option to check
* @returnTrue if changing the config option requires a reload of all dynmap markers
*/ - public static boolean requiresDynmapReload(ConfigOption configOption) { + public static boolean requiresDynmapReload(@NotNull ConfigOption configOption) { return DYNMAP.isTagged(configOption); } @@ -69,7 +82,7 @@ public enum ConfigTag { * @param optionThe config option to check
* @returnTrue if changing the config option requires a portal reload to take effect
*/ - public static boolean requiresPortalReload(ConfigOption option) { + public static boolean requiresPortalReload(@NotNull ConfigOption option) { return COLOR.isTagged(option) || FOLDER.isTagged(option) || option == ConfigOption.VERIFY_PORTALS; } @@ -79,7 +92,7 @@ public enum ConfigTag { * @param optionThe config option to check
* @returnTrue if the language loader requires a reload
*/ - public static boolean requiresLanguageReload(ConfigOption option) { + public static boolean requiresLanguageReload(@NotNull ConfigOption option) { return option == ConfigOption.LANGUAGE; } @@ -89,7 +102,7 @@ public enum ConfigTag { * @param optionThe config option to check
* @returnTrue if economy requires a reload
*/ - public static boolean requiresEconomyReload(ConfigOption option) { + public static boolean requiresEconomyReload(@NotNull ConfigOption option) { return option == ConfigOption.USE_ECONOMY; } diff --git a/src/main/java/net/knarcraft/stargate/config/DynmapManager.java b/src/main/java/net/knarcraft/stargate/config/DynmapManager.java index 497ea84..67b015f 100644 --- a/src/main/java/net/knarcraft/stargate/config/DynmapManager.java +++ b/src/main/java/net/knarcraft/stargate/config/DynmapManager.java @@ -1,6 +1,7 @@ package net.knarcraft.stargate.config; import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.container.RelativeBlockVector; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalRegistry; import org.bukkit.Location; @@ -10,6 +11,8 @@ import org.dynmap.markers.GenericMarker; import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerIcon; import org.dynmap.markers.MarkerSet; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * A manager for dealing with everything Dynmap @@ -29,7 +32,7 @@ public final class DynmapManager { * @param dynmapAPIA reference
* @throws NullPointerExceptionIf dynmap has an invalid state
*/ - public static void initialize(DynmapAPI dynmapAPI) throws NullPointerException { + public static void initialize(@Nullable DynmapAPI dynmapAPI) throws NullPointerException { if (dynmapAPI == null || !dynmapAPI.markerAPIInitialized() || dynmapAPI.getMarkerAPI() == null) { markerSet = null; portalIcon = null; @@ -67,7 +70,7 @@ public final class DynmapManager { * * @param portalThe portal to add a marker for
*/ - public static void addPortalMarker(Portal portal) { + public static void addPortalMarker(@NotNull Portal portal) { if (markerSet == null || Stargate.getStargateConfig().isDynmapDisabled()) { return; } @@ -76,7 +79,13 @@ public final class DynmapManager { return; } - Location location = portal.getBlockAt(portal.getGate().getLayout().getExit()); + Location location; + @Nullable RelativeBlockVector exit = portal.getGate().getLayout().getExit(); + if (exit == null) { + location = portal.getTopLeft(); + } else { + location = portal.getBlockAt(exit); + } Marker marker = markerSet.createMarker(getPortalMarkerId(portal), portal.getName(), world.getName(), location.getX(), location.getY(), location.getZ(), portalIcon, false); if (marker == null) { @@ -112,7 +121,7 @@ public final class DynmapManager { * * @param portalThe portal to remove the marker for
*/ - public static void removePortalMarker(Portal portal) { + public static void removePortalMarker(@NotNull Portal portal) { if (markerSet == null || Stargate.getStargateConfig().isDynmapDisabled()) { return; } @@ -128,7 +137,7 @@ public final class DynmapManager { * @param portalThe portal to get a marker id for
* @return */ - private static String getPortalMarkerId(Portal portal) { + private static String getPortalMarkerId(@NotNull Portal portal) { return portal.getNetwork() + "-:-" + portal.getName(); } diff --git a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java index b3b5d34..d421ba8 100644 --- a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java @@ -11,6 +11,8 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Map; @@ -29,7 +31,7 @@ public final class EconomyConfig { * * @param configOptionsThe loaded config options to read
*/ - public EconomyConfig(MapAn economy object, or null if economy is disabled or not initialized
*/ + @Nullable public Economy getEconomy() { return economy; } @@ -71,6 +74,7 @@ public final class EconomyConfig { * * @returnAn instance of the Vault plugin, or null if Vault is not loaded
*/ + @Nullable public Plugin getVault() { return vault; } @@ -137,6 +141,7 @@ public final class EconomyConfig { * * @returnThe account all taxes are paid to
*/ + @Nullable public String getTaxAccount() { return (String) configOptions.get(ConfigOption.TAX_ACCOUNT); } @@ -158,6 +163,7 @@ public final class EconomyConfig { * @param amountThe amount to display
* @returnA formatted text string describing the amount
*/ + @NotNull public String format(int amount) { if (isEconomyEnabled()) { return economy.format(amount); @@ -172,7 +178,7 @@ public final class EconomyConfig { * @param pluginManagerThe plugin manager to get plugins from
* @returnTrue if economy was enabled
*/ - public boolean setupEconomy(PluginManager pluginManager) { + public boolean setupEconomy(@NotNull PluginManager pluginManager) { if (!isEconomyEnabled()) { return false; } @@ -186,10 +192,10 @@ public final class EconomyConfig { this.vault = vault; return true; } else { - Stargate.logInfo(Stargate.getString("ecoLoadError")); + Stargate.logInfo(Stargate.getString(Message.ECONOMY_LOAD_ERROR)); } } else { - Stargate.logInfo(Stargate.getString("vaultLoadError")); + Stargate.logInfo(Stargate.getString(Message.VAULT_LOAD_ERROR)); } configOptions.put(ConfigOption.USE_ECONOMY, false); return false; @@ -211,7 +217,7 @@ public final class EconomyConfig { * @param gateThe gate type used
* @returnThe cost of creating the gate
*/ - public int getCreateCost(Player player, Gate gate) { + public int getCreateCost(@NotNull Player player, @NotNull Gate gate) { if (isFree(player, "create")) { return 0; } else { @@ -226,7 +232,7 @@ public final class EconomyConfig { * @param gateThe gate type used
* @returnThe cost of destroying the gate
*/ - public int getDestroyCost(Player player, Gate gate) { + public int getDestroyCost(@NotNull Player player, @NotNull Gate gate) { if (isFree(player, "destroy")) { return 0; } else { @@ -241,7 +247,7 @@ public final class EconomyConfig { * @param permissionNodeThe free.permissionNode necessary to allow free gate {action}
* @return */ - private boolean isFree(Player player, String permissionNode) { + private boolean isFree(@NotNull Player player, @NotNull String permissionNode) { return !useEconomy() || PermissionHelper.hasPermission(player, "stargate.free." + permissionNode); } diff --git a/src/main/java/net/knarcraft/stargate/config/LanguageLoader.java b/src/main/java/net/knarcraft/stargate/config/LanguageLoader.java index bd8a335..cbfcfe6 100644 --- a/src/main/java/net/knarcraft/stargate/config/LanguageLoader.java +++ b/src/main/java/net/knarcraft/stargate/config/LanguageLoader.java @@ -3,15 +3,16 @@ package net.knarcraft.stargate.config; import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.util.FileHelper; import net.knarcraft.stargate.Stargate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; +import java.util.EnumMap; import java.util.Map; -import java.util.Set; /** * This class is responsible for loading all strings which are translated into several languages @@ -19,9 +20,9 @@ import java.util.Set; public final class LanguageLoader { private final String languageFolder; - private final MapThe folder containing the language files
*/ - public LanguageLoader(String languageFolder) { + public LanguageLoader(@NotNull String languageFolder) { this.languageFolder = languageFolder; File testFile = new File(languageFolder, "en.txt"); if (!testFile.exists()) { @@ -60,32 +61,34 @@ public final class LanguageLoader { } /** - * Gets the string to display given its name/key + * Gets the string to display given its message key * - * @param nameThe name/key of the string to display
- * @returnThe string in the user's preferred language
+ * @param messageThe message to display
+ * @returnThe message in the user's preferred language
*/ - public String getString(String name) { + @NotNull + public String getString(@NotNull Message message) { String value = null; if (loadedStringTranslations != null) { - value = loadedStringTranslations.get(name); + value = loadedStringTranslations.get(message); } if (value == null) { - value = getBackupString(name); + value = getBackupString(message); } return value; } /** - * Gets the string to display given its name/key + * Gets the string to display given its message key * - * @param nameThe name/key of the string to display
+ * @param messageThe message to display
* @returnThe string in the backup language (English)
*/ - public String getBackupString(String name) { + @NotNull + public String getBackupString(@NotNull Message message) { String value = null; if (loadedBackupStrings != null) { - value = loadedBackupStrings.get(name); + value = loadedBackupStrings.get(message); } if (value == null) { return ""; @@ -98,7 +101,7 @@ public final class LanguageLoader { * * @param chosenLanguageThe new plugin language
*/ - public void setChosenLanguage(String chosenLanguage) { + public void setChosenLanguage(@NotNull String chosenLanguage) { this.chosenLanguage = chosenLanguage; } @@ -107,8 +110,8 @@ public final class LanguageLoader { * * @param languageThe language to update
*/ - private void updateLanguage(String language) { - MapThe current values of the loaded/processed language
* @throws IOExceptionif unable to read a language file
*/ - private void readChangedLanguageStrings(InputStream inputStream, String language, MapAny custom language strings not recognized
* @throws IOExceptionIf unable to write to the language file
*/ - private void updateLanguageFile(String language, MapThe language to load
* @returnA mapping between loaded string indexes and the strings to display
*/ - private MapAn optional input stream to use. Defaults to using a file input stream
* @returnA mapping between loaded string indexes and the strings to display
*/ - private MapThe key of the message in the language files
+ */ + Message(@NotNull String key) { + this.key = key; + } + + /** + * Gets the language file key for this message + * + * @returnThis message's key
+ */ + @NotNull + public String getKey() { + return this.key; + } + + /** + * Gets the message corresponding to the given key + * + * @param keyThe key to get a message from
+ * @returnThe message, or null if not found
+ */ + @Nullable + public static Message getFromKey(@NotNull String key) { + for (Message message : Message.values()) { + if (message.getKey().equalsIgnoreCase(key)) { + return message; + } + } + + return null; + } + + @Override + @NotNull + public String toString() { + return this.getKey(); + } + +} diff --git a/src/main/java/net/knarcraft/stargate/config/MessageSender.java b/src/main/java/net/knarcraft/stargate/config/MessageSender.java index bfc706a..80ac7f2 100644 --- a/src/main/java/net/knarcraft/stargate/config/MessageSender.java +++ b/src/main/java/net/knarcraft/stargate/config/MessageSender.java @@ -2,6 +2,7 @@ package net.knarcraft.stargate.config; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; /** * The message sender is responsible sending messages to players with correct coloring and formatting @@ -15,7 +16,7 @@ public final class MessageSender { * * @param languageLoaderThe language loader to get translated strings from
*/ - public MessageSender(LanguageLoader languageLoader) { + public MessageSender(@NotNull LanguageLoader languageLoader) { this.languageLoader = languageLoader; } @@ -25,7 +26,7 @@ public final class MessageSender { * @param playerThe player to send the message to
* @param messageThe message to send
*/ - public void sendErrorMessage(CommandSender player, String message) { + public void sendErrorMessage(@NotNull CommandSender player, @NotNull String message) { sendMessage(player, message, true); } @@ -35,7 +36,7 @@ public final class MessageSender { * @param playerThe player to send the message to
* @param messageThe message to send
*/ - public void sendSuccessMessage(CommandSender player, String message) { + public void sendSuccessMessage(@NotNull CommandSender player, @NotNull String message) { sendMessage(player, message, false); } @@ -46,15 +47,15 @@ public final class MessageSender { * @param messageThe message to send
* @param errorWhether the message sent is an error
*/ - private void sendMessage(CommandSender sender, String message, boolean error) { + private void sendMessage(@NotNull CommandSender sender, @NotNull String message, boolean error) { if (message.isEmpty()) { return; } message = ChatColor.translateAlternateColorCodes('&', message); if (error) { - sender.sendMessage(ChatColor.RED + languageLoader.getString("prefix") + ChatColor.WHITE + message); + sender.sendMessage(ChatColor.RED + languageLoader.getString(Message.PREFIX) + ChatColor.WHITE + message); } else { - sender.sendMessage(ChatColor.GREEN + languageLoader.getString("prefix") + ChatColor.WHITE + message); + sender.sendMessage(ChatColor.GREEN + languageLoader.getString(Message.PREFIX) + ChatColor.WHITE + message); } } diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index 768277e..7a0004d 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -19,6 +19,7 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.messaging.Messenger; import org.dynmap.DynmapAPI; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; @@ -29,6 +30,8 @@ import java.util.Queue; import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * The stargate config is responsible for keeping track of all configuration values @@ -58,7 +61,7 @@ public final class StargateConfig { * * @param loggerThe logger to use for logging errors
*/ - public StargateConfig(Logger logger) { + public StargateConfig(@NotNull Logger logger) { this.logger = logger; configOptions = new HashMap<>(); @@ -76,6 +79,7 @@ public final class StargateConfig { * * @returnA reference to the config options map
*/ + @NotNull public MapThe loaded config options
*/ + @NotNull public MapThe open portals queue
*/ + @NotNull public QueueThe active portals queue
*/ + @NotNull public QueueThe object containing economy config values
*/ + @NotNull public EconomyConfig getEconomyConfig() { return this.economyConfig; } @@ -212,7 +220,7 @@ public final class StargateConfig { * * @param senderThe sender of the reload request
*/ - public void reload(CommandSender sender) { + public void reload(@NotNull CommandSender sender) { //Unload all saved data unload(); @@ -238,7 +246,7 @@ public final class StargateConfig { //Reload portal markers DynmapManager.addAllPortalMarkers(); - messageSender.sendErrorMessage(sender, languageLoader.getString("reloaded")); + messageSender.sendErrorMessage(sender, languageLoader.getString(Message.RELOADED)); } /** @@ -285,6 +293,7 @@ public final class StargateConfig { * * @returnThe managed worlds
*/ + @NotNull public SetThe name of the world to manage
*/ - public void addManagedWorld(String worldName) { + public void addManagedWorld(@NotNull String worldName) { managedWorlds.add(worldName); } @@ -303,7 +312,7 @@ public final class StargateConfig { * * @param worldNameThe name of the world to stop managing
*/ - public void removeManagedWorld(String worldName) { + public void removeManagedWorld(@NotNull String worldName) { managedWorlds.remove(worldName); } @@ -422,11 +431,18 @@ public final class StargateConfig { portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER); if (portalFolder.isEmpty()) { portalFolder = dataFolderPath + "/portals/"; + } else { + portalFolder = replacePluginFolderPath(portalFolder); } + Stargate.debug("StargateConfig::loadConfig", "Portal folder is " + portalFolder); + gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER); if (gateFolder.isEmpty()) { gateFolder = dataFolderPath + "/gates/"; + } else { + gateFolder = replacePluginFolderPath(gateFolder); } + Stargate.debug("StargateConfig::loadConfig", "Gate folder is " + gateFolder); //If users have an outdated config, assume they also need to update their default gates if (isMigrating) { @@ -443,11 +459,29 @@ public final class StargateConfig { Stargate.getInstance().saveConfig(); } + /** + * Replaces "plugins/Stargate" in a folder path, and replaces it with the full path relative to the data folder + * + * @param inputThe input string to replace in
+ * @returnThe replaced path, or the input if not applicable
+ */ + @NotNull + private String replacePluginFolderPath(@NotNull String input) { + Pattern pattern = Pattern.compile("(?i)^plugins[\\\\/]Stargate"); + Matcher matcher = pattern.matcher(input); + if (matcher.find()) { + return dataFolderPath + matcher.replaceAll(""); + } else { + return input; + } + } + /** * Gets the object containing configuration values regarding gates * * @returnGets the gate config
*/ + @NotNull public StargateGateConfig getStargateGateConfig() { return stargateGateConfig; } @@ -465,13 +499,13 @@ public final class StargateConfig { * * @param currentConfigurationThe current config to back up
*/ - private void migrateConfig(FileConfiguration currentConfiguration) { + private void migrateConfig(@NotNull FileConfiguration currentConfiguration) { String debugPath = "StargateConfig::migrateConfig"; //Save the old config just in case something goes wrong try { currentConfiguration.save(new File(dataFolderPath, "config.yml.old")); - } catch (IOException e) { + } catch (IOException exception) { Stargate.debug(debugPath, "Unable to save old backup and do migration"); return; } @@ -489,7 +523,7 @@ public final class StargateConfig { migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream( FileHelper.getInputStreamForInternalFile("/config-migrations.txt")), "=", ColorConversion.NORMAL); - } catch (IOException e) { + } catch (IOException exception) { Stargate.debug(debugPath, "Unable to load config migration file"); return; } @@ -530,9 +564,10 @@ public final class StargateConfig { */ private void setupVaultEconomy() { EconomyConfig economyConfig = getEconomyConfig(); - if (economyConfig.setupEconomy(Stargate.getPluginManager()) && economyConfig.getEconomy() != null) { + if (economyConfig.setupEconomy(Stargate.getPluginManager()) && economyConfig.getEconomy() != null && + economyConfig.getVault() != null) { String vaultVersion = economyConfig.getVault().getDescription().getVersion(); - Stargate.logInfo(Stargate.replaceVars(Stargate.getString("vaultLoaded"), "%version%", vaultVersion)); + Stargate.logInfo(Stargate.replacePlaceholders(Stargate.getString(Message.VAULT_LOADED), "%version%", vaultVersion)); } } @@ -578,6 +613,7 @@ public final class StargateConfig { * * @returnThe portal folder
*/ + @NotNull public String getPortalFolder() { return portalFolder; } @@ -589,6 +625,7 @@ public final class StargateConfig { * * @returnThe folder storing gate files
*/ + @NotNull public String getGateFolder() { return gateFolder; } @@ -598,6 +635,7 @@ public final class StargateConfig { * * @returnThe sender for sending messages to players
*/ + @NotNull public MessageSender getMessageSender() { return messageSender; } @@ -607,6 +645,7 @@ public final class StargateConfig { * * @returnThe language loader
*/ + @NotNull public LanguageLoader getLanguageLoader() { return languageLoader; } diff --git a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java index f97cd16..f3dc071 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java @@ -6,6 +6,7 @@ import net.knarcraft.stargate.portal.PortalSignDrawer; import net.md_5.bungee.api.ChatColor; import org.bukkit.Color; import org.bukkit.Material; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; @@ -26,7 +27,7 @@ public final class StargateGateConfig { * * @param configOptionsThe loaded config options to use
*/ - public StargateGateConfig(MapThe specified default colors
* @param colorMapsThe list of color maps to save the resulting colors to
*/ - private void parsePerSignColors(Object signColorSpecification, ChatColor[] defaultColors, - List