From 2689b35ddf68efb8dd5bf285a358055a7ff4ec72 Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Thu, 21 Jul 2022 23:21:16 +0200 Subject: [PATCH] Load vault economy in a dedicated package --- .../java/de/epiceric/shopchest/ShopChest.java | 31 +++------- .../shopchest/utils/EconomyLoader.java | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 plugin/src/main/java/de/epiceric/shopchest/utils/EconomyLoader.java diff --git a/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java b/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java index 63944b4..e8260a6 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/plugin/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -22,7 +22,6 @@ import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; @@ -43,7 +42,7 @@ public class ShopChest extends JavaPlugin { private Platform platform; private HologramFormat hologramFormat; private ShopCommand shopCommand; - private Economy econ = null; + private Economy economy; private Database database; private boolean isUpdateNeeded = false; private String latestVersion = ""; @@ -62,19 +61,6 @@ public class ShopChest extends JavaPlugin { return instance; } - /** - * Sets up the economy of Vault - * @return Whether an economy plugin has been registered - */ - private boolean setupEconomy() { - RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class); - if (rsp == null) { - return false; - } - econ = rsp.getProvider(); - return econ != null; - } - @Override public void onLoad() { instance = this; @@ -103,14 +89,11 @@ public class ShopChest extends JavaPlugin { debugLogger.debug("Enabling ShopChest version " + getDescription().getVersion()); // Load Vault - // TODO Load Vault in dedicated class - if (!getServer().getPluginManager().isPluginEnabled("Vault")) { - cancelLoading("Could not find plugin \"Vault\""); - return; - } - - if (!setupEconomy()) { - cancelLoading("Could not find any Vault economy dependency!"); + final EconomyLoader economyLoader = new EconomyLoader(); + try { + economy = economyLoader.loadEconomy(); + } catch (RuntimeException e) { + cancelLoading(e.getMessage()); return; } @@ -385,7 +368,7 @@ public class ShopChest extends JavaPlugin { * @return Registered Economy of Vault */ public Economy getEconomy() { - return econ; + return economy; } /** diff --git a/plugin/src/main/java/de/epiceric/shopchest/utils/EconomyLoader.java b/plugin/src/main/java/de/epiceric/shopchest/utils/EconomyLoader.java new file mode 100644 index 0000000..a79ef11 --- /dev/null +++ b/plugin/src/main/java/de/epiceric/shopchest/utils/EconomyLoader.java @@ -0,0 +1,58 @@ +package de.epiceric.shopchest.utils; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.RegisteredServiceProvider; + +public class EconomyLoader { + + /** + * Load the Vault {@link Economy} + * + * @return The loaded {@link Economy} implementation + */ + public Economy loadEconomy() { + checkVaultPlugin(); + return getEconomy(); + } + + /** + * Check Vault plugin. + * + * @throws RuntimeException if vault is not present, not enable or is not the correct 'Vault' plugin + */ + private void checkVaultPlugin() { + final PluginManager plManager = Bukkit.getPluginManager(); + final Plugin vaultPlugin = plManager.getPlugin("Vault"); + if (vaultPlugin == null) { + // Supposed to be impossible as Vault is in the 'depend' section in plugin.yml + throw new RuntimeException("'Vault' plugin is not present"); + } + if (!plManager.isPluginEnabled(vaultPlugin)) { + throw new RuntimeException("'Vault' plugin is not enable"); + } + try { + Class.forName("net.milkbowl.vault.economy.Economy"); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Can retrieve the 'Vault' economy interface." + + " You may use an unsupported version of the 'Vault' plugin or the wrong 'Vault' plugin"); + } + } + + /** + * Retrieve the registered {@link Economy} implementation + * + * @return The Vault {@link Economy} implementation + * @throws RuntimeException if vault does not provide an {@link Economy} implementation + */ + private Economy getEconomy() { + final RegisteredServiceProvider rsp = Bukkit.getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + throw new RuntimeException("Could not retrieve any economy implementation. Maybe you did not install any economy plugin."); + } + return rsp.getProvider(); + } + +}