Fixed API ALOT, added bypass on iconomy calls for ico-bridge.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1315 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
xeology 2011-05-02 09:55:22 +00:00
parent affb889e42
commit e327aee283
2 changed files with 195 additions and 158 deletions

View File

@ -8,35 +8,37 @@ import java.text.DecimalFormat;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class EcoAPI {
protected static Essentials ess; public class EcoAPI
protected static Settings set; {
protected static Essentials ess=Essentials.getStatic();
//Does the file exists? //Does the file exists?
protected static boolean accountCreated(String name)
protected static boolean accountCreated(String name){ {
File folder = new File(ess.getDataFolder(), "userdata"); File folder = new File(ess.getDataFolder(), "userdata");
File account = new File(folder, Util.sanitizeFileName(name) + ".yml"); File account = new File(folder, Util.sanitizeFileName(name) + ".yml");
return account.exists(); return account.exists();
} }
//We create the file for the NPC //We create the file for the NPC
protected static void createAccount(String name)
protected static void createAccount(String name){ {
//Where we will store npc accounts! //Where we will store npc accounts!
File folder = new File(ess.getDataFolder(), "userdata"); File folder = new File(ess.getDataFolder(), "userdata");
File npcFile = new File(folder, name + ".yml"); File npcFile = new File(folder, name + ".yml");
try try
{ {
if(!npcFile.createNewFile()) if (!npcFile.createNewFile())
{
System.out.println("Failed file creation"); System.out.println("Failed file creation");
}
return; return;
} }
catch(IOException e) catch (IOException e)
{ {
System.out.println("Could not create Non-player account file!"); System.out.println("Could not create Non-player account file!");
} }
@ -44,8 +46,10 @@ public class EcoAPI {
BufferedWriter bufferWriter = null; BufferedWriter bufferWriter = null;
try try
{ {
if(!npcFile.exists()) if (!npcFile.exists())
{
npcFile.createNewFile(); npcFile.createNewFile();
}
fileWriter = new FileWriter(npcFile); fileWriter = new FileWriter(npcFile);
bufferWriter = new BufferedWriter(fileWriter); bufferWriter = new BufferedWriter(fileWriter);
@ -53,10 +57,10 @@ public class EcoAPI {
//This is the default for NPC's, 0 //This is the default for NPC's, 0
bufferWriter.append("money: "); bufferWriter.append("money: ");
bufferWriter.append(((Integer) 0).toString()); bufferWriter.append(((Integer)0).toString());
bufferWriter.newLine(); bufferWriter.newLine();
} }
catch(IOException e) catch (IOException e)
{ {
System.out.println("Exception on config creation: "); System.out.println("Exception on config creation: ");
} }
@ -64,16 +68,18 @@ public class EcoAPI {
{ {
try try
{ {
if(bufferWriter != null) if (bufferWriter != null)
{ {
bufferWriter.flush(); bufferWriter.flush();
bufferWriter.close(); bufferWriter.close();
} }
if(fileWriter != null) if (fileWriter != null)
{
fileWriter.close(); fileWriter.close();
} }
catch(IOException e) }
catch (IOException e)
{ {
System.out.println("IO Exception writing file: " + npcFile.getName()); System.out.println("IO Exception writing file: " + npcFile.getName());
} }
@ -81,207 +87,221 @@ public class EcoAPI {
} }
//Convert a string into an essentials User //Convert a string into an essentials User
protected static User usrConv(String name)
protected static User usrConv(String name){ {
User user=null; User user = null;
if (Bukkit.getServer().getPlayer(name)!=null){ if (Bukkit.getServer().getPlayer(name) != null)
user=ess.getUser(Bukkit.getServer().getPlayer(name)); {
user = ess.getUser(Bukkit.getServer().getPlayer(name));
return user; return user;
} }
else{ else
user=ess.getOfflineUser(name); {
user = ess.getOfflineUser(name);
} }
return user; return user;
} }
//We have to make sure the user exists, or they are an NPC! //We have to make sure the user exists, or they are an NPC!
public static boolean exist(String name)
{
public static boolean exist(String name){ if (name == null)
{
if (name==null){
System.out.println("EcoAPI - Whatever plugin is calling for users that are null is BROKEN!"); System.out.println("EcoAPI - Whatever plugin is calling for users that are null is BROKEN!");
return false; return false;
} }
if (Bukkit.getServer().getPlayer(name)!=null){ if (Bukkit.getServer().getPlayer(name) != null)
{
return true; return true;
} }
return false; return false;
} }
//Eco return balance //Eco return balance
public static double getMoney(String name)
public static double getMoney(String name){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
{
User user = usrConv(name);
return user.getMoney(); return user.getMoney();
} }
return 0; return 0;
} }
User user=usrConv(name); User user = usrConv(name);
return user.getMoney(); return user.getMoney();
} }
//Eco Set Money //Eco Set Money
public static void setMoney(String name, double bal)
public static void setMoney(String name, double bal){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
{
User user = usrConv(name);
user.setMoney(bal); user.setMoney(bal);
} }
return; return;
} }
User user=usrConv(name); User user = usrConv(name);
user.setMoney(bal); user.setMoney(bal);
return; return;
} }
//Eco add balance //Eco add balance
public static void add(String name, double money)
public static void add(String name, double money){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
double result=user.getMoney()+money; {
User user = usrConv(name);
double result = user.getMoney() + money;
user.setMoney(money); user.setMoney(money);
} }
return; return;
} }
User user=usrConv(name); double result = user.getMoney() + money;
double result=user.getMoney()+money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
//Eco divide balance //Eco divide balance
public static void divide(String name, double money)
public static void divide(String name, double money){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
double result=user.getMoney()/money; {
User user = usrConv(name);
double result = user.getMoney() / money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
return; return;
} }
User user=usrConv(name); User user = usrConv(name);
double result=user.getMoney()/money; double result = user.getMoney() / money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
//Eco multiply balance //Eco multiply balance
public static void multiply(String name, double money)
public static void multiply(String name, double money){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
double result=user.getMoney()*money; {
User user = usrConv(name);
double result = user.getMoney() * money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
return; return;
} }
User user=usrConv(name); User user = usrConv(name);
double result=user.getMoney()*money; double result = user.getMoney() * money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
//Eco subtract balance //Eco subtract balance
public static void subtract(String name, double money)
public static void subtract(String name, double money){ {
if (!exist(name)){ if (!exist(name))
if (accountCreated(name)){ {
User user=usrConv(name); if (accountCreated(name))
double result=user.getMoney()-money; {
User user = usrConv(name);
double result = user.getMoney() - money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
return; return;
} }
User user=usrConv(name); User user = usrConv(name);
double result=user.getMoney()-money; double result = user.getMoney() - money;
user.setMoney(result); user.setMoney(result);
return; return;
} }
//Eco reset balance! //Eco reset balance!
public static void resetBalance(String name)
public static void resetBalance(String name){ {
setMoney(name, set.getStartingBalance()); setMoney(name, 0);
} }
//Eco has enough check //Eco has enough check
public static boolean hasEnough(String name, double amount)
public static boolean hasEnough(String name, double amount){ {
return amount <= getMoney(name); return amount <= getMoney(name);
} }
//Eco hasMore balance check //Eco hasMore balance check
public static boolean hasMore(String name, double amount)
public static boolean hasMore(String name, double amount){ {
return amount < getMoney(name); return amount < getMoney(name);
} }
//Eco hasLess balance check //Eco hasLess balance check
public static boolean hasLess(String name, double amount)
public static boolean hasLess(String name, double amount){ {
return amount > getMoney(name); return amount > getMoney(name);
} }
//Eco currency //Eco currency
public static String getCurrency()
public static String getCurrency(){ {
return set.getCurrency(); return ess.getSettings().getCurrency();
} }
//Eco currency Plural //Eco currency Plural
public static String getCurrencyPlural()
public static String getCurrencyPlural(){ {
return set.getCurrencyPlural(); return ess.getSettings().getCurrencyPlural();
} }
//Eco is negative check! //Eco is negative check!
public static boolean isNegative(String name)
public static boolean isNegative(String name){ {
return getMoney(name) < 0.0; return getMoney(name) < 0.0;
} }
//Eco Formatter //Eco Formatter
public static String format(double amount)
public static String format(double amount) { {
DecimalFormat ecoForm = new DecimalFormat("#,##0.##"); DecimalFormat ecoForm = new DecimalFormat("#,##0.##");
String fakeFormed = ecoForm.format(amount); String formed = ecoForm.format(amount);
if (fakeFormed.endsWith(".")) { if (formed.endsWith("."))
fakeFormed = fakeFormed.substring(0, fakeFormed.length() - 1); {
formed = formed.substring(0, formed.length() - 1);
} }
getCurrency();
return fakeFormed + " " + ((amount <= 1 && amount >= -1) ? set.getCurrency() : set.getCurrencyPlural()); getCurrencyPlural();
return formed + " " + ((amount <= 1 && amount >= -1) ? ess.getSettings().getCurrency() : ess.getSettings().getCurrency());
} }
//************************!WARNING!************************** //************************!WARNING!**************************
//**********DO NOT USING THE FOLLOWING FOR PLAYERS!********** //**********DO NOT USING THE FOLLOWING FOR PLAYERS!**********
//**************THESE ARE FOR NPC ACCOUNTS ONLY!************* //**************THESE ARE FOR NPC ACCOUNTS ONLY!*************
//Eco account exist for NPCs ONLY! //Eco account exist for NPCs ONLY!
public static boolean accountExist(String account)
public static boolean accountExist(String account) { {
return accountCreated(account); return accountCreated(account);
} }
//Eco NPC account creator! Will return false if it already exists. //Eco NPC account creator! Will return false if it already exists.
public static boolean newAccount(String account)
{
public static boolean newAccount(String account){ if (!exist(account))
{
if (!exist(account)){ if (!accountCreated(account))
if (!accountCreated(account)){ {
createAccount(account); createAccount(account);
return true; return true;
} }
@ -291,11 +311,13 @@ public class EcoAPI {
} }
//Eco remove account, only use this for NPCS! //Eco remove account, only use this for NPCS!
public static void removeAccount(String name)
{
public static void removeAccount(String name){ if (!exist(name))
{
if (!exist(name)){ if (accountCreated(name))
if (accountCreated(name)){ {
File folder = new File(ess.getDataFolder(), "userdata"); File folder = new File(ess.getDataFolder(), "userdata");
File account = new File(folder, Util.sanitizeFileName(name) + ".yml"); File account = new File(folder, Util.sanitizeFileName(name) + ".yml");
account.delete(); account.delete();

View File

@ -256,4 +256,19 @@ public class Util
} }
return isBlockAboveAir(world, x, y, z); return isBlockAboveAir(world, x, y, z);
} }
public static boolean detectFay()
{
try
{
if (com.nijiko.coelho.iConomy.iConomy.isFay())
{
return true;
}
}
catch (Throwable ex)
{
return false;
}
return false;
}
} }