Economy integration is now handled through Vault instead of Register. You will need to download and install the Vault plugin now if you want to use any Economy-related features. If you're not using the Register plugin for anything other than Factions, it should be safe to remove it from your server.

Vault: http://dev.bukkit.org/server-mods/vault/

Note: for proper faction bank support in iCo5 and EssentialsEco, I recommend waiting for Vault 1.2.5 to be released which addresses faction account creation issues.
This commit is contained in:
Brettflan
2012-01-16 19:36:32 -06:00
parent 1a4dfd8409
commit a5e2a62eac
15 changed files with 111 additions and 96 deletions

View File

@@ -2,14 +2,11 @@ package com.massivecraft.factions.integration;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import com.nijikokun.register.Register;
import com.nijikokun.register.payment.Method;
import com.nijikokun.register.payment.Method.MethodAccount;
import com.nijikokun.register.payment.Methods;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
@@ -19,75 +16,66 @@ import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.RelationUtil;
import java.util.logging.Level;
import net.milkbowl.vault.economy.Economy;
public class Econ
{
private static Register register = null;
public static Method getMethod()
{
if ( ! isSetup()) return null;
return Methods.getMethod();
}
private static Economy econ = null;
public static boolean shouldBeUsed()
{
return Conf.econEnabled && register != null && register.isEnabled() && getMethod() != null;
return Conf.econEnabled && econ != null && econ.isEnabled();
}
public static boolean isSetup()
{
return register != null;
return econ != null;
}
public static void doSetup()
{
if (isSetup()) return;
Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Register");
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register"))
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") != null)
{
register = (Register)plug;
P.p.log("Economy integration through Register plugin successful.");
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
econ = rsp.getProvider();
P.p.log("Economy integration through Vault plugin successful.");
if ( ! Conf.econEnabled)
P.p.log("NOTE: Economy is disabled. Enable in conf \"econEnabled\": true");
P.p.log("NOTE: Economy is disabled. Enable with command: f config econEnabled true");
}
else
{
P.p.log("Economy integration is "+(Conf.econEnabled ? "enabled, but" : "disabled, and")+" the plugin \"Register\" is not installed.");
P.p.log("Economy integration is "+(Conf.econEnabled ? "enabled, but" : "disabled, and")+" the plugin \"Vault\" is not installed.");
}
P.p.cmdBase.cmdHelp.updateHelp();
}
public static MethodAccount getUniverseAccount()
{
if (Conf.econUniverseAccount == null) return null;
if (Conf.econUniverseAccount.length() == 0) return null;
return getMethod().getAccount(Conf.econUniverseAccount);
}
public static void modifyUniverseMoney(double delta)
{
if (!shouldBeUsed()) return;
MethodAccount acc = getUniverseAccount();
if (acc == null) return;
acc.add(delta);
if (Conf.econUniverseAccount == null) return;
if (Conf.econUniverseAccount.length() == 0) return;
if ( ! econ.hasAccount(Conf.econUniverseAccount)) return;
modifyBalance(Conf.econUniverseAccount, delta);
}
public static void sendBalanceInfo(FPlayer to, EconomyParticipator about)
{
if (!shouldBeUsed())
{
P.p.log(Level.WARNING, "Register does not appear to be hooked into an economy plugin.");
P.p.log(Level.WARNING, "Vault does not appear to be hooked into an economy plugin.");
return;
}
to.msg("<a>%s's<i> balance is <h>%s<i>.", about.describeTo(to, true), Econ.moneyString(about.getAccount().balance()));
to.msg("<a>%s's<i> balance is <h>%s<i>.", about.describeTo(to, true), Econ.moneyString(econ.getBalance(about.getAccountId())));
}
public static boolean canIControllYou(EconomyParticipator i, EconomyParticipator you)
@@ -116,11 +104,15 @@ public class Econ
if (you instanceof Faction && fI == fYou && (Conf.bankMembersCanWithdraw || ((FPlayer)i).getRole().value >= Role.MODERATOR.value)) return true;
// Otherwise you may not! ;,,;
i.msg("<h>%s<i> lack permission to controll <h>%s's<i> money.", i.describeTo(i, true), you.describeTo(i));
i.msg("<h>%s<i> lacks permission to control <h>%s's<i> money.", i.describeTo(i, true), you.describeTo(i));
return false;
}
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount)
{
return transferMoney(invoker, from, to, amount, true);
}
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount, boolean notify)
{
if ( ! shouldBeUsed()) return false;
@@ -138,22 +130,22 @@ public class Econ
if ( ! canIControllYou(invoker, from)) return false;
// Is there enough money for the transaction to happen?
if ( ! from.getAccount().hasEnough(amount))
if ( ! econ.has(from.getAccountId(), amount))
{
// There was not enough money to pay
if (invoker != null)
{
if (invoker != null && notify)
invoker.msg("<h>%s<b> can't afford to transfer <h>%s<b> to %s<b>.", from.describeTo(invoker, true), moneyString(amount), to.describeTo(invoker));
}
return false;
}
// Transfer money
from.getAccount().subtract(amount);
to.getAccount().add(amount);
econ.withdrawPlayer(from.getAccountId(), amount);
econ.depositPlayer(to.getAccountId(), amount);
// Inform
sendTransferInfo(invoker, from, to, amount);
if (notify)
sendTransferInfo(invoker, from, to, amount);
return true;
}
@@ -219,14 +211,14 @@ public class Econ
{
if ( ! shouldBeUsed()) return false;
MethodAccount acc = ep.getAccount();
String acc = ep.getAccountId();
String You = ep.describeTo(ep, true);
if (delta >= 0)
{
// The player should gain money
// There is no risk of failure
acc.add(delta);
econ.depositPlayer(acc, delta);
modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis);
return true;
@@ -236,10 +228,10 @@ public class Econ
// The player should loose money
// The player might not have enough.
if (acc.hasEnough(-delta))
if (econ.has(acc, -delta))
{
// There is enough money to pay
acc.add(delta);
econ.withdrawPlayer(acc, -delta);
modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis);
return true;
@@ -256,7 +248,7 @@ public class Econ
// format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount)
{
return getMethod().format(amount);
return econ.format(amount);
}
public static void oldMoneyDoTransfer()
@@ -267,7 +259,7 @@ public class Econ
{
if (faction.money > 0)
{
faction.getAccount().add(faction.money);
econ.depositPlayer(faction.getAccountId(), faction.money);
faction.money = 0;
}
}
@@ -308,4 +300,46 @@ public class Econ
{
return calculateTotalLandValue(ownedLand) * Conf.econClaimRefundMultiplier;
}
// -------------------------------------------- //
// Standard account management methods
// -------------------------------------------- //
public static boolean hasAccount(String name)
{
return econ.hasAccount(name);
}
public static double getBalance(String account)
{
return econ.getBalance(account);
}
public static boolean setBalance(String account, double amount)
{
double current = econ.getBalance(account);
if (current > amount)
return econ.withdrawPlayer(account, current - amount).transactionSuccess();
else
return econ.depositPlayer(account, amount - current).transactionSuccess();
}
public static boolean modifyBalance(String account, double amount)
{
if (amount < 0)
return econ.withdrawPlayer(account, -amount).transactionSuccess();
else
return econ.depositPlayer(account, amount).transactionSuccess();
}
public static boolean deposit(String account, double amount)
{
return econ.depositPlayer(account, amount).transactionSuccess();
}
public static boolean withdraw(String account, double amount)
{
return econ.withdrawPlayer(account, amount).transactionSuccess();
}
}