package com.massivecraft.factions.integration; import java.util.HashSet; import java.util.Set; import com.massivecraft.factions.EconomyParticipator; import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.money.Money; public class Econ { // -------------------------------------------- // // STATE // -------------------------------------------- // public static boolean isEnabled() { return MConf.get().econEnabled && Money.enabled(); } // -------------------------------------------- // // UTIL // -------------------------------------------- // public static boolean payForAction(double cost, MPlayer usender, String actionDescription) { if (!isEnabled()) return true; if (cost == 0D) return true; if (usender.isOverriding()) return true; Faction usenderFaction = usender.getFaction(); if (MConf.get().bankEnabled && MConf.get().bankFactionPaysCosts && usenderFaction.isNormal()) { return modifyMoney(usenderFaction, -cost, actionDescription); } else { return modifyMoney(usender, -cost, actionDescription); } } // -------------------------------------------- // // ASSORTED // -------------------------------------------- // public static void modifyUniverseMoney(Object universe, double delta) { if ( ! isEnabled()) return; if (MConf.get().econUniverseAccount == null) return; if (MConf.get().econUniverseAccount.length() == 0) return; if ( ! Money.exists(MConf.get().econUniverseAccount)) return; Money.spawn(MConf.get().econUniverseAccount, null, delta); } public static void sendBalanceInfo(MPlayer to, EconomyParticipator about) { to.msg("%s's balance is %s.", about.describeTo(to, true), Money.format(Money.get(about))); } public static boolean isMePermittedYou(EconomyParticipator me, EconomyParticipator you, MPerm mperm) { // Null means special system invocation and is always to be accepted. if (me == null) return true; // Always accept when in admin mode. if (me instanceof MPlayer && ((MPlayer)me).isOverriding()) return true; // Always accept control of self if (me == you) return true; Faction fMe = RelationUtil.getFaction(me); Faction fYou = RelationUtil.getFaction(you); // 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 (me == fMe && fMe == fYou) return true; // Factions can be controlled by those that have permissions if (you instanceof Faction) { if (me instanceof Faction && mperm.has((Faction)me, fYou)) return true; if (me instanceof MPlayer && mperm.has((MPlayer)me, fYou, false)) return true; } // Otherwise you may not! ;,,; return false; } public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount) { return transferMoney(from, to, invoker, amount, true); } public static boolean transferMoney(EconomyParticipator from, EconomyParticipator to, EconomyParticipator by, double amount, boolean notify) { if ( ! isEnabled()) return false; // The amount must be positive. // If the amount is negative we must flip and multiply amount with -1. if (amount < 0) { amount *= -1; EconomyParticipator temp = from; from = to; to = temp; } // Check Permissions if ( ! isMePermittedYou(by, from, MPerm.getPermWithdraw())) { by.msg("%s lack permission to withdraw money from %s.", by.describeTo(by, true), from.describeTo(by)); return false; } if ( ! isMePermittedYou(by, to, MPerm.getPermDeposit())) { by.msg("%s lack permission to deposit money to %s.", by.describeTo(by, true), to.describeTo(by)); return false; } // Is there enough money for the transaction to happen? if (Money.get(from) < amount) { // There was not enough money to pay if (by != null && notify) { by.msg("%s can't afford to transfer %s to %s.", from.describeTo(by, true), Money.format(amount), to.describeTo(by)); } return false; } // Transfer money if (Money.move(from, to, by, amount, "Factions")) { if (notify) { sendTransferInfo(by, from, to, amount); } return true; } else { // if we get here something with the transaction failed if (by != null && notify) { by.msg("Unable to transfer %s to %s from %s.", Money.format(amount), to.describeTo(by), from.describeTo(by, true)); } return false; } } public static Set getMPlayers(EconomyParticipator ep) { Set mplayers = new HashSet(); if (ep == null) { // Add nothing } else if (ep instanceof MPlayer) { mplayers.add((MPlayer)ep); } else if (ep instanceof Faction) { mplayers.addAll(((Faction)ep).getMPlayers()); } return mplayers; } public static void sendTransferInfo(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount) { Set recipients = new HashSet(); recipients.addAll(getMPlayers(invoker)); recipients.addAll(getMPlayers(from)); recipients.addAll(getMPlayers(to)); if (invoker == null) { for (MPlayer recipient : recipients) { recipient.msg("%s was transfered from %s to %s.", Money.format(amount), from.describeTo(recipient), to.describeTo(recipient)); } } else if (invoker == from) { for (MPlayer recipient : recipients) { recipient.msg("%s gave %s to %s.", from.describeTo(recipient, true), Money.format(amount), to.describeTo(recipient)); } } else if (invoker == to) { for (MPlayer recipient : recipients) { recipient.msg("%s took %s from %s.", to.describeTo(recipient, true), Money.format(amount), from.describeTo(recipient)); } } else { for (MPlayer recipient : recipients) { recipient.msg("%s transfered %s from %s to %s.", invoker.describeTo(recipient, true), Money.format(amount), from.describeTo(recipient), to.describeTo(recipient)); } } } public static boolean hasAtLeast(EconomyParticipator ep, double delta, String toDoThis) { if ( ! isEnabled()) return true; if (Money.get(ep) < delta) { if (toDoThis != null && !toDoThis.isEmpty()) { ep.msg("%s can't afford %s %s.", ep.describeTo(ep, true), Money.format(delta), toDoThis); } return false; } return true; } public static boolean modifyMoney(EconomyParticipator ep, double delta, String actionDescription) { if ( ! isEnabled()) return false; if (delta == 0) return true; String You = ep.describeTo(ep, true); String you = ep.describeTo(ep, false); boolean hasActionDesctription = (actionDescription != null && !actionDescription.isEmpty()); if (Money.spawn(ep, null, delta, "Factions")) { modifyUniverseMoney(ep, -delta); if (hasActionDesctription) { if (delta > 0) { ep.msg("%s gained %s since %s did %s.", You, Money.format(delta), you, actionDescription); } else { ep.msg("%s lost %s since %s did %s.", You, Money.format(-delta), you, actionDescription); } } return true; } else { if (hasActionDesctription) { if (delta > 0) { ep.msg("%s would have gained %s since %s did %s, but the deposit failed.", You, Money.format(delta), you, actionDescription); } else { ep.msg("%s can't afford %s to %s.", You, Money.format(-delta), actionDescription); } } return false; } } }