From 04d4bc8303d51ec0f5663b3a9f5481bcd9368168 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 7 Nov 2022 01:43:38 +0100 Subject: [PATCH] Uses KnarLib where possible --- pom.xml | 26 +++++- .../BooksWithoutBorders.java | 5 +- .../command/CommandCopy.java | 5 +- .../command/CommandDelete.java | 2 +- .../command/CommandEncrypt.java | 2 +- .../command/CommandGive.java | 7 +- .../command/CommandLoad.java | 7 +- .../command/CommandSetBookPrice.java | 5 +- .../command/CommandSetLore.java | 5 +- .../command/CommandSetTitle.java | 5 +- .../utility/BookFormatter.java | 23 +---- .../utility/FileHelper.java | 15 --- ...lper.java => TabCompletionTypeHelper.java} | 38 +------- .../utility/UpdateChecker.java | 92 ------------------- 14 files changed, 54 insertions(+), 183 deletions(-) rename src/main/java/net/knarcraft/bookswithoutborders/utility/{TabCompletionHelper.java => TabCompletionTypeHelper.java} (51%) delete mode 100644 src/main/java/net/knarcraft/bookswithoutborders/utility/UpdateChecker.java diff --git a/pom.xml b/pom.xml index e99ab06..f43b0ad 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ A continuation of the original Books Without Borders - 17 + 16 UTF-8 @@ -31,8 +31,8 @@ maven-compiler-plugin 3.8.1 - 17 - 17 + ${java.version} + ${java.version} @@ -47,6 +47,20 @@ false + + + net.knarcraft:knarlib + + net/knarcraft/knarlib/** + + + + + *.MF + *.yml + + + @@ -84,6 +98,12 @@ 1.7 provided + + net.knarcraft + knarlib + 1.0-SNAPSHOT + compile + org.jetbrains annotations diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 8dd8c05..d8c6243 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -27,7 +27,8 @@ import net.knarcraft.bookswithoutborders.listener.BookEventListener; import net.knarcraft.bookswithoutborders.listener.PlayerEventListener; import net.knarcraft.bookswithoutborders.listener.SignEventListener; import net.knarcraft.bookswithoutborders.utility.FileHelper; -import net.knarcraft.bookswithoutborders.utility.UpdateChecker; +import net.knarcraft.knarlib.KnarLib; +import net.knarcraft.knarlib.util.UpdateChecker; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -110,6 +111,8 @@ public class BooksWithoutBorders extends JavaPlugin { @Override public void onEnable() { + KnarLib.setPlugin(this); + FileConfiguration config = this.getConfig(); config.options().copyDefaults(true); this.saveDefaultConfig(); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java index 1b4fdd5..9f4557f 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java @@ -5,7 +5,8 @@ import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.EconomyHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -142,7 +143,7 @@ public class CommandCopy implements TabExecutor { @NotNull String[] args) { int argumentCount = args.length; if (argumentCount == 1) { - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getNumbers(1, 20), args[0]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getNumbers(1, 20), args[0]); } return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 9f3ee34..371e402 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -4,7 +4,7 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index 7d66aef..a44126e 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -5,7 +5,7 @@ import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index b82e32d..bf02400 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -4,7 +4,8 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -132,12 +133,12 @@ public class CommandGive implements TabExecutor { return null; } else if (argumentCount == 3) { //Number of copies - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleansAndNumbers(1, 3), args[2]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), args[2]); } else if (argumentCount == 4) { //Signed try { Integer.parseInt(args[2]); - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleans(), args[3]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), args[3]); } catch (NumberFormatException e) { return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index 1538c3c..fb6919b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -4,7 +4,8 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -114,12 +115,12 @@ public class CommandLoad implements TabExecutor { args[0]); } else if (argumentCount == 2) { //Number of copies - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleansAndNumbers(1, 3), args[1]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), args[1]); } else if (argumentCount == 3) { //Signed try { Integer.parseInt(args[1]); - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleans(), args[2]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), args[2]); } catch (NumberFormatException e) { return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java index 2c8f681..efc0237 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java @@ -4,7 +4,8 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.utility.EconomyHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; -import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; +import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -144,7 +145,7 @@ public class CommandSetBookPrice implements TabExecutor { if (argumentCount == 1) { return TabCompletionHelper.filterMatchingStartsWith(paymentTypes, args[0]); } else if (argumentCount == 2) { - return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getNumbers(1, 3), args[1]); + return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getNumbers(1, 3), args[1]); } return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java index 89dfe10..3bf26ee 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java @@ -2,8 +2,9 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; -import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -44,7 +45,7 @@ public class CommandSetLore implements TabExecutor { String rawLore = String.join(" ", args); //Format lore - rawLore = BookFormatter.translateAllColorCodes(rawLore); + rawLore = ColorHelper.translateColorCodes(rawLore, ColorConversion.RGB); String[] loreParts = rawLore.split(BooksWithoutBordersConfig.getLoreSeparator()); List newLore = new ArrayList<>(Arrays.asList(loreParts)); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java index 8e1c909..f7aefa1 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java @@ -1,8 +1,9 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; -import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -41,7 +42,7 @@ public class CommandSetTitle implements TabExecutor { } String title = String.join(" ", args); - title = BookFormatter.translateAllColorCodes(title); + title = ColorHelper.translateColorCodes(title, ColorConversion.RGB); ItemMeta itemMetadata = heldItem.getItemMeta(); if (itemMetadata == null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java index 186ade1..686cb21 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java @@ -1,13 +1,12 @@ package net.knarcraft.bookswithoutborders.utility; -import net.md_5.bungee.api.ChatColor; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * A class for formatting text to fit books @@ -109,26 +108,10 @@ public final class BookFormatter { public static BookMeta formatPages(BookMeta bookMeta) { List formattedPages = new ArrayList<>(Objects.requireNonNull(bookMeta).getPageCount()); for (String page : bookMeta.getPages()) { - formattedPages.add(BookFormatter.translateAllColorCodes(page)); + formattedPages.add(ColorHelper.translateColorCodes(page, ColorConversion.RGB)); } bookMeta.setPages(formattedPages); return bookMeta; } - /** - * Translates all found color codes to formatting in a string - * - * @param message

The string to search for color codes

- * @return

The 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/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java index 6fe0158..8dd0f70 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java @@ -6,11 +6,7 @@ import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -187,15 +183,4 @@ public final class FileHelper { return foundDuplicates; } - /** - * Gets a buffered reader given an input stream - * - * @param inputStream

The input stream to read

- * @return

A buffered reader reading the input stream

- */ - public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream) { - InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); - return new BufferedReader(inputStreamReader); - } - } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionTypeHelper.java similarity index 51% rename from src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionHelper.java rename to src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionTypeHelper.java index b40454c..a2b105a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/TabCompletionTypeHelper.java @@ -6,43 +6,9 @@ import java.util.List; /** * Helper class for getting string lists required for auto-completion */ -public final class TabCompletionHelper { +public final class TabCompletionTypeHelper { - private TabCompletionHelper() { - } - - /** - * Finds tab complete values that contain the typed text - * - * @param values

The values to filter

- * @param typedText

The text the player has started typing

- * @return

The given string values that contain the player's typed text

- */ - public static List filterMatchingContains(List values, String typedText) { - List configValues = new ArrayList<>(); - for (String value : values) { - if (value.toLowerCase().contains(typedText.toLowerCase())) { - configValues.add(value); - } - } - return configValues; - } - - /** - * Finds tab complete values that match the start of the typed text - * - * @param values

The values to filter

- * @param typedText

The text the player has started typing

- * @return

The given string values that start with the player's typed text

- */ - public static List filterMatchingStartsWith(List values, String typedText) { - List configValues = new ArrayList<>(); - for (String value : values) { - if (value.toLowerCase().startsWith(typedText.toLowerCase())) { - configValues.add(value); - } - } - return configValues; + private TabCompletionTypeHelper() { } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/UpdateChecker.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/UpdateChecker.java deleted file mode 100644 index 9e455db..0000000 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/UpdateChecker.java +++ /dev/null @@ -1,92 +0,0 @@ -package net.knarcraft.bookswithoutborders.utility; - -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitScheduler; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.logging.Level; - -/** - * The update checker is responsible for looking for new updates - */ -public final class UpdateChecker { - - 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(Plugin plugin, String apiResourceURL, Supplier getVersionMethod, - Consumer setVersionMethod) { - BukkitScheduler scheduler = plugin.getServer().getScheduler(); - scheduler.runTaskAsynchronously(plugin, () -> UpdateChecker.queryAPI(plugin, apiResourceURL, getVersionMethod, - setVersionMethod)); - } - - /** - * Queries the spigot API to check for a newer version, and informs the user - */ - private static void queryAPI(Plugin plugin, String APIResourceURL, Supplier getVersionMethod, - Consumer setVersionMethod) { - 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 = getVersionMethod.get(); - //If there is a newer version, notify the user - if (isVersionHigher(oldVersion, newVersion)) { - plugin.getLogger().log(Level.INFO, getUpdateAvailableString(newVersion, oldVersion)); - if (setVersionMethod != null) { - setVersionMethod.accept(newVersion); - } - } - } catch (IOException e) { - plugin.getLogger().log(Level.WARNING, "Unable to get newest version."); - } - } - - /** - * Gets the string to display to a user to alert about a new update - * - * @param newVersion

The new available plugin version

- * @param oldVersion

The old (current) plugin version

- * @return

The 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 oldVersion

The old version to check

- * @param newVersion

The new version to check

- * @return

True 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; - } - -}