Jump to content

[Snippet] Item Converter


Alias

Recommended Posts

This is a simple class that can be used to convert String names into Item ID's or visa versa

There are a few edge cases where there is more than one ID for the same item so that is a possibility if you're using this.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ItemConverter {

    private final static String URL = "https://rsbuddy.com/exchange/summary.json";
    private final JsonObject json;

    public ItemConverter() {
        this.json = new JsonParser().parse(getJson()).getAsJsonObject();
    }

    public int getID(final String item) {
        for (String key : json.keySet()) {
            final JsonObject obj = json.get(key).getAsJsonObject();
            if (obj.get("name").getAsString().equals(item)) {
                return obj.get("id").getAsInt();
            }
        }

        return -1;
    }

    public String getName(final int itemId) {
        for (String key : json.keySet()) {
            final JsonObject obj = json.get(key).getAsJsonObject();
            if (obj.get("id").getAsInt() == itemId) {
                return obj.get("name").getAsString();
            }
        }

        return "";
    }

    private String getJson() {

        try {
            final URL url = new URL(URL);
            final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            String inputLine;
            String out = null;
            while ((inputLine = in.readLine()) != null) {
                out = inputLine;
            }
            in.close();
            return out;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
Edited by Alias
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

16 hours ago, Alias said:

This is a simple class that can be used to convert String names into Item ID's or visa versa

There are a few edge cases where there is more than one ID for the same item so that is a possibility if you're using this.

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ItemConverter {

    private final static String URL = "https://rsbuddy.com/exchange/summary.json";
    private final JsonObject json;

    public ItemConverter() {
        this.json = new JsonParser().parse(getJson()).getAsJsonObject();
    }

    public int getID(final String item) {
        for (String key : json.keySet()) {
            final JsonObject obj = json.get(key).getAsJsonObject();
            if (obj.get("name").getAsString().equals(item)) {
                return obj.get("id").getAsInt();
            }
        }

        return -1;
    }

    public String getName(final int itemId) {
        for (String key : json.keySet()) {
            final JsonObject obj = json.get(key).getAsJsonObject();
            if (obj.get("id").getAsInt() == itemId) {
                return obj.get("name").getAsString();
            }
        }

        return "";
    }

    private String getJson() {

        try {
            final URL url = new URL(URL);
            final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            String inputLine;
            String out = null;
            while ((inputLine = in.readLine()) != null) {
                out = inputLine;
            }
            in.close();
            return out;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

can this sort out this equation? 


x³+y³+z³=k

Nice btw 😛

 

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.

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...