Saber-Factions/src/com/massivecraft/factions/integration/Econ.java

304 lines
8.6 KiB
Java
Raw Normal View History

package com.massivecraft.factions.integration;
import java.util.HashSet;
import java.util.Set;
2011-10-12 17:25:01 +02:00
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
2011-10-12 17:25:01 +02:00
import com.nijikokun.register.Register;
import com.nijikokun.register.payment.Method;
import com.nijikokun.register.payment.Method.MethodAccount;
2011-10-12 17:25:01 +02:00
import com.nijikokun.register.payment.Methods;
import com.massivecraft.factions.Conf;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.struct.Permission;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.RelationUtil;
2011-10-10 01:21:05 +02:00
public class Econ
{
2011-10-12 17:25:01 +02:00
private static Register register = null;
public static Method getMethod()
2011-10-10 01:21:05 +02:00
{
2011-10-12 17:25:01 +02:00
if ( ! isSetup()) return null;
return Methods.getMethod();
}
2011-10-12 17:25:01 +02:00
public static boolean shouldBeUsed()
{
2011-10-12 18:48:47 +02:00
return Conf.econEnabled && register != null && register.isEnabled() && getMethod() != null;
}
2011-10-12 17:25:01 +02:00
public static boolean isSetup()
{
2011-10-12 18:48:47 +02:00
return register != null;
}
2011-10-12 17:25:01 +02:00
public static void doSetup()
{
2011-10-12 17:25:01 +02:00
if (isSetup()) return;
Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Register");
2011-10-12 18:48:47 +02:00
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register"))
{
2011-10-12 18:48:47 +02:00
register = (Register)plug;
P.p.log("Economy integration through register successful.");
2011-10-12 17:25:01 +02:00
if ( ! Conf.econEnabled)
{
2011-10-12 18:48:47 +02:00
P.p.log("NOTE: Economy is disabled. Enable in conf \"econEnabled\": true");
2011-10-12 17:25:01 +02:00
}
}
else
{
2011-10-12 18:48:47 +02:00
P.p.log("Economy integration failed. The plugin \"Register\" is not installed.");
}
2011-10-12 17:25:01 +02:00
2011-10-10 01:21:05 +02:00
P.p.cmdBase.cmdHelp.updateHelp();
}
2011-10-12 17:25:01 +02:00
public static MethodAccount getUniverseAccount()
{
2011-10-12 17:25:01 +02:00
if (Conf.econUniverseAccount == null) return null;
if (Conf.econUniverseAccount.length() == 0) return null;
return getMethod().getAccount(Conf.econUniverseAccount);
}
2011-10-12 17:25:01 +02:00
public static void modifyUniverseMoney(double delta)
{
if (!shouldBeUsed()) return;
2011-10-12 17:25:01 +02:00
MethodAccount acc = getUniverseAccount();
if (acc == null) return;
acc.add(delta);
}
2011-10-12 17:25:01 +02:00
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()));
}
public static boolean canIControllYou(EconomyParticipator i, EconomyParticipator you)
{
Faction fInvoker = RelationUtil.getFaction(i);
Faction fFrom = RelationUtil.getFaction(you);
2011-10-12 17:25:01 +02:00
// This is a system invoker. Accept it.
if (fInvoker == null) return true;
// Bypassing players can do any kind of transaction
if (i instanceof FPlayer && ((FPlayer)i).isAdminBypassing()) return true;
2011-10-12 17:25:01 +02:00
// Players with the any withdraw can do.
if (i instanceof FPlayer && Permission.MONEY_WITHDRAW_ANY.has(((FPlayer)i).getPlayer())) return true;
2011-10-12 17:25:01 +02:00
// You can deposit to anywhere you feel like. It's your loss if you can't withdraw it again.
if (i == you) return true;
2011-10-12 17:25:01 +02:00
// A faction can always transfer away the money of it's members and its own money...
// This will however probably never happen as a faction does not have free will.
// Ohh by the way... Yes it could. For daily rent to the faction.
if (i == fInvoker && fInvoker == fFrom) return true;
2011-10-12 17:25:01 +02:00
// If you are part of the same faction as from and members can withdraw or you are at least moderator... then it is ok.
if (fInvoker == fFrom && (Conf.bankMembersCanWithdraw || ((FPlayer)i).getRole().value >= Role.MODERATOR.value)) return true;
2011-10-12 17:25:01 +02:00
// 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));
2011-10-12 17:25:01 +02:00
return false;
}
2011-10-12 17:25:01 +02:00
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount)
{
if ( ! shouldBeUsed()) return false;
2011-10-12 17:25:01 +02:00
// The amount must be positive.
// If the amount is negative we must flip and multiply amount with -1.
if (amount < 0)
{
2011-10-12 17:25:01 +02:00
amount *= -1;
EconomyParticipator temp = from;
from = to;
to = temp;
}
2011-10-12 17:25:01 +02:00
// Check the rights
if ( ! canIControllYou(invoker, from)) return false;
2011-10-12 17:25:01 +02:00
// Is there enough money for the transaction to happen?
if ( ! from.getAccount().hasEnough(amount))
{
// There was not enough money to pay
if (invoker != null)
{
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));
2011-10-12 17:25:01 +02:00
}
return false;
}
// Transfer money
from.getAccount().subtract(amount);
to.getAccount().add(amount);
// Inform
sendTransferInfo(invoker, from, to, amount);
return true;
}
public static Set<FPlayer> getFplayers(EconomyParticipator ep)
{
Set<FPlayer> fplayers = new HashSet<FPlayer>();
if (ep == null)
{
// Add nothing
}
else if (ep instanceof FPlayer)
{
fplayers.add((FPlayer)ep);
}
else if (ep instanceof Faction)
{
fplayers.addAll(((Faction)ep).getFPlayers());
}
return fplayers;
}
public static void sendTransferInfo(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount)
{
Set<FPlayer> recipients = new HashSet<FPlayer>();
recipients.addAll(getFplayers(invoker));
recipients.addAll(getFplayers(from));
recipients.addAll(getFplayers(to));
2011-10-12 17:25:01 +02:00
if (invoker == null)
{
for (FPlayer recipient : recipients)
{
recipient.msg("<h>%s<i> was transfered from <h>%s<i> to <h>%s<i>.", moneyString(amount), from.describeTo(recipient), to.describeTo(recipient));
}
}
else if (invoker == from)
{
for (FPlayer recipient : recipients)
{
recipient.msg("<h>%s<i> <h>gave %s<i> to <h>%s<i>.", from.describeTo(recipient, true), moneyString(amount), to.describeTo(recipient));
}
2011-10-12 17:25:01 +02:00
}
else if (invoker == to)
2011-10-12 17:25:01 +02:00
{
for (FPlayer recipient : recipients)
{
recipient.msg("<h>%s<i> <h>took %s<i> from <h>%s<i>.", to.describeTo(recipient, true), moneyString(amount), from.describeTo(recipient));
}
2011-10-12 17:25:01 +02:00
}
else
{
for (FPlayer recipient : recipients)
{
recipient.msg("<h>%s<i> transfered <h>%s<i> from <h>%s<i> to <h>%s<i>.", invoker.describeTo(recipient, true), moneyString(amount), from.describeTo(recipient), to.describeTo(recipient));
}
}
}
2011-10-12 17:25:01 +02:00
public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis)
{
if ( ! shouldBeUsed()) return false;
2011-10-12 17:25:01 +02:00
MethodAccount acc = ep.getAccount();
String You = ep.describeTo(ep, true);
if (delta >= 0)
{
2011-10-12 17:25:01 +02:00
// The player should gain money
// There is no risk of failure
acc.add(delta);
modifyUniverseMoney(-delta);
2011-10-12 18:48:47 +02:00
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis);
2011-10-12 17:25:01 +02:00
return true;
}
2011-10-12 17:25:01 +02:00
else
{
2011-10-12 17:25:01 +02:00
// The player should loose money
// The player might not have enough.
if (acc.hasEnough(-delta))
{
// There is enough money to pay
acc.add(delta);
modifyUniverseMoney(-delta);
2011-10-12 18:48:47 +02:00
ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis);
2011-10-12 17:25:01 +02:00
return true;
}
else
{
// There was not enough money to pay
2011-10-12 18:48:47 +02:00
ep.msg("<h>%s<i> can't afford <h>%s<i> %s.", You, moneyString(-delta), toDoThis);
2011-10-12 17:25:01 +02:00
return false;
}
}
}
// format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount)
{
2011-10-12 17:25:01 +02:00
return getMethod().format(amount);
}
public static void oldMoneyDoTransfer()
{
if ( ! shouldBeUsed()) return;
for (Faction faction : Factions.i.get())
{
faction.getAccount().add(faction.money);
faction.money = 0;
}
}
// calculate the cost for claiming land
public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction)
{
2011-10-12 17:25:01 +02:00
if ( ! shouldBeUsed())
{
2011-10-12 17:25:01 +02:00
return 0d;
}
// basic claim cost, plus land inflation cost, minus the potential bonus given for claiming from another faction
return Conf.econCostClaimWilderness
+ (Conf.econCostClaimWilderness * Conf.econClaimAdditionalMultiplier * ownedLand)
- (takingFromAnotherFaction ? Conf.econCostClaimFromFactionBonus: 0);
}
// calculate refund amount for unclaiming land
public static double calculateClaimRefund(int ownedLand)
{
return calculateClaimCost(ownedLand - 1, false) * Conf.econClaimRefundMultiplier;
}
// calculate value of all owned land
public static double calculateTotalLandValue(int ownedLand)
{
double amount = 0;
for (int x = 0; x < ownedLand; x++) {
amount += calculateClaimCost(x, false);
}
return amount;
}
// calculate refund amount for all owned land
public static double calculateTotalLandRefund(int ownedLand)
{
return calculateTotalLandValue(ownedLand) * Conf.econClaimRefundMultiplier;
}
}