From 0a0cc047ae32306ab183db4017d4e629c213ebcb Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Wed, 6 Feb 2019 12:39:03 +0100 Subject: [PATCH] Add possibility for new money system --- .../factions/cmd/CmdFactionsTop.java | 4 +- .../factions/engine/EngineEcon.java | 4 +- .../factions/engine/EngineShow.java | 2 +- .../massivecraft/factions/entity/Faction.java | 32 +++++++-- .../massivecraft/factions/entity/MConf.java | 2 + .../factions/integration/Econ.java | 67 +++++++++++++++++-- 6 files changed, 95 insertions(+), 16 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsTop.java b/src/com/massivecraft/factions/cmd/CmdFactionsTop.java index edce63f1..1e0d3f0a 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsTop.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsTop.java @@ -76,7 +76,7 @@ public class CmdFactionsTop extends FactionsCommand { switch(category) { - case MONEY: return Money.get(faction); + case MONEY: return Econ.getMoney(faction); case MEMBERS: return faction.getMPlayers().size(); case TERRITORY: return faction.getLandCount(); case AGE: return faction.getAge(); @@ -89,7 +89,7 @@ public class CmdFactionsTop extends FactionsCommand String ret = Txt.parse("%s: ", faction.getName(mplayer)); switch(category) { - case MONEY: ret += Money.format(Money.get(faction), true); break; + case MONEY: ret += Money.format(Econ.getMoney(faction), true); break; case MEMBERS: ret += faction.getMPlayers().size() + " members"; break; case TERRITORY: ret += faction.getLandCount() + " chunks"; break; case AGE: diff --git a/src/com/massivecraft/factions/engine/EngineEcon.java b/src/com/massivecraft/factions/engine/EngineEcon.java index 5f972e20..bc47a42f 100644 --- a/src/com/massivecraft/factions/engine/EngineEcon.java +++ b/src/com/massivecraft/factions/engine/EngineEcon.java @@ -59,7 +59,7 @@ public class EngineEcon extends Engine if (oldFaction.getMPlayers().size() > 1) return; // ... then transfer all money to the player. - double money = Money.get(oldFaction); + double money = Econ.getMoney(oldFaction); if (money == 0) return; Econ.transferMoney(mplayer, oldFaction, mplayer, money); } @@ -81,7 +81,7 @@ public class EngineEcon extends Engine // ... then transfer all the faction money to the sender. Faction faction = event.getFaction(); - double amount = Money.get(faction); + double amount = Econ.getMoney(faction); // Check that there is an amount if (amount == 0) return; diff --git a/src/com/massivecraft/factions/engine/EngineShow.java b/src/com/massivecraft/factions/engine/EngineShow.java index 45197dc4..9780b99e 100644 --- a/src/com/massivecraft/factions/engine/EngineShow.java +++ b/src/com/massivecraft/factions/engine/EngineShow.java @@ -153,7 +153,7 @@ public class EngineShow extends Engine // BANK if (MConf.get().bankEnabled) { - double bank = Money.get(faction); + double bank = Econ.getMoney(faction); String bankDesc = Txt.parse("%s", Money.format(bank, true)); show(idPriorityLiness, SHOW_ID_FACTION_BANK, SHOW_PRIORITY_FACTION_BANK, "Bank", bankDesc); } diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 2e588fec..08f5ee97 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -135,12 +135,10 @@ public class Faction extends Entity implements FactionsParticipator, MP // The powerBoost is a custom increase/decrease to default and maximum power. // Null means the faction has powerBoost (0). private Double powerBoost = null; - - // Can anyone join the Faction? - // If the faction is open they can. - // If the faction is closed an invite is required. - // Null means default. - // private Boolean open = null; + + // The money a Faction has + // null means 0.0 + private Double money = null; // This is the ids of the invited players. // They are actually "senderIds" since you can invite "@console" to your faction. @@ -425,6 +423,28 @@ public class Faction extends Entity implements FactionsParticipator, MP // Mark as changed this.changed(); } + + // -------------------------------------------- // + // FIELD: money + // -------------------------------------------- // + + public double getMoney() + { + if (!MConf.get().econEnabled) throw new UnsupportedOperationException("econ not enabled"); + if (!MConf.get().bankEnabled) throw new UnsupportedOperationException("bank not enabled"); + if (!MConf.get().useNewMoneySystem) throw new UnsupportedOperationException("this server does not use the new econ system"); + + return this.convertGet(this.money, 0D); + } + + public void setMoney(Double money) + { + if (!MConf.get().econEnabled) throw new UnsupportedOperationException("econ not enabled"); + if (!MConf.get().bankEnabled) throw new UnsupportedOperationException("bank not enabled"); + if (!MConf.get().useNewMoneySystem) throw new UnsupportedOperationException("this server does not use the new econ system"); + + this.money = this.convertSet(money, this.money, 0D); + } // -------------------------------------------- // // FIELD: open diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index b394cc09..da2eae39 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -625,4 +625,6 @@ public class MConf extends Entity // If you set this to false the player executing the command will pay instead. public boolean bankFactionPaysCosts = true; + public boolean useNewMoneySystem = false; + } diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index b388c6a6..1b9206e4 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -63,7 +63,7 @@ public class Econ public static void sendBalanceInfo(MPlayer to, EconomyParticipator about) { - to.msg("%s's balance is %s.", about.describeTo(to, true), Money.format(Money.get(about))); + to.msg("%s's balance is %s.", about.describeTo(to, true), Money.format(getMoney(about))); } public static boolean isMePermittedYou(EconomyParticipator me, EconomyParticipator you, MPerm mperm) @@ -128,7 +128,7 @@ public class Econ } // Is there enough money for the transaction to happen? - if (Money.get(from) < amount) + if (getMoney(from) < amount) { // There was not enough money to pay if (by != null && notify) @@ -139,7 +139,7 @@ public class Econ } // Transfer money - if (Money.move(from, to, by, amount, "Factions")) + if (moveMoney(from, to, by, amount)) { if (notify) { @@ -219,7 +219,7 @@ public class Econ { if ( ! isEnabled()) return true; - if (Money.get(ep) < delta) + if (getMoney(ep) < delta) { if (toDoThis != null && !toDoThis.isEmpty()) { @@ -240,7 +240,7 @@ public class Econ boolean hasActionDesctription = (actionDescription != null && !actionDescription.isEmpty()); - if (Money.spawn(ep, null, delta, "Factions")) + if (moveMoney(null, ep, null, delta)) { modifyUniverseMoney(ep, -delta); @@ -273,5 +273,62 @@ public class Econ return false; } } + + public static double getMoney(EconomyParticipator ep) + { + if (ep instanceof Faction && MConf.get().useNewMoneySystem) + { + return ((Faction) ep).getMoney(); + } + else + { + return Money.get(ep); + } + } + + public static boolean moveMoney(EconomyParticipator from, EconomyParticipator to, EconomyParticipator by, double amount) + { + final boolean fromFaction = from instanceof Faction; + final boolean toFaction = to instanceof Faction; + + // If the old money system is used just do that + if (!MConf.get().useNewMoneySystem) + { + return Money.move(from, to, by, amount, "Factions"); + } + + // Or if neither to or from is a faction + if (!fromFaction && !toFaction) + { + return Money.move(from, to, by, amount, "Factions"); + } + + // Handle from + if (fromFaction) + { + Faction faction = (Faction) from; + double money = faction.getMoney(); + if (amount > money) return false; + faction.setMoney(money - amount); + } + else if (from != null) + { + Money.despawn(from, by, amount); + } + + // Handle to + if (toFaction) + { + Faction faction = (Faction) to; + double money = faction.getMoney(); + faction.setMoney(money + amount); + } + else if (to != null) + { + Money.spawn(to, by, amount); + } + + return true; + } }