The input containing the variables
- * @param searchThe variables to replace
- * @param valuesThe replacement values
- * @returnThe input string with the search values replaced with the given values
- */ - public static String replaceVars(String input, String[] search, String[] values) { - if (search.length != values.length) { - throw new IllegalArgumentException("The number of search values and replace values do not match."); - } - for (int i = 0; i < search.length; i++) { - input = replaceVars(input, search[i], values[i]); - } - return input; - } - /** * Replaces a variable in a string * @@ -383,7 +365,8 @@ public class Stargate extends JavaPlugin { this.registerCommands(); //Check for any available updates - UpdateChecker.checkForUpdate(); + UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=97784", + Stargate::getPluginVersion, Stargate::setUpdateAvailable); } /** diff --git a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java index cb5447d..ff09aa9 100644 --- a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java @@ -14,6 +14,8 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingStartsWith; + /** * This is the completer for stargates config sub-command (/sg config) */ @@ -48,27 +50,10 @@ public class ConfigTabCompleter implements TabCompleter { for (ConfigOption option : ConfigOption.values()) { configOptionNames.add(option.getName()); } - return filterMatching(configOptionNames, args[0]); + return filterMatchingStartsWith(configOptionNames, args[0]); } } - /** - * Find completable strings which match the text typed by the command's sender - * - * @param valuesThe values to filter
- * @param typedTextThe text the player has started typing
- * @returnThe given string values which start with the player's typed text
- */ - private ListThe input to translate color codes for
+ * @returnThe input with color codes converted translated from & to ยง
+ */ + private String translateAllColorCodes(String input) { + return ColorHelper.translateColorCodes(input, ColorConversion.RGB); + } + } diff --git a/src/main/java/net/knarcraft/stargate/portal/teleporter/VehicleTeleporter.java b/src/main/java/net/knarcraft/stargate/portal/teleporter/VehicleTeleporter.java index 20d4353..4f3e42c 100644 --- a/src/main/java/net/knarcraft/stargate/portal/teleporter/VehicleTeleporter.java +++ b/src/main/java/net/knarcraft/stargate/portal/teleporter/VehicleTeleporter.java @@ -169,7 +169,7 @@ public class VehicleTeleporter extends EntityTeleporter { //Spawn a new vehicle Vehicle newVehicle = vehicleWorld.spawn(exit, teleportingVehicle.getClass()); if (teleportingVehicle instanceof Boat boat) { - ((Boat) newVehicle).setWoodType(boat.getWoodType()); + ((Boat) newVehicle).setBoatType(boat.getBoatType()); } //Remove the old vehicle if (teleportingVehicle.eject()) { diff --git a/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java b/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java deleted file mode 100644 index 0886b94..0000000 --- a/src/main/java/net/knarcraft/stargate/utility/ColorHelper.java +++ /dev/null @@ -1,54 +0,0 @@ -package net.knarcraft.stargate.utility; - -import net.md_5.bungee.api.ChatColor; -import org.bukkit.Color; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A helper class for dealing with colors - */ -public final class ColorHelper { - - private ColorHelper() { - - } - - /** - * Inverts the given color - * - * @param colorThe color to invert
- * @returnThe inverted color
- */ - public static Color invert(Color color) { - return color.setRed(255 - color.getRed()).setGreen(255 - color.getGreen()).setBlue(255 - color.getBlue()); - } - - /** - * Gets the chat color corresponding to the given color - * - * @param colorThe color to convert into a chat color
- * @returnThe resulting chat color
- */ - public static ChatColor fromColor(Color color) { - return ChatColor.of(String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue())); - } - - /** - * Translates all found color codes to formatting in a string - * - * @param messageThe string to search for color codes
- * @returnThe message with color codes translated
- */ - public static String translateAllColorCodes(String message) { - message = ChatColor.translateAlternateColorCodes('&', message); - Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); - Matcher matcher = pattern.matcher(message); - while (matcher.find()) { - message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); - } - return message; - } - -} diff --git a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java index e3ab32c..6c58e52 100644 --- a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java @@ -1,5 +1,6 @@ package net.knarcraft.stargate.utility; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.EconomyConfig; import net.knarcraft.stargate.portal.Portal; @@ -78,7 +79,7 @@ public final class EconomyHelper { */ public static void sendObtainMessage(String portalName, Player portalOwner, int earnings) { String obtainedMsg = Stargate.getString("ecoObtain"); - obtainedMsg = replaceVars(obtainedMsg, portalName, earnings); + obtainedMsg = replacePlaceholders(obtainedMsg, portalName, earnings); Stargate.getMessageSender().sendSuccessMessage(portalOwner, obtainedMsg); } @@ -91,7 +92,7 @@ public final class EconomyHelper { */ public static void sendDeductMessage(String portalName, Player player, int cost) { String deductMsg = Stargate.getString("ecoDeduct"); - deductMsg = replaceVars(deductMsg, portalName, cost); + deductMsg = replacePlaceholders(deductMsg, portalName, cost); Stargate.getMessageSender().sendSuccessMessage(player, deductMsg); } @@ -104,7 +105,7 @@ public final class EconomyHelper { */ public static void sendInsufficientFundsMessage(String portalName, Player player, int cost) { String inFundMsg = Stargate.getString("ecoInFunds"); - inFundMsg = replaceVars(inFundMsg, portalName, cost); + inFundMsg = replacePlaceholders(inFundMsg, portalName, cost); Stargate.getMessageSender().sendErrorMessage(player, inFundMsg); } @@ -117,7 +118,7 @@ public final class EconomyHelper { */ public static void sendRefundMessage(String portalName, Player player, int cost) { String refundMsg = Stargate.getString("ecoRefund"); - refundMsg = replaceVars(refundMsg, portalName, -cost); + refundMsg = replacePlaceholders(refundMsg, portalName, -cost); Stargate.getMessageSender().sendSuccessMessage(player, refundMsg); } @@ -239,8 +240,8 @@ public final class EconomyHelper { * @param costThe cost for a given interaction
* @returnThe same string with cost and portal variables replaced
*/ - private static String replaceVars(String message, String portalName, int cost) { - return Stargate.replaceVars(message, new String[]{"%cost%", "%portal%"}, + private static String replacePlaceholders(String message, String portalName, int cost) { + return StringFormatter.replacePlaceholders(message, new String[]{"%cost%", "%portal%"}, new String[]{Stargate.getEconomyConfig().format(cost), portalName}); } diff --git a/src/main/java/net/knarcraft/stargate/utility/FileHelper.java b/src/main/java/net/knarcraft/stargate/utility/FileHelper.java deleted file mode 100644 index b4cdac5..0000000 --- a/src/main/java/net/knarcraft/stargate/utility/FileHelper.java +++ /dev/null @@ -1,127 +0,0 @@ -package net.knarcraft.stargate.utility; - -import net.md_5.bungee.api.ChatColor; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -/** - * Helper class for reading files - */ -public final class FileHelper { - - private FileHelper() { - - } - - /** - * Gets an input stream from a string pointing to an internal file - * - *This is used for getting an input stream for reading a file contained within the compiled .jar file. The file - * should be in the resources directory, and the file path should start with a forward slash ("/") character.
- * - * @param fileThe file to read
- * @returnAn input stream for the file
- */ - public static InputStream getInputStreamForInternalFile(String file) { - return FileHelper.class.getResourceAsStream(file); - } - - /** - * Gets a buffered reader from a string pointing to a file - * - * @param fileThe file to read
- * @returnA buffered reader reading the file
- * @throws FileNotFoundExceptionIf the given file does not exist
- */ - public static BufferedReader getBufferedReaderFromString(String file) throws FileNotFoundException { - FileInputStream fileInputStream = new FileInputStream(file); - return getBufferedReaderFromInputStream(fileInputStream); - } - - /** - * Gets a buffered reader given an input stream - * - * @param inputStreamThe input stream to read
- * @returnA buffered reader reading the input stream
- */ - public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream) { - InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); - return new BufferedReader(inputStreamReader); - } - - /** - * Gets a buffered writer from a string pointing to a file - * - * @param fileThe file to write to
- * @returnA buffered writer writing to the file
- * @throws FileNotFoundExceptionIf the file does not exist
- */ - public static BufferedWriter getBufferedWriterFromString(String file) throws FileNotFoundException { - FileOutputStream fileOutputStream = new FileOutputStream(file); - OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8); - return new BufferedWriter(outputStreamWriter); - } - - /** - * Reads key/value pairs from an input stream - * - * @param bufferedReaderThe buffered reader to read
- * @returnA map containing the read pairs
- * @throws IOExceptionIf unable to read from the stream
- */ - public static MapThe string to remove the BOM from
- * @returnA string guaranteed without a BOM
- */ - private static String removeUTF8BOM(String string) { - String UTF8_BOM = "\uFEFF"; - if (string.startsWith(UTF8_BOM)) { - string = string.substring(1); - } - return string; - } - -} diff --git a/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java b/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java deleted file mode 100644 index 2df40f4..0000000 --- a/src/main/java/net/knarcraft/stargate/utility/UpdateChecker.java +++ /dev/null @@ -1,87 +0,0 @@ -package net.knarcraft.stargate.utility; - -import net.knarcraft.stargate.Stargate; -import org.bukkit.scheduler.BukkitScheduler; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.logging.Level; - -/** - * The update checker is responsible for looking for new updates - */ -public final class UpdateChecker { - - private final static String APIResourceURL = "https://api.spigotmc.org/legacy/update.php?resource=97784"; - private final static String updateNotice = "A new update is available: %s (You are still on %s)"; - - private UpdateChecker() { - - } - - /** - * Checks if there's a new update available, and alerts the user if necessary - */ - public static void checkForUpdate() { - BukkitScheduler scheduler = Stargate.getInstance().getServer().getScheduler(); - scheduler.runTaskAsynchronously(Stargate.getInstance(), UpdateChecker::queryAPI); - } - - /** - * Queries the spigot API to check for a newer version, and informs the user - */ - private static void queryAPI() { - try { - InputStream inputStream = new URL(APIResourceURL).openStream(); - BufferedReader reader = FileHelper.getBufferedReaderFromInputStream(inputStream); - //There should only be one line of output - String newVersion = reader.readLine(); - reader.close(); - - String oldVersion = Stargate.getPluginVersion(); - //If there is a newer version, notify the user - if (isVersionHigher(oldVersion, newVersion)) { - Stargate.getConsoleLogger().log(Level.INFO, Stargate.getBackupString("prefix") + - getUpdateAvailableString(newVersion, oldVersion)); - Stargate.setUpdateAvailable(newVersion); - } - } catch (IOException e) { - Stargate.debug("UpdateChecker", "Unable to get newest version."); - } - } - - /** - * Gets the string to display to a user to alert about a new update - * - * @param newVersionThe new available plugin version
- * @param oldVersionThe old (current) plugin version
- * @returnThe string to display
- */ - public static String getUpdateAvailableString(String newVersion, String oldVersion) { - return String.format(updateNotice, newVersion, oldVersion); - } - - /** - * Decides whether one version number is higher than another - * - * @param oldVersionThe old version to check
- * @param newVersionThe new version to check
- * @returnTrue if the new version is higher than the old one
- */ - public static boolean isVersionHigher(String oldVersion, String newVersion) { - String[] oldVersionParts = oldVersion.split("\\."); - String[] newVersionParts = newVersion.split("\\."); - int versionLength = Math.max(oldVersionParts.length, newVersionParts.length); - for (int i = 0; i < versionLength; i++) { - int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0; - int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0; - if (newVersionNumber != oldVersionNumber) { - return newVersionNumber > oldVersionNumber; - } - } - return false; - } - -} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c717434..283fe85 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: Stargate main: net.knarcraft.stargate.Stargate -version: 0.9.4.1 +version: '${project.version}' description: Stargate mod for Bukkit Revived author: EpicKnarvik97 authors: [ Drakia, PseudoKnight, EpicKnarvik97 ]