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).
This commit is contained in:
Brettflan 2012-03-13 09:48:31 -05:00
parent 30c2eaeac3
commit 2dbaee836b
8 changed files with 77 additions and 90 deletions

View File

@ -588,6 +588,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
public void leave(boolean makePay) public void leave(boolean makePay)
{ {
Faction myFaction = this.getFaction(); Faction myFaction = this.getFaction();
makePay = makePay && Econ.shouldBeUsed() && ! this.isAdminBypassing();
if (myFaction == null) if (myFaction == null)
{ {
@ -609,17 +610,16 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
return; return;
} }
// if economy is enabled and they're not on the bypass list, make 'em pay // if economy is enabled and they're not on the bypass list, make sure they can pay
if (makePay && Econ.shouldBeUsed() && ! this.isAdminBypassing()) if (makePay && ! Econ.hasAtLeast(this, Conf.econCostLeave, "to leave your faction.")) return;
{
double cost = Conf.econCostLeave;
if ( ! Econ.modifyMoney(this, -cost, "to leave your faction.", "for leaving your faction.")) return;
}
FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(this,myFaction,FPlayerLeaveEvent.PlayerLeaveReason.LEAVE); FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(this,myFaction,FPlayerLeaveEvent.PlayerLeaveReason.LEAVE);
Bukkit.getServer().getPluginManager().callEvent(leaveEvent); Bukkit.getServer().getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) return; 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? // Am I the last one in the faction?
if (myFaction.getFPlayers().size() == 1) if (myFaction.getFPlayers().size() == 1)
{ {
@ -791,30 +791,33 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
int ownedLand = forFaction.getLandRounded(); int ownedLand = forFaction.getLandRounded();
if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false; if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false;
// if economy is enabled and they're not on the bypass list, make 'em pay // if economy is enabled and they're not on the bypass list, make sure they can pay
if (Econ.shouldBeUsed() && ! this.isAdminBypassing() && ! forFaction.isSafeZone() && ! forFaction.isWarZone()) 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)) if (Conf.econClaimUnconnectedFee != 0.0 && forFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && !Board.isConnectedLocation(flocation, forFaction))
cost += Conf.econClaimUnconnectedFee; cost += Conf.econClaimUnconnectedFee;
if(Conf.bankEnabled && Conf.bankFactionPaysLandCosts && this.hasFaction()) if(Conf.bankEnabled && Conf.bankFactionPaysLandCosts && this.hasFaction())
{ payee = this.getFaction();
Faction faction = this.getFaction();
if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false;
}
else else
{ payee = this;
if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false;
} if ( ! Econ.hasAtLeast(payee, cost, "to claim this land")) return false;
} }
LandClaimEvent claimEvent = new LandClaimEvent(flocation, forFaction, this); LandClaimEvent claimEvent = new LandClaimEvent(flocation, forFaction, this);
Bukkit.getServer().getPluginManager().callEvent(claimEvent); Bukkit.getServer().getPluginManager().callEvent(claimEvent);
if(claimEvent.isCancelled()) return false; 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) if (LWCFeatures.getEnabled() && forFaction.isNormal() && Conf.onCaptureResetLwcLocks)
{ {
LWCFeatures.clearOtherChests(flocation, this.getFaction()); LWCFeatures.clearOtherChests(flocation, this.getFaction());

View File

@ -59,12 +59,15 @@ public class CmdCreate extends FCommand
return; 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) // trigger the faction creation event (cancellable)
FactionCreateEvent createEvent = new FactionCreateEvent(me, tag); FactionCreateEvent createEvent = new FactionCreateEvent(me, tag);
Bukkit.getServer().getPluginManager().callEvent(createEvent); Bukkit.getServer().getPluginManager().callEvent(createEvent);
if(createEvent.isCancelled()) return; 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; if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return;
Faction faction = Factions.i.create(); Faction faction = Factions.i.create();

View File

@ -82,14 +82,17 @@ public class CmdJoin extends FCommand
return; return;
} }
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay // 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 && ! payForCommand(Conf.econCostJoin, "to join a faction", "for joining a faction")) return; if (samePlayer && ! canAffordCommand(Conf.econCostJoin, "to join a faction")) return;
// trigger the join event (cancellable) // trigger the join event (cancellable)
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.COMMAND); FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.COMMAND);
Bukkit.getServer().getPluginManager().callEvent(joinEvent); Bukkit.getServer().getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) return; 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("<i>%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme)); fme.msg("<i>%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme));
if (!samePlayer) if (!samePlayer)

View File

@ -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 economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay
if ( ! payForCommand(Conf.econCostKick, "to kick someone from the faction", "for kicking someone from the faction")) return; if ( ! canAffordCommand(Conf.econCostKick, "to kick someone from the faction")) return;
// trigger the leave event (cancellable) [reason:kicked] // trigger the leave event (cancellable) [reason:kicked]
FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED); FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return; 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<i> kicked %s<i> from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true)); yourFaction.msg("%s<i> kicked %s<i> from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true));
you.msg("%s<i> kicked you from %s<i>! :O", fme.describeTo(you, true), yourFaction.describeTo(you)); you.msg("%s<i> kicked you from %s<i>! :O", fme.describeTo(you, true), yourFaction.describeTo(you));
if (yourFaction != myFaction) if (yourFaction != myFaction)

View File

@ -51,14 +51,17 @@ public class CmdTag extends FCommand
return; return;
} }
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay // if economy is enabled, they're not on the bypass list, and this command has a cost set, make sure they can pay
if ( ! payForCommand(Conf.econCostTag, "to change the faction tag", "for changing the faction tag")) return; if ( ! canAffordCommand(Conf.econCostTag, "to change the faction tag")) return;
// trigger the faction rename event (cancellable) // trigger the faction rename event (cancellable)
FactionRenameEvent renameEvent = new FactionRenameEvent(fme, tag); FactionRenameEvent renameEvent = new FactionRenameEvent(fme, tag);
Bukkit.getServer().getPluginManager().callEvent(renameEvent); Bukkit.getServer().getPluginManager().callEvent(renameEvent);
if(renameEvent.isCancelled()) return; 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(); String oldtag = myFaction.getTag();
myFaction.setTag(tag); myFaction.setTag(tag);

View File

@ -104,6 +104,10 @@ public class CmdUnclaim extends FCommand
return; return;
} }
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(flocation, otherFaction, fme);
Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
if(unclaimEvent.isCancelled()) return;
if (Econ.shouldBeUsed()) if (Econ.shouldBeUsed())
{ {
double refund = Econ.calculateClaimRefund(myFaction.getLandRounded()); 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); Board.removeAt(flocation);
SpoutFeatures.updateTerritoryDisplayLoc(flocation); SpoutFeatures.updateTerritoryDisplayLoc(flocation);
myFaction.msg("%s<i> unclaimed some land.", fme.describeTo(myFaction, true)); myFaction.msg("%s<i> unclaimed some land.", fme.describeTo(myFaction, true));

View File

@ -319,63 +319,19 @@ public abstract class FCommand extends MCommand<P>
if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) return true; if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) return true;
if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction())
{ return Econ.modifyMoney(myFaction, -cost, toDoThis, forDoingThis);
if ( ! Econ.modifyMoney(myFaction, -cost, toDoThis, forDoingThis)) return false;
}
else else
{ return Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis);
if ( ! Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis)) return false; }
}
return true; // 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;
// pay up if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction())
if (cost > 0.0) return Econ.hasAtLeast(myFaction, -cost, toDoThis);
{
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?
else else
{ return Econ.hasAtLeast(fme, -cost, toDoThis);
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;*/
} }
} }

View File

@ -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("<h>%s<i> can't afford <h>%s<i> %s.", ep.describeTo(ep, true), moneyString(delta), toDoThis);
return false;
}
return true;
}
public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis) public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis)
{ {
if ( ! shouldBeUsed()) return false; if ( ! shouldBeUsed()) return false;
@ -235,7 +248,8 @@ public class Econ
// There is no risk of failure // There is no risk of failure
econ.depositPlayer(acc, delta); econ.depositPlayer(acc, delta);
modifyUniverseMoney(-delta); modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis); if (forDoingThis != null && !forDoingThis.isEmpty())
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis);
return true; return true;
} }
else else
@ -248,13 +262,15 @@ public class Econ
// There is enough money to pay // There is enough money to pay
econ.withdrawPlayer(acc, -delta); econ.withdrawPlayer(acc, -delta);
modifyUniverseMoney(-delta); modifyUniverseMoney(-delta);
ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis); if (forDoingThis != null && !forDoingThis.isEmpty())
ep.msg("<h>%s<i> lost <h>%s<i> %s.", You, moneyString(-delta), forDoingThis);
return true; return true;
} }
else else
{ {
// There was not enough money to pay // There was not enough money to pay
ep.msg("<h>%s<i> can't afford <h>%s<i> %s.", You, moneyString(-delta), toDoThis); if (toDoThis != null && !toDoThis.isEmpty())
ep.msg("<h>%s<i> can't afford <h>%s<i> %s.", You, moneyString(-delta), toDoThis);
return false; return false;
} }
} }