diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 8365e580..546db69b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -77,11 +77,10 @@ import com.onarandombox.MultiverseCore.utils.MVPlayerSession; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; +import com.onarandombox.MultiverseCore.utils.VaultHandler; import com.onarandombox.MultiverseCore.utils.WorldManager; import com.pneumaticraft.commandhandler.CommandHandler; import me.main__.util.SerializationConfig.SerializationConfig; -import net.milkbowl.vault.economy.Economy; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World.Environment; @@ -92,12 +91,7 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.PluginDisableEvent; -import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.mcstats.Metrics; @@ -117,7 +111,7 @@ import java.util.logging.Level; /** * The implementation of the Multiverse-{@link Core}. */ -public class MultiverseCore extends JavaPlugin implements MVPlugin, Core, Listener { +public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { private static final int PROTOCOL = 18; // TODO: Investigate if this one is really needed to be static. // Doubt it. -- FernFerret @@ -207,7 +201,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core, Listen // HashMap to contain information relating to the Players. private HashMap playerSessions; - private Economy vaultEco = null; + private VaultHandler vaultHandler; private GenericBank bank = null; private AllPay banker; private Buscript buscript; @@ -260,12 +254,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core, Listen return this.bank; } - /** - * {@inheritDoc} - */ @Override - public Economy getVaultEconomy() { - return vaultEco; + public VaultHandler getVaultHandler() { + return vaultHandler; } /** @@ -342,48 +333,13 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core, Listen this.initializeBuscript(); this.setupMetrics(); - // Listen out for vault. - getServer().getPluginManager().registerEvents(this, this); - this.setupVaultEconomy(); + + this.vaultHandler = new VaultHandler(this); // Output a little snippet to show it's enabled. Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); } - private boolean setupVaultEconomy() { - if (Bukkit.getPluginManager().getPlugin("Vault") != null) { - final RegisteredServiceProvider economyProvider - = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); - if (economyProvider != null) { - Logging.fine("Vault economy enabled."); - vaultEco = economyProvider.getProvider(); - } else { - Logging.finer("Vault economy not detected."); - vaultEco = null; - } - } else { - Logging.finer("Vault was not found."); - vaultEco = null; - } - - return (vaultEco != null); - } - - @EventHandler - private void vaultEnabled(PluginEnableEvent event) { - if (event.getPlugin() != null && event.getPlugin().getName().equals("Vault")) { - setupVaultEconomy(); - } - } - - @EventHandler - private void vaultDisabled(PluginDisableEvent event) { - if (event.getPlugin() != null && event.getPlugin().getName().equals("Vault")) { - Logging.fine("Vault economy disabled"); - setupVaultEconomy(); - } - } - /** * Initializes the buscript javascript library. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java index 8f6f5287..1d683b7d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java @@ -11,9 +11,15 @@ import buscript.Buscript; import com.fernferret.allpay.AllPay; import com.fernferret.allpay.GenericBank; import com.onarandombox.MultiverseCore.destination.DestinationFactory; -import com.onarandombox.MultiverseCore.utils.*; +import com.onarandombox.MultiverseCore.utils.AnchorManager; +import com.onarandombox.MultiverseCore.utils.MVPermissions; +import com.onarandombox.MultiverseCore.utils.MVPlayerSession; +import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; +import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; +import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; +import com.onarandombox.MultiverseCore.utils.VaultHandler; +import com.onarandombox.MultiverseCore.utils.WorldManager; import com.pneumaticraft.commandhandler.CommandHandler; -import net.milkbowl.vault.economy.Economy; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -37,17 +43,18 @@ public interface Core { * Gets the Banking system that Multiverse-Core has hooked into. * * @return A {@link GenericBank} that can be used for payments. - * @deprecated Now using vault, see {@link #getVaultEconomy} + * @deprecated Now using vault, see {@link VaultHandler#getEconomy} */ @Deprecated GenericBank getBank(); /** - * Returns the Vault economy system if Vault is present and has an economy system enabled. + * Returns the Vault handler used by Multiverse. The returned object will have all methods necessary for + * interfacing with Vault. * - * @return The vault economy system or null if not configured. + * @return the Vault handler for Multiverse. */ - Economy getVaultEconomy(); + VaultHandler getVaultHandler(); /** * Reloads the Multiverse Configuration files: @@ -149,7 +156,7 @@ public interface Core { * Sets the {@link GenericBank}-Bank AllPay is using. * * @param bank The new {@link GenericBank} - * @deprecated Now using vault, see {@link #getVaultEconomy} + * @deprecated Now using vault, see {@link VaultHandler#getEconomy} */ @Deprecated void setBank(GenericBank bank); @@ -158,7 +165,7 @@ public interface Core { * Gets this plugin's {@link AllPay}-Banker. * * @return An {@link AllPay}-Banker - * @deprecated Now using vault, see {@link #getVaultEconomy} + * @deprecated Now using vault, see {@link VaultHandler#getEconomy} */ @Deprecated AllPay getBanker(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index 67463f0a..bbcf9472 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -125,8 +125,8 @@ public class InfoCommand extends MultiverseCommand { message.add(new FancyMessage("World Scale: ", world.getScaling() + "", colors)); if (world.getPrice() > 0) { final String formattedAmount; - if (world.getCurrency() <= 0 && plugin.getVaultEconomy() != null) { - formattedAmount = plugin.getVaultEconomy().format(world.getPrice()); + if (world.getCurrency() <= 0 && plugin.getVaultHandler().getEconomy() != null) { + formattedAmount = plugin.getVaultHandler().getEconomy().format(world.getPrice()); } else { formattedAmount = this.plugin.getBank().getFormattedAmount(p, world.getPrice(), world.getCurrency()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java index ad10cc0a..f4624f9c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java @@ -53,10 +53,10 @@ public class VersionCommand extends MultiverseCommand { buffer.append("[Multiverse-Core] Bukkit Version: ").append(this.plugin.getServer().getVersion()).append('\n'); buffer.append("[Multiverse-Core] Loaded Worlds: ").append(this.plugin.getMVWorldManager().getMVWorlds()).append('\n'); buffer.append("[Multiverse-Core] Multiverse Plugins Loaded: ").append(this.plugin.getPluginCount()).append('\n'); - final boolean usingVault = this.plugin.getVaultEconomy() != null; + final boolean usingVault = plugin.getVaultHandler().getEconomy() != null; buffer.append("[Multiverse-Core] Using Vault: ").append(usingVault).append('\n'); if (usingVault) { - buffer.append("[Multiverse-Core] Economy being used: ").append(this.plugin.getVaultEconomy().getName()).append('\n'); + buffer.append("[Multiverse-Core] Economy being used: ").append(plugin.getVaultHandler().getEconomy().getName()).append('\n'); } else { buffer.append("[Multiverse-Core] Economy being used: ").append(this.plugin.getBank().getEconUsed()).append('\n'); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPluginListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPluginListener.java index aa271cf5..b73a704d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPluginListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPluginListener.java @@ -34,7 +34,7 @@ public class MVPluginListener implements Listener { */ @EventHandler(priority = EventPriority.MONITOR) public void pluginEnable(PluginEnableEvent event) { - if (plugin.getVaultEconomy() != null) { + if (plugin.getVaultHandler().getEconomy() != null) { // Don't hook 2 economy plugins. return; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index d8fd2822..f8cce3dc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -145,9 +145,9 @@ public class PermissionTools { } final boolean usingVault; final String formattedAmount; - if (toWorld.getCurrency() <= 0 && plugin.getVaultEconomy() != null) { + if (toWorld.getCurrency() <= 0 && plugin.getVaultHandler().getEconomy() != null) { usingVault = true; - formattedAmount = plugin.getVaultEconomy().format(toWorld.getPrice()); + formattedAmount = plugin.getVaultHandler().getEconomy().format(toWorld.getPrice()); } else { usingVault = false; formattedAmount = this.plugin.getBank().getFormattedAmount(teleporterPlayer, toWorld.getPrice(), toWorld.getCurrency()); @@ -157,13 +157,13 @@ public class PermissionTools { errString = "You need " + formattedAmount + " to enter " + toWorld.getColoredWorldString(); } if (usingVault) { - if (!plugin.getVaultEconomy().has(teleporterPlayer.getName(), toWorld.getPrice())) { + if (!plugin.getVaultHandler().getEconomy().has(teleporterPlayer.getName(), toWorld.getPrice())) { return false; } else if (pay) { if (toWorld.getPrice() < 0D) { - plugin.getVaultEconomy().depositPlayer(teleporterPlayer.getName(), toWorld.getPrice() * -1D); + plugin.getVaultHandler().getEconomy().depositPlayer(teleporterPlayer.getName(), toWorld.getPrice() * -1D); } else { - plugin.getVaultEconomy().withdrawPlayer(teleporterPlayer.getName(), toWorld.getPrice()); + plugin.getVaultHandler().getEconomy().withdrawPlayer(teleporterPlayer.getName(), toWorld.getPrice()); } } } else { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/VaultHandler.java b/src/main/java/com/onarandombox/MultiverseCore/utils/VaultHandler.java new file mode 100644 index 00000000..2a1b5a81 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/VaultHandler.java @@ -0,0 +1,67 @@ +package com.onarandombox.MultiverseCore.utils; + +import com.dumptruckman.minecraft.util.Logging; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.event.server.PluginEnableEvent; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredServiceProvider; + +/** + * A class we use to interface with Vault when it is present. + */ +public class VaultHandler implements Listener { + + private Economy economy; + + public VaultHandler(final Plugin plugin) { + Bukkit.getPluginManager().registerEvents(this, plugin); + setupVaultEconomy(); + } + + private boolean setupVaultEconomy() { + if (Bukkit.getPluginManager().getPlugin("Vault") != null) { + final RegisteredServiceProvider economyProvider + = Bukkit.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); + if (economyProvider != null) { + Logging.fine("Vault economy enabled."); + economy = economyProvider.getProvider(); + } else { + Logging.finer("Vault economy not detected."); + economy = null; + } + } else { + Logging.finer("Vault was not found."); + economy = null; + } + + return (economy != null); + } + + @EventHandler + private void vaultEnabled(PluginEnableEvent event) { + if (event.getPlugin() != null && event.getPlugin().getName().equals("Vault")) { + setupVaultEconomy(); + } + } + + @EventHandler + private void vaultDisabled(PluginDisableEvent event) { + if (event.getPlugin() != null && event.getPlugin().getName().equals("Vault")) { + Logging.fine("Vault economy disabled"); + setupVaultEconomy(); + } + } + + /** + * Returns the Vault economy system if Vault is present and has an economy system enabled. + * + * @return The vault economy system or null if not configured. + */ + public Economy getEconomy() { + return economy; + } +}