2011-05-02 18:22:17 +00:00
|
|
|
package com.earth2me.essentials.api;
|
2011-05-02 03:56:51 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
import com.earth2me.essentials.EssentialsConf;
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-07-15 23:33:22 +00:00
|
|
|
import com.earth2me.essentials.IEssentials;
|
2011-05-02 18:22:17 +00:00
|
|
|
import com.earth2me.essentials.User;
|
|
|
|
import com.earth2me.essentials.Util;
|
2011-05-02 03:56:51 +00:00
|
|
|
import java.io.File;
|
2011-05-13 16:57:45 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-05-02 03:56:51 +00:00
|
|
|
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-22 20:30:19 +00:00
|
|
|
/**
|
|
|
|
* Instead of using this api directly, we recommend to use the register plugin:
|
|
|
|
* http://bit.ly/RegisterMethod
|
|
|
|
*/
|
2011-06-01 10:40:12 +00:00
|
|
|
public final class Economy
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
private Economy()
|
|
|
|
{
|
|
|
|
}
|
2011-05-13 16:57:45 +00:00
|
|
|
private static final Logger logger = Logger.getLogger("Minecraft");
|
2011-07-15 23:33:22 +00:00
|
|
|
private static IEssentials ess;
|
2011-10-09 14:53:01 +00:00
|
|
|
private static final String noCallBeforeLoad = "Essentials API is called before Essentials is loaded.";
|
2011-07-15 23:33:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aEss the ess to set
|
|
|
|
*/
|
|
|
|
public static void setEss(IEssentials aEss)
|
|
|
|
{
|
|
|
|
ess = aEss;
|
|
|
|
}
|
2011-05-02 18:22:17 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
private static void createNPCFile(String name)
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-07-15 23:33:22 +00:00
|
|
|
File folder = new File(ess.getDataFolder(), "userdata");
|
2011-05-13 19:18:22 +00:00
|
|
|
if (!folder.exists())
|
|
|
|
{
|
|
|
|
folder.mkdirs();
|
|
|
|
}
|
2011-05-13 16:57:45 +00:00
|
|
|
EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml"));
|
|
|
|
npcConfig.load();
|
|
|
|
npcConfig.setProperty("npc", true);
|
2011-07-15 23:33:22 +00:00
|
|
|
npcConfig.setProperty("money", ess.getSettings().getStartingBalance());
|
2011-05-13 16:57:45 +00:00
|
|
|
npcConfig.save();
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
private static void deleteNPC(String name)
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-07-15 23:33:22 +00:00
|
|
|
File folder = new File(ess.getDataFolder(), "userdata");
|
2011-05-13 19:18:22 +00:00
|
|
|
if (!folder.exists())
|
|
|
|
{
|
|
|
|
folder.mkdirs();
|
|
|
|
}
|
2011-05-13 16:57:45 +00:00
|
|
|
File config = new File(folder, Util.sanitizeFileName(name) + ".yml");
|
|
|
|
EssentialsConf npcConfig = new EssentialsConf(config);
|
|
|
|
npcConfig.load();
|
2011-07-15 23:33:22 +00:00
|
|
|
if (npcConfig.hasProperty("npc") && npcConfig.getBoolean("npc", false))
|
|
|
|
{
|
|
|
|
if (!config.delete())
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
logger.log(Level.WARNING, _("deleteFileError", config));
|
2011-05-02 09:55:22 +00:00
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
ess.getUserMap().removeUser(name);
|
2011-05-02 09:55:22 +00:00
|
|
|
}
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
private static User getUserByName(String name)
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-10-09 14:53:01 +00:00
|
|
|
if (ess == null)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(noCallBeforeLoad);
|
|
|
|
}
|
2012-10-26 20:34:36 +00:00
|
|
|
return ess.getUser(name);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the balance of a user
|
|
|
|
* @param name Name of the user
|
|
|
|
* @return balance
|
|
|
|
* @throws UserDoesNotExistException
|
|
|
|
*/
|
|
|
|
public static double getMoney(String name) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
User user = getUserByName(name);
|
2011-07-15 23:33:22 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
throw new UserDoesNotExistException(name);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-06-25 13:34:33 +00:00
|
|
|
return user.getMoney();
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-05-02 09:55:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
User user = getUserByName(name);
|
2011-07-15 23:33:22 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
throw new UserDoesNotExistException(name);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2012-02-26 04:15:14 +00:00
|
|
|
if (balance < ess.getSettings().getMinMoney())
|
|
|
|
{
|
|
|
|
throw new NoLoanPermittedException();
|
|
|
|
}
|
2011-05-13 16:57:45 +00:00
|
|
|
if (balance < 0.0 && !user.isAuthorized("essentials.eco.loan"))
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
throw new NoLoanPermittedException();
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-06-25 13:34:33 +00:00
|
|
|
user.setMoney(balance);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Adds money to the balance of a user
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
double result = getMoney(name) + amount;
|
|
|
|
setMoney(name, result);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Substracts money from the balance of a user
|
|
|
|
* @param name Name of the user
|
|
|
|
* @param amount The money you want to substract
|
|
|
|
* @throws UserDoesNotExistException If a user by that name does not exists
|
|
|
|
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
|
|
|
*/
|
|
|
|
public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
double result = getMoney(name) - amount;
|
|
|
|
setMoney(name, result);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-05-02 09:55:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Divides the balance of a user by a value
|
|
|
|
* @param name Name of the user
|
|
|
|
* @param value The balance is divided by this value
|
|
|
|
* @throws UserDoesNotExistException If a user by that name does not exists
|
|
|
|
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
|
|
|
*/
|
|
|
|
public static void divide(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
double result = getMoney(name) / value;
|
|
|
|
setMoney(name, result);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Multiplies the balance of a user by a value
|
|
|
|
* @param name Name of the user
|
|
|
|
* @param value The balance is multiplied by this value
|
|
|
|
* @throws UserDoesNotExistException If a user by that name does not exists
|
|
|
|
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
|
|
|
|
*/
|
|
|
|
public static void multiply(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
double result = getMoney(name) * value;
|
|
|
|
setMoney(name, result);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
|
|
|
public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-10-09 14:53:01 +00:00
|
|
|
if (ess == null)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(noCallBeforeLoad);
|
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
setMoney(name, ess.getSettings().getStartingBalance());
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
|
|
|
public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-02 03:56:51 +00:00
|
|
|
return amount <= getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
|
|
|
public static boolean hasMore(String name, double amount) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-02 03:56:51 +00:00
|
|
|
return amount < getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
|
|
|
public static boolean hasLess(String name, double amount) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-02 03:56:51 +00:00
|
|
|
return amount > getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
|
|
|
public static boolean isNegative(String name) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-02 03:56:51 +00:00
|
|
|
return getMoney(name) < 0.0;
|
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00: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
|
|
|
|
*/
|
2011-05-02 09:55:22 +00:00
|
|
|
public static String format(double amount)
|
|
|
|
{
|
2011-10-09 14:53:01 +00:00
|
|
|
if (ess == null)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(noCallBeforeLoad);
|
|
|
|
}
|
2012-03-04 10:11:58 +00:00
|
|
|
return Util.displayCurrency(amount, ess);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Test if a player exists to avoid the UserDoesNotExistException
|
|
|
|
* @param name Name of the user
|
|
|
|
* @return true, if the user exists
|
|
|
|
*/
|
2011-07-15 23:33:22 +00:00
|
|
|
public static boolean playerExists(String name)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
return getUserByName(name) != null;
|
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Test if a player is a npc
|
|
|
|
* @param name Name of the player
|
|
|
|
* @return true, if it's a npc
|
|
|
|
* @throws UserDoesNotExistException
|
|
|
|
*/
|
|
|
|
public static boolean isNPC(String name) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
User user = getUserByName(name);
|
2011-07-15 23:33:22 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
throw new UserDoesNotExistException(name);
|
|
|
|
}
|
|
|
|
return user.isNPC();
|
2011-05-02 09:55:22 +00:00
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Creates dummy files for a npc, if there is no player yet with that name.
|
|
|
|
* @param name Name of the player
|
2011-05-13 19:18:22 +00:00
|
|
|
* @return true, if a new npc was created
|
2011-05-13 16:57:45 +00:00
|
|
|
*/
|
2011-05-13 19:18:22 +00:00
|
|
|
public static boolean createNPC(String name)
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
User user = getUserByName(name);
|
2011-07-15 23:33:22 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
createNPCFile(name);
|
2011-05-13 19:18:22 +00:00
|
|
|
return true;
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-05-13 19:18:22 +00:00
|
|
|
return false;
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 16:57:45 +00:00
|
|
|
/**
|
|
|
|
* Deletes a user, if it is marked as npc.
|
|
|
|
* @param name Name of the player
|
|
|
|
* @throws UserDoesNotExistException
|
|
|
|
*/
|
|
|
|
public static void removeNPC(String name) throws UserDoesNotExistException
|
2011-05-02 09:55:22 +00:00
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
User user = getUserByName(name);
|
2011-07-15 23:33:22 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-05-13 16:57:45 +00:00
|
|
|
throw new UserDoesNotExistException(name);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
2011-05-13 16:57:45 +00:00
|
|
|
deleteNPC(name);
|
2011-05-02 03:56:51 +00:00
|
|
|
}
|
|
|
|
}
|