2011-05-02 20:22:17 +02:00
|
|
|
package com.earth2me.essentials.api;
|
2011-05-02 05:56:51 +02:00
|
|
|
|
2011-05-02 20:22:17 +02:00
|
|
|
import com.earth2me.essentials.Essentials;
|
|
|
|
import com.earth2me.essentials.User;
|
|
|
|
import com.earth2me.essentials.Util;
|
2011-05-02 05:56:51 +02:00
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
|
|
|
2011-05-02 20:22:17 +02:00
|
|
|
public class Economy
|
2011-05-02 11:55:22 +02:00
|
|
|
{
|
2011-05-02 20:22:17 +02:00
|
|
|
protected static Essentials ess = Essentials.getStatic();
|
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
//Does the file exists?
|
2011-05-02 11:55:22 +02:00
|
|
|
protected static boolean accountCreated(String name)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
File folder = new File(ess.getDataFolder(), "userdata");
|
2011-05-06 03:36:26 +02:00
|
|
|
File account = new File(folder, name.toLowerCase() + ".yml");
|
2011-05-02 05:56:51 +02:00
|
|
|
return account.exists();
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
//We create the file for the NPC
|
2011-05-02 11:55:22 +02:00
|
|
|
protected static void createAccount(String name)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
|
|
|
|
//Where we will store npc accounts!
|
|
|
|
|
|
|
|
File folder = new File(ess.getDataFolder(), "userdata");
|
|
|
|
File npcFile = new File(folder, name + ".yml");
|
2011-05-02 20:22:17 +02:00
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
try
|
|
|
|
{
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!npcFile.createNewFile())
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
System.out.println("Failed file creation");
|
2011-05-02 11:55:22 +02:00
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
catch (IOException e)
|
2011-05-02 05:56:51 +02:00
|
|
|
{
|
|
|
|
System.out.println("Could not create Non-player account file!");
|
|
|
|
}
|
|
|
|
FileWriter fileWriter = null;
|
|
|
|
BufferedWriter bufferWriter = null;
|
|
|
|
try
|
|
|
|
{
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!npcFile.exists())
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
npcFile.createNewFile();
|
2011-05-02 11:55:22 +02:00
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
|
|
|
|
fileWriter = new FileWriter(npcFile);
|
|
|
|
bufferWriter = new BufferedWriter(fileWriter);
|
|
|
|
|
|
|
|
//This is the default for NPC's, 0
|
|
|
|
|
|
|
|
bufferWriter.append("money: ");
|
2011-05-02 11:55:22 +02:00
|
|
|
bufferWriter.append(((Integer)0).toString());
|
2011-05-02 05:56:51 +02:00
|
|
|
bufferWriter.newLine();
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
System.out.println("Exception on config creation: ");
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
try
|
2011-05-02 05:56:51 +02:00
|
|
|
{
|
2011-05-02 11:55:22 +02:00
|
|
|
if (bufferWriter != null)
|
2011-05-02 05:56:51 +02:00
|
|
|
{
|
2011-05-02 11:55:22 +02:00
|
|
|
bufferWriter.flush();
|
|
|
|
bufferWriter.close();
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
|
|
|
|
if (fileWriter != null)
|
2011-05-02 05:56:51 +02:00
|
|
|
{
|
2011-05-02 11:55:22 +02:00
|
|
|
fileWriter.close();
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
System.out.println("IO Exception writing file: " + npcFile.getName());
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
//Convert a string into an essentials User
|
2011-05-02 11:55:22 +02:00
|
|
|
protected static User usrConv(String name)
|
|
|
|
{
|
|
|
|
User user = null;
|
|
|
|
if (Bukkit.getServer().getPlayer(name) != null)
|
|
|
|
{
|
|
|
|
user = ess.getUser(Bukkit.getServer().getPlayer(name));
|
2011-05-02 05:56:51 +02:00
|
|
|
return user;
|
2011-05-02 11:55:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user = ess.getOfflineUser(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We have to make sure the user exists, or they are an NPC!
|
2011-05-02 11:55:22 +02:00
|
|
|
public static boolean exist(String name)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
if (name == null)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
System.out.println("EcoAPI - Whatever plugin is calling for users that are null is BROKEN!");
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
if (Bukkit.getServer().getPlayer(name) != null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
//Eco return balance
|
2011-05-02 11:55:22 +02:00
|
|
|
public static double getMoney(String name)
|
|
|
|
{
|
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
2011-05-02 20:22:17 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
return user.getMoney();
|
2011-05-02 11:55:22 +02:00
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
return user.getMoney();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Eco Set Money
|
2011-05-02 11:55:22 +02:00
|
|
|
public static void setMoney(String name, double bal)
|
|
|
|
{
|
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
|
|
|
User user = usrConv(name);
|
|
|
|
user.setMoney(bal);
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(bal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Eco add balance
|
2011-05-02 11:55:22 +02:00
|
|
|
public static void add(String name, double money)
|
|
|
|
{
|
2011-05-08 21:09:04 +02:00
|
|
|
double result;
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
2011-05-08 21:40:24 +02:00
|
|
|
{
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) + money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(money);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) + money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 11:55:22 +02:00
|
|
|
user.setMoney(result);
|
|
|
|
return;
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco divide balance
|
|
|
|
public static void divide(String name, double money)
|
|
|
|
{
|
2011-05-08 21:09:04 +02:00
|
|
|
double result;
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name)/ money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
2011-05-02 11:55:22 +02:00
|
|
|
return;
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) / money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-02 11:55:22 +02:00
|
|
|
|
2011-05-02 05:56:51 +02:00
|
|
|
//Eco multiply balance
|
2011-05-02 11:55:22 +02:00
|
|
|
public static void multiply(String name, double money)
|
|
|
|
{
|
2011-05-08 21:09:04 +02:00
|
|
|
double result;
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) * money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
2011-05-02 11:55:22 +02:00
|
|
|
return;
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) * money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Eco subtract balance
|
2011-05-02 11:55:22 +02:00
|
|
|
public static void subtract(String name, double money)
|
|
|
|
{
|
2011-05-08 21:09:04 +02:00
|
|
|
double result;
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) - money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
2011-05-02 11:55:22 +02:00
|
|
|
return;
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-05-08 21:25:44 +02:00
|
|
|
result = getMoney(name) - money;
|
2011-05-08 21:40:24 +02:00
|
|
|
User user = usrConv(name);
|
2011-05-02 05:56:51 +02:00
|
|
|
user.setMoney(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco reset balance!
|
|
|
|
public static void resetBalance(String name)
|
|
|
|
{
|
|
|
|
setMoney(name, 0);
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco has enough check
|
|
|
|
public static boolean hasEnough(String name, double amount)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
return amount <= getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco hasMore balance check
|
|
|
|
public static boolean hasMore(String name, double amount)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
return amount < getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco hasLess balance check
|
|
|
|
public static boolean hasLess(String name, double amount)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
return amount > getMoney(name);
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco currency
|
|
|
|
public static String getCurrency()
|
|
|
|
{
|
|
|
|
return ess.getSettings().getCurrency();
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco currency Plural
|
|
|
|
public static String getCurrencyPlural()
|
|
|
|
{
|
|
|
|
return ess.getSettings().getCurrencyPlural();
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco is negative check!
|
|
|
|
public static boolean isNegative(String name)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
return getMoney(name) < 0.0;
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco Formatter
|
|
|
|
public static String format(double amount)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
DecimalFormat ecoForm = new DecimalFormat("#,##0.##");
|
2011-05-02 11:55:22 +02:00
|
|
|
String formed = ecoForm.format(amount);
|
|
|
|
if (formed.endsWith("."))
|
|
|
|
{
|
|
|
|
formed = formed.substring(0, formed.length() - 1);
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
2011-05-06 03:36:26 +02:00
|
|
|
return formed + " " + ((amount <= 1 && amount >= -1) ? getCurrency() : getCurrencyPlural());
|
2011-05-02 05:56:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//************************!WARNING!**************************
|
|
|
|
//**********DO NOT USING THE FOLLOWING FOR PLAYERS!**********
|
|
|
|
//**************THESE ARE FOR NPC ACCOUNTS ONLY!*************
|
2011-05-02 05:56:51 +02:00
|
|
|
//Eco account exist for NPCs ONLY!
|
2011-05-02 11:55:22 +02:00
|
|
|
public static boolean accountExist(String account)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
return accountCreated(account);
|
2011-05-02 11:55:22 +02:00
|
|
|
}
|
2011-05-02 05:56:51 +02:00
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
//Eco NPC account creator! Will return false if it already exists.
|
|
|
|
public static boolean newAccount(String account)
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
|
2011-05-02 11:55:22 +02:00
|
|
|
if (!exist(account))
|
|
|
|
{
|
|
|
|
if (!accountCreated(account))
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
createAccount(account);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Eco remove account, only use this for NPCS!
|
2011-05-02 11:55:22 +02:00
|
|
|
public static void removeAccount(String name)
|
|
|
|
{
|
|
|
|
if (!exist(name))
|
|
|
|
{
|
|
|
|
if (accountCreated(name))
|
|
|
|
{
|
2011-05-02 05:56:51 +02:00
|
|
|
File folder = new File(ess.getDataFolder(), "userdata");
|
|
|
|
File account = new File(folder, Util.sanitizeFileName(name) + ".yml");
|
|
|
|
account.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|