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, if you're using iCo5 or EssentialsEco, I recommend waiting for Vault 1.2.5 to be released which addresses faction account creation issues related to those.
This commit is contained in:
Brettflan 2012-01-16 19:38:14 -06:00
parent de14985d1e
commit 2a9b475012
15 changed files with 115 additions and 93 deletions

Binary file not shown.

BIN
lib/Vault.jar Normal file

Binary file not shown.

View File

@ -2,7 +2,7 @@ name: Factions
version: 1.7_dev version: 1.7_dev
main: com.massivecraft.factions.P main: com.massivecraft.factions.P
authors: [Olof Larsson, Brett Flannigan] authors: [Olof Larsson, Brett Flannigan]
softdepend: [PermissionsEx, Permissions, Essentials, EssentialsChat, HeroChat, iChat, LocalAreaChat, LWC, nChat, ChatManager, CAPI, AuthMe, Register, Spout, WorldEdit, WorldGuard] softdepend: [PermissionsEx, Permissions, Essentials, EssentialsChat, HeroChat, iChat, LocalAreaChat, LWC, nChat, ChatManager, CAPI, AuthMe, Vault, Spout, WorldEdit, WorldGuard]
commands: commands:
factions: factions:
description: Reference command for Factions. description: Reference command for Factions.

View File

@ -19,7 +19,6 @@ import com.massivecraft.factions.struct.FPerm;
import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.factions.zcore.persist.PlayerEntity; import com.massivecraft.factions.zcore.persist.PlayerEntity;
import com.nijikokun.register.payment.Method.MethodAccount;
/** /**
@ -109,11 +108,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
public boolean isSpyingChat() { return spyingChat; } public boolean isSpyingChat() { return spyingChat; }
// FIELD: account // FIELD: account
public MethodAccount getAccount() public String getAccountId() { return this.getId(); }
{
if ( ! Econ.shouldBeUsed()) return null;
return Econ.getMethod().getAccount(this.getId());
}
// -------------------------------------------- // // -------------------------------------------- //
// Construct // Construct
@ -507,7 +502,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
{ {
// Transfer all money // Transfer all money
if (Econ.shouldBeUsed()) if (Econ.shouldBeUsed())
Econ.transferMoney(this, myFaction, this, myFaction.getAccount().balance()); Econ.transferMoney(this, myFaction, this, Econ.getBalance(myFaction.getAccountId()));
} }
if (myFaction.isNormal()) if (myFaction.isNormal())

View File

@ -15,7 +15,6 @@ import com.massivecraft.factions.struct.FPerm;
import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.util.*; import com.massivecraft.factions.util.*;
import com.massivecraft.factions.zcore.persist.Entity; import com.massivecraft.factions.zcore.persist.Entity;
import com.nijikokun.register.payment.Method.MethodAccount;
public class Faction extends Entity implements EconomyParticipator public class Faction extends Entity implements EconomyParticipator
@ -85,24 +84,15 @@ public class Faction extends Entity implements EconomyParticipator
// FIELD: account (fake field) // FIELD: account (fake field)
// Bank functions // Bank functions
public double money; public double money;
public String getAccountId() { return "faction-"+this.getId(); } public String getAccountId()
public MethodAccount getAccount()
{ {
String aid = this.getAccountId(); String aid = "faction-"+this.getId();
// We need to override the default money given to players. // We need to override the default money given to players.
if ( ! Econ.getMethod().hasAccount(aid)) if ( ! Econ.hasAccount(aid))
{ Econ.setBalance(aid, 0);
if ( ! Econ.getMethod().createAccount(aid))
{
P.p.log(Level.SEVERE, "Error creating faction bank account through Register: "+aid);
// return null;
}
MethodAccount acc = Econ.getMethod().getAccount(aid);
acc.set(0);
}
return Econ.getMethod().getAccount(aid); return aid;
} }
// FIELDS: Flag management // FIELDS: Flag management
@ -543,7 +533,7 @@ public class Faction extends Entity implements EconomyParticipator
{ {
if (Econ.shouldBeUsed()) if (Econ.shouldBeUsed())
{ {
Econ.getMethod().getAccount(getAccountId()).remove(); Econ.setBalance(getAccountId(), 0);
} }
this.getAccountId(); this.getAccountId();

View File

@ -64,9 +64,8 @@ public class CmdDisband extends FCommand
if (Econ.shouldBeUsed()) if (Econ.shouldBeUsed())
{ {
//Give all the faction's money to the disbander //Give all the faction's money to the disbander
double amount = faction.getAccount().balance(); double amount = Econ.getBalance(faction.getAccountId());
fme.getAccount().add(amount); Econ.transferMoney(fme, faction, fme, amount, false);
faction.getAccount().remove();
if (amount > 0.0) if (amount > 0.0)
{ {

View File

@ -36,10 +36,10 @@ public class CmdMoneyDeposit extends FCommand
double amount = this.argAsDouble(0, 0d); double amount = this.argAsDouble(0, 0d);
EconomyParticipator faction = this.argAsFaction(1, myFaction); EconomyParticipator faction = this.argAsFaction(1, myFaction);
if (faction == null) return; if (faction == null) return;
Econ.transferMoney(fme, fme, faction, amount); boolean success = Econ.transferMoney(fme, fme, faction, amount);
if (Conf.logMoneyTransactions) if (success && Conf.logMoneyTransactions)
P.p.log(ChatColor.stripColor(P.p.txt.parse("%s deposited %s from the faction bank: %s", fme.getName(), Econ.moneyString(amount), faction.describeTo(null)))); P.p.log(ChatColor.stripColor(P.p.txt.parse("%s deposited %s in the faction bank: %s", fme.getName(), Econ.moneyString(amount), faction.describeTo(null))));
} }
} }

View File

@ -39,9 +39,9 @@ public class CmdMoneyTransferFf extends FCommand
EconomyParticipator to = this.argAsFaction(2); EconomyParticipator to = this.argAsFaction(2);
if (to == null) return; if (to == null) return;
Econ.transferMoney(fme, from, to, amount); boolean success = Econ.transferMoney(fme, from, to, amount);
if (Conf.logMoneyTransactions) if (success && Conf.logMoneyTransactions)
P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the faction \"%s\" to the faction \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null)))); P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the faction \"%s\" to the faction \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null))));
} }
} }

View File

@ -39,9 +39,9 @@ public class CmdMoneyTransferFp extends FCommand
EconomyParticipator to = this.argAsBestFPlayerMatch(2); EconomyParticipator to = this.argAsBestFPlayerMatch(2);
if (to == null) return; if (to == null) return;
Econ.transferMoney(fme, from, to, amount); boolean success = Econ.transferMoney(fme, from, to, amount);
if (Conf.logMoneyTransactions) if (success && Conf.logMoneyTransactions)
P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the faction \"%s\" to the player \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null)))); P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the faction \"%s\" to the player \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null))));
} }
} }

View File

@ -39,9 +39,9 @@ public class CmdMoneyTransferPf extends FCommand
EconomyParticipator to = this.argAsFaction(2); EconomyParticipator to = this.argAsFaction(2);
if (to == null) return; if (to == null) return;
Econ.transferMoney(fme, from, to, amount); boolean success = Econ.transferMoney(fme, from, to, amount);
if (Conf.logMoneyTransactions) if (success && Conf.logMoneyTransactions)
P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the player \"%s\" to the faction \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null)))); P.p.log(ChatColor.stripColor(P.p.txt.parse("%s transferred %s from the player \"%s\" to the faction \"%s\"", fme.getName(), Econ.moneyString(amount), from.describeTo(null), to.describeTo(null))));
} }
} }

View File

@ -34,9 +34,9 @@ public class CmdMoneyWithdraw extends FCommand
double amount = this.argAsDouble(0, 0d); double amount = this.argAsDouble(0, 0d);
EconomyParticipator faction = this.argAsFaction(1, myFaction); EconomyParticipator faction = this.argAsFaction(1, myFaction);
if (faction == null) return; if (faction == null) return;
Econ.transferMoney(fme, faction, fme, amount); boolean success = Econ.transferMoney(fme, faction, fme, amount);
if (Conf.logMoneyTransactions) if (success && Conf.logMoneyTransactions)
P.p.log(ChatColor.stripColor(P.p.txt.parse("%s withdrew %s from the faction bank: %s", fme.getName(), Econ.moneyString(amount), faction.describeTo(null)))); P.p.log(ChatColor.stripColor(P.p.txt.parse("%s withdrew %s from the faction bank: %s", fme.getName(), Econ.moneyString(amount), faction.describeTo(null))));
} }
} }

View File

@ -83,7 +83,7 @@ public class CmdShow extends FCommand
//Show bank contents //Show bank contents
if(Conf.bankEnabled) if(Conf.bankEnabled)
{ {
msg("<a>Bank contains: <i>"+Econ.moneyString(faction.getAccount().balance())); msg("<a>Bank contains: <i>"+Econ.moneyString(Econ.getBalance(faction.getAccountId())));
} }
} }

View File

@ -1,10 +1,9 @@
package com.massivecraft.factions.iface; package com.massivecraft.factions.iface;
import com.nijikokun.register.payment.Method.MethodAccount;
public interface EconomyParticipator extends RelationParticipator public interface EconomyParticipator extends RelationParticipator
{ {
public MethodAccount getAccount(); public String getAccountId();
public void msg(String str, Object... args); public void msg(String str, Object... args);
} }

View File

@ -2,14 +2,11 @@ package com.massivecraft.factions.integration;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import org.bukkit.Bukkit; 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.Conf;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
@ -19,68 +16,64 @@ import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.FPerm;
import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.RelationUtil;
import net.milkbowl.vault.economy.Economy;
public class Econ public class Econ
{ {
private static Register register = null; private static Economy econ = null;
public static Method getMethod()
{
if ( ! isSetup()) return null;
return Methods.getMethod();
}
public static boolean shouldBeUsed() public static boolean shouldBeUsed()
{ {
return Conf.econEnabled && register != null && register.isEnabled() && getMethod() != null; return Conf.econEnabled && econ != null && econ.isEnabled();
} }
public static boolean isSetup() public static boolean isSetup()
{ {
return register != null; return econ != null;
} }
public static void doSetup() public static void doSetup()
{ {
if (isSetup()) return; if (isSetup()) return;
Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Register"); if (Bukkit.getServer().getPluginManager().getPlugin("Vault") != null)
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register"))
{ {
register = (Register)plug; RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
econ = rsp.getProvider();
P.p.log("Economy integration through Register plugin successful."); P.p.log("Economy integration through Vault plugin successful.");
if ( ! Conf.econEnabled) 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 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(); 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) public static void modifyUniverseMoney(double delta)
{ {
if (!shouldBeUsed()) return; if (!shouldBeUsed()) return;
MethodAccount acc = getUniverseAccount(); if (Conf.econUniverseAccount == null) return;
if (acc == null) return; if (Conf.econUniverseAccount.length() == 0) return;
acc.add(delta); if ( ! econ.hasAccount(Conf.econUniverseAccount)) return;
modifyBalance(Conf.econUniverseAccount, delta);
} }
public static void sendBalanceInfo(FPlayer to, EconomyParticipator about) public static void sendBalanceInfo(FPlayer to, EconomyParticipator about)
{ {
to.msg("<a>%s's<i> balance is <h>%s<i>.", about.describeTo(to, true), Econ.moneyString(about.getAccount().balance())); if (!shouldBeUsed())
{
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(econ.getBalance(about.getAccountId())));
} }
public static boolean canIControllYou(EconomyParticipator i, EconomyParticipator you) public static boolean canIControllYou(EconomyParticipator i, EconomyParticipator you)
@ -106,11 +99,15 @@ public class Econ
if (you instanceof Faction && FPerm.WITHDRAW.has(i, fYou)) return true; if (you instanceof Faction && FPerm.WITHDRAW.has(i, fYou)) return true;
// Otherwise you may not! ;,,; // 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; return false;
} }
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount) 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; if ( ! shouldBeUsed()) return false;
@ -128,22 +125,22 @@ public class Econ
if ( ! canIControllYou(invoker, from)) return false; if ( ! canIControllYou(invoker, from)) return false;
// Is there enough money for the transaction to happen? // 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 // 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)); 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; return false;
} }
// Transfer money // Transfer money
from.getAccount().subtract(amount); econ.withdrawPlayer(from.getAccountId(), amount);
to.getAccount().add(amount); econ.depositPlayer(to.getAccountId(), amount);
// Inform // Inform
sendTransferInfo(invoker, from, to, amount); if (notify)
sendTransferInfo(invoker, from, to, amount);
return true; return true;
} }
@ -209,14 +206,14 @@ public class Econ
{ {
if ( ! shouldBeUsed()) return false; if ( ! shouldBeUsed()) return false;
MethodAccount acc = ep.getAccount(); String acc = ep.getAccountId();
String You = ep.describeTo(ep, true); String You = ep.describeTo(ep, true);
if (delta >= 0) if (delta >= 0)
{ {
// The player should gain money // The player should gain money
// There is no risk of failure // There is no risk of failure
acc.add(delta); econ.depositPlayer(acc, delta);
modifyUniverseMoney(-delta); modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis); ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis);
return true; return true;
@ -226,10 +223,10 @@ public class Econ
// The player should loose money // The player should loose money
// The player might not have enough. // The player might not have enough.
if (acc.hasEnough(-delta)) if (econ.has(acc, -delta))
{ {
// There is enough money to pay // There is enough money to pay
acc.add(delta); econ.withdrawPlayer(acc, -delta);
modifyUniverseMoney(-delta); modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis); ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis);
return true; return true;
@ -246,7 +243,7 @@ public class Econ
// format money string based on server's set currency type, like "24 gold" or "$24.50" // format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount) public static String moneyString(double amount)
{ {
return getMethod().format(amount); return econ.format(amount);
} }
public static void oldMoneyDoTransfer() public static void oldMoneyDoTransfer()
@ -257,7 +254,7 @@ public class Econ
{ {
if (faction.money > 0) if (faction.money > 0)
{ {
faction.getAccount().add(faction.money); econ.depositPlayer(faction.getAccountId(), faction.money);
faction.money = 0; faction.money = 0;
} }
} }
@ -298,4 +295,46 @@ public class Econ
{ {
return calculateTotalLandValue(ownedLand) * Conf.econClaimRefundMultiplier; 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();
}
} }

View File

@ -25,7 +25,7 @@ public class RelationUtil
if (thatFaction == null) return "ERROR"; // ERROR if (thatFaction == null) return "ERROR"; // ERROR
Faction myFaction = getFaction(me); Faction myFaction = getFaction(me);
if (myFaction == null) return "ERROR"; // ERROR if (myFaction == null) return thatFaction.getTag(); // no relation, but can show basic faction tag
if (that instanceof Faction) if (that instanceof Faction)
{ {