Removed AllPay dependency and implemented the new MVEconomist which is used to delegate all economy matters of importance to Multiverse.

This commit is contained in:
Jeremy Wood 2015-07-14 15:00:33 -04:00
parent fadb3280e3
commit 9bb7dd6e5d
9 changed files with 278 additions and 195 deletions

12
pom.xml
View File

@ -185,14 +185,9 @@
<include>com.dumptruckman.minecraft:buscript</include>
<include>org.mcstats.bukkit:metrics</include>
<include>com.dumptruckman.minecraft:Logging</include>
<include>com.fernferret.allpay:AllPay</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.fernferret.allpay</pattern>
<shadedPattern>com.fernferret.allpay.multiverse</shadedPattern>
</relocation>
<relocation>
<pattern>me.main__.util</pattern>
<shadedPattern>me.main__.util.multiverse</shadedPattern>
@ -282,13 +277,6 @@
</dependency>
<!-- End of SerializationConfig Dependency -->
<!-- Start of Economy Dependency -->
<dependency>
<groupId>com.fernferret.allpay</groupId>
<artifactId>AllPay</artifactId>
<version>13</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>

View File

@ -9,8 +9,6 @@ package com.onarandombox.MultiverseCore;
import buscript.Buscript;
import com.dumptruckman.minecraft.util.Logging;
import com.fernferret.allpay.AllPay;
import com.fernferret.allpay.commons.GenericBank;
import com.onarandombox.MultiverseCore.MVWorld.NullLocation;
import com.onarandombox.MultiverseCore.api.BlockSafety;
import com.onarandombox.MultiverseCore.api.Core;
@ -70,12 +68,12 @@ import com.onarandombox.MultiverseCore.listeners.MVEntityListener;
import com.onarandombox.MultiverseCore.listeners.MVMapListener;
import com.onarandombox.MultiverseCore.listeners.MVPlayerChatListener;
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener;
import com.onarandombox.MultiverseCore.listeners.MVPluginListener;
import com.onarandombox.MultiverseCore.listeners.MVPortalListener;
import com.onarandombox.MultiverseCore.listeners.MVWeatherListener;
import com.onarandombox.MultiverseCore.listeners.MVWorldInitListener;
import com.onarandombox.MultiverseCore.listeners.MVWorldListener;
import com.onarandombox.MultiverseCore.utils.AnchorManager;
import com.onarandombox.MultiverseCore.utils.MVEconomist;
import com.onarandombox.MultiverseCore.utils.MVMessaging;
import com.onarandombox.MultiverseCore.utils.MVPermissions;
import com.onarandombox.MultiverseCore.utils.MVPlayerSession;
@ -92,6 +90,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World.Environment;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -101,6 +100,8 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics;
@ -131,6 +132,19 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// TODO please let's make this non-static
private volatile MultiverseCoreConfiguration config;
public MultiverseCore() {
super();
}
/**
* This is for unit testing.
* @deprecated
*/
@Deprecated
public MultiverseCore(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file) {
super(loader, server, description, dataFolder, file);
}
/**
* This method is used to find out who is teleporting a player.
* @param playerName The teleported player (the teleportee).
@ -204,7 +218,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// Setup the block/player/entity listener.
private final MVPlayerListener playerListener = new MVPlayerListener(this);
private final MVEntityListener entityListener = new MVEntityListener(this);
private final MVPluginListener pluginListener = new MVPluginListener(this);
private final MVWeatherListener weatherListener = new MVWeatherListener(this);
private final MVPortalListener portalListener = new MVPortalListener(this);
private final MVWorldListener worldListener = new MVWorldListener(this);
@ -212,9 +225,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// HashMap to contain information relating to the Players.
private HashMap<String, MVPlayerSession> playerSessions;
private VaultHandler vaultHandler;
private GenericBank bank = null;
private AllPay banker;
private MVEconomist economist;
private Buscript buscript;
private int pluginCount;
private DestinationFactory destFactory;
@ -247,19 +258,15 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
this.unsafeCallWrapper = new UnsafeCallWrapper(this);
}
/**
* {@inheritDoc}
* @deprecated Now using Vault.
*/
@Override
@Deprecated
public GenericBank getBank() {
return this.bank;
}
@Override
@Deprecated
public VaultHandler getVaultHandler() {
return vaultHandler;
return getEconomist().getVaultHandler();
}
public MVEconomist getEconomist() {
return economist;
}
/**
@ -270,8 +277,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
getServer().getPluginManager().registerEvents(new MVWorldInitListener(this), this);
this.messaging = new MVMessaging();
this.banker = new AllPay(this, LOG_TAG + " ");
this.vaultHandler = new VaultHandler(this);
this.economist = new MVEconomist(this);
// Load the defaultWorldGenerators
this.worldManager.getDefaultWorldGenerators();
@ -279,8 +285,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// Setup Permissions, we'll do an initial check for the Permissions plugin then fall back on isOP().
this.ph = new MVPermissions(this);
this.bank = this.banker.loadEconPlugin();
// Setup the command manager
this.commandHandler = new CommandHandler(this, this.ph);
// Call the Function to assign all the Commands to their Class.
@ -462,7 +466,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(this.playerListener, this);
pm.registerEvents(this.entityListener, this);
pm.registerEvents(this.pluginListener, this);
pm.registerEvents(this.weatherListener, this);
pm.registerEvents(this.portalListener, this);
pm.registerEvents(this.worldListener, this);
@ -845,8 +848,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
@Override
public void onDisable() {
this.saveMVConfigs();
this.banker = null;
this.bank = null;
Logging.shutdown();
}
@ -1032,26 +1033,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
this.pluginCount -= 1;
}
/**
* {@inheritDoc}
* @deprecated Now using Vault.
*/
@Override
@Deprecated
public AllPay getBanker() {
return this.banker;
}
/**
* {@inheritDoc}
* @deprecated Now using Vault.
*/
@Override
@Deprecated
public void setBank(GenericBank bank) {
this.bank = bank;
}
/**
* {@inheritDoc}
*/

View File

@ -8,10 +8,9 @@
package com.onarandombox.MultiverseCore.api;
import buscript.Buscript;
import com.fernferret.allpay.AllPay;
import com.fernferret.allpay.commons.GenericBank;
import com.onarandombox.MultiverseCore.destination.DestinationFactory;
import com.onarandombox.MultiverseCore.utils.AnchorManager;
import com.onarandombox.MultiverseCore.utils.MVEconomist;
import com.onarandombox.MultiverseCore.utils.MVPermissions;
import com.onarandombox.MultiverseCore.utils.MVPlayerSession;
import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety;
@ -28,23 +27,24 @@ import org.bukkit.entity.Player;
*/
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 VaultHandler#getEconomy}
*/
@Deprecated
GenericBank getBank();
/**
* Returns the Vault handler used by Multiverse. The returned object will have all methods necessary for
* interfacing with Vault.
*
* @return the Vault handler for Multiverse.
* @deprecated we are now using {@link #getEconomist()} for all economy needs.
*/
@Deprecated
VaultHandler getVaultHandler();
/**
* Retrieves Multiverse's friendly economist. The economist can be used for dealing with economies without
* worrying about any of the messy details.
*
* @return the economy manager for Multiverse.
*/
MVEconomist getEconomist();
/**
* Reloads the Multiverse Configuration files:
* worlds.yml and config.yml.
@ -108,7 +108,7 @@ public interface Core {
/**
* Gets the primary class responsible for managing Multiverse Worlds.
*
* @return {@link WorldManager}.
* @return {@link MVWorldManager}.
*/
MVWorldManager getMVWorldManager();
@ -141,24 +141,6 @@ public interface Core {
@Deprecated
Boolean regenWorld(String name, Boolean useNewSeed, Boolean randomSeed, String seed);
/**
* Sets the {@link GenericBank}-Bank AllPay is using.
*
* @param bank The new {@link GenericBank}
* @deprecated Now using vault, see {@link VaultHandler#getEconomy}
*/
@Deprecated
void setBank(GenericBank bank);
/**
* Gets this plugin's {@link AllPay}-Banker.
*
* @return An {@link AllPay}-Banker
* @deprecated Now using vault, see {@link VaultHandler#getEconomy}
*/
@Deprecated
AllPay getBanker();
/**
* Decrements the number of plugins that have specifically hooked into core.
*/

View File

@ -125,13 +125,13 @@ public class InfoCommand extends MultiverseCommand {
message.add(new FancyMessage("World Scale: ", String.valueOf(world.getScaling()), colors));
message.add(new FancyMessage("World Seed: ", String.valueOf(world.getSeed()), colors));
if (world.getPrice() > 0) {
final String formattedAmount;
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());
}
message.add(new FancyMessage("Price to enter this world: ", formattedAmount, colors));
message.add(new FancyMessage("Price to enter this world: ",
plugin.getEconomist().formatPrice(world.getPrice(), world.getCurrency()),
colors));
} else if (world.getPrice() < 0) {
message.add(new FancyMessage("Reward for entering this world: ",
plugin.getEconomist().formatPrice(-world.getPrice(), world.getCurrency()),
colors));
} else {
message.add(new FancyMessage("Price to enter this world: ", ChatColor.GREEN + "FREE!", colors));
}

View File

@ -53,13 +53,7 @@ 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 = plugin.getVaultHandler().getEconomy() != null;
buffer.append("[Multiverse-Core] Using Vault: ").append(usingVault).append('\n');
if (usingVault) {
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');
}
buffer.append("[Multiverse-Core] Economy being used: ").append(plugin.getEconomist().getEconomyName()).append('\n');
buffer.append("[Multiverse-Core] Permissions Plugin: ").append(this.plugin.getMVPerms().getType()).append('\n');
buffer.append("[Multiverse-Core] Dumping Config Values: (version ")
.append(this.plugin.getMVConfig().getVersion()).append(")").append('\n');

View File

@ -1,63 +0,0 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.listeners;
import com.fernferret.allpay.AllPay;
import com.onarandombox.MultiverseCore.MultiverseCore;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import java.util.Arrays;
/**
* Multiverse's Plugin {@link Listener}.
*/
public class MVPluginListener implements Listener {
private MultiverseCore plugin;
public MVPluginListener(MultiverseCore plugin) {
this.plugin = plugin;
}
/**
* Keep an eye out for Plugins which we can utilize.
* @param event The event.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void pluginEnable(PluginEnableEvent event) {
if (plugin.getVaultHandler().getEconomy() != null) {
// Don't hook 2 economy plugins.
return;
}
// Let AllPay handle all econ plugin loadings, only go for econ plugins we support
if (Arrays.asList(AllPay.getValidEconPlugins()).contains(event.getPlugin().getDescription().getName())) {
this.plugin.setBank(this.plugin.getBanker().loadEconPlugin());
}
/*
if (event.getPlugin().getDescription().getName().equals("Spout")) {
this.plugin.setSpout();
this.plugin.log(Level.INFO, "Spout integration enabled.");
}
*/
}
/**
* We'll check if any of the plugins we rely on decide to Disable themselves.
* @param event The event.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void pluginDisable(PluginDisableEvent event) {
// TODO: Disable econ when it disables.
}
}

View File

@ -0,0 +1,204 @@
package com.onarandombox.MultiverseCore.utils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
/**
* Multiverse's Friendly Economist. This is used to deal with external economies and also item costs for stuff in MV.
*/
public class MVEconomist {
private final VaultHandler vaultHandler;
public MVEconomist(Plugin plugin) {
vaultHandler = new VaultHandler(plugin);
}
private boolean isUsingVault(int currency) {
return !isItemCurrency(currency) && getVaultHandler().hasEconomy();
}
/**
* Formats the amount to a human readable currency string.
*
* @param amount the amount of currency.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @return the human readable currency string.
*/
public String formatPrice(double amount, int currency) {
if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().format(amount);
} else {
return ItemEconomy.getFormattedPrice(amount, currency);
}
}
/**
* Returns the name of the economy in use.
*
* @return the name of the economy in use.
*/
public String getEconomyName() {
if (getVaultHandler().hasEconomy()) {
return getVaultHandler().getEconomy().getName();
} else {
return ItemEconomy.getName();
}
}
/**
* Determines if a player has enough of a given currency.
* @param player the player to check for currency.
* @param amount the amount of currency.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @return true if the player has enough of the given currency or the amount is 0 or less.
*/
public boolean isPlayerWealthyEnough(Player player, double amount, int currency) {
if (amount <= 0D) {
return true;
} else if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().has(player.getName(), amount);
} else {
return ItemEconomy.hasEnough(player, amount, currency);
}
}
/**
* Formats a message for a player indicating they don't have enough currency.
*
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @param message The more specific message to append to the generic message of not having enough.
* @return the formatted insufficient funds message.
*/
public String getNSFMessage(int currency, String message) {
return "Sorry, you don't have enough " + (isItemCurrency(currency) ? "items" : "funds") + ". " + message;
}
/**
* Deposits a given amount of currency either into the player's economy account or inventory if the currency
* represents an item.
*
* @param player the player to give currency to.
* @param amount the amount to give.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
*/
public void deposit(Player player, double amount, int currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().depositPlayer(player.getName(), amount);
} else {
ItemEconomy.deposit(player, amount, currency);
}
}
/**
* Withdraws a given amount of currency either from the player's economy account or inventory if the currency
* represents an item.
*
* @param player the player to take currency from.
* @param amount the amount to take.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
*/
public void withdraw(Player player, double amount, int currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().withdrawPlayer(player.getName(), amount);
} else {
ItemEconomy.withdraw(player, amount, currency);
}
}
/**
* This method is public for backwards compatibility.
*
* @return the old VaultHandler.
* @deprecated just use the other methods in this class for economy stuff.
*/
// TODO make private
@Deprecated
public VaultHandler getVaultHandler() {
return vaultHandler;
}
private static boolean isItemCurrency(int currency) {
return currency >= 0;
}
private static class ItemEconomy {
private static final String ECONOMY_NAME = "Simple Item Economy";
private static String getFormattedPrice(double amount, int currency) {
if (isItemCurrency(currency)) {
Material m = Material.getMaterial(currency);
return m != null ? amount + " " + m.toString() : "NO ITEM FOUND";
} else {
return "";
}
}
private static String getName() {
return ECONOMY_NAME;
}
private static boolean hasEnough(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
return player.getInventory().contains(currency, (int) amount);
} else {
return true;
}
}
private static void deposit(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
giveItem(player, amount, currency);
}
}
private static void withdraw(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
takeItem(player, amount, currency);
}
}
private static void giveItem(Player player, double amount, int type) {
ItemStack item = new ItemStack(type, (int) amount);
player.getInventory().addItem(item);
showReceipt(player, (amount * -1), type);
}
private static void takeItem(Player player, double amount, int type) {
int removed = 0;
HashMap<Integer, ItemStack> items = (HashMap<Integer, ItemStack>) player.getInventory().all(type);
for (int i : items.keySet()) {
if (removed >= amount) {
break;
}
int diff = (int) (amount - removed);
int amt = player.getInventory().getItem(i).getAmount();
if (amt - diff > 0) {
player.getInventory().getItem(i).setAmount(amt - diff);
break;
} else {
removed += amt;
player.getInventory().clear(i);
}
}
showReceipt(player, amount, type);
}
private static void showReceipt(Player player, double price, int item) {
if (price > 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.WHITE, "You have been charged", ChatColor.GREEN, getFormattedPrice(price, item)));
} else if (price < 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.DARK_GREEN, getFormattedPrice((price * -1), item),
ChatColor.WHITE, "has been added to your account."));
}
}
}
}

View File

@ -7,7 +7,6 @@
package com.onarandombox.MultiverseCore.utils;
import com.fernferret.allpay.commons.GenericBank;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.command.CommandSender;
@ -135,48 +134,37 @@ public class PermissionTools {
// Only check payments if it's a different world:
if (!toWorld.equals(fromWorld)) {
final double price = toWorld.getPrice();
// Don't bother checking economy stuff if it doesn't even cost to enter.
if (toWorld.getPrice() == 0D) {
if (price == 0D) {
return true;
}
// If the player does not have to pay, return now.
if (this.plugin.getMVPerms().hasPermission(teleporter, toWorld.getExemptPermission().getName(), true)) {
return true;
}
final boolean usingVault;
final String formattedAmount;
if (toWorld.getCurrency() <= 0 && plugin.getVaultHandler().getEconomy() != null) {
usingVault = true;
formattedAmount = plugin.getVaultHandler().getEconomy().format(toWorld.getPrice());
} else {
usingVault = false;
formattedAmount = this.plugin.getBank().getFormattedAmount(teleporterPlayer, toWorld.getPrice(), toWorld.getCurrency());
}
String errString = "You need " + formattedAmount + " to send " + teleportee + " to " + toWorld.getColoredWorldString();
if (teleportee.equals(teleporter)) {
errString = "You need " + formattedAmount + " to enter " + toWorld.getColoredWorldString();
}
if (usingVault) {
if (!plugin.getVaultHandler().getEconomy().has(teleporterPlayer.getName(), toWorld.getPrice())) {
return false;
} else if (pay) {
if (toWorld.getPrice() < 0D) {
plugin.getVaultHandler().getEconomy().depositPlayer(teleporterPlayer.getName(), toWorld.getPrice() * -1D);
final MVEconomist economist = plugin.getEconomist();
final int currency = toWorld.getCurrency();
final String formattedAmount = economist.formatPrice(price, currency);
if (economist.isPlayerWealthyEnough(teleporterPlayer, price, currency)) {
if (pay) {
if (price < 0D) {
economist.deposit(teleporterPlayer, -price, currency);
} else {
plugin.getVaultHandler().getEconomy().withdrawPlayer(teleporterPlayer.getName(), toWorld.getPrice());
economist.withdraw(teleporterPlayer, price, currency);
}
}
} else {
GenericBank bank = plugin.getBank();
if (!bank.hasEnough(teleporterPlayer, toWorld.getPrice(), toWorld.getCurrency(), errString)) {
return false;
} else if (pay) {
if (toWorld.getPrice() < 0D) {
bank.give(teleporterPlayer, toWorld.getPrice() * -1D, toWorld.getCurrency());
} else {
bank.take(teleporterPlayer, toWorld.getPrice(), toWorld.getCurrency());
}
if (teleportee.equals(teleporter)) {
teleporterPlayer.sendMessage(economist.getNSFMessage(currency,
"You need " + formattedAmount + " to enter " + toWorld.getColoredWorldString()));
} else {
teleporterPlayer.sendMessage(economist.getNSFMessage(currency,
"You need " + formattedAmount + " to send " + teleportee + " to " + toWorld.getColoredWorldString()));
}
return false;
}
}
return true;

View File

@ -56,11 +56,20 @@ public class VaultHandler implements Listener {
private void vaultDisabled(PluginDisableEvent event) {
if (event.getPlugin() != null && event.getPlugin().getName().equals("Vault")) {
Logging.fine("Vault economy disabled");
setupVaultEconomy();
economy = null;
}
}
}
/**
* Checks whether Vault is in use and has an economy system enabled.
*
* @return true if vault is in use and has an economy system enabled.
*/
public boolean hasEconomy() {
return economy != null;
}
/**
* Returns the Vault economy system if Vault is present and has an economy system enabled.
*