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:34 -05:00
parent fd8ca30af6
commit 7b9674dc4b
8 changed files with 80 additions and 94 deletions

View File

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

View File

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

View File

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

View File

@ -60,14 +60,17 @@ public class CmdKick extends FCommand
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<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,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();

View File

@ -40,6 +40,10 @@ public class CmdUnclaim extends FCommand
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 = "<i>";
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<i> unclaimed some land.", fme.describeTo(myFaction, true));

View File

@ -390,63 +390,19 @@ public abstract class FCommand extends MCommand<P>
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 Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis);
}
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.hasAdminMode()) 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;
}
if(Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction())
return Econ.hasAtLeast(myFaction, -cost, toDoThis);
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
{
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

@ -211,6 +211,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;
@ -231,6 +244,7 @@ public class Econ
// There is no risk of failure
econ.depositPlayer(acc, delta);
modifyUniverseMoney(-delta);
if (forDoingThis != null && !forDoingThis.isEmpty())
ep.msg("<h>%s<i> gained <h>%s<i> %s.", You, moneyString(delta), forDoingThis);
return true;
}
@ -244,12 +258,14 @@ public class Econ
// There is enough money to pay
econ.withdrawPlayer(acc, -delta);
modifyUniverseMoney(-delta);
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
if (toDoThis != null && !toDoThis.isEmpty())
ep.msg("<h>%s<i> can't afford <h>%s<i> %s.", You, moneyString(-delta), toDoThis);
return false;
}