Jump to content

Getting itemID by name


Butters

Recommended Posts

I was wondering is there a way to failproof code by not hardcoding item(or any other) ID's? 

 

For example I know that I can get the ID if I have have the item in inventory or somewhere else

		Item item = inventory.getItem(new Filter<Item>() {
			@[member=Override]
			public boolean match(Item item) {
				return item.getName() == "SOME ITEM NAME";
			}
		});
		
		log(item.getId());

But what if I don't have the item in my inventory/region/etc? 

Link to comment
Share on other sites

Why do you want to know it? Well in most cases there are multiple items that have the same name. The easiest example to mention is noted and unnoted items.

 

You can use the ItemDefinition class, but I believe you have to load the items first.

 

Thanks, but any idea how to load them? I need the ID for grand exchange (and probably would find more places where to apply them), as I don't have the items on me when I want to buy them. I'm using the OSBot GE API for the buying part, but it requires item ID's. 

Edited by krapstaunosi
Link to comment
Share on other sites

Thanks, but any idea how to load them? I need the ID for grand exchange (and probably would find more places where to apply them), as I don't have the items on me when I want to buy them. I'm using the OSBot GE API for the buying part, but it requires item ID's. 

 

You have several options.

 

  1. If you know what items you want to buy, just hardcode the item ids
  2. Write your own buy method that uses item names instead of ids
  3. Use an existing buy method that uses item names instead of ids, there are already snippets around on the forum, for example: http://osbot.org/forum/topic/90148-simple-ge-api
Edited by Explv
  • Like 1
Link to comment
Share on other sites

 

You have several options.

 

  1. If you know what items you want to buy, just hardcode the item ids
  2. Write your own buy method that uses item names instead of ids
  3. Use an existing buy method that uses item names instead of ids, there are already snippets around on the forum, for example: http://osbot.org/forum/topic/90148-simple-ge-api

 

 

Additionally you also do an API suggestion here:

 

http://osbot.org/forum/forum/102-client-bugs-suggestions/

 

Could be useful for everybody else if an method is added that supports item names.

Link to comment
Share on other sites

 

You have several options.

 

  1. If you know what items you want to buy, just hardcode the item ids
  2. Write your own buy method that uses item names instead of ids
  3. Use an existing buy method that uses item names instead of ids, there are already snippets around on the forum, for example: http://osbot.org/forum/topic/90148-simple-ge-api

 

 

Thanks

Link to comment
Share on other sites

itemdb.biz smile.png

thats a site just copy paste it (mainly used for rsps spawns but works for rs aswell obv)

 

Thanks for the heads up. Didn't know about this one. But immediately I see a couple of concerns:

 

1) How do I know if the site will be available for a long time?

2) This is strange

itemdb.png

 

The current raw lobbie D is 377. The site gives me two ID's with the same revision number.

Link to comment
Share on other sites

Thanks for the heads up. Didn't know about this one. But immediately I see a couple of concerns:

 

1) How do I know if the site will be available for a long time?

2) This is strange

itemdb.png

 

The current raw lobbie D is 377. The site gives me two ID's with the same revision number.

 

2nd one is noted ID. 1st one is regular ID

Link to comment
Share on other sites

Thanks for the heads up. Didn't know about this one. But immediately I see a couple of concerns:

 

1) How do I know if the site will be available for a long time?

2) This is strange

itemdb.png

 

The current raw lobbie D is 377. The site gives me two ID's with the same revision number.

One is probably the noted ID and the other is without, just a guess though :D

Link to comment
Share on other sites

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {

    public static void main(String[] args) {
        Example example = new Example();
        example.getItemsJson().ifPresent(json -> {
            example.getItemID(json, "Greenman's ale").ifPresent(System.out::println);
            example.getItemID(json, "Cannonball").ifPresent(System.out::println);
            example.getItemID(json, "Raw trout").ifPresent(System.out::println);
            example.getItemID(json, "Rune scimitar").ifPresent(System.out::println);
            example.getItemID(json, "Lobster").ifPresent(System.out::println);
        });
    }

    private Optional<Integer> getItemID(final String json, final String itemName) {
        return getItemFromJson(json, itemName).flatMap(this::getItemIDFromItemJson);
    }

    private Optional<String> getItemFromJson(final String json, final String itemName) {
        Matcher matcher = Pattern.compile("(\\{[^}]*\"name\"\\s*:\\s*\"" + Pattern.quote(itemName) + "\"[^}]*})").matcher(json);
        return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
    }

    private Optional<Integer> getItemIDFromItemJson(final String json) {
        Matcher matcher = Pattern.compile("\"id\"\\s*:\\s*(\\d*)").matcher(json);
        return matcher.find() ? Optional.of(Integer.parseInt(matcher.group(1))) : Optional.empty();
    }

    private Optional<String> getItemsJson() {
        try {
            URL url = new URL("https://rsbuddy.com/exchange/summary.json");
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            con.setUseCaches(true);

            try (InputStreamReader in = new InputStreamReader(con.getInputStream());
                 BufferedReader br = new BufferedReader(in)) {
                String json = br.readLine();
                json = json.replaceAll("\\\\u0027", "'");
                json = json.replaceAll("\\\\u0026", "&");
                return Optional.of(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }
}


Output:

 

8522
2
335
1333
379

 

Edited by Explv
  • Like 2
Link to comment
Share on other sites

Thanks for the heads up. Didn't know about this one. But immediately I see a couple of concerns:

 

1) How do I know if the site will be available for a long time?

2) This is strange

itemdb.png

 

The current raw lobbie D is 377. The site gives me two ID's with the same revision number.

One is noted other isnt. i thougt first one was un-noted not 100% sure bout that tho

Also this site been around since 2007 i think lol, so yea doubt they leave anytime soon

Link to comment
Share on other sites

  • 1 year later...
On 11/27/2016 at 8:29 PM, Explv said:

 

 

Also another solution if you just want to get the IDs of Grand Exchange items, and can't hardcode the IDs into your script you can do something like this:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {

    public static void main(String[] args) {
        Example example = new Example();
        example.getItemsJson().ifPresent(json -> {
            example.getItemID(json, "Cannonball").ifPresent(System.out::println);
            example.getItemID(json, "Raw trout").ifPresent(System.out::println);
            example.getItemID(json, "Rune scimitar").ifPresent(System.out::println);
            example.getItemID(json, "Lobster").ifPresent(System.out::println);
        });
    }

    private Optional<Integer> getItemID(final String json, final String itemName) {
        return getItemFromJson(json, itemName).flatMap(this::getItemIDFromItemJson);
    }

    private Optional<String> getItemFromJson(final String json, final String itemName) {
        Matcher matcher = Pattern.compile("(\\{[^}]*\"name\"\\s*:\\s*\"" + Pattern.quote(itemName) + "\"[^}]*})").matcher(json);
        return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
    }

    private Optional<Integer> getItemIDFromItemJson(final String json) {
        Matcher matcher = Pattern.compile("\"id\"\\s*:\\s*(\\d*)").matcher(json);
        return matcher.find() ? Optional.of(Integer.parseInt(matcher.group(1))) : Optional.empty();
    }

    private Optional<String> getItemsJson() {
        try {
            URL url = new URL("https://rsbuddy.com/exchange/summary.json");
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            con.setUseCaches(true);
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String json = br.readLine();
            br.close();
            return Optional.of(json);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }
}

Output:


2
335
1333
379

great example,

but if you input item name with Apostrophe character, e.g Greenman's ale,

it doesn't print anything.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...