Thread: Linksbro's Grand Exchange v1

Page 1 of 9 123 ... LastLast
Results 1 to 10 of 81
  1. #1 Linksbro's Grand Exchange v1 
    Registered Member Linksbro's Avatar
    Join Date
    Mar 2008
    Posts
    118
    Thanks given
    13
    Thanks received
    22
    Rep Power
    25
    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.


    Credits:
    100% Myself.

    inb4: LOL PROCESS()
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    May 2007
    Posts
    277
    Thanks given
    9
    Thanks received
    6
    Rep Power
    42
    looks like you put alot of effort in. Good job. Rep'd.
    Reply With Quote  
     

  3. #3  
    Nuthin
    Guest
    Make it save it or before you logout return item to your bank/inventory.
    Reply With Quote  
     

  4. #4  
    Registered Member Linksbro's Avatar
    Join Date
    Mar 2008
    Posts
    118
    Thanks given
    13
    Thanks received
    22
    Rep Power
    25
    I could, but I'll leave that up to the person who is adding it.
    Reply With Quote  
     

  5. #5  
    Expert Programmer


    Join Date
    Dec 2007
    Posts
    2,018
    Thanks given
    52
    Thanks received
    84
    Rep Power
    986
    Not bad, but this would be better more object orientated

    Code:
    public class GrandExangeItem {
    
          public GrandExchangeItem(int id, int amount, double price, String seller) {
                this.id = id;
                this.amount = amount;
                this.price = price;
                this.seller = seller;
          }
    
          private int id;
          private int amount;
          private double price;
          private String seller;
    
    
         //getters here
    }
    Code:
          private List<GrandExchangeItem> geItems = new ArrayList<GrandExchangeItem>();
    Code:
           geItems.add(new GrandExchangeItem(id, amount, price, seller));
    This would save you from creating so many array list , Good Job anyway.
    Reply With Quote  
     

  6. #6  
    Remember The Name
    .Chris's Avatar
    Join Date
    May 2009
    Posts
    346
    Thanks given
    9
    Thanks received
    32
    Rep Power
    460
    any1 got version for emulous
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. #7  
    Registered Member Linksbro's Avatar
    Join Date
    Mar 2008
    Posts
    118
    Thanks given
    13
    Thanks received
    22
    Rep Power
    25
    Quote Originally Posted by Ultimate View Post
    Not bad, but this would be better more object orientated

    Code:
    public class GrandExangeItem {
    
          public GrandExchangeItem(int id, int amount, double price, String seller) {
                this.id = id;
                this.amount = amount;
                this.price = price;
                this.seller = seller;
          }
    
          private int id;
          private int amount;
          private double price;
          private String seller;
    
    
         //getters here
    }
    Code:
          private List<GrandExchangeItem> geItems = new ArrayList<GrandExchangeItem>();
    Code:
           geItems.add(new GrandExchangeItem(id, amount, price, seller));
    This would save you from creating so many array list , Good Job anyway.
    Thanks for the suggestion, I'll be doing something like this in v2.
    Reply With Quote  
     

  8. #8  
    Registered Member
    Join Date
    Sep 2007
    Posts
    273
    Thanks given
    0
    Thanks received
    7
    Rep Power
    9
    wow look like there was loads of work on this nice work and congrats.
    Reply With Quote  
     

  9. #9  
    Remember The Name
    .Chris's Avatar
    Join Date
    May 2009
    Posts
    346
    Thanks given
    9
    Thanks received
    32
    Rep Power
    460
    make it emulous also
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  10. #10  
    Registered Member
    Join Date
    Jul 2008
    Posts
    430
    Thanks given
    6
    Thanks received
    29
    Rep Power
    12
    Hope you don't mind if i take your Grand Exchange as a base as i fix it up. Thought this might be useful as well for the item id's.

    Code:
    public void checkgeid() {
    int k = 8147;
    
    for(int i = 0; i < playerItems.length; i++) {    
    sendQuest("Grand Exchange Ids", 8144);
      
      sendQuest(""+GetItemName(playerItems[i]-1)+"                                          ID:"+(playerItems[i]-1), k);
                
    k++;
    
    }
       
    sendQuestSomething(8143);
        
    showInterface(8134);
        
    flushOutStream();
    
    }
    }
    Reply With Quote  
     

Page 1 of 9 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •