Jump to content

Welcome to DreamBot

VIP Enhancement

Want to upgrade your DreamBot experience? Consider signing up for VIP!
VIP allows you to run as many accounts as you want, view the forums ad-free, receive 10% off all script purchases, and so much more!
Visit the store to learn about all of these great features!

Upgrade to VIP Now
Frequently Asked Questions
  • Are you not able to open the client? Make sure you have Java 8 installed
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
Download the DreamBot client today!
klongrich

Grand Exchange Mishap

Recommended Posts

package main;


import org.dreambot.api.methods.grandexchange.GrandExchange;
import org.dreambot.api.methods.grandexchange.GrandExchangeItem;
import org.dreambot.api.script.AbstractScript;
import org.dreambot.api.script.Category;
import org.dreambot.api.script.ScriptManifest;
import org.dreambot.api.methods.map.Area;
import org.dreambot.api.wrappers.interactive.NPC;
import org.dreambot.api.wrappers.widgets.WidgetChild;

import main.GrandExchangeApi;

@ScriptManifest(category = Category.MISC, name="Hide Tanner", author="klongrich", version=1)
public class hideTanner extends AbstractScript {

    GrandExchangeApi exchangeApi = new GrandExchangeApi();

    private Area AlkhairdBank = new Area(3270, 3166, 3271, 3168);
    private Area TanningShop = new Area(3277, 3190, 3274, 3193);
    private Area GrandexchangeLocation = new Area (3147, 3476, 3180,3504);

    private void BankCowhides() {
        getWalking().walk(AlkhairdBank.getRandomTile());
        if(AlkhairdBank.contains(getLocalPlayer())) {
            NPC Banker = getNpcs().closest("Banker");
            Banker.interact("Bank");
            getBank().depositAllExcept("Coins");
            getBank().withdrawAll("Cowhide");
        }
    }

    private void TanCowhides() {
        NPC Ellis = getNpcs().closest("Ellis");
        Ellis.interact("Trade");
        WidgetChild TanInterface = getWidgets().getWidgetChild(324, 133);
        TanInterface.interact("Tan all");
    }

    private void sellCowhides(){
        int salePrice;

        GrandExchangeApi.GELookupResult lookupResult = exchangeApi.lookup(1743);

        salePrice = lookupResult.price - 20;

        getGrandExchange().open("Exchange");
        getGrandExchange().openSellScreen(1);
        getGrandExchange().sellItem("Hard leather", getInventory().count("Hard leather"), salePrice);
        getGrandExchange().goBack();
        sleepUntil(() -> getGrandExchange().isReadyToCollect(1), 5000);
        getGrandExchange().collect();
    }

    private void buyCowhides(){
        int AmountOfCowhides;
        int CurrentPrice;

        GrandExchangeApi.GELookupResult lookupResult = exchangeApi.lookup(1739);

        CurrentPrice = lookupResult.price;
        AmountOfCowhides = getInventory().count("coins") / (CurrentPrice + 15);

        getGrandExchange().open("Exchange");
        getGrandExchange().openBuyScreen(2);
        getGrandExchange().searchItem("Cowhide");
        getGrandExchange().buyItem("Cowhide", AmountOfCowhides, CurrentPrice + 15);
        getGrandExchange().goBack();
    }

    private void ExchangeCowhides(){
        if (!GrandexchangeLocation.contains(getLocalPlayer())) {
            getWalking().walk(GrandexchangeLocation.getRandomTile());
        } else {
            sellCowhides();
            buyCowhides();
        }
    }

    @Override
    public int onLoop() {
        if (getInventory().contains("Hard leather") && !GrandexchangeLocation.contains(getLocalPlayer())) {
            BankCowhides();
        }
        else if (!getInventory().contains("Cowhide")){
            ExchangeCowhides();
        }
        else if (TanningShop.contains(getLocalPlayer())) {
            TanCowhides();
        } else {
            getWalking().walk(TanningShop.getRandomTile());
        }
        return 1000;
    }
}

Then Here is the API that I got From this forum 

package main;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for access to the Grand Exchange API.
 */
public class GrandExchangeApi {
    private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
            "/api/catalogue/detail.json?item=%d";
    private static final long TEN_MINUTES = 600000;
    private final Map<Integer, GELookupResult> cache;
    private long startTime;

    /**
     * Caching-enabled default constructor
     */
    public GrandExchangeApi() {
        this(true);
    }

    /**
     * Creates a new Grand Exchange API instance. Starts cache-refresh timer.
     * @[member='param'] cache Whether to enable caching of results.
     */
    public GrandExchangeApi(boolean cache) {
        startTime = System.currentTimeMillis();
        this.cache = cache ? new HashMap<>() : null;
    }

    /**
     * If caching is enabled, clears the cache so that new results are fetched on lookup.
     */
    public void flushCache() {
        if(cache != null) {
            cache.clear();
        }
    }

    /**
     * Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result.
     * @[member='param'] itemId the id to look up.
     * @return the result returned by the api. May be null if an error has occurred.
     */
    public GELookupResult lookup(int itemId) {
        if((System.currentTimeMillis() - TEN_MINUTES) > startTime){ //Flush cache after 10 minutes. Auto-update prices.
            flushCache();
            startTime = System.currentTimeMillis();
        }

        if(cache != null && !cache.isEmpty()) {
            GELookupResult result = cache.get(itemId);
            if(result != null) {
                return result;
            }
        }

        String json;
        try {
            URL url = new URL(String.format(API_LOCATION, itemId));
            Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
            json = scan.next();
            scan.close();
        } catch(IOException e) {
            return null;
        }

        GELookupResult result = parse(itemId, json);

        if(cache != null) {
            cache.put(itemId, result);
        }

        return result;
    }

    /**
     * Parses a GELookupResult from the JSON returned by the API.
     * @[member='param'] itemId The item ID.
     * @[member='param'] json The JSON returned by the RuneScape's API.
     * @return The serialized result.
     */
    private static GELookupResult parse(int itemId, String json) {
        Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
        Matcher m = pattern.matcher(json);
        Map<String, String> results = new HashMap<>();
        while(m.find()) {
            results.put(m.group("key"), m.group("value"));
        }

        int price = 0;
        Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json);
        if (priceMatcher.find()) {
            price = Integer.parseInt(priceMatcher.group("price"));
        }

        return new GELookupResult(
                results.get("icon"),
                results.get("icon_large"),
                results.get("type"),
                results.get("typeIcon"),
                results.get("name"),
                results.get("description"),
                Boolean.parseBoolean(results.get("members")),
                itemId,
                price
        );
    }

    /**
     * A class representing a result from an API lookup.
     */
    public static final class GELookupResult {
        public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
        public final boolean isMembers;
        public final int id, price;

        private GELookupResult(String smallIconUrl,
                               String largeIconUrl,
                               String type,
                               String typeIcon,
                               String name,
                               String itemDescription,
                               boolean isMembers,
                               int id,
                               int price) {

            this.smallIconUrl = smallIconUrl;
            this.largeIconUrl = largeIconUrl;
            this.type = type;
            this.typeIcon = typeIcon;
            this.name = name;
            this.itemDescription = itemDescription;
            this.isMembers = isMembers;
            this.id = id;
            this.price = price;
        }
    }
}

Every time I run this the got always buys first I've tried moving the code around and it makes no sense. If they are ran in sequence it always buys first even though the code shouldn't be compile this way.  Is it the why your guys are compiling the grand exchange api or something? 

Share this post


Link to post
Share on other sites

I advise you to simply use the GE DB Api RAW, I made a guide on Grand Exchange buying and selling instead of relying on a possibly outdated class.

 

Also an advice would be to use:

if(getWalking().shouldWalk()) {

getWalking().walk(areaGEExample.getRandomTile());

}

when banking/walking to stop the map spam clicking!

 

If you have any question regarding hard coding in GE activites, contact me via discord or pm.. I'll help whenever I have time!

Share this post


Link to post
Share on other sites

I was only using the one api to get current prices. I couldn't figure out how to do it with the DB api but there is probably a way. I will check it out. Also that is nice to know, the spam clicking was getting annoying some times lol. Thanks for the feedback!

Share this post


Link to post
Share on other sites
9 hours ago, klongrich said:

I was only using the one api to get current prices. I couldn't figure out how to do it with the DB api but there is probably a way. I will check it out. Also that is nice to know, the spam clicking was getting annoying some times lol. Thanks for the feedback!

Thats awesome!

Good luck!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...