diff --git a/build.gradle.kts b/build.gradle.kts index 4edfc0b..5c7de3d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,13 +15,15 @@ java { repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") + maven("https://repo.fancyplugins.de/releases") } dependencies { compileOnly("dev.folia:folia-api:1.20.4-R0.1-SNAPSHOT") + compileOnly("de.oliver.FancyAnalytics:logger:0.0.5") // database drivers - compileOnly("org.xerial:sqlite-jdbc:3.42.0.1") + compileOnly("org.xerial:sqlite-jdbc:3.46.0.0") compileOnly("mysql:mysql-connector-java:8.0.33") } diff --git a/src/main/java/de/oliver/fancylib/DistributedWorkload.java b/src/main/java/de/oliver/fancylib/DistributedWorkload.java deleted file mode 100644 index 3257247..0000000 --- a/src/main/java/de/oliver/fancylib/DistributedWorkload.java +++ /dev/null @@ -1,113 +0,0 @@ -package de.oliver.fancylib; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.function.Supplier; - -public class DistributedWorkload implements Runnable { - - private final String workloadName; - private final Consumer action; - private final Predicate escapeCondition; - private final List>> suppliedValueMatrix; - private final int distributionSize; - private final boolean runAsync; - private int currentPosition; - - public DistributedWorkload(String workloadName, Consumer action, Predicate escapeCondition, int distributionSize, boolean runAsync) { - this.workloadName = workloadName; - this.action = action; - this.escapeCondition = escapeCondition; - this.distributionSize = distributionSize; - this.runAsync = runAsync; - this.currentPosition = 0; - this.suppliedValueMatrix = new ArrayList<>(distributionSize); - for (int i = 0; i < distributionSize; i++) { - this.suppliedValueMatrix.add(new LinkedList<>()); - } - } - - public void addValue(Supplier valueSupplier) { - List> smallestList = suppliedValueMatrix.get(0); - - for (int i = 1; i < distributionSize; i++) { - if (smallestList.size() == 0) { - break; - } - - if (suppliedValueMatrix.get(i).size() < smallestList.size()) { - smallestList = suppliedValueMatrix.get(i); - } - } - - smallestList.add(valueSupplier); - } - - private void proceedPosition() { - currentPosition++; - if (currentPosition == distributionSize) { - currentPosition = 0; - } - } - - private boolean executeThenCheck(Supplier valueSupplier) { - T value = valueSupplier.get(); - action.accept(value); - return escapeCondition.test(value); - } - - @Override - public void run() { - if (runAsync) { - new Thread(this::runWorkload).start(); - } else { - runWorkload(); - } - } - - public void runAll() { - new Thread(() -> { - long time = System.currentTimeMillis(); - - for (LinkedList> valueMatrix : suppliedValueMatrix) { - for (Supplier supplier : valueMatrix) { - executeThenCheck(supplier); - } - } - - time = System.currentTimeMillis() - time; - -// Bukkit.getLogger().info("Executed all workloads of '" + workloadName + "' in " + time + "ms"); - - }).start(); - } - - private void runWorkload() { - long startTime = System.currentTimeMillis(); - - List> suppliers = suppliedValueMatrix.get(currentPosition); - int amount = suppliers.size(); - - suppliers.removeIf(this::executeThenCheck); - - proceedPosition(); - - long time = System.currentTimeMillis() - startTime; - if (amount > 0) { -// Bukkit.getLogger().info("DistributedWorkload '" + workloadName + "' round " + (currentPosition) + "/" + distributionSize + " with " + amount + " suppliers took " + time + "ms"); - } - } - - - public List getAllSuppliers() { - List allSuppliers = new ArrayList<>(); - for (LinkedList> ll : suppliedValueMatrix) { - ll.stream().map(Supplier::get).forEach(allSuppliers::add); - } - - return allSuppliers; - } -} \ No newline at end of file diff --git a/src/main/java/de/oliver/fancylib/FancyLib.java b/src/main/java/de/oliver/fancylib/FancyLib.java index 1b29323..dd2cd86 100644 --- a/src/main/java/de/oliver/fancylib/FancyLib.java +++ b/src/main/java/de/oliver/fancylib/FancyLib.java @@ -1,6 +1,6 @@ package de.oliver.fancylib; -import de.oliver.fancylib.checksumChecker.ChecksumFetcher; +import de.oliver.fancyanalytics.logger.ExtendedFancyLogger; import de.oliver.fancylib.gui.inventoryClick.InventoryClickListener; import de.oliver.fancylib.gui.inventoryClick.impl.CancelInventoryItemClick; import de.oliver.fancylib.gui.inventoryClick.impl.ChangePageInventoryItemClick; @@ -9,80 +9,38 @@ import de.oliver.fancylib.serverSoftware.schedulers.BukkitScheduler; import de.oliver.fancylib.serverSoftware.schedulers.FancyScheduler; import de.oliver.fancylib.serverSoftware.schedulers.FoliaScheduler; -import org.apache.maven.artifact.versioning.ComparableVersion; import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.ApiStatus; -import java.io.File; - public class FancyLib { - public static final ComparableVersion VERSION = new ComparableVersion("1.0.3"); - - private static JavaPlugin plugin; - private static FancyScheduler scheduler; + private static FancyLib instance; + private static ExtendedFancyLogger logger = new ExtendedFancyLogger("FancyLib"); - public static Plugin getPlugin() { - return plugin; - } + private final JavaPlugin plugin; + private final FancyScheduler scheduler; - /** - * @param pluginJarFile you can get this by calling JavaPlugin#getJarFile - */ - public static void setPlugin(JavaPlugin plugin, File pluginJarFile) { - if (FancyLib.plugin != null) { - return; - } - - FancyLib.plugin = plugin; - scheduler = ServerSoftware.isFolia() + public FancyLib(JavaPlugin plugin) { + instance = this; + this.plugin = plugin; + this.scheduler = ServerSoftware.isFolia() ? new FoliaScheduler(plugin) : new BukkitScheduler(plugin); } - private static void checkChecksums() { - String actualChecksum = ChecksumFetcher.getChecksum(plugin.getName(), plugin.getDescription().getVersion()); - String fileChecksum = FileUtils.getSHA256Checksum(FileUtils.findFirstFileByName(new File("plugins"), plugin.getName())); + public static FancyLib getInstance() { + return instance; + } - if (!actualChecksum.equals("N/A") && !fileChecksum.equals("N/A")) { - if (!actualChecksum.equals(fileChecksum)) { - plugin.getLogger().warning(""" - ---------------------------------------------------------------- - [!] - [!] - [!] - [!] Potential security risk detected!! - [!] - [!] - [!] - [!] The checksum of the plugin jar does not match the official checksum! - [!] This version might be modified and could contain maleware! - [!] Please download the plugin from the official source! - [!] Official checksum: %actualChecksum% - [!] This file's checksum: %fileChecksum% - [!] - [!] - [!] - [!] - [!] - [!] - [!] - ---------------------------------------------------------------- - """ - .replace("%actualChecksum%", actualChecksum) - .replace("%fileChecksum%", fileChecksum) - ); - Bukkit.getPluginManager().disablePlugin(plugin); - } - } + public static ExtendedFancyLogger getLogger() { + return logger; } /** * Registers the listeners for the inventory click and player interact events. */ - public static void registerListeners() { + public void registerListeners() { CancelInventoryItemClick.INSTANCE.register(); ChangePageInventoryItemClick.INSTANCE.register(); @@ -91,7 +49,11 @@ public static void registerListeners() { } @ApiStatus.Internal - public static FancyScheduler getScheduler() { + public FancyScheduler getScheduler() { return scheduler; } + + public JavaPlugin getPlugin() { + return plugin; + } } diff --git a/src/main/java/de/oliver/fancylib/LanguageConfig.java b/src/main/java/de/oliver/fancylib/LanguageConfig.java deleted file mode 100644 index 9aff557..0000000 --- a/src/main/java/de/oliver/fancylib/LanguageConfig.java +++ /dev/null @@ -1,84 +0,0 @@ -package de.oliver.fancylib; - -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.Plugin; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Pattern; - -@Deprecated -public class LanguageConfig { - - private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{([^}]+)\\}"); - - private final Plugin plugin; - private final Map lang; - private final Map defaultLang; - private File configFile; - - @Deprecated - public LanguageConfig(Plugin plugin) { - this.plugin = plugin; - this.configFile = new File("plugins" + File.separator + plugin.getName() + File.separator + "lang.yml"); - this.lang = new HashMap<>(); - this.defaultLang = new HashMap<>(); - } - - @Deprecated - public void addDefaultLang(String key, String message) { - defaultLang.put(key, message); - } - - /** - * @param placeholders format: placeholder1, replacement1, placeholder2, replacement2 ... - */ - @Deprecated - public String get(String key, String... placeholders) { - String message = lang.getOrDefault(key, "Error: message not found"); - - for (int i = 0; i < placeholders.length; i += 2) { - String placeholder = placeholders[i]; - String replacement = placeholders[i + 1]; - - message = message.replace("{" + placeholder + "}", replacement); - } - - return message; - } - - @Deprecated - public void load() { - lang.clear(); - - if (!configFile.exists()) { - try { - configFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); - config.options().width(1000); - - for (Map.Entry entry : defaultLang.entrySet()) { - if (!config.isSet("messages." + entry.getKey())) { - config.set("messages." + entry.getKey(), entry.getValue()); - } - } - - for (String key : config.getConfigurationSection("messages").getKeys(false)) { - String message = config.getString("messages." + key); - lang.put(key, message); - } - - try { - config.save(configFile); - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff --git a/src/main/java/de/oliver/fancylib/checksumChecker/ChecksumFetcher.java b/src/main/java/de/oliver/fancylib/checksumChecker/ChecksumFetcher.java deleted file mode 100644 index 2bd3839..0000000 --- a/src/main/java/de/oliver/fancylib/checksumChecker/ChecksumFetcher.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.oliver.fancylib.checksumChecker; - -import de.oliver.fancylib.versionFetcher.VersionFetcher; - -public class ChecksumFetcher { - - private static final String CHECKSUM_URL = "https://raw.githubusercontent.com/FancyMcPlugins/%plugin%/main/checksums.txt"; - - public static String getChecksum(String plugin, String version) { - String data = VersionFetcher.getDataFromUrl(CHECKSUM_URL.replace("%plugin%", plugin)); - String[] lines = data.split("\n"); - for (String line : lines) { - - String[] parts = line.split(" "); - if (parts.length != 2) { - continue; - } - - if (parts[0].equals(version)) { - return parts[1]; - } - } - - return "N/A"; - } - -} diff --git a/src/main/java/de/oliver/fancylib/gui/customInventories/PageInventory.java b/src/main/java/de/oliver/fancylib/gui/customInventories/PageInventory.java index 1c92321..0ea3c40 100644 --- a/src/main/java/de/oliver/fancylib/gui/customInventories/PageInventory.java +++ b/src/main/java/de/oliver/fancylib/gui/customInventories/PageInventory.java @@ -12,7 +12,7 @@ public interface PageInventory { - NamespacedKey PAGE_KEY = new NamespacedKey(FancyLib.getPlugin(), "page"); + NamespacedKey PAGE_KEY = new NamespacedKey(FancyLib.getInstance().getPlugin(), "page"); static ItemStack previousPage(int currentPage) { ItemStack previousPage = new ItemStack(Material.ARROW); diff --git a/src/main/java/de/oliver/fancylib/gui/inventoryClick/InventoryItemClick.java b/src/main/java/de/oliver/fancylib/gui/inventoryClick/InventoryItemClick.java index dce845a..253c5b9 100644 --- a/src/main/java/de/oliver/fancylib/gui/inventoryClick/InventoryItemClick.java +++ b/src/main/java/de/oliver/fancylib/gui/inventoryClick/InventoryItemClick.java @@ -11,7 +11,7 @@ public interface InventoryItemClick { - NamespacedKey ON_CLICK_KEY = new NamespacedKey(FancyLib.getPlugin(), "onclick"); + NamespacedKey ON_CLICK_KEY = new NamespacedKey(FancyLib.getInstance().getPlugin(), "onclick"); InventoryItemClick EMPTY = new InventoryItemClick() { @Override diff --git a/src/main/java/de/oliver/fancylib/itemClick/ItemClick.java b/src/main/java/de/oliver/fancylib/itemClick/ItemClick.java index 60027e8..c8cad35 100644 --- a/src/main/java/de/oliver/fancylib/itemClick/ItemClick.java +++ b/src/main/java/de/oliver/fancylib/itemClick/ItemClick.java @@ -6,7 +6,7 @@ import org.bukkit.event.player.PlayerInteractEvent; public interface ItemClick { - NamespacedKey ON_CLICK_KEY = new NamespacedKey(FancyLib.getPlugin(), "oninteract"); + NamespacedKey ON_CLICK_KEY = new NamespacedKey(FancyLib.getInstance().getPlugin(), "oninteract"); ItemClick EMPTY = new ItemClick() { @Override