From 7b9674dc4bb1c88953d4971f757958056c452cb6 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Tue, 13 Mar 2012 09:48:34 -0500 Subject: [PATCH] Ability to pay for commands (through economy) is now checked before firing events which can be canceled, and actual payment made after making sure the event isn't canceled. Added Econ.hasAtLeast(EconomyParticipator ep, double delta, String toDoThis) method to check if an account has at least a specified amount of money in it. Also added related FCommand.canAffordCommand(double cost, String toDoThis). --- src/com/massivecraft/factions/FPlayer.java | 44 ++++++------ .../massivecraft/factions/cmd/CmdCreate.java | 5 +- .../massivecraft/factions/cmd/CmdJoin.java | 7 +- .../massivecraft/factions/cmd/CmdKick.java | 11 +-- src/com/massivecraft/factions/cmd/CmdTag.java | 5 +- .../massivecraft/factions/cmd/CmdUnclaim.java | 10 +-- .../massivecraft/factions/cmd/FCommand.java | 68 ++++--------------- .../factions/integration/Econ.java | 24 +++++-- 8 files changed, 80 insertions(+), 94 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 607983cc..89190094 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -483,6 +483,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator public void leave(boolean makePay) { Faction myFaction = this.getFaction(); + makePay = makePay && Econ.shouldBeUsed() && ! this.hasAdminMode(); if (myFaction == null) { @@ -504,17 +505,16 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator return; } - // if economy is enabled and they're not on the bypass list, make 'em pay - if (makePay && Econ.shouldBeUsed() && ! this.hasAdminMode()) - { - double cost = Conf.econCostLeave; - if ( ! Econ.modifyMoney(this, -cost, "to leave your faction.", "for leaving your faction.")) return; - } - + // if economy is enabled and they're not on the bypass list, make sure they can pay + if (makePay && ! Econ.hasAtLeast(this, Conf.econCostLeave, "to leave your faction.")) return; + FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(this,myFaction,FPlayerLeaveEvent.PlayerLeaveReason.LEAVE); Bukkit.getServer().getPluginManager().callEvent(leaveEvent); if (leaveEvent.isCancelled()) return; - + + // then make 'em pay (if applicable) + if (makePay && ! Econ.modifyMoney(this, -Conf.econCostLeave, "to leave your faction.", "for leaving your faction.")) return; + // Am I the last one in the faction? if (myFaction.getFPlayers().size() == 1) { @@ -641,31 +641,33 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false; - // if economy is enabled and they're not on the bypass list, make 'em pay // TODO: Add flag no costs?? - //if (Econ.shouldBeUsed() && ! this.isAdminBypassing() && ! forFaction.isSafeZone() && ! forFaction.isWarZone()) - if (Econ.shouldBeUsed() && ! this.hasAdminMode()) + // if economy is enabled and they're not on the bypass list, make sure they can pay + boolean mustPay = Econ.shouldBeUsed() && ! this.hasAdminMode(); + double cost = 0.0; + EconomyParticipator payee = null; + if (mustPay) { - double cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal()); + cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal()); if (Conf.econClaimUnconnectedFee != 0.0 && forFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && !Board.isConnectedLocation(flocation, forFaction)) cost += Conf.econClaimUnconnectedFee; if(Conf.bankEnabled && Conf.bankFactionPaysLandCosts && this.hasFaction()) - { - Faction faction = this.getFaction(); - if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false; - } + payee = this.getFaction(); else - { - if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false; - } + payee = this; + + if ( ! Econ.hasAtLeast(payee, cost, "to claim this land")) return false; } - + LandClaimEvent claimEvent = new LandClaimEvent(flocation, forFaction, this); Bukkit.getServer().getPluginManager().callEvent(claimEvent); if(claimEvent.isCancelled()) return false; - + + // then make 'em pay (if applicable) + if (mustPay && ! Econ.modifyMoney(payee, -cost, "to claim this land", "for claiming this land")) return false; + if (LWCFeatures.getEnabled() && forFaction.isNormal() && Conf.onCaptureResetLwcLocks) LWCFeatures.clearOtherChests(flocation, this.getFaction()); diff --git a/src/com/massivecraft/factions/cmd/CmdCreate.java b/src/com/massivecraft/factions/cmd/CmdCreate.java index 91fc632e..67cb2d11 100644 --- a/src/com/massivecraft/factions/cmd/CmdCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdCreate.java @@ -58,12 +58,15 @@ public class CmdCreate extends FCommand return; } + // if economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay + if ( ! canAffordCommand(Conf.econCostCreate, "to create a new faction")) return; + // trigger the faction creation event (cancellable) FactionCreateEvent createEvent = new FactionCreateEvent(me, tag); Bukkit.getServer().getPluginManager().callEvent(createEvent); if(createEvent.isCancelled()) return; - // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay + // then make 'em pay (if applicable) if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return; Faction faction = Factions.i.create(); diff --git a/src/com/massivecraft/factions/cmd/CmdJoin.java b/src/com/massivecraft/factions/cmd/CmdJoin.java index f87cb48b..91a0bbff 100644 --- a/src/com/massivecraft/factions/cmd/CmdJoin.java +++ b/src/com/massivecraft/factions/cmd/CmdJoin.java @@ -76,14 +76,17 @@ public class CmdJoin extends FCommand return; } - // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay - if (samePlayer && ! payForCommand(Conf.econCostJoin, "to join a faction", "for joining a faction")) return; + // if economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay + if (samePlayer && ! canAffordCommand(Conf.econCostJoin, "to join a faction")) return; // trigger the join event (cancellable) FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.COMMAND); Bukkit.getServer().getPluginManager().callEvent(joinEvent); if (joinEvent.isCancelled()) return; + // then make 'em pay (if applicable) + if (samePlayer && ! payForCommand(Conf.econCostJoin, "to join a faction", "for joining a faction")) return; + fme.msg("%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme)); if (!samePlayer) diff --git a/src/com/massivecraft/factions/cmd/CmdKick.java b/src/com/massivecraft/factions/cmd/CmdKick.java index 71cb51cf..fae97e0c 100644 --- a/src/com/massivecraft/factions/cmd/CmdKick.java +++ b/src/com/massivecraft/factions/cmd/CmdKick.java @@ -59,15 +59,18 @@ public class CmdKick extends FCommand Faction yourFaction = you.getFaction(); if (fme != null && ! FPerm.KICK.has(fme, yourFaction)) return; - - // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay - if ( ! payForCommand(Conf.econCostKick, "to kick someone from the faction", "for kicking someone from the faction")) return; + + // if economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay + if ( ! canAffordCommand(Conf.econCostKick, "to kick someone from the faction")) return; // trigger the leave event (cancellable) [reason:kicked] FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) return; - + + // then make 'em pay (if applicable) + if ( ! payForCommand(Conf.econCostKick, "to kick someone from the faction", "for kicking someone from the faction")) return; + yourFaction.msg("%s kicked %s from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true)); you.msg("%s kicked you from %s! :O", fme.describeTo(you, true), yourFaction.describeTo(you)); if (yourFaction != myFaction) diff --git a/src/com/massivecraft/factions/cmd/CmdTag.java b/src/com/massivecraft/factions/cmd/CmdTag.java index 75c2d45f..97e736f5 100644 --- a/src/com/massivecraft/factions/cmd/CmdTag.java +++ b/src/com/massivecraft/factions/cmd/CmdTag.java @@ -51,12 +51,15 @@ public class CmdTag extends FCommand return; } + // if economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay + if ( ! canAffordCommand(Conf.econCostTag, "to change the faction tag")) return; + // trigger the faction rename event (cancellable) FactionRenameEvent renameEvent = new FactionRenameEvent(fme, tag); Bukkit.getServer().getPluginManager().callEvent(renameEvent); if(renameEvent.isCancelled()) return; - // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay + // then make 'em pay (if applicable) if ( ! payForCommand(Conf.econCostTag, "to change the faction tag", "for changing the faction tag")) return; String oldtag = myFaction.getTag(); diff --git a/src/com/massivecraft/factions/cmd/CmdUnclaim.java b/src/com/massivecraft/factions/cmd/CmdUnclaim.java index 1f008c10..109d02da 100644 --- a/src/com/massivecraft/factions/cmd/CmdUnclaim.java +++ b/src/com/massivecraft/factions/cmd/CmdUnclaim.java @@ -39,7 +39,11 @@ public class CmdUnclaim extends FCommand Faction otherFaction = Board.getFactionAt(flocation); if ( ! FPerm.TERRITORY.has(sender, otherFaction, true)) return; - + + LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(flocation, otherFaction, fme); + Bukkit.getServer().getPluginManager().callEvent(unclaimEvent); + if(unclaimEvent.isCancelled()) return; + //String moneyBack = ""; if (Econ.shouldBeUsed()) { @@ -55,10 +59,6 @@ public class CmdUnclaim extends FCommand } } - LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(flocation, otherFaction, fme); - Bukkit.getServer().getPluginManager().callEvent(unclaimEvent); - if(unclaimEvent.isCancelled()) return; - Board.removeAt(flocation); SpoutFeatures.updateTerritoryDisplayLoc(flocation); myFaction.msg("%s unclaimed some land.", fme.describeTo(myFaction, true)); diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index 98c1e6a6..faa099e2 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -390,63 +390,19 @@ public abstract class FCommand extends MCommand

if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.hasAdminMode()) return true; if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) - { - if ( ! Econ.modifyMoney(myFaction, -cost, toDoThis, forDoingThis)) return false; - } + return Econ.modifyMoney(myFaction, -cost, toDoThis, forDoingThis); else - { - if ( ! Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis)) return false; - } - return true; - /* - - - - // pay up - if (cost > 0.0) - { - String costString = Econ.moneyString(cost); - if(Conf.bankFactionPaysCosts && fme.hasFaction() ) - { - if( ! faction.getAccount().subtract(cost)) - { - sendMessage("It costs "+costString+" to "+desc+", which your faction can't currently afford."); - return false; - } - else - { - sendMessage(faction.getTag()+" has paid "+costString+" to "+desc+"."); - } - - } - else - { - if ( ! Econ.deductMoney(fme.getName(), cost)) - { - sendMessage("It costs "+costString+" to "+desc+", which you can't currently afford."); - return false; - } - sendMessage("You have paid "+costString+" to "+desc+"."); - } - } - // wait... we pay you to use this command? + return Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis); + } + + // like above, but just make sure they can pay; returns true unless person can't afford the cost + public boolean canAffordCommand(double cost, String toDoThis) + { + if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.hasAdminMode()) return true; + + if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) + return Econ.hasAtLeast(myFaction, -cost, toDoThis); else - { - String costString = Econ.moneyString(-cost); - - if(Conf.bankFactionPaysCosts && fme.hasFaction() ) - { - faction.getAccount().add(-cost); - sendMessage(faction.getTag()+" has been paid "+costString+" to "+desc+"."); - } - else - { - Econ.addMoney(fme.getName(), -cost); - } - - - sendMessage("You have been paid "+costString+" to "+desc+"."); - } - return true;*/ + return Econ.hasAtLeast(fme, -cost, toDoThis); } } diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index f3911c13..cb18c735 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -210,7 +210,20 @@ public class Econ } } } - + + public static boolean hasAtLeast(EconomyParticipator ep, double delta, String toDoThis) + { + if ( ! shouldBeUsed()) return true; + + if ( ! econ.has(ep.getAccountId(), delta)) + { + if (toDoThis != null && !toDoThis.isEmpty()) + ep.msg("%s can't afford %s %s.", ep.describeTo(ep, true), moneyString(delta), toDoThis); + return false; + } + return true; + } + public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis) { if ( ! shouldBeUsed()) return false; @@ -231,7 +244,8 @@ public class Econ // There is no risk of failure econ.depositPlayer(acc, delta); modifyUniverseMoney(-delta); - ep.msg("%s gained %s %s.", You, moneyString(delta), forDoingThis); + if (forDoingThis != null && !forDoingThis.isEmpty()) + ep.msg("%s gained %s %s.", You, moneyString(delta), forDoingThis); return true; } else @@ -244,13 +258,15 @@ public class Econ // There is enough money to pay econ.withdrawPlayer(acc, -delta); modifyUniverseMoney(-delta); - ep.msg("%s lost %s %s.", You, moneyString(-delta), forDoingThis); + if (forDoingThis != null && !forDoingThis.isEmpty()) + ep.msg("%s lost %s %s.", You, moneyString(-delta), forDoingThis); return true; } else { // There was not enough money to pay - ep.msg("%s can't afford %s %s.", You, moneyString(-delta), toDoThis); + if (toDoThis != null && !toDoThis.isEmpty()) + ep.msg("%s can't afford %s %s.", You, moneyString(-delta), toDoThis); return false; } }