2011-05-02 20:22:17 +02:00
package com.earth2me.essentials.api ;
2011-05-02 05:56:51 +02:00
2014-04-15 01:06:29 +02:00
import com.earth2me.essentials.EssentialsUserConf ;
2014-05-31 21:37:44 +02:00
import com.earth2me.essentials.Trade ;
2013-07-13 19:40:46 +02:00
import com.earth2me.essentials.User ;
import com.earth2me.essentials.utils.NumberUtil ;
2013-10-11 04:44:41 +02:00
import com.earth2me.essentials.utils.StringUtil ;
2014-04-19 03:03:39 +02:00
import com.google.common.base.Charsets ;
2015-04-15 06:06:16 +02:00
import net.ess3.api.IEssentials ;
import net.ess3.api.MaxMoneyException ;
2019-12-23 14:16:34 +01:00
import net.ess3.api.events.UserBalanceUpdateEvent ;
2019-02-22 19:10:36 +01:00
import org.bukkit.entity.Player ;
2015-04-15 06:06:16 +02:00
2013-07-13 19:40:46 +02:00
import java.io.File ;
import java.math.BigDecimal ;
import java.math.MathContext ;
2021-02-08 16:50:57 +01:00
import java.text.MessageFormat ;
2014-04-19 03:03:39 +02:00
import java.util.UUID ;
2013-07-13 19:40:46 +02:00
import java.util.logging.Level ;
import java.util.logging.Logger ;
2011-05-02 05:56:51 +02:00
2013-07-13 19:40:46 +02:00
/ * *
2019-02-22 19:10:36 +01:00
* You should use Vault instead of directly using this class .
2013-07-13 19:40:46 +02:00
* /
2015-04-15 06:06:16 +02:00
public class Economy {
2020-10-03 19:46:05 +02:00
public static final MathContext MATH_CONTEXT = MathContext . DECIMAL128 ;
2021-02-08 16:50:57 +01:00
private static final Logger LOGGER = Logger . getLogger ( " Essentials " ) ;
2020-10-03 19:46:05 +02:00
private static IEssentials ess ;
2021-02-08 16:50:57 +01:00
private static final String WARN_CALL_BEFORE_LOAD = " Essentials API is called before Essentials is loaded. " ;
private static final String WARN_EXISTING_NPC_CREATE = " Account creation was requested for NPC account {0}, but account already exists (UUID: {1}). Not creating an account. " ;
private static final String WARN_PLAYER_UUID_NO_NAME = " Found player {0} by UUID {1} but not by their actual name. They may have changed their username. " ;
private static final String WARN_NPC_RECREATE_1 = " Account creation was requested for NPC user {0}, but an account file with UUID {1} already exists. " ;
private static final String WARN_NPC_RECREATE_2 = " Essentials will create a new account as requested by the other plugin, but this is almost certainly a bug and should be reported. " ;
2020-10-03 19:46:05 +02:00
protected Economy ( ) {
}
2015-04-15 06:06:16 +02:00
/ * *
* @param aEss the ess to set
* /
2020-10-03 19:46:05 +02:00
public static void setEss ( final IEssentials aEss ) {
2015-04-15 06:06:16 +02:00
ess = aEss ;
}
private static void createNPCFile ( String name ) {
2020-10-03 19:46:05 +02:00
final File folder = new File ( ess . getDataFolder ( ) , " userdata " ) ;
2015-04-15 06:06:16 +02:00
name = StringUtil . safeString ( name ) ;
if ( ! folder . exists ( ) ) {
2020-05-04 12:00:25 +02:00
if ( ! folder . mkdirs ( ) ) {
throw new RuntimeException ( " Error while creating userdata directory! " ) ;
}
2015-04-15 06:06:16 +02:00
}
2020-10-03 19:46:05 +02:00
final UUID npcUUID = UUID . nameUUIDFromBytes ( ( " NPC: " + name ) . getBytes ( Charsets . UTF_8 ) ) ;
2021-02-08 16:50:57 +01:00
final File npcFile = new File ( folder , npcUUID . toString ( ) + " .yml " ) ;
if ( npcFile . exists ( ) ) {
LOGGER . log ( Level . SEVERE , MessageFormat . format ( WARN_NPC_RECREATE_1 , name , npcUUID . toString ( ) ) , new RuntimeException ( ) ) ;
LOGGER . log ( Level . SEVERE , WARN_NPC_RECREATE_2 ) ;
}
final EssentialsUserConf npcConfig = new EssentialsUserConf ( name , npcUUID , npcFile ) ;
2015-04-15 06:06:16 +02:00
npcConfig . load ( ) ;
npcConfig . setProperty ( " npc " , true ) ;
npcConfig . setProperty ( " lastAccountName " , name ) ;
npcConfig . setProperty ( " money " , ess . getSettings ( ) . getStartingBalance ( ) ) ;
npcConfig . forceSave ( ) ;
ess . getUserMap ( ) . trackUUID ( npcUUID , name , false ) ;
}
2020-10-03 19:46:05 +02:00
private static void deleteNPC ( final String name ) {
final User user = ess . getUser ( name ) ;
2015-04-15 06:06:16 +02:00
user . reset ( ) ;
}
2020-10-03 19:46:05 +02:00
private static User getUserByName ( final String name ) {
2015-04-15 06:06:16 +02:00
if ( ess = = null ) {
2021-02-08 16:50:57 +01:00
throw new RuntimeException ( WARN_CALL_BEFORE_LOAD ) ;
2015-04-15 06:06:16 +02:00
}
if ( name = = null ) {
2020-05-04 12:00:25 +02:00
throw new IllegalArgumentException ( " Economy username cannot be null " ) ;
2015-04-15 06:06:16 +02:00
}
2019-02-22 19:10:36 +01:00
User user = ess . getUser ( name ) ;
if ( user = = null ) {
/ *
Attempt lookup using UUID - this prevents balance resets when accessing economy
via Vault during player join .
See : https : //github.com/EssentialsX/Essentials/issues/2400
* /
2020-10-03 19:46:05 +02:00
final Player player = ess . getServer ( ) . getPlayerExact ( name ) ;
2019-02-22 19:10:36 +01:00
if ( player ! = null ) {
user = ess . getUser ( player . getUniqueId ( ) ) ;
if ( user ! = null ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . INFO , MessageFormat . format ( WARN_PLAYER_UUID_NO_NAME , name , player . getUniqueId ( ) . toString ( ) ) , new RuntimeException ( ) ) ;
2019-02-22 19:10:36 +01:00
}
}
}
return user ;
2015-04-15 06:06:16 +02:00
}
2020-10-03 19:46:05 +02:00
private static User getUserByUUID ( final UUID uuid ) {
2020-05-04 12:00:25 +02:00
if ( ess = = null ) {
2021-02-08 16:50:57 +01:00
throw new RuntimeException ( WARN_CALL_BEFORE_LOAD ) ;
2020-05-04 12:00:25 +02:00
}
if ( uuid = = null ) {
throw new IllegalArgumentException ( " Economy uuid cannot be null " ) ;
}
return ess . getUser ( uuid ) ;
}
2015-04-15 06:06:16 +02:00
/ * *
* Returns the balance of a user
*
* @param name Name of the user
* @return balance
* @throws UserDoesNotExistException
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # getMoneyExact ( UUID ) } or { @link Economy # getMoneyExact ( User ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static double getMoney ( final String name ) throws UserDoesNotExistException {
final BigDecimal exactAmount = getMoneyExact ( name ) ;
2018-08-22 00:45:58 +02:00
double amount = exactAmount . doubleValue ( ) ;
if ( new BigDecimal ( amount ) . compareTo ( exactAmount ) > 0 ) {
// closest double is bigger than the exact amount
// -> get the previous double value to not return more money than the user has
amount = Math . nextAfter ( amount , Double . NEGATIVE_INFINITY ) ;
}
return amount ;
2015-04-15 06:06:16 +02:00
}
2020-05-04 12:00:25 +02:00
/ * *
* @param name Name of user
* @return Exact balance of user
* @throws UserDoesNotExistException
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # getMoneyExact ( UUID ) } or { @link Economy # getMoneyExact ( User ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static BigDecimal getMoneyExact ( final String name ) throws UserDoesNotExistException {
final User user = getUserByName ( name ) ;
2015-04-15 06:06:16 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
2020-05-04 12:00:25 +02:00
return getMoneyExact ( user ) ;
}
2020-10-04 18:03:52 +02:00
/ * *
* Get the exact balance of the account with the given UUID .
*
* @param uuid The UUID of the user account to retrieve the balance for
* @return The account ' s balance
* @throws UserDoesNotExistException If the user does not exist
* /
2020-10-03 19:46:05 +02:00
public static BigDecimal getMoneyExact ( final UUID uuid ) throws UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( uuid ) ;
}
return getMoneyExact ( user ) ;
}
2020-10-04 18:03:52 +02:00
/ * *
* Get the exact balance of the account with the given UUID .
*
* @param user The user account to retrieve the balance for
* @return The account ' s balance
* /
2020-10-03 19:46:05 +02:00
public static BigDecimal getMoneyExact ( final User user ) {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2015-04-15 06:06:16 +02:00
return user . getMoney ( ) ;
}
/ * *
* Sets the balance of a user
*
* @param name Name of the user
* @param balance The balance you want to set
* @throws UserDoesNotExistException If a user by that name does not exists
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # setMoney ( UUID , BigDecimal ) } or { @link Economy # setMoney ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void setMoney ( final String name , final double balance ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
try {
setMoney ( name , BigDecimal . valueOf ( balance ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to set balance of " + name + " to " + balance + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
}
}
2020-05-04 12:00:25 +02:00
/ * *
* Sets the balance of a user
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change use { @link Economy # setMoney ( UUID , BigDecimal ) } or { @link Economy # setMoney ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void setMoney ( final String name , final BigDecimal balance ) throws UserDoesNotExistException , NoLoanPermittedException {
final User user = getUserByName ( name ) ;
2015-04-15 06:06:16 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static void setMoney ( final UUID uuid , final BigDecimal balance ) throws NoLoanPermittedException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static void setMoney ( final User user , final BigDecimal balance ) throws NoLoanPermittedException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2015-04-15 06:06:16 +02:00
if ( balance . compareTo ( ess . getSettings ( ) . getMinMoney ( ) ) < 0 ) {
throw new NoLoanPermittedException ( ) ;
}
if ( balance . signum ( ) < 0 & & ! user . isAuthorized ( " essentials.eco.loan " ) ) {
throw new NoLoanPermittedException ( ) ;
}
try {
2019-12-23 14:16:34 +01:00
user . setMoney ( balance , UserBalanceUpdateEvent . Cause . API ) ;
2020-10-03 19:46:05 +02:00
} catch ( final MaxMoneyException ex ) {
2015-04-15 06:06:16 +02:00
//TODO: Update API to show max balance errors
}
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Set " , " API " , user . getName ( ) , new Trade ( balance , ess ) , null , null , null , balance , ess ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
* Adds money to the balance of a user
2020-10-03 19:46:05 +02:00
* < p >
2020-05-04 12:00:25 +02:00
* Use { @link Economy # add ( UUID , BigDecimal ) } or { @link Economy # add ( User , BigDecimal ) }
*
2015-04-15 06:06:16 +02:00
* @param name Name of the user
* @param amount The money you want to add
* @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
2020-10-03 19:46:05 +02:00
public static void add ( final String name , final double amount ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
try {
add ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to add " + amount + " to balance of " + name + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
}
}
2020-05-04 12:00:25 +02:00
/ * *
* Adds money to the balance of a user
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # add ( UUID , BigDecimal ) } or { @link Economy # add ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void add ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , NoLoanPermittedException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
add ( user , amount ) ;
}
/ * *
* 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
* /
2020-10-03 19:46:05 +02:00
public static void add ( final UUID uuid , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( uuid ) ;
}
add ( user , amount ) ;
}
/ * *
* Adds money to the balance of a user
*
* @param user User
* @param amount The money you want to add
2020-10-03 19:46:05 +02:00
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-05-04 12:00:25 +02:00
* @throws ArithmeticException
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # add ( UUID , BigDecimal ) } or { @link Economy # add ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
2020-10-03 19:46:05 +02:00
public static void add ( final User user , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2020-10-03 19:46:05 +02:00
final BigDecimal result = getMoneyExact ( user ) . add ( amount , MATH_CONTEXT ) ;
2020-05-04 12:00:25 +02:00
setMoney ( user , result ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Add " , " API " , user . getName ( ) , new Trade ( amount , ess ) , null , null , null , result , ess ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
2020-05-04 12:00:25 +02:00
* Subtracts money from the balance of a user
*
2015-04-15 06:06:16 +02:00
* @param name Name of the user
2020-05-04 12:00:25 +02:00
* @param amount The money you want to subtract
2015-04-15 06:06:16 +02:00
* @throws UserDoesNotExistException If a user by that name does not exists
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # subtract ( UUID , BigDecimal ) } or { @link Economy # subtract ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void subtract ( final String name , final double amount ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
try {
substract ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to subtract " + amount + " of balance of " + name + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
}
}
2020-05-04 12:00:25 +02:00
/ * *
* Subtracts money from the balance of a user
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # subtract ( UUID , BigDecimal ) } or { @link Economy # subtract ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void substract ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , NoLoanPermittedException , ArithmeticException {
final BigDecimal result = getMoneyExact ( name ) . subtract ( amount , MATH_CONTEXT ) ;
2015-04-15 06:06:16 +02:00
setMoney ( name , result ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Subtract " , " API " , name , new Trade ( amount , ess ) , null , null , null , result , ess ) ;
2015-04-15 06:06:16 +02:00
}
2020-05-04 12:00:25 +02:00
/ * *
* 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
* /
2020-10-03 19:46:05 +02:00
public static void subtract ( final UUID uuid , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static void subtract ( final User user , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2020-10-03 19:46:05 +02:00
final BigDecimal result = getMoneyExact ( user ) . subtract ( amount , MATH_CONTEXT ) ;
2020-05-04 12:00:25 +02:00
setMoney ( user , result ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Subtract " , " API " , user . getName ( ) , new Trade ( amount , ess ) , null , null , null , result , ess ) ;
2020-05-04 12:00:25 +02:00
}
2015-04-15 06:06:16 +02:00
/ * *
* Divides the balance of a user by a value
*
2020-10-03 19:46:05 +02:00
* @param name Name of the user
2018-01-19 02:52:44 +01:00
* @param amount The balance is divided by this value
2015-04-15 06:06:16 +02:00
* @throws UserDoesNotExistException If a user by that name does not exists
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # divide ( UUID , BigDecimal ) } or { @link Economy # divide ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void divide ( final String name , final double amount ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
try {
divide ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to divide balance of " + name + " by " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
}
}
2020-05-04 12:00:25 +02:00
/ * *
* Divides the balance of a user by a value
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # divide ( UUID , BigDecimal ) } or { @link Economy # divide ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void divide ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , NoLoanPermittedException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static void divide ( final UUID uuid , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
2020-10-03 19:46:05 +02:00
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-05-04 12:00:25 +02:00
* @throws ArithmeticException
* /
2020-10-03 19:46:05 +02:00
public static void divide ( final User user , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2020-10-03 19:46:05 +02:00
final BigDecimal result = getMoneyExact ( user ) . divide ( amount , MATH_CONTEXT ) ;
2020-05-04 12:00:25 +02:00
setMoney ( user , result ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Divide " , " API " , user . getName ( ) , new Trade ( amount , ess ) , null , null , null , result , ess ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
* Multiplies the balance of a user by a value
*
2020-10-03 19:46:05 +02:00
* @param name Name of the user
2018-01-19 02:52:44 +01:00
* @param amount The balance is multiplied by this value
2015-04-15 06:06:16 +02:00
* @throws UserDoesNotExistException If a user by that name does not exists
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # multiply ( UUID , BigDecimal ) } or { @link Economy # multiply ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void multiply ( final String name , final double amount ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
try {
multiply ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to multiply balance of " + name + " by " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
}
}
2020-05-04 12:00:25 +02:00
/ * *
* Multiplies the balance of a user by a value
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # multiply ( UUID , BigDecimal ) } or { @link Economy # multiply ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static void multiply ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , NoLoanPermittedException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static void multiply ( final UUID uuid , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
2020-10-03 19:46:05 +02:00
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-05-04 12:00:25 +02:00
* @throws ArithmeticException
* /
2020-10-03 19:46:05 +02:00
public static void multiply ( final User user , final BigDecimal amount ) throws NoLoanPermittedException , ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
2020-10-03 19:46:05 +02:00
final BigDecimal result = getMoneyExact ( user ) . multiply ( amount , MATH_CONTEXT ) ;
2020-05-04 12:00:25 +02:00
setMoney ( user , result ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Multiply " , " API " , user . getName ( ) , new Trade ( amount , ess ) , null , null , null , result , ess ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
* Resets the balance of a user to the starting balance
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # resetBalance ( UUID ) } or { @link Economy # resetBalance ( User ) }
2015-04-15 06:06:16 +02:00
* /
2020-05-04 12:00:25 +02:00
@Deprecated
2020-10-03 19:46:05 +02:00
public static void resetBalance ( final String name ) throws UserDoesNotExistException , NoLoanPermittedException {
2015-04-15 06:06:16 +02:00
if ( ess = = null ) {
2021-02-08 16:50:57 +01:00
throw new RuntimeException ( WARN_CALL_BEFORE_LOAD ) ;
2015-04-15 06:06:16 +02:00
}
setMoney ( name , ess . getSettings ( ) . getStartingBalance ( ) ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Reset " , " API " , name , new Trade ( BigDecimal . ZERO , ess ) , null , null , null , ess . getSettings ( ) . getStartingBalance ( ) , ess ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
2020-05-04 12:00:25 +02:00
* 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
* /
2020-10-03 19:46:05 +02:00
public static void resetBalance ( final UUID uuid ) throws NoLoanPermittedException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( uuid ) ;
}
resetBalance ( user ) ;
}
/ * *
* Resets the balance of a user to the starting balance
*
* @param user User
2020-10-03 19:46:05 +02:00
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
2020-05-04 12:00:25 +02:00
* /
2020-10-03 19:46:05 +02:00
public static void resetBalance ( final User user ) throws NoLoanPermittedException {
2020-05-04 12:00:25 +02:00
if ( ess = = null ) {
2021-02-08 16:50:57 +01:00
throw new RuntimeException ( WARN_CALL_BEFORE_LOAD ) ;
2020-05-04 12:00:25 +02:00
}
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
setMoney ( user , ess . getSettings ( ) . getStartingBalance ( ) ) ;
2021-01-02 06:12:53 +01:00
Trade . log ( " API " , " Reset " , " API " , user . getName ( ) , new Trade ( BigDecimal . ZERO , ess ) , null , null , null , ess . getSettings ( ) . getStartingBalance ( ) , ess ) ;
2020-05-04 12:00:25 +02:00
}
/ * *
2015-04-15 06:06:16 +02:00
* @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 exists
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # hasEnough ( UUID , BigDecimal ) } or { @link Economy # hasEnough ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean hasEnough ( final String name , final double amount ) throws UserDoesNotExistException {
2015-04-15 06:06:16 +02:00
try {
return hasEnough ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to compare balance of " + name + " with " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
return false ;
}
}
2020-05-04 12:00:25 +02:00
/ * *
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # hasEnough ( UUID , BigDecimal ) } or { @link Economy # hasEnough ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
2020-10-03 19:46:05 +02:00
public static boolean hasEnough ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasEnough ( final UUID uuid , final BigDecimal amount ) throws ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( uuid ) ;
}
return hasEnough ( user , amount ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
2020-05-04 12:00:25 +02:00
* @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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasEnough ( final User user , final BigDecimal amount ) throws ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
return amount . compareTo ( getMoneyExact ( user ) ) < = 0 ;
}
/ * *
2015-04-15 06:06:16 +02:00
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # hasMore ( UUID , BigDecimal ) } or { @link Economy # hasMore ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean hasMore ( final String name , final double amount ) throws UserDoesNotExistException {
2015-04-15 06:06:16 +02:00
try {
return hasMore ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to compare balance of " + name + " with " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
return false ;
}
}
2020-05-04 12:00:25 +02:00
/ * *
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # hasMore ( UUID , BigDecimal ) } or { @link Economy # hasMore ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean hasMore ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
return hasMore ( user , amount ) ;
2015-04-15 06:06:16 +02:00
}
/ * *
2020-05-04 12:00:25 +02:00
* @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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasMore ( final UUID uuid , final BigDecimal amount ) throws ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasMore ( final User user , final BigDecimal amount ) throws ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
return amount . compareTo ( getMoneyExact ( user ) ) < 0 ;
}
/ * *
2015-04-15 06:06:16 +02:00
* @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 exists
2020-10-03 19:46:05 +02:00
* @deprecated Use { @link Economy # hasLess ( UUID , BigDecimal ) } or { @link Economy # hasLess ( User , BigDecimal ) }
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean hasLess ( final String name , final double amount ) throws UserDoesNotExistException {
2015-04-15 06:06:16 +02:00
try {
return hasLess ( name , BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final ArithmeticException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to compare balance of " + name + " with " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
return false ;
}
}
2020-05-04 12:00:25 +02:00
/ * *
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # hasLess ( UUID , BigDecimal ) } or { @link Economy # hasLess ( User , BigDecimal ) }
2020-05-04 12:00:25 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean hasLess ( final String name , final BigDecimal amount ) throws UserDoesNotExistException , ArithmeticException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasLess ( final UUID uuid , final BigDecimal amount ) throws ArithmeticException , UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
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
* /
2020-10-03 19:46:05 +02:00
public static boolean hasLess ( final User user , final BigDecimal amount ) throws ArithmeticException {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
return amount . compareTo ( getMoneyExact ( user ) ) > 0 ;
2015-04-15 06:06:16 +02:00
}
/ * *
* Test if the user has a negative balance
*
* @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
2020-10-03 19:46:05 +02:00
* @deprecated Usernames can change , use { @link Economy # isNegative ( UUID ) } or { @link Economy # isNegative ( User ) }
2015-04-15 06:06:16 +02:00
* /
2020-05-04 12:00:25 +02:00
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean isNegative ( final String name ) throws UserDoesNotExistException {
final User user = getUserByName ( name ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
return isNegative ( user ) ;
}
/ * *
* Test if the user has a negative balance
2020-10-03 19:46:05 +02:00
*
2020-05-04 12:00:25 +02:00
* @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
* /
2020-10-03 19:46:05 +02:00
public static boolean isNegative ( final UUID uuid ) throws UserDoesNotExistException {
final User user = getUserByUUID ( uuid ) ;
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( uuid ) ;
}
return isNegative ( user ) ;
}
/ * *
* Test if the user has a negative balance
*
2020-10-03 19:46:05 +02:00
* @param user User
2020-05-04 12:00:25 +02:00
* @return true , if the user has a negative balance
* /
2020-10-03 19:46:05 +02:00
public static boolean isNegative ( final User user ) {
2020-05-04 12:00:25 +02:00
if ( user = = null ) {
throw new IllegalArgumentException ( " Economy user cannot be null " ) ;
}
return getMoneyExact ( user ) . signum ( ) < 0 ;
2015-04-15 06:06:16 +02:00
}
/ * *
* Formats the amount of money like all other Essentials functions . Example : $100000 or $12345 . 67
*
* @param amount The amount of money
* @return Formatted money
2020-10-04 18:03:52 +02:00
* @deprecated Use { @link # format ( BigDecimal ) } if your input is already a { @link BigDecimal } .
2015-04-15 06:06:16 +02:00
* /
@Deprecated
2020-10-03 19:46:05 +02:00
public static String format ( final double amount ) {
2015-04-15 06:06:16 +02:00
try {
return format ( BigDecimal . valueOf ( amount ) ) ;
2020-10-03 19:46:05 +02:00
} catch ( final NumberFormatException e ) {
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , " Failed to display " + amount + " : " + e . getMessage ( ) , e ) ;
2015-04-15 06:06:16 +02:00
return " NaN " ;
}
}
2020-10-04 18:03:52 +02:00
/ * *
* Formats the amount of money like all other Essentials functions . Example : $100000 or $12345 . 67
*
* @param amount The amount of money
* @return Formatted money
* /
2020-10-03 19:46:05 +02:00
public static String format ( final BigDecimal amount ) {
2015-04-15 06:06:16 +02:00
if ( ess = = null ) {
2021-02-08 16:50:57 +01:00
throw new RuntimeException ( WARN_CALL_BEFORE_LOAD ) ;
2015-04-15 06:06:16 +02:00
}
return NumberUtil . displayCurrency ( amount , ess ) ;
}
/ * *
* Test if a player exists to avoid the UserDoesNotExistException
*
* @param name Name of the user
* @return true , if the user exists
2020-10-03 19:46:05 +02:00
* @deprecated Essentials is moving away from username based economy methods . This may be removed in the future .
2015-04-15 06:06:16 +02:00
* /
2020-05-04 12:00:25 +02:00
@Deprecated
2020-10-03 19:46:05 +02:00
public static boolean playerExists ( final String name ) {
2015-04-15 06:06:16 +02:00
return getUserByName ( name ) ! = null ;
}
2020-05-04 12:00:25 +02:00
/ * *
* Test if a player exists to avoid the UserDoesNotExistException
*
* @param uuid UUID of the user
* @return true , if the user exists
* /
2020-10-03 19:46:05 +02:00
public static boolean playerExists ( final UUID uuid ) {
2020-05-04 12:00:25 +02:00
return getUserByUUID ( uuid ) ! = null ;
}
2015-04-15 06:06:16 +02:00
/ * *
* Test if a player is a npc
*
* @param name Name of the player
* @return true , if it ' s a npc
* @throws UserDoesNotExistException
* /
2020-10-03 19:46:05 +02:00
public static boolean isNPC ( final String name ) throws UserDoesNotExistException {
final User user = getUserByName ( name ) ;
2015-04-15 06:06:16 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
return user . isNPC ( ) ;
}
/ * *
* Creates dummy files for a npc , if there is no player yet with that name .
*
* @param name Name of the player
* @return true , if a new npc was created
* /
2020-10-03 19:46:05 +02:00
public static boolean createNPC ( final String name ) {
final User user = getUserByName ( name ) ;
2015-04-15 06:06:16 +02:00
if ( user = = null ) {
createNPCFile ( name ) ;
return true ;
}
2021-02-08 16:50:57 +01:00
LOGGER . log ( Level . WARNING , MessageFormat . format ( WARN_EXISTING_NPC_CREATE , name , user . getConfigUUID ( ) ) , new RuntimeException ( ) ) ;
2015-04-15 06:06:16 +02:00
return false ;
}
/ * *
* Deletes a user , if it is marked as npc .
*
* @param name Name of the player
* @throws UserDoesNotExistException
* /
2020-10-03 19:46:05 +02:00
public static void removeNPC ( final String name ) throws UserDoesNotExistException {
final User user = getUserByName ( name ) ;
2015-04-15 06:06:16 +02:00
if ( user = = null ) {
throw new UserDoesNotExistException ( name ) ;
}
deleteNPC ( name ) ;
}
2011-05-02 05:56:51 +02:00
}