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)
{
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)
{
@ -792,29 +792,32 @@ 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
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());

View File

@ -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();

View File

@ -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("<i>%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme));
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 ( ! 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<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));
if (yourFaction != myFaction)

View File

@ -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);

View File

@ -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<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(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;
/*
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;
// 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?
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);
}
}

View File

@ -215,6 +215,19 @@ 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)
{
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("<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;
}
else
@ -248,13 +262,15 @@ public class Econ
// There is enough money to pay
econ.withdrawPlayer(acc, -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;
}
else
{
// 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;
}
}