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

278 lines
7.9 KiB
Java
Raw Normal View History

package com.massivecraft.factions.integration;
import java.util.HashSet;
import java.util.Set;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.EconomyParticipator;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.entity.UConf;
import com.massivecraft.factions.entity.UPlayer;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.entity.Faction;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.util.RelationUtil;
2013-04-22 09:37:53 +02:00
import com.massivecraft.mcore.money.Money;
2011-10-10 01:21:05 +02:00
public class Econ
{
2013-04-19 09:50:33 +02:00
// -------------------------------------------- //
// STATE
// -------------------------------------------- //
2013-04-22 09:37:53 +02:00
// TODO: Do we really need that config option?
// TODO: Could we not have it enabled as long as Money.enabled is true?
public static boolean isEnabled(Object universe)
{
return UConf.get(universe).econEnabled && Money.enabled(universe);
}
2013-04-19 09:50:33 +02:00
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
2013-04-25 14:15:25 +02:00
public static boolean payForAction(double cost, UPlayer usender, String actionDescription)
{
2013-04-25 14:15:25 +02:00
if (!isEnabled(usender)) return true;
2013-04-19 09:50:33 +02:00
if (cost == 0D) return true;
2013-04-25 14:15:25 +02:00
if (usender.isUsingAdminMode()) return true;
UConf uconf = UConf.get(usender);
Faction usenderFaction = usender.getFaction();
2013-04-25 14:15:25 +02:00
if (uconf.bankEnabled && uconf.bankFactionPaysCosts && usenderFaction.isNormal())
2013-04-19 09:50:33 +02:00
{
2013-04-25 14:15:25 +02:00
return modifyMoney(usenderFaction, -cost, actionDescription);
2013-04-19 09:50:33 +02:00
}
else
{
2013-04-25 14:15:25 +02:00
return modifyMoney(usender, -cost, actionDescription);
2013-04-19 09:50:33 +02:00
}
}
// -------------------------------------------- //
// ASSORTED
// -------------------------------------------- //
2013-04-22 09:37:53 +02:00
public static void modifyUniverseMoney(Object universe, double delta)
{
2013-04-22 09:37:53 +02:00
if (!isEnabled(universe)) return;
UConf uconf = UConf.get(universe);
if (uconf.econUniverseAccount == null) return;
if (uconf.econUniverseAccount.length() == 0) return;
2013-04-22 09:37:53 +02:00
if (!Money.exists(universe, uconf.econUniverseAccount)) return;
Money.add(universe, uconf.econUniverseAccount, delta);
}
public static void sendBalanceInfo(UPlayer to, EconomyParticipator about)
{
2013-04-22 09:37:53 +02:00
to.msg("<a>%s's<i> balance is <h>%s<i>.", about.describeTo(to, true), Money.format(about, Money.get(about)));
}
public static boolean canIControllYou(EconomyParticipator i, EconomyParticipator you)
{
Faction fI = RelationUtil.getFaction(i);
Faction fYou = RelationUtil.getFaction(you);
2011-10-12 17:25:01 +02:00
// This is a system invoker. Accept it.
if (fI == null) return true;
2011-10-12 17:25:01 +02:00
// Bypassing players can do any kind of transaction
if (i instanceof UPlayer && ((UPlayer)i).isUsingAdminMode()) 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 == fI && fI == fYou) return true;
2011-10-12 17:25:01 +02:00
// Factions can be controlled by those that have permissions
if (you instanceof Faction)
{
if (i instanceof Faction && FPerm.WITHDRAW.has((Faction)i, fYou)) return true;
if (i instanceof UPlayer && FPerm.WITHDRAW.has((UPlayer)i, fYou, false)) return true;
}
2011-10-12 17:25:01 +02:00
// Otherwise you may not! ;,,;
i.msg("<h>%s<i> lacks permission to control <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)
{
return transferMoney(invoker, from, to, amount, true);
}
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount, boolean notify)
{
2013-04-22 09:37:53 +02:00
if (!isEnabled(from)) 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?
2013-04-22 09:37:53 +02:00
if (Money.get(from) < amount)
2011-10-12 17:25:01 +02:00
{
// There was not enough money to pay
if (invoker != null && notify)
2013-04-19 09:50:33 +02:00
{
2013-04-22 09:37:53 +02:00
invoker.msg("<h>%s<b> can't afford to transfer <h>%s<b> to %s<b>.", from.describeTo(invoker, true), Money.format(from, amount), to.describeTo(invoker));
2013-04-19 09:50:33 +02:00
}
2011-10-12 17:25:01 +02:00
return false;
}
// Transfer money
2013-04-22 09:37:53 +02:00
if (Money.subtract(from, amount))
2013-04-19 09:50:33 +02:00
{
2013-04-26 08:02:32 +02:00
if (Money.add(to, amount))
2013-04-19 09:50:33 +02:00
{
if (notify)
{
sendTransferInfo(invoker, from, to, amount);
}
return true;
2013-04-19 09:50:33 +02:00
}
else
{
2013-04-22 09:37:53 +02:00
// We failed. Try a rollback
Money.add(from, amount);
}
}
// if we get here something with the transaction failed
if (notify)
2013-04-22 09:37:53 +02:00
{
invoker.msg("Unable to transfer %s<b> to <h>%s<b> from <h>%s<b>.", Money.format(from, amount), to.describeTo(invoker), from.describeTo(invoker, true));
}
return false;
}
public static Set<UPlayer> getUPlayers(EconomyParticipator ep)
{
Set<UPlayer> uplayers = new HashSet<UPlayer>();
if (ep == null)
{
// Add nothing
}
else if (ep instanceof UPlayer)
{
uplayers.add((UPlayer)ep);
}
else if (ep instanceof Faction)
{
uplayers.addAll(((Faction)ep).getUPlayers());
}
return uplayers;
}
public static void sendTransferInfo(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount)
{
Set<UPlayer> recipients = new HashSet<UPlayer>();
recipients.addAll(getUPlayers(invoker));
recipients.addAll(getUPlayers(from));
recipients.addAll(getUPlayers(to));
2011-10-12 17:25:01 +02:00
if (invoker == null)
{
for (UPlayer recipient : recipients)
{
2013-04-22 09:37:53 +02:00
recipient.msg("<h>%s<i> was transfered from <h>%s<i> to <h>%s<i>.", Money.format(from, amount), from.describeTo(recipient), to.describeTo(recipient));
}
}
else if (invoker == from)
{
for (UPlayer recipient : recipients)
{
2013-04-22 09:37:53 +02:00
recipient.msg("<h>%s<i> <h>gave %s<i> to <h>%s<i>.", from.describeTo(recipient, true), Money.format(from, 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 (UPlayer recipient : recipients)
{
2013-04-22 09:37:53 +02:00
recipient.msg("<h>%s<i> <h>took %s<i> from <h>%s<i>.", to.describeTo(recipient, true), Money.format(from, amount), from.describeTo(recipient));
}
2011-10-12 17:25:01 +02:00
}
else
{
for (UPlayer recipient : recipients)
{
2013-04-22 09:37:53 +02:00
recipient.msg("<h>%s<i> transfered <h>%s<i> from <h>%s<i> to <h>%s<i>.", invoker.describeTo(recipient, true), Money.format(from, amount), from.describeTo(recipient), to.describeTo(recipient));
}
}
}
public static boolean hasAtLeast(EconomyParticipator ep, double delta, String toDoThis)
{
2013-04-22 09:37:53 +02:00
if (!isEnabled(ep)) return true;
2013-04-22 09:37:53 +02:00
if (Money.get(ep) < delta)
{
if (toDoThis != null && !toDoThis.isEmpty())
2013-04-22 09:37:53 +02:00
{
ep.msg("<h>%s<i> can't afford <h>%s<i> %s.", ep.describeTo(ep, true), Money.format(ep, delta), toDoThis);
}
return false;
}
return true;
}
2013-04-19 09:50:33 +02:00
public static boolean modifyMoney(EconomyParticipator ep, double delta, String actionDescription)
{
2013-04-22 09:37:53 +02:00
if (!isEnabled(ep)) return false;
2013-04-19 09:50:33 +02:00
if (delta == 0) return true;
2011-10-12 17:25:01 +02:00
String You = ep.describeTo(ep, true);
2013-04-19 09:50:33 +02:00
boolean hasActionDesctription = (actionDescription != null && !actionDescription.isEmpty());
2013-04-22 09:37:53 +02:00
if (Money.add(ep, delta))
{
2013-04-22 09:37:53 +02:00
modifyUniverseMoney(ep, -delta);
if (hasActionDesctription)
2013-04-19 09:50:33 +02:00
{
2013-04-22 09:37:53 +02:00
if (delta > 0)
2013-04-19 09:50:33 +02:00
{
2013-04-22 09:37:53 +02:00
ep.msg("<h>%s<i> gained <h>%s<i> since did %s.", You, Money.format(ep, delta), actionDescription);
2013-04-19 09:50:33 +02:00
}
2013-04-22 09:37:53 +02:00
else
2013-04-19 09:50:33 +02:00
{
2013-04-26 08:02:32 +02:00
ep.msg("<h>%s<i> lost <h>%s<i> since did %s.", You, Money.format(ep, -delta), actionDescription);
2013-04-19 09:50:33 +02:00
}
}
2013-04-22 09:37:53 +02:00
return true;
}
2011-10-12 17:25:01 +02:00
else
{
2013-04-22 09:37:53 +02:00
if (hasActionDesctription)
2011-10-12 17:25:01 +02:00
{
2013-04-22 09:37:53 +02:00
if (delta > 0)
2013-04-19 09:50:33 +02:00
{
2013-04-22 09:37:53 +02:00
ep.msg("<h>%s<i> would have gained <h>%s<i> since did %s, but the deposit failed.", You, Money.format(ep, delta), actionDescription);
2013-04-19 09:50:33 +02:00
}
2013-04-22 09:37:53 +02:00
else
2013-04-19 09:50:33 +02:00
{
2013-04-26 08:02:32 +02:00
ep.msg("<h>%s<i> can't afford <h>%s<i> to %s.", You, Money.format(ep, -delta), actionDescription);
2013-04-19 09:50:33 +02:00
}
2011-10-12 17:25:01 +02:00
}
2013-04-22 09:37:53 +02:00
return false;
}
}
}