mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-09 09:57:34 +01:00
Add UUID and User based economy methods (#3085)
Adds methods to the Economy API to use UUIDs and User objects. Additionally, deprecates all the username (String) based methods. Backwards compatibility has been maintained and I added User validation to UUID-based methods and null checks in User-based methods
This commit is contained in:
parent
b19dec120a
commit
8ad55cb634
@ -42,7 +42,9 @@ public class Economy {
|
||||
File folder = new File(ess.getDataFolder(), "userdata");
|
||||
name = StringUtil.safeString(name);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs();
|
||||
if (!folder.mkdirs()) {
|
||||
throw new RuntimeException("Error while creating userdata directory!");
|
||||
}
|
||||
}
|
||||
UUID npcUUID = UUID.nameUUIDFromBytes(("NPC:" + name).getBytes(Charsets.UTF_8));
|
||||
EssentialsUserConf npcConfig = new EssentialsUserConf(name, npcUUID, new File(folder, npcUUID.toString() + ".yml"));
|
||||
@ -64,7 +66,7 @@ public class Economy {
|
||||
throw new RuntimeException(noCallBeforeLoad);
|
||||
}
|
||||
if (name == null) {
|
||||
throw new RuntimeException("Economy username cannot be null");
|
||||
throw new IllegalArgumentException("Economy username cannot be null");
|
||||
}
|
||||
|
||||
User user = ess.getUser(name);
|
||||
@ -86,9 +88,20 @@ public class Economy {
|
||||
return user;
|
||||
}
|
||||
|
||||
private static User getUserByUUID(UUID uuid) {
|
||||
if (ess == null) {
|
||||
throw new RuntimeException(noCallBeforeLoad);
|
||||
}
|
||||
if (uuid == null) {
|
||||
throw new IllegalArgumentException("Economy uuid cannot be null");
|
||||
}
|
||||
return ess.getUser(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the balance of a user
|
||||
*
|
||||
* @deprecated Use {@link Economy#getMoneyExact(UUID)} or {@link Economy#getMoneyExact(User)}
|
||||
* @param name Name of the user
|
||||
*
|
||||
* @return balance
|
||||
@ -107,17 +120,41 @@ public class Economy {
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Usernames can change, use {@link Economy#getMoneyExact(UUID)} or {@link Economy#getMoneyExact(User)}
|
||||
* @param name Name of user
|
||||
* @return Exact balance of user
|
||||
* @throws UserDoesNotExistException
|
||||
*/
|
||||
@Deprecated
|
||||
public static BigDecimal getMoneyExact(String name) throws UserDoesNotExistException {
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
return getMoneyExact(user);
|
||||
}
|
||||
|
||||
public static BigDecimal getMoneyExact(UUID uuid) throws UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
return getMoneyExact(user);
|
||||
}
|
||||
|
||||
public static BigDecimal getMoneyExact(User user) {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
return user.getMoney();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the balance of a user
|
||||
*
|
||||
* @deprecated Use {@link Economy#setMoney(UUID, BigDecimal)} or {@link Economy#setMoney(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param balance The balance you want to set
|
||||
*
|
||||
@ -133,11 +170,55 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the balance of a user
|
||||
*
|
||||
* @deprecated Usernames can change use {@link Economy#setMoney(UUID, BigDecimal)} or {@link Economy#setMoney(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of user
|
||||
* @param balance The balance you want to set
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
@Deprecated
|
||||
public static void setMoney(String name, BigDecimal balance) throws UserDoesNotExistException, NoLoanPermittedException {
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
setMoney(user, balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the balance of a user
|
||||
*
|
||||
* @param uuid UUID of user
|
||||
* @param balance The balance you want to set
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that uuid does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
public static void setMoney(UUID uuid, BigDecimal balance) throws NoLoanPermittedException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
setMoney(user, balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the balance of a user
|
||||
*
|
||||
* @param user User
|
||||
* @param balance The balance you want to set
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
public static void setMoney(User user, BigDecimal balance) throws NoLoanPermittedException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
if (balance.compareTo(ess.getSettings().getMinMoney()) < 0) {
|
||||
throw new NoLoanPermittedException();
|
||||
}
|
||||
@ -149,12 +230,14 @@ public class Economy {
|
||||
} catch (MaxMoneyException ex) {
|
||||
//TODO: Update API to show max balance errors
|
||||
}
|
||||
Trade.log("API", "Set", "API", name, new Trade(balance, ess), null, null, null, ess);
|
||||
Trade.log("API", "Set", "API", user.getName(), new Trade(balance, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds money to the balance of a user
|
||||
*
|
||||
* Use {@link Economy#add(UUID, BigDecimal)} or {@link Economy#add(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The money you want to add
|
||||
*
|
||||
@ -170,17 +253,72 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds money to the balance of a user
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#add(UUID, BigDecimal)} or {@link Economy#add(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money to be added to the user's account
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static void add(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
|
||||
BigDecimal result = getMoneyExact(name).add(amount, MATH_CONTEXT);
|
||||
setMoney(name, result);
|
||||
Trade.log("API", "Add", "API", name, new Trade(amount, ess), null, null, null, ess);
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
add(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Substracts money from the balance of a user
|
||||
* Adds money to the balance of a user
|
||||
*
|
||||
* @param uuid UUID of the user
|
||||
* @param amount The money you want to add
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that uuid does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void add(UUID uuid, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
add(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds money to the balance of a user
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#add(UUID, BigDecimal)} or {@link Economy#add(User, BigDecimal)}
|
||||
*
|
||||
* @param user User
|
||||
* @param amount The money you want to add
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void add(User user, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
BigDecimal result = getMoneyExact(user).add(amount, MATH_CONTEXT);
|
||||
setMoney(user, result);
|
||||
Trade.log("API", "Add", "API", user.getName(), new Trade(amount, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts money from the balance of a user
|
||||
*
|
||||
* @deprecated Use {@link Economy#subtract(UUID, BigDecimal)} or {@link Economy#subtract(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The money you want to substract
|
||||
* @param amount The money you want to subtract
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exists
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
@ -190,19 +328,70 @@ public class Economy {
|
||||
try {
|
||||
substract(name, BigDecimal.valueOf(amount));
|
||||
} catch (ArithmeticException e) {
|
||||
logger.log(Level.WARNING, "Failed to substract " + amount + " of balance of " + name + ": " + e.getMessage(), e);
|
||||
logger.log(Level.WARNING, "Failed to subtract " + amount + " of balance of " + name + ": " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts money from the balance of a user
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#subtract(UUID, BigDecimal)} or {@link Economy#subtract(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The money you want to subtract
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static void substract(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
|
||||
BigDecimal result = getMoneyExact(name).subtract(amount, MATH_CONTEXT);
|
||||
setMoney(name, result);
|
||||
Trade.log("API", "Subtract", "API", name, new Trade(amount, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts money from the balance of a user
|
||||
*
|
||||
* @param uuid UUID of the user
|
||||
* @param amount The money you want to subtract
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void subtract(UUID uuid, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
subtract(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts money from the balance of a user
|
||||
*
|
||||
* @param user User
|
||||
* @param amount The money you want to subtract
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void subtract(User user, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
BigDecimal result = getMoneyExact(user).subtract(amount, MATH_CONTEXT);
|
||||
setMoney(user, result);
|
||||
Trade.log("API", "Subtract", "API", user.getName(), new Trade(amount, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the balance of a user by a value
|
||||
*
|
||||
* @deprecated Use {@link Economy#divide(UUID, BigDecimal)} or {@link Economy#divide(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The balance is divided by this value
|
||||
*
|
||||
@ -218,15 +407,68 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the balance of a user by a value
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#divide(UUID, BigDecimal)} or {@link Economy#divide(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The balance is divided by this value
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static void divide(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
|
||||
BigDecimal result = getMoneyExact(name).divide(amount, MATH_CONTEXT);
|
||||
setMoney(name, result);
|
||||
Trade.log("API", "Divide", "API", name, new Trade(amount, ess), null, null, null, ess);
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
divide(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the balance of a user by a value
|
||||
*
|
||||
* @param uuid Name of the user
|
||||
* @param amount The balance is divided by this value
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void divide(UUID uuid, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
divide(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the balance of a user by a value
|
||||
*
|
||||
* @param user Name of the user
|
||||
* @param amount The balance is divided by this value
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void divide(User user, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
BigDecimal result = getMoneyExact(user).divide(amount, MATH_CONTEXT);
|
||||
setMoney(user, result);
|
||||
Trade.log("API", "Divide", "API", user.getName(), new Trade(amount, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the balance of a user by a value
|
||||
*
|
||||
* @deprecated Use {@link Economy#multiply(UUID, BigDecimal)} or {@link Economy#multiply(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The balance is multiplied by this value
|
||||
*
|
||||
@ -242,20 +484,74 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the balance of a user by a value
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#multiply(UUID, BigDecimal)} or {@link Economy#multiply(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The balance is multiplied by the this value
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static void multiply(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
|
||||
BigDecimal result = getMoneyExact(name).multiply(amount, MATH_CONTEXT);
|
||||
setMoney(name, result);
|
||||
Trade.log("API", "Multiply", "API", name, new Trade(amount, ess), null, null, null, ess);
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
multiply(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the balance of a user by a value
|
||||
*
|
||||
* @param uuid Name of the user
|
||||
* @param amount The balance is multiplied by the this value
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that uuid does not exist
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void multiply(UUID uuid, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
multiply(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the balance of a user by a value
|
||||
*
|
||||
* @param user Name of the user
|
||||
* @param amount The balance is multiplied by the this value
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static void multiply(User user, BigDecimal amount) throws NoLoanPermittedException, ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
BigDecimal result = getMoneyExact(user).multiply(amount, MATH_CONTEXT);
|
||||
setMoney(user, result);
|
||||
Trade.log("API", "Multiply", "API", user.getName(), new Trade(amount, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the balance of a user to the starting balance
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#resetBalance(UUID)} or {@link Economy#resetBalance(User)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exists
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
@Deprecated
|
||||
public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException {
|
||||
if (ess == null) {
|
||||
throw new RuntimeException(noCallBeforeLoad);
|
||||
@ -265,6 +561,42 @@ public class Economy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the balance of a user to the starting balance
|
||||
*
|
||||
* @param uuid UUID of the user
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exists
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
public static void resetBalance(UUID uuid) throws NoLoanPermittedException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
resetBalance(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the balance of a user to the starting balance
|
||||
*
|
||||
* @param user User
|
||||
*
|
||||
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
||||
*/
|
||||
public static void resetBalance(User user) throws NoLoanPermittedException {
|
||||
if (ess == null) {
|
||||
throw new RuntimeException(noCallBeforeLoad);
|
||||
}
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
setMoney(user, ess.getSettings().getStartingBalance());
|
||||
Trade.log("API", "Reset", "API", user.getName(), new Trade(BigDecimal.ZERO, ess), null, null, null, ess);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link Economy#hasEnough(UUID, BigDecimal)} or {@link Economy#hasEnough(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
@ -282,11 +614,60 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Usernames can change, use {@link Economy#hasEnough(UUID, BigDecimal)} or {@link Economy#hasEnough(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more or an equal amount of money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasEnough(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException {
|
||||
return amount.compareTo(getMoneyExact(name)) <= 0;
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
return hasEnough(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uuid UUID of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more or an equal amount of money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exist
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasEnough(UUID uuid, BigDecimal amount) throws ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
return hasEnough(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user User
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more or an equal amount of money
|
||||
*
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasEnough(User user, BigDecimal amount) throws ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
return amount.compareTo(getMoneyExact(user)) <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link Economy#hasMore(UUID, BigDecimal)} or {@link Economy#hasMore(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
@ -304,11 +685,61 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Usernames can change, use {@link Economy#hasMore(UUID, BigDecimal)} or {@link Economy#hasMore(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exists
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static boolean hasMore(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException {
|
||||
return amount.compareTo(getMoneyExact(name)) < 0;
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
return hasMore(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uuid UUID of the user
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exists
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasMore(UUID uuid, BigDecimal amount) throws ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
return hasMore(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user User
|
||||
* @param amount The amount of money the user should have
|
||||
*
|
||||
* @return true, if the user has more money
|
||||
*
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasMore(User user, BigDecimal amount) throws ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
return amount.compareTo(getMoneyExact(user)) < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link Economy#hasLess(UUID, BigDecimal)} or {@link Economy#hasLess(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should not have
|
||||
*
|
||||
@ -326,21 +757,107 @@ public class Economy {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Usernames can change, use {@link Economy#hasLess(UUID, BigDecimal)} or {@link Economy#hasLess(User, BigDecimal)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
* @param amount The amount of money the user should not have
|
||||
*
|
||||
* @return true, if the user has less money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exist
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
@Deprecated
|
||||
public static boolean hasLess(String name, BigDecimal amount) throws UserDoesNotExistException, ArithmeticException {
|
||||
return amount.compareTo(getMoneyExact(name)) > 0;
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
return hasLess(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uuid UUID of the user
|
||||
* @param amount The amount of money the user should not have
|
||||
*
|
||||
* @return true, if the user has less money
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exist
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasLess(UUID uuid, BigDecimal amount) throws ArithmeticException, UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
return hasLess(user, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user User
|
||||
* @param amount The amount of money the user should not have
|
||||
*
|
||||
* @return true, if the user has less money
|
||||
*
|
||||
* @throws ArithmeticException
|
||||
*/
|
||||
public static boolean hasLess(User user, BigDecimal amount) throws ArithmeticException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
return amount.compareTo(getMoneyExact(user)) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the user has a negative balance
|
||||
*
|
||||
* @deprecated Usernames can change, use {@link Economy#isNegative(UUID)} or {@link Economy#isNegative(User)}
|
||||
*
|
||||
* @param name Name of the user
|
||||
*
|
||||
* @return true, if the user has a negative balance
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that name does not exists
|
||||
*/
|
||||
@Deprecated
|
||||
public static boolean isNegative(String name) throws UserDoesNotExistException {
|
||||
return getMoneyExact(name).signum() < 0;
|
||||
User user = getUserByName(name);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(name);
|
||||
}
|
||||
return isNegative(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the user has a negative balance
|
||||
*
|
||||
* @param uuid UUID of the user
|
||||
*
|
||||
* @return true, if the user has a negative balance
|
||||
*
|
||||
* @throws UserDoesNotExistException If a user by that UUID does not exists
|
||||
*/
|
||||
public static boolean isNegative(UUID uuid) throws UserDoesNotExistException {
|
||||
User user = getUserByUUID(uuid);
|
||||
if (user == null) {
|
||||
throw new UserDoesNotExistException(uuid);
|
||||
}
|
||||
return isNegative(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the user has a negative balance
|
||||
*
|
||||
* @param user User
|
||||
*
|
||||
* @return true, if the user has a negative balance
|
||||
*/
|
||||
public static boolean isNegative(User user) {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("Economy user cannot be null");
|
||||
}
|
||||
return getMoneyExact(user).signum() < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -370,14 +887,28 @@ public class Economy {
|
||||
/**
|
||||
* Test if a player exists to avoid the UserDoesNotExistException
|
||||
*
|
||||
* @deprecated Essentials is moving away from username based economy methods. This may be removed in the future.
|
||||
*
|
||||
* @param name Name of the user
|
||||
*
|
||||
* @return true, if the user exists
|
||||
*/
|
||||
@Deprecated
|
||||
public static boolean playerExists(String name) {
|
||||
return getUserByName(name) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a player exists to avoid the UserDoesNotExistException
|
||||
*
|
||||
* @param uuid UUID of the user
|
||||
*
|
||||
* @return true, if the user exists
|
||||
*/
|
||||
public static boolean playerExists(UUID uuid) {
|
||||
return getUserByUUID(uuid) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a player is a npc
|
||||
*
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.earth2me.essentials.api;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.earth2me.essentials.I18n.tl;
|
||||
|
||||
|
||||
@ -7,4 +9,8 @@ public class UserDoesNotExistException extends Exception {
|
||||
public UserDoesNotExistException(String name) {
|
||||
super(tl("userDoesNotExist", name));
|
||||
}
|
||||
|
||||
public UserDoesNotExistException(UUID uuid) {
|
||||
super(tl("uuidDoesNotExist", uuid.toString()));
|
||||
}
|
||||
}
|
||||
|
@ -585,6 +585,7 @@ userAFKWithMessage=\u00a77{0} \u00a75is currently AFK and may not respond: {1}
|
||||
userdataMoveBackError=Failed to move userdata/{0}.tmp to userdata/{1}\!
|
||||
userdataMoveError=Failed to move userdata/{0} to userdata/{1}.tmp\!
|
||||
userDoesNotExist=\u00a74The user\u00a7c {0} \u00a74does not exist.
|
||||
uuidDoesNotExist=\u00a74The user with UUID\u00a7c {0} \u00a74does not exist.
|
||||
userIsAway=\u00a77* {0} \u00a77is now AFK.
|
||||
userIsAwayWithMessage=\u00a77* {0} \u00a77is now AFK.
|
||||
userIsNotAway=\u00a77* {0} \u00a77is no longer AFK.
|
||||
|
Loading…
Reference in New Issue
Block a user