mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-06 11:24:39 +08:00
Integrated EcoAPI at EcoAPI.java
Fixed worth's ugly decimal display, does math with decimals now also! git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1311 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
parent
87c937ff3c
commit
894a270a0e
306
Essentials/src/com/earth2me/essentials/EcoAPI.java
Normal file
306
Essentials/src/com/earth2me/essentials/EcoAPI.java
Normal file
@ -0,0 +1,306 @@
|
||||
package com.earth2me.essentials;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
|
||||
public class EcoAPI {
|
||||
|
||||
protected static Essentials ess;
|
||||
protected static Settings set;
|
||||
|
||||
//Does the file exists?
|
||||
|
||||
protected static boolean accountCreated(String name){
|
||||
|
||||
File folder = new File(ess.getDataFolder(), "userdata");
|
||||
File account = new File(folder, Util.sanitizeFileName(name) + ".yml");
|
||||
return account.exists();
|
||||
}
|
||||
|
||||
//We create the file for the NPC
|
||||
|
||||
protected static void createAccount(String name){
|
||||
|
||||
//Where we will store npc accounts!
|
||||
|
||||
File folder = new File(ess.getDataFolder(), "userdata");
|
||||
File npcFile = new File(folder, name + ".yml");
|
||||
try
|
||||
{
|
||||
if(!npcFile.createNewFile())
|
||||
System.out.println("Failed file creation");
|
||||
return;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("Could not create Non-player account file!");
|
||||
}
|
||||
FileWriter fileWriter = null;
|
||||
BufferedWriter bufferWriter = null;
|
||||
try
|
||||
{
|
||||
if(!npcFile.exists())
|
||||
npcFile.createNewFile();
|
||||
|
||||
fileWriter = new FileWriter(npcFile);
|
||||
bufferWriter = new BufferedWriter(fileWriter);
|
||||
|
||||
//This is the default for NPC's, 0
|
||||
|
||||
bufferWriter.append("money: ");
|
||||
bufferWriter.append(((Integer) 0).toString());
|
||||
bufferWriter.newLine();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("Exception on config creation: ");
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if(bufferWriter != null)
|
||||
{
|
||||
bufferWriter.flush();
|
||||
bufferWriter.close();
|
||||
}
|
||||
|
||||
if(fileWriter != null)
|
||||
fileWriter.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("IO Exception writing file: " + npcFile.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Convert a string into an essentials User
|
||||
|
||||
protected static User usrConv(String name){
|
||||
User user=null;
|
||||
if (Bukkit.getServer().getPlayer(name)!=null){
|
||||
user=ess.getUser(Bukkit.getServer().getPlayer(name));
|
||||
return user;
|
||||
}
|
||||
else{
|
||||
user=ess.getOfflineUser(name);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
//We have to make sure the user exists, or they are an NPC!
|
||||
|
||||
public static boolean exist(String name){
|
||||
|
||||
if (name==null){
|
||||
System.out.println("EcoAPI - Whatever plugin is calling for users that are null is BROKEN!");
|
||||
return false;
|
||||
}
|
||||
if (Bukkit.getServer().getPlayer(name)!=null){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Eco return balance
|
||||
|
||||
public static double getMoney(String name){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
return user.getMoney();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
return user.getMoney();
|
||||
}
|
||||
|
||||
//Eco Set Money
|
||||
|
||||
public static void setMoney(String name, double bal){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
user.setMoney(bal);
|
||||
}
|
||||
return;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
user.setMoney(bal);
|
||||
return;
|
||||
}
|
||||
|
||||
//Eco add balance
|
||||
|
||||
public static void add(String name, double money){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()+money;
|
||||
user.setMoney(money);
|
||||
}
|
||||
return;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()+money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
|
||||
//Eco divide balance
|
||||
|
||||
public static void divide(String name, double money){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()/money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()/money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
|
||||
//Eco multiply balance
|
||||
|
||||
public static void multiply(String name, double money){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()*money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()*money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
|
||||
//Eco subtract balance
|
||||
|
||||
public static void subtract(String name, double money){
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()-money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
User user=usrConv(name);
|
||||
double result=user.getMoney()-money;
|
||||
user.setMoney(result);
|
||||
return;
|
||||
}
|
||||
|
||||
//Eco reset balance!
|
||||
|
||||
public static void resetBalance(String name){
|
||||
setMoney(name, set.getStartingBalance());
|
||||
}
|
||||
|
||||
//Eco has enough check
|
||||
|
||||
public static boolean hasEnough(String name, double amount){
|
||||
return amount <= getMoney(name);
|
||||
}
|
||||
|
||||
//Eco hasMore balance check
|
||||
|
||||
public static boolean hasMore(String name, double amount){
|
||||
return amount < getMoney(name);
|
||||
}
|
||||
|
||||
//Eco hasLess balance check
|
||||
|
||||
public static boolean hasLess(String name, double amount){
|
||||
return amount > getMoney(name);
|
||||
}
|
||||
|
||||
//Eco currency
|
||||
|
||||
public static String getCurrency(){
|
||||
return set.getCurrency();
|
||||
}
|
||||
|
||||
//Eco currency Plural
|
||||
|
||||
public static String getCurrencyPlural(){
|
||||
return set.getCurrencyPlural();
|
||||
}
|
||||
|
||||
//Eco is negative check!
|
||||
|
||||
public static boolean isNegative(String name){
|
||||
return getMoney(name) < 0.0;
|
||||
}
|
||||
|
||||
//Eco Formatter
|
||||
|
||||
public static String format(double amount) {
|
||||
DecimalFormat ecoForm = new DecimalFormat("#,##0.##");
|
||||
String fakeFormed = ecoForm.format(amount);
|
||||
if (fakeFormed.endsWith(".")) {
|
||||
fakeFormed = fakeFormed.substring(0, fakeFormed.length() - 1);
|
||||
}
|
||||
|
||||
return fakeFormed + " " + ((amount <= 1 && amount >= -1) ? set.getCurrency() : set.getCurrencyPlural());
|
||||
}
|
||||
|
||||
|
||||
|
||||
//************************!WARNING!**************************
|
||||
//**********DO NOT USING THE FOLLOWING FOR PLAYERS!**********
|
||||
//**************THESE ARE FOR NPC ACCOUNTS ONLY!*************
|
||||
|
||||
|
||||
|
||||
//Eco account exist for NPCs ONLY!
|
||||
|
||||
public static boolean accountExist(String account) {
|
||||
return accountCreated(account);
|
||||
}
|
||||
|
||||
//Eco NPC account creator! Will return false if it already exists.
|
||||
|
||||
public static boolean newAccount(String account){
|
||||
|
||||
if (!exist(account)){
|
||||
if (!accountCreated(account)){
|
||||
createAccount(account);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Eco remove account, only use this for NPCS!
|
||||
|
||||
public static void removeAccount(String name){
|
||||
|
||||
if (!exist(name)){
|
||||
if (accountCreated(name)){
|
||||
File folder = new File(ess.getDataFolder(), "userdata");
|
||||
File account = new File(folder, Util.sanitizeFileName(name) + ".yml");
|
||||
account.delete();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
@ -47,7 +47,17 @@ public class Settings implements IConf
|
||||
{
|
||||
return config.getInt("default-stack-size", 64);
|
||||
}
|
||||
|
||||
|
||||
public String getCurrency()
|
||||
{
|
||||
return config.getString("currencyName", "Coin");
|
||||
}
|
||||
|
||||
public String getCurrencyPlural()
|
||||
{
|
||||
return config.getString("currencyNamePlural", "Coins");
|
||||
}
|
||||
|
||||
public int getStartingBalance()
|
||||
{
|
||||
return config.getInt("starting-balance", 0);
|
||||
|
@ -4,11 +4,13 @@ import org.bukkit.Server;
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import com.earth2me.essentials.ItemDb;
|
||||
import com.earth2me.essentials.User;
|
||||
import java.text.DecimalFormat;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
public class Commandworth extends EssentialsCommand
|
||||
{
|
||||
private static DecimalFormat df = new DecimalFormat("0.##");
|
||||
public Commandworth()
|
||||
{
|
||||
super("worth");
|
||||
@ -45,6 +47,8 @@ public class Commandworth extends EssentialsCommand
|
||||
}
|
||||
|
||||
user.charge(this);
|
||||
user.sendMessage("§7Stack of " + is.getType().toString().toLowerCase().replace("_", "") + " worth §c$" + (worth * amount) + "§7 (" + amount + " item(s) at $" + worth + " each)");
|
||||
String d = df.format(Double.parseDouble(Double.toString(worth)));
|
||||
String d2 = df.format(Double.parseDouble(Double.toString(Double.parseDouble(d)*amount)));
|
||||
user.sendMessage("§7Stack of " + is.getType().toString().toLowerCase().replace("_", "") + " worth §c$" + d2 + "§7 (" + amount + " item(s) at $" + d + " each)");
|
||||
}
|
||||
}
|
||||
|
@ -219,6 +219,10 @@ command-costs:
|
||||
#example: 1000
|
||||
# /kit tools costs $1500 PER USE
|
||||
# kit-tools: 1500
|
||||
|
||||
#This defines the name of your currencies, in singular and plural
|
||||
currnceyName: Coin
|
||||
currencyNamePlural: Coins
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user