From 0888e027adad3714c8294856aa63649b97de899e Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 13 Oct 2011 14:41:07 +0200 Subject: [PATCH] Econ messages improved. All money commands is now under the money parent command. --- plugin.yml | 41 +-- src/com/massivecraft/factions/FPlayer.java | 1 - src/com/massivecraft/factions/Faction.java | 233 +----------------- src/com/massivecraft/factions/P.java | 2 + .../factions/cmd/CmdAutoHelp.java | 48 ++++ .../massivecraft/factions/cmd/CmdBalance.java | 54 ---- .../massivecraft/factions/cmd/CmdDeposit.java | 60 ----- .../massivecraft/factions/cmd/CmdHelp.java | 8 +- .../massivecraft/factions/cmd/CmdMoney.java | 44 ++++ .../factions/cmd/CmdMoneyBalance.java | 43 ++++ .../factions/cmd/CmdMoneyDeposit.java | 38 +++ .../factions/cmd/CmdMoneyPay.java | 37 +++ ...CmdWithdraw.java => CmdMoneyWithdraw.java} | 23 +- src/com/massivecraft/factions/cmd/CmdPay.java | 73 ------ .../massivecraft/factions/cmd/FCmdRoot.java | 13 +- .../massivecraft/factions/cmd/FCommand.java | 20 ++ .../factions/integration/Econ.java | 231 ++++++----------- .../factions/struct/Permission.java | 11 +- 18 files changed, 361 insertions(+), 619 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdAutoHelp.java delete mode 100644 src/com/massivecraft/factions/cmd/CmdBalance.java delete mode 100644 src/com/massivecraft/factions/cmd/CmdDeposit.java create mode 100644 src/com/massivecraft/factions/cmd/CmdMoney.java create mode 100644 src/com/massivecraft/factions/cmd/CmdMoneyBalance.java create mode 100644 src/com/massivecraft/factions/cmd/CmdMoneyDeposit.java create mode 100644 src/com/massivecraft/factions/cmd/CmdMoneyPay.java rename src/com/massivecraft/factions/cmd/{CmdWithdraw.java => CmdMoneyWithdraw.java} (74%) delete mode 100644 src/com/massivecraft/factions/cmd/CmdPay.java diff --git a/plugin.yml b/plugin.yml index bc78b7b3..a9297c8f 100644 --- a/plugin.yml +++ b/plugin.yml @@ -20,6 +20,7 @@ permissions: factions.setpermanent: true factions.setpeaceful: true factions.sethome.any: true + factions.money.withdraw.any: true factions.kit.halfmod: description: Zones, bypassing, and kicking children: @@ -39,14 +40,9 @@ permissions: children: factions.admin: true factions.autoclaim: true - factions.balance: true - factions.balance.any: true - factions.withdraw: true - factions.pay: true factions.chat: true factions.claim: true factions.deinvite: true - factions.deposit: true factions.description: true factions.disband: true factions.help: true @@ -58,6 +54,11 @@ permissions: factions.list: true factions.map: true factions.mod: true + factions.money.balance: true + factions.money.balance.any: true + factions.money.deposit: true + factions.money.pay: true + factions.money.withdraw: true factions.noboom: true factions.open: true factions.owner: true @@ -76,14 +77,6 @@ permissions: description: hand over your admin rights factions.autoclaim: description: auto-claim land as you walk around - factions.balance: - description: show current faction balance - factions.balance.any: - description: show on other factions balance - factions.withdraw: - description: withdraw money from your faction bank - factions.pay: - description: pay another faction from your bank factions.bypass: description: enable admin bypass mode factions.chat: @@ -96,8 +89,6 @@ permissions: description: create a new faction factions.deinvite: description: remove a pending invitation - factions.deposit: - description: deposit money into your faction bank factions.description: description: change the faction description factions.disband: @@ -130,6 +121,26 @@ permissions: description: show the territory map, and set optional auto update factions.mod: description: give or revoke moderator rights + factions.money.*: + children: + factions.money.balance: true + factions.money.balance.any: true + factions.money.deposit: true + factions.money.pay: true + factions.money.withdraw: true + factions.money.withdraw.any: true + factions.money.balance: + description: show your factions current money balance + factions.money.balance.any: + description: show money balance for factions other than your own + factions.money.deposit: + description: deposit money into a faction bank + factions.money.pay: + description: pay another faction from your bank + factions.money.withdraw: + description: withdraw money from your faction bank + factions.money.withdraw.any: + description: withdraw money from your factions other than your own factions.noboom: description: toggle explosions (peaceful factions only) factions.open: diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index c1223d1a..034cf605 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -123,7 +123,6 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator // FIELD: account public MethodAccount getAccount() { - P.p.log("Econ.shouldBeUsed() "+Econ.shouldBeUsed()); if ( ! Econ.shouldBeUsed()) return null; return Econ.getMethod().getAccount(this.getId()); } diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index 134e1906..5852b48b 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -104,7 +104,7 @@ public class Faction extends Entity implements EconomyParticipator return; } - sendMessage("Your faction home has been un-set since it is no longer in your territory."); + msg("Your faction home has been un-set since it is no longer in your territory."); this.home = null; } @@ -113,7 +113,7 @@ public class Faction extends Entity implements EconomyParticipator // FIELD: account (fake field) // Bank functions - public double money; // Deprecated TODO: Hantera. + public double money; public String getAccountId() { return "faction-"+this.getId(); } public MethodAccount getAccount() { @@ -129,26 +129,6 @@ public class Faction extends Entity implements EconomyParticipator return Econ.getMethod().getAccount(aid); } - /*public double getMoney() { return this.money; } - public boolean addMoney(double amount) - { - if ( amount > 0.0 ) - { - this.money += amount; - return true; - } - return false; - } - public boolean removeMoney( double amount ) - { - if (amount <= 0.0 ) return false; - - if (amount > this.money ) return false; - - this.money -= amount; - return true; - }*/ - // -------------------------------------------- // // Construct // -------------------------------------------- // @@ -214,25 +194,25 @@ public class Faction extends Entity implements EconomyParticipator @Override public String describeTo(RelationParticipator that, boolean ucfirst) { - return RelationUtil.describeThatToMe(that, this, ucfirst); + return RelationUtil.describeThatToMe(this, that, ucfirst); } @Override public String describeTo(RelationParticipator that) { - return RelationUtil.describeThatToMe(that, this); + return RelationUtil.describeThatToMe(this, that); } @Override public Relation getRelationTo(RelationParticipator rp) { - return RelationUtil.getRelationTo(rp, this); + return RelationUtil.getRelationTo(this, rp); } @Override public Relation getRelationTo(RelationParticipator rp, boolean ignorePeaceful) { - return RelationUtil.getRelationTo(rp, this, ignorePeaceful); + return RelationUtil.getRelationTo(this, rp, ignorePeaceful); } @Override @@ -262,44 +242,6 @@ public class Faction extends Entity implements EconomyParticipator } } - /*public Relation getRelationTo(Faction otherFaction) - { - return getRelationTo(otherFaction, false); - } - - public Relation getRelationTo(Faction otherFaction, boolean ignorePeaceful) - { - if (!otherFaction.isNormal() || !this.isNormal()) - { - return Relation.NEUTRAL; - } - - if (otherFaction.equals(this)) - { - return Relation.MEMBER; - } - - if (!ignorePeaceful && (this.peaceful || otherFaction.isPeaceful())) - { - return Relation.NEUTRAL; - } - - if(this.getRelationWish(otherFaction).value >= otherFaction.getRelationWish(this).value) - { - return otherFaction.getRelationWish(this); - } - - return this.getRelationWish(otherFaction); - } - - public Relation getRelationTo(FPlayer fplayer) - { - if (fplayer == null) - return Relation.NEUTRAL; - else - return getRelationTo(fplayer.getFaction()); - }*/ - //----------------------------------------------// // Power //----------------------------------------------// @@ -662,82 +604,9 @@ public class Faction extends Entity implements EconomyParticipator } - - - //----------------------------------------------// // Persistance and entity management //----------------------------------------------// - - /*public static boolean save() { - //Factions.log("Saving factions to disk"); - - try { - DiscUtil.write(file, P.p.gson.toJson(instances)); - } catch (IOException e) { - e.printStackTrace(); - P.log("Failed to save the factions to disk due to I/O exception."); - return false; - } catch (Exception e) { - e.printStackTrace(); - P.log("Failed to save the factions to disk."); - return false; - } - - return true; - } - */ - /*public static boolean load() { - P.log("Loading factions from disk"); - - if ( ! file.exists()) { - if ( ! loadOld()) - P.log("No factions to load from disk. Creating new file."); - save(); - } - - try { - Type type = new TypeToken>(){}.getType(); - Map instancesFromFile = P.p.gson.fromJson(DiscUtil.read(file), type); - instances.clear(); - instances.putAll(instancesFromFile); - } catch (Exception e) { - e.printStackTrace(); - P.log("Failed to load the factions from disk."); - return false; - } - - fillIds(); - - // Make sure the default neutral faction exists - if ( ! instances.containsKey(0)) { - Faction faction = new Faction(); - faction.tag = ChatColor.DARK_GREEN+"Wilderness"; - faction.description = ""; - faction.id = 0; - instances.put(faction.id, faction); - } - - // Make sure the safe zone faction exists - if ( ! instances.containsKey(-1)) { - Faction faction = new Faction(); - faction.tag = ChatColor.GOLD+"Safe Zone"; - faction.description = "Free from PVP and monsters"; - faction.id = -1; - instances.put(faction.id, faction); - } - - // Make sure the war zone faction exists - if ( ! instances.containsKey(-2)) { - Faction faction = new Faction(); - faction.tag = ChatColor.DARK_RED+"War Zone"; - faction.description = "Not the safest place to be"; - faction.id = -2; - instances.put(faction.id, faction); - } - - return true; - }*/ @Override @@ -756,94 +625,4 @@ public class Faction extends Entity implements EconomyParticipator // Clean the fplayers FPlayers.i.clean(); } - - /*public static Faction get(Integer factionId) - { - if ( ! instances.containsKey(factionId)) - { - P.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing cleaning!"); - Board.clean(); - FPlayer.clean(); - } - return instances.get(factionId); - }*/ - - /* - public static Faction getNone() { - return instances.get(0); - } - - public static Faction getSafeZone() { - return instances.get(-1); - } - - public static Faction getWarZone() { - return instances.get(-2); - }*/ - - /* - public static boolean exists(Integer factionId) { - return instances.containsKey(factionId); - } - - - //TODO ta parametrar här. All info som behövs ska matas in här och så sparar vi i denna method. - public static Faction create() - { - Faction faction = new Faction(); - faction.id = nextId; - nextId += 1; - instances.put(faction.id, faction); - P.log("created new faction "+faction.id); - //faction.save(); - return faction; - } - - public static void delete(Integer id) { - // Remove the faction - instances.remove(id); - - // Clean the board - Board.clean(); - - // Clean the fplayers - FPlayer.clean(); - } - - private static boolean loadOld() { - File folderFaction = new File(P.p.getDataFolder(), "faction"); - - if ( ! folderFaction.isDirectory()) - return false; - - P.log("Factions file doesn't exist, attempting to load old pre-1.1 data."); - - String ext = ".json"; - - class jsonFileFilter implements FileFilter { - @Override - public boolean accept(File file) { - return (file.getName().toLowerCase().endsWith(".json") && file.isFile()); - } - } - - File[] jsonFiles = folderFaction.listFiles(new jsonFileFilter()); - for (File jsonFile : jsonFiles) { - // Extract the name from the filename. The name is filename minus ".json" - String name = jsonFile.getName(); - name = name.substring(0, name.length() - ext.length()); - int id = Integer.parseInt(name); - - try { - Faction faction = P.p.gson.fromJson(DiscUtil.read(jsonFile), Faction.class); - faction.id = id; - instances.put(faction.id, faction); - P.log("loaded pre-1.1 faction "+id); - } catch (Exception e) { - e.printStackTrace(); - P.log(Level.WARNING, "Failed to load faction "+id); - } - } - return true; - }*/ } diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index 3ffe4153..42daee49 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -53,6 +53,7 @@ public class P extends MPlugin // Commands public FCmdRoot cmdBase; + public CmdAutoHelp cmdAutoHelp; public P() { @@ -83,6 +84,7 @@ public class P extends MPlugin // Add Base Commands this.cmdBase = new FCmdRoot(); + this.cmdAutoHelp = new CmdAutoHelp(); this.getBaseCommands().add(cmdBase); //setupPermissions(); diff --git a/src/com/massivecraft/factions/cmd/CmdAutoHelp.java b/src/com/massivecraft/factions/cmd/CmdAutoHelp.java new file mode 100644 index 00000000..193a5cad --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdAutoHelp.java @@ -0,0 +1,48 @@ +package com.massivecraft.factions.cmd; + +import java.util.ArrayList; + +import com.massivecraft.factions.P; +import com.massivecraft.factions.zcore.CommandVisibility; +import com.massivecraft.factions.zcore.MCommand; + +public class CmdAutoHelp extends MCommand

+{ + public CmdAutoHelp() + { + super(P.p); + this.aliases.add("?"); + this.aliases.add("h"); + this.aliases.add("help"); + + this.setHelpShort(""); + + this.optionalArgs.put("page","1"); + } + + @Override + public void perform() + { + if (this.commandChain.size() == 0) return; + MCommand pcmd = this.commandChain.get(this.commandChain.size()-1); + + ArrayList lines = new ArrayList(); + + lines.addAll(pcmd.helpLong); + + for(MCommand scmd : pcmd.subCommands) + { + if + ( + scmd.visibility == CommandVisibility.VISIBLE + || + (scmd.visibility == CommandVisibility.SECRET && scmd.validSenderPermissions(sender, false)) + ) + { + lines.add(scmd.getUseageTemplate(this.commandChain, true)); + } + } + + sendMessage(p.txt.getPage(lines, this.argAsInt(0, 1), "Help for command \""+pcmd.aliases.get(0)+"\"")); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdBalance.java b/src/com/massivecraft/factions/cmd/CmdBalance.java deleted file mode 100644 index 44072cfd..00000000 --- a/src/com/massivecraft/factions/cmd/CmdBalance.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.Conf; -import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.struct.Permission; -import com.massivecraft.factions.Faction; - -public class CmdBalance extends FCommand -{ - public CmdBalance() - { - super(); - this.aliases.add("balance"); - this.aliases.add("money"); - - //this.requiredArgs.add("player name"); - this.optionalArgs.put("factiontag", "yours"); - - this.permission = Permission.BALANCE.node; - this.disableOnLock = false; - - senderMustBePlayer = true; - senderMustBeMember = true; - senderMustBeModerator = false; - senderMustBeAdmin = false; - } - - @Override - public void perform() - { - if ( ! Conf.bankEnabled) - { - return; - } - - Faction faction = this.argAsFaction(0, myFaction); - - // TODO MAKE HIERARCHIAL COMMAND STRUCTURE HERE - if ( faction != myFaction && ! Permission.BALANCE_ANY.has(sender)) - { - msg("You do not have sufficient permissions to view the bank balance of other factions."); - return; - } - - if (faction == null) - { - msg("Faction %s could not be found.", args.get(0)); - return; - } - - msg("%s balance: %s", faction.getTag(fme), Econ.moneyString(faction.getAccount().balance())); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdDeposit.java b/src/com/massivecraft/factions/cmd/CmdDeposit.java deleted file mode 100644 index 4762ada8..00000000 --- a/src/com/massivecraft/factions/cmd/CmdDeposit.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.Conf; -import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.struct.Permission; - - -public class CmdDeposit extends FCommand -{ - - public CmdDeposit() - { - super(); - this.aliases.add("deposit"); - - this.requiredArgs.add("amount"); - //this.optionalArgs - - this.permission = Permission.DEPOSIT.node; - this.disableOnLock = true; - - senderMustBePlayer = true; - senderMustBeMember = true; - senderMustBeModerator = false; - senderMustBeAdmin = false; - } - - @Override - public void perform() - { - if ( ! Conf.bankEnabled) return; - Econ.transferMoney(fme, fme, myFaction, this.argAsDouble(0, 0)); - /* - if( amount > 0.0 ) - { - String amountString = Econ.moneyString(amount); - - if( ! Econ.deductMoney(fme.getName(), amount ) ) - { - msg("You cannot afford to deposit that much."); - } - else - { - faction.addMoney(amount); - msg("You have deposited %s into %s's bank.", amountString, faction.getTag()); - msg("%s now has %s", faction.getTag(fme), Econ.moneyString(faction.getMoney())); - P.p.log(fme.getName() + " deposited "+amountString+" into "+faction.getTag()+"'s bank."); - - for (FPlayer fplayer : FPlayers.i.getOnline()) - { - if (fplayer.getFaction() == faction) - { - fplayer.msg("%s has deposited %s", fme.getNameAndRelevant(fplayer), amountString); - } - } - } - }*/ - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdHelp.java b/src/com/massivecraft/factions/cmd/CmdHelp.java index 58fb404d..6d72e12f 100644 --- a/src/com/massivecraft/factions/cmd/CmdHelp.java +++ b/src/com/massivecraft/factions/cmd/CmdHelp.java @@ -89,11 +89,11 @@ public class CmdHelp extends FCommand pageLines.add( "" ); pageLines.add( p.txt.parse("Your faction has a bank which is used to pay for certain" )); pageLines.add( p.txt.parse("things, so it will need to have money deposited into it." )); + pageLines.add( p.txt.parse("To lear more use the money command." )); + pageLines.add( "" ); + pageLines.add( p.cmdBase.cmdMoney.getUseageTemplate() ); + pageLines.add( "" ); pageLines.add( "" ); - pageLines.add( p.cmdBase.cmdBalance.getUseageTemplate() ); - pageLines.add( p.cmdBase.cmdDeposit.getUseageTemplate() ); - pageLines.add( p.cmdBase.cmdWithdraw.getUseageTemplate() ); - pageLines.add( p.cmdBase.cmdPay.getUseageTemplate() ); pageLines.add( "" ); helpPages.add(pageLines); } diff --git a/src/com/massivecraft/factions/cmd/CmdMoney.java b/src/com/massivecraft/factions/cmd/CmdMoney.java new file mode 100644 index 00000000..dee5504a --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdMoney.java @@ -0,0 +1,44 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.P; + +public class CmdMoney extends FCommand +{ + public CmdMoneyBalance cmdMoneyBalance = new CmdMoneyBalance(); + public CmdMoneyDeposit cmdMoneyDeposit = new CmdMoneyDeposit(); + public CmdMoneyWithdraw cmdMoneyWithdraw = new CmdMoneyWithdraw(); + public CmdMoneyPay cmdMoneyPay = new CmdMoneyPay(); + + public CmdMoney() + { + super(); + this.aliases.add("money"); + + //this.requiredArgs.add(""); + //this.optionalArgs.put("","") + + this.isMoneyCommand = true; + this.isBankCommand = false; + + senderMustBePlayer = false; + senderMustBeMember = false; + senderMustBeModerator = false; + senderMustBeAdmin = false; + + this.setHelpShort("faction money commands"); + this.helpLong.add(p.txt.parseTags("The faction money commands.")); + + this.addSubCommand(this.cmdMoneyBalance); + this.addSubCommand(this.cmdMoneyDeposit); + this.addSubCommand(this.cmdMoneyWithdraw); + this.addSubCommand(this.cmdMoneyPay); + } + + @Override + public void perform() + { + this.commandChain.add(this); + P.p.cmdAutoHelp.execute(this.sender, this.args, this.commandChain); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdMoneyBalance.java b/src/com/massivecraft/factions/cmd/CmdMoneyBalance.java new file mode 100644 index 00000000..5a12dfed --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdMoneyBalance.java @@ -0,0 +1,43 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.Faction; + +public class CmdMoneyBalance extends FCommand +{ + public CmdMoneyBalance() + { + super(); + this.aliases.add("b"); + this.aliases.add("balance"); + + //this.requiredArgs.add(""); + this.optionalArgs.put("faction", "yours"); + + this.permission = Permission.MONEY_BALANCE.node; + this.isMoneyCommand = true; + this.isBankCommand = true; + + senderMustBePlayer = false; + senderMustBeMember = false; + senderMustBeModerator = false; + senderMustBeAdmin = false; + } + + @Override + public void perform() + { + Faction faction = myFaction; + if (this.argIsSet(0)) + { + faction = this.argAsFaction(0); + } + + if (faction == null) return; + if (faction != myFaction && ! Permission.MONEY_BALANCE_ANY.has(sender, true)) return; + + Econ.sendBalanceInfo(fme, faction); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdMoneyDeposit.java b/src/com/massivecraft/factions/cmd/CmdMoneyDeposit.java new file mode 100644 index 00000000..6eae216f --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdMoneyDeposit.java @@ -0,0 +1,38 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.struct.Permission; + + +public class CmdMoneyDeposit extends FCommand +{ + + public CmdMoneyDeposit() + { + super(); + this.aliases.add("deposit"); + + this.requiredArgs.add("amount"); + this.optionalArgs.put("faction", "yours"); + + this.permission = Permission.MONEY_DEPOSIT.node; + this.isMoneyCommand = true; + this.isBankCommand = true; + + senderMustBePlayer = true; + senderMustBeMember = false; + senderMustBeModerator = false; + senderMustBeAdmin = false; + } + + @Override + public void perform() + { + double amount = this.argAsDouble(0, 0); + Faction faction = this.argAsFaction(1, myFaction); + if (faction == null) return; + Econ.transferMoney(fme, fme, faction, amount); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdMoneyPay.java b/src/com/massivecraft/factions/cmd/CmdMoneyPay.java new file mode 100644 index 00000000..0e0df2ae --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdMoneyPay.java @@ -0,0 +1,37 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.struct.Permission; + + +public class CmdMoneyPay extends FCommand +{ + public CmdMoneyPay() + { + this.aliases.add("pay"); + + this.requiredArgs.add("amount"); + this.requiredArgs.add("faction"); + + //this.optionalArgs.put("", ""); + + this.permission = Permission.MONEY_PAY.node; + this.isMoneyCommand = true; + this.isBankCommand = true; + + senderMustBePlayer = true; + senderMustBeMember = true; + senderMustBeModerator = false; + senderMustBeAdmin = false; + } + + @Override + public void perform() + { + double amount = this.argAsDouble(0, 0); + Faction faction = this.argAsFaction(1); + if (faction == null) return; + Econ.transferMoney(fme, myFaction, faction, amount); + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdWithdraw.java b/src/com/massivecraft/factions/cmd/CmdMoneyWithdraw.java similarity index 74% rename from src/com/massivecraft/factions/cmd/CmdWithdraw.java rename to src/com/massivecraft/factions/cmd/CmdMoneyWithdraw.java index 1c8f283b..ec2eff00 100644 --- a/src/com/massivecraft/factions/cmd/CmdWithdraw.java +++ b/src/com/massivecraft/factions/cmd/CmdMoneyWithdraw.java @@ -1,25 +1,25 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.Conf; +import com.massivecraft.factions.Faction; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.struct.Permission; - -public class CmdWithdraw extends FCommand +public class CmdMoneyWithdraw extends FCommand { - public CmdWithdraw() + public CmdMoneyWithdraw() { this.aliases.add("withdraw"); this.requiredArgs.add("amount"); - //this.optionalArgs.put("factiontag", "yours"); + this.optionalArgs.put("faction", "yours"); - this.permission = Permission.WITHDRAW.node; - this.disableOnLock = true; + this.permission = Permission.MONEY_WITHDRAW.node; + this.isMoneyCommand = true; + this.isBankCommand = true; senderMustBePlayer = true; - senderMustBeMember = true; + senderMustBeMember = false; senderMustBeModerator = false; senderMustBeAdmin = false; } @@ -27,9 +27,10 @@ public class CmdWithdraw extends FCommand @Override public void perform() { - if ( ! Conf.bankEnabled) return; - - Econ.transferMoney(fme, myFaction, fme, this.argAsDouble(0, 0)); + double amount = this.argAsDouble(0, 0); + Faction faction = this.argAsFaction(1, myFaction); + if (faction == null) return; + Econ.transferMoney(fme, faction, fme, amount); /*if ( ! Conf.bankMembersCanWithdraw && ! assertMinRole(Role.MODERATOR)) { diff --git a/src/com/massivecraft/factions/cmd/CmdPay.java b/src/com/massivecraft/factions/cmd/CmdPay.java deleted file mode 100644 index 76027243..00000000 --- a/src/com/massivecraft/factions/cmd/CmdPay.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.Conf; -import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.FPlayers; -import com.massivecraft.factions.Faction; -import com.massivecraft.factions.P; -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.struct.Permission; -import com.massivecraft.factions.struct.Role; - - -public class CmdPay extends FCommand -{ - public CmdPay() - { - this.aliases.add("pay"); - - this.requiredArgs.add("faction"); - this.requiredArgs.add("amount"); - //this.optionalArgs.put("", ""); - - this.permission = Permission.PAY.node; - this.disableOnLock = true; - - senderMustBePlayer = true; - senderMustBeMember = true; - senderMustBeModerator = false; - senderMustBeAdmin = false; - } - - @Override - public void perform() - { - if ( ! Conf.bankEnabled) return; - - if ( ! Conf.bankMembersCanWithdraw && ! assertMinRole(Role.MODERATOR)) - { - msg("Only faction moderators or admins are able to pay another faction."); - return; - } - - Faction us = fme.getFaction(); - Faction them = this.argAsFaction(0); - if ( them == null ) return; - double amount = this.argAsDouble(1, 0d); - - if( amount > 0.0 ) - { - String amountString = Econ.moneyString(amount); - - if( amount > us.getAccount().balance() ) - { - amount = us.getAccount().balance(); - } - - us.getAccount().subtract(amount); - them.getAccount().add(amount); - - msg("You have paid "+amountString+" from "+us.getTag()+"'s bank to "+them.getTag()+"'s bank."); - msg(""+us.getTag()+" now has "+Econ.moneyString(us.getAccount().balance())); - P.p.log(fme.getName() + " paid "+amountString+" from "+us.getTag()+"'s bank to "+them.getTag()+"'s bank."); - - for (FPlayer fplayer : FPlayers.i.getOnline()) - { - if (fplayer.getFaction() == us || fplayer.getFaction() == them) - { - fplayer.msg(fme.getNameAndRelevant(fplayer)+" has sent "+amountString+" from "+us.getTag()+" to "+them.getTag()); - } - } - } - } -} diff --git a/src/com/massivecraft/factions/cmd/FCmdRoot.java b/src/com/massivecraft/factions/cmd/FCmdRoot.java index c4b6d14a..8babf412 100644 --- a/src/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/com/massivecraft/factions/cmd/FCmdRoot.java @@ -8,7 +8,6 @@ public class FCmdRoot extends FCommand public CmdAutoClaim cmdAutoClaim = new CmdAutoClaim(); public CmdAutoSafeclaim cmdAutoSafeclaim = new CmdAutoSafeclaim(); public CmdAutoWarclaim cmdAutoWarclaim = new CmdAutoWarclaim(); - public CmdBalance cmdBalance = new CmdBalance(); public CmdBoom cmdBoom = new CmdBoom(); public CmdBypass cmdBypass = new CmdBypass(); public CmdChat cmdChat = new CmdChat(); @@ -16,7 +15,6 @@ public class FCmdRoot extends FCommand public CmdConfig cmdConfig = new CmdConfig(); public CmdCreate cmdCreate = new CmdCreate(); public CmdDeinvite cmdDeinvite = new CmdDeinvite(); - public CmdDeposit cmdDeposit = new CmdDeposit(); public CmdDescription cmdDescription = new CmdDescription(); public CmdDisband cmdDisband = new CmdDisband(); public CmdHelp cmdHelp = new CmdHelp(); @@ -29,10 +27,10 @@ public class FCmdRoot extends FCommand public CmdLock cmdLock = new CmdLock(); public CmdMap cmdMap = new CmdMap(); public CmdMod cmdMod = new CmdMod(); + public CmdMoney cmdMoney = new CmdMoney(); public CmdOpen cmdOpen = new CmdOpen(); public CmdOwner cmdOwner = new CmdOwner(); public CmdOwnerList cmdOwnerList = new CmdOwnerList(); - public CmdPay cmdPay = new CmdPay(); public CmdPeaceful cmdPeaceful = new CmdPeaceful(); public CmdPermanent cmdPermanent = new CmdPermanent(); public CmdPower cmdPower = new CmdPower(); @@ -52,7 +50,6 @@ public class FCmdRoot extends FCommand public CmdVersion cmdVersion = new CmdVersion(); public CmdWarclaim cmdWarclaim = new CmdWarclaim(); public CmdWarunclaimall cmdWarunclaimall = new CmdWarunclaimall(); - public CmdWithdraw cmdWithdraw = new CmdWithdraw(); public FCmdRoot() { @@ -60,6 +57,9 @@ public class FCmdRoot extends FCommand this.aliases.addAll(Conf.baseCommandAliases); this.allowNoSlashAccess = Conf.allowNoSlashCommand; + //this.requiredArgs.add(""); + //this.optionalArgs.put("","") + senderMustBePlayer = false; senderMustBeMember = false; senderMustBeModerator = false; @@ -76,7 +76,6 @@ public class FCmdRoot extends FCommand this.addSubCommand(this.cmdAutoClaim); this.addSubCommand(this.cmdAutoSafeclaim); this.addSubCommand(this.cmdAutoWarclaim); - this.addSubCommand(this.cmdBalance); this.addSubCommand(this.cmdBoom); this.addSubCommand(this.cmdBypass); this.addSubCommand(this.cmdChat); @@ -84,7 +83,6 @@ public class FCmdRoot extends FCommand this.addSubCommand(this.cmdConfig); this.addSubCommand(this.cmdCreate); this.addSubCommand(this.cmdDeinvite); - this.addSubCommand(this.cmdDeposit); this.addSubCommand(this.cmdDescription); this.addSubCommand(this.cmdDisband); this.addSubCommand(this.cmdHelp); @@ -97,10 +95,10 @@ public class FCmdRoot extends FCommand this.addSubCommand(this.cmdLock); this.addSubCommand(this.cmdMap); this.addSubCommand(this.cmdMod); + this.addSubCommand(this.cmdMoney); this.addSubCommand(this.cmdOpen); this.addSubCommand(this.cmdOwner); this.addSubCommand(this.cmdOwnerList); - this.addSubCommand(this.cmdPay); this.addSubCommand(this.cmdPeaceful); this.addSubCommand(this.cmdPermanent); this.addSubCommand(this.cmdPower); @@ -120,7 +118,6 @@ public class FCmdRoot extends FCommand this.addSubCommand(this.cmdVersion); this.addSubCommand(this.cmdWarclaim); this.addSubCommand(this.cmdWarunclaimall); - this.addSubCommand(this.cmdWithdraw); } @Override diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index 0feb45fb..4cae6c89 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -26,6 +26,9 @@ public abstract class FCommand extends MCommand

public boolean senderMustBeModerator; public boolean senderMustBeAdmin; + public boolean isMoneyCommand; + public boolean isBankCommand; + public FCommand() { super(P.p); @@ -33,6 +36,10 @@ public abstract class FCommand extends MCommand

// Due to safety reasons it defaults to disable on lock. disableOnLock = true; + // The money commands must be disabled if money should not be used. + isMoneyCommand = false; + isBankCommand = false; + senderMustBeMember = false; senderMustBeModerator = false; senderMustBeAdmin = false; @@ -62,6 +69,19 @@ public abstract class FCommand extends MCommand

msg("Factions was locked by an admin. Please try again later."); return false; } + + if (this.isMoneyCommand && ! Conf.econEnabled) + { + msg("Faction economy features are diabled on this server."); + return false; + } + + if (this.isBankCommand && ! Conf.bankEnabled) + { + msg("The faction bank system is diabled on this server."); + return false; + } + return true; } diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index bc85f481..aecd24b7 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -1,5 +1,8 @@ package com.massivecraft.factions.integration; +import java.util.HashSet; +import java.util.Set; + import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; @@ -76,30 +79,35 @@ public class Econ acc.add(delta); } - public static boolean canInvokerTransferFrom(EconomyParticipator invoker, EconomyParticipator from) + public static void sendBalanceInfo(FPlayer to, EconomyParticipator about) { - Faction fInvoker = RelationUtil.getFaction(invoker); - Faction fFrom = RelationUtil.getFaction(from); + to.msg("%s's balance is %s.", 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); // This is a system invoker. Accept it. if (fInvoker == null) return true; // Bypassing players can do any kind of transaction - if (invoker instanceof FPlayer && ((FPlayer)invoker).isAdminBypassing()) return true; + if (i instanceof FPlayer && ((FPlayer)i).isAdminBypassing()) return true; // You can deposit to anywhere you feel like. It's your loss if you can't withdraw it again. - if (invoker == from) return true; + if (i == you) return true; // 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 (invoker == fInvoker && fInvoker == fFrom) return true; + if (i == fInvoker && fInvoker == fFrom) return true; // 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)invoker).getRole().value < Role.MODERATOR.value)) return true; + if (fInvoker == fFrom && (Conf.bankMembersCanWithdraw || ((FPlayer)i).getRole().value >= Role.MODERATOR.value)) return true; // Otherwise you may not! ;,,; - invoker.msg("%s don't have the right to transfer money from %s.", invoker.describeTo(invoker, true), from.describeTo(invoker)); + i.msg("%s lack permission to controll %s's money.", i.describeTo(i, true), you.describeTo(i)); return false; } @@ -118,23 +126,15 @@ public class Econ } // Check the rights - if ( ! canInvokerTransferFrom(invoker, from)) return false; - - //Faction fFrom = RelationUtil.getFaction(from); - //Faction fTo = RelationUtil.getFaction(to); - //Faction fInvoker = RelationUtil.getFaction(invoker); + if ( ! canIControllYou(invoker, from)) return false; // Is there enough money for the transaction to happen? - - P.p.log("from "+from); - P.p.log("from.getAccount() "+from.getAccount()); - if ( ! from.getAccount().hasEnough(amount)) { // There was not enough money to pay if (invoker != null) { - invoker.msg("%s can't afford to transfer %s to %s.", from.describeTo(invoker, true), moneyString(amount), to.describeTo(invoker)); + invoker.msg("%s can't afford to transfer %s to %s.", from.describeTo(invoker, true), moneyString(amount), to.describeTo(invoker)); } return false; } @@ -144,23 +144,66 @@ public class Econ to.getAccount().add(amount); // Inform + sendTransferInfo(invoker, from, to, amount); + + return true; + } + + public static Set getFplayers(EconomyParticipator ep) + { + Set fplayers = new HashSet(); + + 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 recipients = new HashSet(); + recipients.addAll(getFplayers(invoker)); + recipients.addAll(getFplayers(from)); + recipients.addAll(getFplayers(to)); + if (invoker == null) { - from.msg("%s was transfered from %s to %s.", moneyString(amount), from.describeTo(from), to.describeTo(from)); - to.msg ("%s was transfered from %s to %s.", moneyString(amount), from.describeTo(to), to.describeTo(to)); + for (FPlayer recipient : recipients) + { + recipient.msg("%s was transfered from %s to %s.", moneyString(amount), from.describeTo(recipient), to.describeTo(recipient)); + } } - else if (invoker == from || invoker == to) + else if (invoker == from) { - from.msg("%s transfered %s to %s.", from.describeTo(from, true), moneyString(amount), to.describeTo(from)); - to.msg ("%s transfered %s to %s.", from.describeTo(to, true), moneyString(amount), to.describeTo(to)); + for (FPlayer recipient : recipients) + { + recipient.msg("%s gave %s to %s.", from.describeTo(recipient, true), moneyString(amount), to.describeTo(recipient)); + } + } + else if (invoker == to) + { + for (FPlayer recipient : recipients) + { + recipient.msg("%s took %s from %s.", to.describeTo(recipient, true), moneyString(amount), from.describeTo(recipient)); + } } else { - from.msg("%s was transfered from %s to %s by %s.", moneyString(amount), from.describeTo(from), to.describeTo(from), invoker.describeTo(from)); - to.msg ("%s was transfered from %s to %s by %s.", moneyString(amount), from.describeTo(to), to.describeTo(to), invoker.describeTo(to)); + for (FPlayer recipient : recipients) + { + recipient.msg("%s transfered %s from %s to %s.", invoker.describeTo(recipient, true), moneyString(amount), from.describeTo(recipient), to.describeTo(recipient)); + } } - - return true; } public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis) @@ -201,8 +244,6 @@ public class Econ } } - - // format money string based on server's set currency type, like "24 gold" or "$24.50" public static String moneyString(double amount) { @@ -220,138 +261,6 @@ public class Econ } } - // whether a player can afford specified amount - /*public static boolean canAfford(String playerName, double amount) { - // if Economy support is not enabled, they can certainly afford to pay nothing - if (!enabled()) - { - return true; - } - - if (registerAvailable()) - { - MethodAccount holdings = getRegisterAccount(playerName); - if (holdings == null) - { - return false; - } - - return holdings.hasEnough(amount); - } - else if (iConomyUse) - { - Holdings holdings = getIconomyHoldings(playerName); - if (holdings == null) - { - return false; - } - - return holdings.hasEnough(amount); - } - else - { - try - { - return Economy.hasEnough(playerName, amount); - } - catch (Exception ex) - { - return false; - } - } - }*/ - - // deduct money from their account; returns true if successful - /*public static boolean deductMoney(String playerName, double amount) - { - if (!enabled()) - { - return true; - } - - if (registerAvailable()) - { - MethodAccount holdings = getRegisterAccount(playerName); - if (holdings == null || !holdings.hasEnough(amount)) - { - return false; - } - - return holdings.subtract(amount); - } - else if (iConomyUse) - { - Holdings holdings = getIconomyHoldings(playerName); - if (holdings == null || !holdings.hasEnough(amount)) - { - return false; - } - - holdings.subtract(amount); - return true; - } - else - { - try - { - if (!Economy.hasEnough(playerName, amount)) - { - return false; - } - Economy.subtract(playerName, amount); - return true; - } - catch (Exception ex) - { - return false; - } - } - }*/ - - // add money to their account; returns true if successful - /*public static boolean addMoney(String playerName, double amount) - { - if (!enabled()) - { - return true; - } - - if (registerAvailable()) - { - MethodAccount holdings = getRegisterAccount(playerName); - if (holdings == null) - { - return false; - } - - return holdings.add(amount); - } - else if (iConomyUse) - { - Holdings holdings = getIconomyHoldings(playerName); - if (holdings == null) - { - return false; - } - - holdings.add(amount); - return true; - } - else - { - try - { - Economy.add(playerName, amount); - return true; - } - catch (Exception ex) - { - return false; - } - } - }*/ - - // calculate the cost for claiming land public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction) { diff --git a/src/com/massivecraft/factions/struct/Permission.java b/src/com/massivecraft/factions/struct/Permission.java index 8bdadc36..5dc87219 100644 --- a/src/com/massivecraft/factions/struct/Permission.java +++ b/src/com/massivecraft/factions/struct/Permission.java @@ -11,17 +11,12 @@ public enum Permission OWNERSHIP_BYPASS("ownershipbypass"), ADMIN("admin"), AUTOCLAIM("autoclaim"), - BALANCE("balance"), - BALANCE_ANY("balance.any"), - WITHDRAW("withdraw"), - PAY("pay"), BYPASS("bypass"), CHAT("chat"), CLAIM("claim"), CONFIG("config"), CREATE("create"), DEINVITE("deinvite"), - DEPOSIT("deposit"), DESCRIPTION("description"), DISBAND("disband"), DISBAND_ANY("disband.any"), @@ -36,6 +31,12 @@ public enum Permission LOCK("lock"), MAP("map"), MOD("mod"), + MONEY_BALANCE("money.balance"), + MONEY_BALANCE_ANY("money.balance.any"), + MONEY_DEPOSIT("money.deposit"), + MONEY_PAY("money.pay"), + MONEY_WITHDRAW("money.withdraw"), + MONEY_WITHDRAW_ANY("money.withdraw.any"), NO_BOOM("noboom"), OPEN("open"), OWNER("owner"),