Total Pageviews

Thursday, March 8, 2012

iConomy 5 & 6 Tutorial

I've been using iConomy for Bukkit, and have noticed a severe lack of any kind of documentation or relevant guides on how to use it.
I decided to make this beginner's guide on how to use iConomy 5 & 6 and navigate the "Java Stupidity" related to it.
This does not provide a complete guide to iConomy, but does give you more than enough information to get started.
- How to get started:
First thing is, you need to add iConomy .jar as an external jar, the same way you added the Bukkit library . There are no Java Docs .
- iConomy Math & Boolean:
iConomy uses Double numbers, which can make coding a pain in the butt at first. Here is now you manipulate Doubles.
You can add, subtract, multiply and divide Doubles in Java like any other number, though the resulting numbers are not perfect. ( 6 * 1.2 = 7.19999 )
The first problem I will address is boolean when dealing with Doubles. Unfortuinnatly you can't just use ...
// will not work
if ( Doubble1 > Double2 || Doubble1 == Doubble2) {}

In order to use boolean functions on Doubles, you must " compare them ". In Java the way to do this is ...
if (Doubble1.compareTo(Double2) > 0) {}
// if (Double1 - Double2) > 0
if (Double1.compareTo(Double2) == 0) {}
// if (Double1 - Double2) == 0


The next useful thing to know s how to use truncation on Doubles.
You can't just snip off decimal places in Java, that would be way too easy. Instead you need to format it. Fun!
DecimalFormat truncation = new DecimalFormat("0.00");
Double1 = Double.valueOf(truncation.format(Double1)).doubleValue();


Next useful thing to know in Java is how to convert strings into Doubles. Never - EVER - directly convert raw user input into a Double with out error handling ...
// converting strings
Double1 = Double.valueOf(String_Here).doubleValue();
// for user input
try{
Double1 = Double.valueOf(Raw_User_Input_Here).doubleValue();
}
catch (Exception e){
Player_Object_Here.sendMessage(ChatColor.RED + "Error !!!");
}
}

Since we now know the basics of Java Double manipulation, lets move on to iConomy commands.

-iConomy 5:


Even though with a good Java IDE you do not need to worry much about imports, here are the basic imports you will need for iConomy 5 ...
import com.iConomy.system.Account;
import com.iConomy.system.Accounts;


With those we can grab a user's account and balance.
// grab the account object
Account account = new Accounts().get(String_Player_Name_Here);
// grab their account balance
Double money = account.getHoldings().balance();


Now we have all the tools to start playing with money ! Even though there are a lot more commands than this, the ones you will use most often are ....
// add money
account.getHoldings().add(Double1);
// subtract money
account.getHoldings().subtract(Double1);
// set account amount
account.getHoldings().set(Double1);


- iConomy 6

Even though with a good Java IDE you do not need to worry much about imports, here are the basic imports you will need for iConomy 6 ...
import com.iCo6.system.Account;
import com.iCo6.system.Accounts;


With those we can grab a user's account and balance.
// grab account object
Account account = new Accounts().get(String_Player_Name_Here);
// grab their account balance
Double money = account.getHoldings().getBalance();


Now we have all the tools to start playing with money ! Even though there are a lot more commands than this, the ones you will use most often are ....
// add money
account.getHoldings().add(Double1);
// subtract money
account.getHoldings().subtract(Doubble1);
// set account amount
account.getHoldings().setBalance(Double1);



- In conclusion:
I hope you find this tutorial helpful in your endeavors to create economic plugins. Please leave a comment below.
Happy coding!
- RLS0812 -

No comments:

Post a Comment