Add Minimum Balance, to allow people to manage overdrafts.

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

View File

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

View File

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

View File

@ -539,6 +539,22 @@ public class Settings implements ISettings
}
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
public boolean isEcoLogEnabled()

View File

@ -143,8 +143,7 @@ public class Trade
{
if (getMoney() != null)
{
final double mon = user.getMoney();
if (mon < getMoney() && getMoney() > 0 && !user.isAuthorized("essentials.eco.loan"))
if (!user.canAfford(getMoney()) && getMoney() > 0)
{
throw new ChargeException(_("notEnoughMoney"));
}
@ -163,9 +162,8 @@ public class Trade
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command))
{
final double mon = user.getMoney();
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"));
}

View File

@ -153,9 +153,18 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
}
public boolean canAfford(final double cost)
{
return canAfford(cost, true);
}
public boolean canAfford(final double cost, final boolean permcheck)
{
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()
@ -384,7 +393,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
public void updateMoneyCache(final double value)
{
if (ess.getPaymentMethod().hasMethod() && super.getMoney() != value)
{
{
super.setMoney(value);
}
}

View File

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

View File

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

View File

@ -196,4 +196,15 @@ public class User extends UserBase implements IUser
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
# 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
# People with essentials.sethome.multiple.unlimited are not limited by these numbers.
sethome-multiple:
# essentials.sethome.multiple
default: 3
# essentials.sethome.multiple.vip
vip: 5
@ -321,6 +321,10 @@ currency-symbol: '$'
# The amount is always limited to 10 trillions because of the limitations of a java double
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
economy-log-enabled: false