From dab244044aa4666dbe6fcd036db26b4d94f445b3 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 20 Feb 2022 21:38:34 +0100 Subject: [PATCH] Change how economy is handled and version check --- pom.xml | 6 +-- src/main/java/tsp/headdb/HeadDB.java | 45 +++++++++---------- .../tsp/headdb/economy/HEconomyProvider.java | 21 +++++++++ .../tsp/headdb/economy/VaultProvider.java | 44 ++++++++++++++++++ .../tsp/headdb/inventory/InventoryUtils.java | 15 ++++--- src/main/java/tsp/headdb/util/Utils.java | 30 +++++++++++++ src/main/resources/config.yml | 2 + src/main/resources/plugin.yml | 2 +- 8 files changed, 132 insertions(+), 33 deletions(-) create mode 100644 src/main/java/tsp/headdb/economy/HEconomyProvider.java create mode 100644 src/main/java/tsp/headdb/economy/VaultProvider.java diff --git a/pom.xml b/pom.xml index 4481470..fcbdbe0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 3.2.4 + 3.3.0 jar HeadDB @@ -30,8 +30,8 @@ - codemc-snapshots - https://repo.codemc.io/repository/maven-snapshots/ + CodeMC + https://repo.codemc.org/repository/maven-public diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 9b44b7d..396ea0b 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -1,12 +1,12 @@ package tsp.headdb; -import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import tsp.headdb.api.HeadAPI; import tsp.headdb.command.CommandHeadDB; import tsp.headdb.database.DatabaseUpdateTask; +import tsp.headdb.economy.HEconomyProvider; +import tsp.headdb.economy.VaultProvider; import tsp.headdb.listener.JoinListener; import tsp.headdb.listener.MenuListener; import tsp.headdb.listener.PagedPaneListener; @@ -14,6 +14,7 @@ import tsp.headdb.storage.PlayerDataFile; import tsp.headdb.util.Localization; import tsp.headdb.util.Log; import tsp.headdb.util.Metrics; +import tsp.headdb.util.Utils; import javax.annotation.Nullable; import java.io.File; @@ -21,7 +22,7 @@ import java.io.File; public class HeadDB extends JavaPlugin { private static HeadDB instance; - private Economy economy; + private HEconomyProvider economyProvider; private PlayerDataFile playerData; private Localization localization; @@ -36,12 +37,11 @@ public class HeadDB extends JavaPlugin { this.playerData.load(); if (getConfig().getBoolean("economy.enable")) { - Log.debug("Starting economy..."); - this.economy = this.setupEconomy(); - if (this.economy == null) { - Log.error("Economy support requires Vault and an economy provider plugin."); - } else { - Log.info("Economy provider: " + this.economy.getName()); + String rawProvider = getConfig().getString("economy.provider", "VAULT"); + Log.debug("Starting economy with provider: " + rawProvider); + if (rawProvider.equalsIgnoreCase("vault")) { + economyProvider = new VaultProvider(); + economyProvider.initProvider(); } } @@ -58,6 +58,14 @@ public class HeadDB extends JavaPlugin { Log.debug("Starting metrics..."); new Metrics(this, 9152); + + Utils.isLatestVersion(this, 84967, latest -> { + if (!latest) { + Log.warning("There is a new update available for HeadDB on spigot!"); + Log.warning("Download: https://www.spigotmc.org/resources/84967"); + } + }); + Log.info("Done!"); } @@ -70,26 +78,17 @@ public class HeadDB extends JavaPlugin { return localization; } - @Nullable - public Economy getEconomy() { - return economy; - } - public PlayerDataFile getPlayerData() { return playerData; } - public static HeadDB getInstance() { - return instance; + @Nullable + public HEconomyProvider getEconomyProvider() { + return economyProvider; } - private Economy setupEconomy() { - if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null; - - RegisteredServiceProvider economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class); - if (economyProvider == null) return null; - - return this.economy = economyProvider.getProvider(); + public static HeadDB getInstance() { + return instance; } private void createLocalizationFile() { diff --git a/src/main/java/tsp/headdb/economy/HEconomyProvider.java b/src/main/java/tsp/headdb/economy/HEconomyProvider.java new file mode 100644 index 0000000..aba0192 --- /dev/null +++ b/src/main/java/tsp/headdb/economy/HEconomyProvider.java @@ -0,0 +1,21 @@ +package tsp.headdb.economy; + +import org.bukkit.entity.Player; + +import java.math.BigDecimal; + +public interface HEconomyProvider { + + default boolean canPurchase(Player player, BigDecimal cost) { + return true; + } + + default void charge(Player player, BigDecimal amount) { + + } + + default void initProvider() { + + } + +} diff --git a/src/main/java/tsp/headdb/economy/VaultProvider.java b/src/main/java/tsp/headdb/economy/VaultProvider.java new file mode 100644 index 0000000..c5c19f1 --- /dev/null +++ b/src/main/java/tsp/headdb/economy/VaultProvider.java @@ -0,0 +1,44 @@ +package tsp.headdb.economy; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.RegisteredServiceProvider; +import tsp.headdb.util.Log; + +import java.math.BigDecimal; + +public class VaultProvider implements HEconomyProvider { + + private Economy economy; + + @Override + public boolean canPurchase(Player player, BigDecimal cost) { + return economy.has(player, cost.doubleValue()); + } + + @Override + public void charge(Player player, BigDecimal amount) { + economy.withdrawPlayer(player, amount.doubleValue()); + } + + public void initProvider() { + if (!Bukkit.getServer().getPluginManager().isPluginEnabled("Vault")) { + Log.error("Vault is not installed!"); + return; + } + + RegisteredServiceProvider economyProvider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); + if (economyProvider == null) { + Log.error("Could not find vault economy provider!"); + return; + } + + economy = economyProvider.getProvider(); + } + + public Economy getProvider() { + return economy; + } + +} diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 329f75b..0a57437 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -13,12 +13,14 @@ import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; import tsp.headdb.api.LocalHead; import tsp.headdb.database.Category; +import tsp.headdb.economy.HEconomyProvider; import tsp.headdb.event.PlayerHeadPurchaseEvent; import tsp.headdb.util.Localization; import tsp.headdb.util.Utils; import net.milkbowl.vault.economy.Economy; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -307,26 +309,27 @@ public class InventoryUtils { } public static boolean processPayment(Player player, int amount, String category, String description) { - Economy economy = HeadDB.getInstance().getEconomy(); + HEconomyProvider economyProvider = HeadDB.getInstance().getEconomyProvider(); // If economy is disabled or no plugin is present, the item is free. // Don't mention receiving it for free in this case, since it is always free. - if (economy == null) { + if (economyProvider == null) { Utils.sendMessage(player, String.format(localization.getMessage("noEconomy"), amount, description)); Utils.playSound(player, "noEconomy"); return true; } - double cost = getCategoryCost(player, category) * amount; + BigDecimal cost = BigDecimal.valueOf(getCategoryCost(player, category) * amount); // If the cost is higher than zero, attempt to charge for it. - if (cost > 0) { - if (economy.has(player, cost)) { - economy.withdrawPlayer(player, cost); + if (cost.compareTo(BigDecimal.ZERO) > 0) { + if (economyProvider.canPurchase(player, cost)) { + economyProvider.charge(player, cost); Utils.sendMessage(player, String.format(localization.getMessage("purchasedHead"), amount, description, cost)); Utils.playSound(player, "paid"); return true; } + Utils.sendMessage(player, String.format(localization.getMessage("notEnoughMoney"), amount, description)); Utils.playSound(player, "unavailable"); return false; diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index ca26eac..68b60b7 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -1,12 +1,20 @@ package tsp.headdb.util; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Sound; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import tsp.headdb.HeadDB; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.function.Consumer; import java.util.regex.Pattern; public class Utils { @@ -14,6 +22,28 @@ public class Utils { private static final FileConfiguration config = HeadDB.getInstance().getConfig(); public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); + /** + * Retrieve the latest release from spigot + * + * @param plugin The plugin + * @param id The resource id on spigot + * @param latest Whether the plugin is on the latest version + */ + public static void isLatestVersion(JavaPlugin plugin, int id, Consumer latest) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, checkTask -> { + try { + URLConnection connection = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + id).openConnection(); + connection.setConnectTimeout(5000); + connection.setRequestProperty("User-Agent", plugin.getName() + "-VersionChecker"); + + latest.accept(new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine().equals(plugin.getDescription().getVersion())); + } catch (IOException e) { + latest.accept(true); // Assume the version is latest if checking fails + e.printStackTrace(); + } + }); + } + /** * Validate a UUID (version 4) * diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7fde965..9de9855 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,6 +11,8 @@ requireCategoryPermission: false # Economy options economy: enable: false + # Supported: VAULT + provider: "VAULT" cost: alphabet: 100 animals: 100 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1b602f4..a19471b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ description: ${project.description} main: tsp.headdb.HeadDB version: ${project.version} -softdepend: [Vault] +softdepend: [Vault, Treasury] api-version: 1.13 author: Silent