From 2dbaee836ba772cbd38e947a9aa9c92b6d1416c0 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Tue, 13 Mar 2012 09:48:31 -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 | 37 +++++----- .../massivecraft/factions/cmd/CmdCreate.java | 5 +- .../massivecraft/factions/cmd/CmdJoin.java | 9 ++- .../massivecraft/factions/cmd/CmdKick.java | 9 ++- src/com/massivecraft/factions/cmd/CmdTag.java | 7 +- .../massivecraft/factions/cmd/CmdUnclaim.java | 8 +-- .../massivecraft/factions/cmd/FCommand.java | 68 ++++--------------- .../factions/integration/Econ.java | 24 +++++-- 8 files changed, 77 insertions(+), 90 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 1a7e0bd4..64e170b9 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -588,6 +588,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator public void leave(boolean makePay) { Faction myFaction = this.getFaction(); + makePay = makePay && Econ.shouldBeUsed() && ! this.isAdminBypassing(); if (myFaction == null) { @@ -609,17 +610,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.isAdminBypassing()) - { - 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) { @@ -791,30 +791,33 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator int ownedLand = forFaction.getLandRounded(); if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false; - - // if economy is enabled and they're not on the bypass list, make 'em pay - if (Econ.shouldBeUsed() && ! this.isAdminBypassing() && ! forFaction.isSafeZone() && ! forFaction.isWarZone()) + + // if economy is enabled and they're not on the bypass list, make sure they can pay + boolean mustPay = Econ.shouldBeUsed() && ! this.isAdminBypassing() && ! forFaction.isSafeZone() && ! forFaction.isWarZone(); + 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 a62e5d1e..e8f687c5 100644 --- a/src/com/massivecraft/factions/cmd/CmdCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdCreate.java @@ -59,12 +59,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 ca4b0416..19ea779d 100644 --- a/src/com/massivecraft/factions/cmd/CmdJoin.java +++ b/src/com/massivecraft/factions/cmd/CmdJoin.java @@ -82,14 +82,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 32081230..e3646af4 100644 --- a/src/com/massivecraft/factions/cmd/CmdKick.java +++ b/src/com/massivecraft/factions/cmd/CmdKick.java @@ -68,14 +68,17 @@ public class CmdKick extends FCommand } } - // 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 4c29b042..d9132ff6 100644 --- a/src/com/massivecraft/factions/cmd/CmdTag.java +++ b/src/com/massivecraft/factions/cmd/CmdTag.java @@ -51,14 +51,17 @@ 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 'em pay - if ( ! payForCommand(Conf.econCostTag, "to change the faction tag", "for changing the faction tag")) 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; + // 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(); myFaction.setTag(tag); diff --git a/src/com/massivecraft/factions/cmd/CmdUnclaim.java b/src/com/massivecraft/factions/cmd/CmdUnclaim.java index 6d3becde..7e3dd65b 100644 --- a/src/com/massivecraft/factions/cmd/CmdUnclaim.java +++ b/src/com/massivecraft/factions/cmd/CmdUnclaim.java @@ -104,6 +104,10 @@ public class CmdUnclaim extends FCommand return; } + LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(flocation, otherFaction, fme); + Bukkit.getServer().getPluginManager().callEvent(unclaimEvent); + if(unclaimEvent.isCancelled()) return; + if (Econ.shouldBeUsed()) { double refund = Econ.calculateClaimRefund(myFaction.getLandRounded()); @@ -118,10 +122,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 f0716976..6963e1a6 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -319,63 +319,19 @@ public abstract class FCommand extends MCommand

if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) 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.isAdminBypassing()) 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 6cb95ca2..acb29309 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -214,7 +214,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; @@ -235,7 +248,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 @@ -248,13 +262,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; } }