This tutorial will show you how to add my GrandExchange to your server, and I'll explain how it works.
It's a solid 3 for difficulty, C&P.
Made on: Deltascape
Made for: Anything
Make a new file called GrandExchange.java. Put this in it:
Code:
import java.util.*;
/* Big Brother is watching you program.
* Hello Users! This is Linksbro Master GrandExchange v1
* I've encluded alot of comments for you to understand what the code does
* If you have any questions, PM me on Silabsoft forums(Username: Linksbro)
* Or, you can MSN me at [Only registered and activated users can see links. ]
* What to expect in v2!
* Requesting to buy an item
* Better ::collect command
* ::lowestbuy buys the item that has the lowest price (2 logs on are ge, one is 10gp, one is 9gp, it buys the 9)
* Stackable selling! IE Selling more then 1 item at a time!
*/
public class GrandExchange
{
public static ArrayList<Integer> itemIdList = new ArrayList<Integer>(); //HI-HO ArrayLists
public static ArrayList<Integer> itemPriceList = new ArrayList<Integer>(); //They are all self-explanatory.
public static ArrayList<String> itemSeller = new ArrayList<String>();
public static ArrayList<String> itemSellerMoney = new ArrayList<String>();
public static ArrayList<Integer> itemSellerMoneyAmount = new ArrayList<Integer>();
public boolean sell(int itemID, int price, String name) //boolean true if adding is successful, false if it isn't, will be handled by client, Takes the ID, Price, and seller and adds them to the list
{
try{
itemIdList.add(itemID);
itemPriceList.add(price);
itemSeller.add(name);
}
catch (Exception e)
{
return false;
}
return true;
}
public static void fatalErrorException() //Something fucks up, this baby is called, clears all lists.
{
itemIdList.clear();
itemPriceList.clear();
itemSeller.clear();
System.out.println("Cleared ALL lists!");
}
public ArrayList<String> buy(int itemID, int itemPrice, String Seller) //Returns a array list with itemId, price, and seller. Will be handled by client.java
{client c = (client) server.playerHandler.players[PlayerHandler.getPlayerID(Seller)];
String itemBuying;
String priceBuying;
ArrayList<String> ans = new ArrayList<String>();
for (int i = 0; i<itemIdList.size(); i++)
{
if(itemIdList.get(i) == itemID && itemPriceList.get(i) == itemPrice)
{
try{
if (c.hasEnoughMoney(itemPrice))
{
itemBuying = ""+itemIdList.get(i);
priceBuying = ""+itemPriceList.get(i);
ans.add(itemBuying);
ans.add(priceBuying);
ans.add(itemSeller.get(i));
itemSellerMoney.add(itemSeller.get(i));
itemSellerMoneyAmount.add(itemPrice);
itemIdList.remove(i);
itemPriceList.remove(i);
itemSeller.remove(i);
return ans;
}
else
{
c.sM("Not enough money!");
}
}
catch(Exception e)
{
return ans;
}
}
}
ans.add("null"); // if buy isn't successful client handles case where null is in ans
return ans;
}
public boolean takeDown(int itemID, String sellerName) //returns true if it was successfully taken down, false if it failed
{
for (int i = 0; i<itemIdList.size(); i++)
{
if ((itemIdList.get(i) == itemID) && (itemSeller.get(i) == sellerName))
{
try{
itemIdList.remove(i);
itemSeller.remove(i);
itemPriceList.remove(i);
return true;
}
catch(Exception e)
{
return false;
}
}
}
return false;
}
}
This holds all the ArrayLists, and methods for buying, selling and taking down stuff.
Next, open up your client.java, and search for "boolean process" and then add this inside the method:
Code:
if (checkingge && checkgewaittimer == 0)//refreshes ge
{
checkge();
checkgewaittimer = 10;
}
if (ge.itemSellerMoney.contains(playerName))
{
for (int i = 0; i < ge.itemSellerMoney.size(); i++)
{
if (ge.itemSellerMoney.get(i) == playerName)
{
if (gewaitTimer == 0)
{
sM("You have money to collect! Type ::collect!");
gewaitTimer = 30;
}
}
}
}
if (gewaitTimer > 0 )
gewaitTimer--;
if (checkgewaittimer >0)
checkgewaittimer--;
Declare this variables
Code:
public int gewaitTimer = 0, checkgewaittimer = 10;
Add this commands
Code:
//Start of GrandExchange commands
if (command.equalsIgnoreCase("checkge"))
{
checkge();
}
if (command.equalsIgnoreCase("gehelp"))
{
gehelp();
}
if (command.startsWith("buy"))
{
try
{
String[] args = command.split(" ");
int itemID = Integer.parseInt(args[1]);
int itemPrice = Integer.parseInt(args[2]);
ArrayList<String> ans = ge.buy(itemID, itemPrice, playerName);
if (ans.contains("null"))
{
sM("Buying Failed! Remeber to check the price!");
return;
}
String itemIds = ans.get(0);
int itemId = Integer.parseInt(itemIds);
String prices = ans.get(1);
int price = Integer.parseInt(prices);
String seller = ans.get(2);
deleteItem(995, getItemSlot(995), price);
addItem(itemId, 1);
sM("You bought one "+GetItemName(itemId)+" for "+price+"gp from "+seller);
}
catch (Exception e){
sM("Unhandled errors");
}
}
if (command.startsWith("takedown"))
{
String[] args = command.split(" ");
int itemID = Integer.parseInt(args[1]);
boolean ans = ge.takeDown(itemID, playerName);
if (ans)
{
sM("Takedown succesful!");
addItem(itemID, 1);
}
if (!ans)
sM("Takedown failed!");
}
if (command.startsWith("sell"))
{
String[] args = command.split(" ");
int itemID = Integer.parseInt(args[1]);
int price = Integer.parseInt(args[2]);
if (playerHasItem(itemID))
{
boolean ans = ge.sell(itemID,price,playerName);
if (ans)
{
sM("Item is on GE!");
deleteItem(itemID, getItemSlot(itemID), 1);
}
if (!ans)
sM("Failed!");
}
else
sM("You need the item to sell it!");
}
if (command.equalsIgnoreCase("collect"))
{
if (ge.itemSellerMoney.contains(playerName))
{
for (int i = 0; i < ge.itemSellerMoney.size(); i++)
{
if (ge.itemSellerMoney.get(i) == playerName)
{
addItem(995, ge.itemSellerMoneyAmount.get(i));
sM("You receive "+ge.itemSellerMoneyAmount.get(i)+"gp!");
ge.itemSellerMoneyAmount.remove(i);
ge.itemSellerMoney.remove(i);
}
}
}
}
if (command.equalsIgnoreCase("clearge") && playerName.equalsIgnoreCase("Linksbro"))
{
ge.fatalErrorException();
}
Declare this
Code:
GrandExchange ge = new GrandExchange();
Add this methods and variables:
Code:
public boolean hasEnoughMoney(int money)//used in grandexcahnge in the buy method
{
return (HasItemAmount(995, money));
}
public boolean checkingge = false;
public void checkge()//displays the items on sale on ge, is checked by process to update.
{
sendQuest("Grand Exchange Items for sale! Type ::gehelp for help!", 8144);
clearQuestInterface();
int k = 8147;
checkingge = true; // refreshes in process
for (int i = 0; i < GrandExchange.itemIdList.size(); i++)
{
sendQuest(""+GetItemName(GrandExchange.itemIdList.get(i))+" "+GrandExchange.itemPriceList.get(i)+"gp ID:"+GrandExchange.itemIdList.get(i)+" "+GrandExchange.itemSeller.get(i), k);
k++;
}
sendQuestSomething(8143);
showInterface(8134);
flushOutStream();
}
public void gehelp()
{
sendQuest("Grand Exchange help!", 8144);
clearQuestInterface();
sendQuest("Linksbro's Master Grand Exchange v1.0", 8147);
sendQuest("::sell ITEMID, PRICE Will put a item on the market", 8148);
sendQuest("::buy ITEMID PRICE will buy an item on the market", 8149);
sendQuest("::collect will collect your money.", 8150);
sendQuest("::takedown ITEMID will take an item off the ge", 8151);
sendQuestSomething(8143);
showInterface(8134);
flushOutStream();
}
Search for
Code:
void closeInterface()
and add
Code:
checkingge = false;
Search for
Code:
void RemoveAllWindows()
and add
Code:
checkingge = false;
Pictures:
Lets sell a glowing dagger for 100 gp, I type
Code:
::sell 747 100
, 747 being the dagger code, and 100 being the price amount
Oh look, Linksbro is selling a glowing dagger for 100gp, I'll buy it!
I just type ::buy 747 100, 747 being the glowing dagger code, and 100gp being the price amount!
I bought it! It's off the market now, and MINE! And Look, Linksbro has money to collect!
Oh lawd, 100gp GET!
Lemons! I ment to sell it for 10k! I should take that down now!
Yea! No one bought it and it's mine again!
Commands
sell (itemid) (price) will put an Item on the market for the price.
buy (itemid) (price) will buy an item on the market with the id and SAME price
takedown (itemid) will takedown an item only if you are selling it
gehelp displays a help interface
checkge displays the ge interneface.
Known Bugs
- You log out while your selling something, your screwed.
- If you have money to collect, and you log out, your screwed.
-Basically, if you log out while selling\have money to collect, we lose the reference to you, and can't reward you with gp, or takedown your item.
Things that you have to do by yourself
-Add a boolean to see if player is selling somethings\has money to collect, don't let the player log out until that boolean is false.