Load vault economy in a dedicated package

This commit is contained in:
Flowsqy 2022-07-21 23:21:16 +02:00
parent 57b1a40dfd
commit 2689b35ddf
2 changed files with 65 additions and 24 deletions

View File

@ -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<Economy> 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;
}
/**

View File

@ -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<Economy> 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();
}
}