Butters Posted November 25, 2016 Share Posted November 25, 2016 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? Quote Link to comment Share on other sites More sharing options...
Xerion Posted November 25, 2016 Share Posted November 25, 2016 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. Quote Link to comment Share on other sites More sharing options...
Butters Posted November 25, 2016 Author Share Posted November 25, 2016 (edited) 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 November 25, 2016 by krapstaunosi Quote Link to comment Share on other sites More sharing options...
Explv Posted November 25, 2016 Share Posted November 25, 2016 (edited) 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. If you know what items you want to buy, just hardcode the item ids Write your own buy method that uses item names instead of ids 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 November 25, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
Xerion Posted November 25, 2016 Share Posted November 25, 2016 You have several options. If you know what items you want to buy, just hardcode the item ids Write your own buy method that uses item names instead of ids 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. Quote Link to comment Share on other sites More sharing options...
Butters Posted November 26, 2016 Author Share Posted November 26, 2016 You have several options. If you know what items you want to buy, just hardcode the item ids Write your own buy method that uses item names instead of ids 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 Quote Link to comment Share on other sites More sharing options...
Ducky Posted November 26, 2016 Share Posted November 26, 2016 (edited) use colour Edited November 26, 2016 by ES Asp 2 Quote Link to comment Share on other sites More sharing options...
Sysm Posted November 26, 2016 Share Posted November 26, 2016 itemdb.biz thats a site just copy paste it (mainly used for rsps spawns but works for rs aswell obv) Quote Link to comment Share on other sites More sharing options...
Butters Posted November 27, 2016 Author Share Posted November 27, 2016 itemdb.biz 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 The current raw lobbie D is 377. The site gives me two ID's with the same revision number. Quote Link to comment Share on other sites More sharing options...
Team Cape Posted November 27, 2016 Share Posted November 27, 2016 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 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 Quote Link to comment Share on other sites More sharing options...
Bamboozled Posted November 27, 2016 Share Posted November 27, 2016 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 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 Quote Link to comment Share on other sites More sharing options...
Explv Posted November 27, 2016 Share Posted November 27, 2016 (edited) 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 June 5, 2018 by Explv 2 Quote Link to comment Share on other sites More sharing options...
Butters Posted November 27, 2016 Author Share Posted November 27, 2016 (edited) @Explv Kudos to you. Extremely useful. Thanks Edited November 27, 2016 by nosepicker Quote Link to comment Share on other sites More sharing options...
Sysm Posted November 27, 2016 Share Posted November 27, 2016 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 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 Quote Link to comment Share on other sites More sharing options...
Pegasus Posted June 5, 2018 Share Posted June 5, 2018 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. Quote Link to comment Share on other sites More sharing options...