Add Minimum Balance, to allow people to manage overdrafts.

This commit is contained in:
KHobbits 2012-02-26 04:15:14 +00:00
parent 5d048d2c1d
commit a5b38ce1a4
9 changed files with 57 additions and 7 deletions

View file

@ -116,6 +116,8 @@ public interface ISettings extends IConf
double getMaxMoney(); double getMaxMoney();
double getMinMoney();
boolean isEcoLogEnabled(); boolean isEcoLogEnabled();
boolean removeGodOnDisconnect(); boolean removeGodOnDisconnect();

View file

@ -33,6 +33,8 @@ public interface IUser extends Player
void giveMoney(double value); void giveMoney(double value);
boolean canAfford(double value);
String getGroup(); String getGroup();
void setLastLocation(); void setLastLocation();

View file

@ -540,6 +540,22 @@ public class Settings implements ISettings
return max; return max;
} }
private final static double MINMONEY = -10000000000000.0;
@Override
public double getMinMoney()
{
double min = config.getDouble("min-money", MINMONEY);
if (min > 0) {
min = -min;
}
if (min < MINMONEY)
{
min = MINMONEY;
}
return min;
}
@Override @Override
public boolean isEcoLogEnabled() public boolean isEcoLogEnabled()
{ {

View file

@ -143,8 +143,7 @@ public class Trade
{ {
if (getMoney() != null) if (getMoney() != null)
{ {
final double mon = user.getMoney(); if (!user.canAfford(getMoney()) && getMoney() > 0)
if (mon < getMoney() && getMoney() > 0 && !user.isAuthorized("essentials.eco.loan"))
{ {
throw new ChargeException(_("notEnoughMoney")); throw new ChargeException(_("notEnoughMoney"));
} }
@ -163,9 +162,8 @@ public class Trade
&& !user.isAuthorized("essentials.nocommandcost.all") && !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command)) && !user.isAuthorized("essentials.nocommandcost." + command))
{ {
final double mon = user.getMoney();
final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command); final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
if (mon < cost && cost > 0 && !user.isAuthorized("essentials.eco.loan")) if (!user.canAfford(cost) && cost > 0)
{ {
throw new ChargeException(_("notEnoughMoney")); throw new ChargeException(_("notEnoughMoney"));
} }

View file

@ -153,9 +153,18 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
public boolean canAfford(final double cost) public boolean canAfford(final double cost)
{
return canAfford(cost, true);
}
public boolean canAfford(final double cost, final boolean permcheck)
{ {
final double mon = getMoney(); final double mon = getMoney();
return mon >= cost || isAuthorized("essentials.eco.loan"); if (!permcheck || isAuthorized("essentials.eco.loan"))
{
return (mon + cost) > ess.getSettings().getMinMoney();
}
return cost <= mon;
} }
public void dispose() public void dispose()

View file

@ -115,6 +115,10 @@ public final class Economy
{ {
throw new UserDoesNotExistException(name); throw new UserDoesNotExistException(name);
} }
if (balance < ess.getSettings().getMinMoney())
{
throw new NoLoanPermittedException();
}
if (balance < 0.0 && !user.isAuthorized("essentials.eco.loan")) if (balance < 0.0 && !user.isAuthorized("essentials.eco.loan"))
{ {
throw new NoLoanPermittedException(); throw new NoLoanPermittedException();

View file

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import java.util.Locale; import java.util.Locale;
import org.bukkit.Server; import org.bukkit.Server;
@ -45,6 +46,9 @@ public class Commandeco extends EssentialsCommand
break; break;
case TAKE: case TAKE:
if (!player.canAfford(amount, false)) {
throw new Exception(_("notEnoughMoney"));
}
player.takeMoney(amount); player.takeMoney(amount);
break; break;

View file

@ -196,4 +196,15 @@ public class User extends UserBase implements IUser
unlock(); unlock();
} }
} }
@Override
public boolean canAfford(final double cost)
{
final double mon = getMoney();
if (isAuthorized("essentials.eco.loan"))
{
return (mon + cost) > ess.getSettings().getMinMoney();
}
return cost <= mon;
}
} }

View file

@ -281,10 +281,10 @@ spawn-if-no-home: true
update-bed-at-daytime: true update-bed-at-daytime: true
# Allow players to have multiple homes. # Allow players to have multiple homes.
# Players need essentials.sethome.multiple before they can have more than 1 home, default to 'default' below.
# Define different amounts of multiple homes for different permissions, e.g. essentials.sethome.multiple.vip # Define different amounts of multiple homes for different permissions, e.g. essentials.sethome.multiple.vip
# People with essentials.sethome.multiple.unlimited are not limited by these numbers. # People with essentials.sethome.multiple.unlimited are not limited by these numbers.
sethome-multiple: sethome-multiple:
# essentials.sethome.multiple
default: 3 default: 3
# essentials.sethome.multiple.vip # essentials.sethome.multiple.vip
vip: 5 vip: 5
@ -321,6 +321,10 @@ currency-symbol: '$'
# The amount is always limited to 10 trillions because of the limitations of a java double # The amount is always limited to 10 trillions because of the limitations of a java double
max-money: 10000000000000 max-money: 10000000000000
# Set the minimum amount of money a player can have
# Setting this to 0, will disable overdrafts/loans compeltely. Users need 'essentials.eco.loan' perm to go below 0.
min-money: -10000000000000
# Enable this to log all interactions with trade/buy/sell signs and sell command # Enable this to log all interactions with trade/buy/sell signs and sell command
economy-log-enabled: false economy-log-enabled: false