Jump to content

Get and save sprite snippet


The Undefeated

Recommended Posts

public class FetchSprites {

    private static String path = "";

    public static File getSprite(String item) {
        item = Character.toUpperCase(item.charAt(0)) + item.toLowerCase().substring(1);
        if (!Files.exists(Paths.get(path + item + ".png"))) {
            fetchSprite(item);
        }
        return new File(path + item + ".png");
    }


    private static void fetchSprite(String item) {
        item = Character.toUpperCase(item.charAt(0)) + item.toLowerCase().substring(1);
        String itemNoSpaces = item.replace(" ","_");
        try {
            final URL url = new URL("http://oldschoolrunescape.wikia.com/wiki/" + itemNoSpaces.toLowerCase());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("/" + itemNoSpaces + ".png")) {
                    int start = line.indexOf("<a href=") + 9;
                    int name = line.indexOf(itemNoSpaces + ".png",start);
                    int end = name + item.length() + 4;
                    URL imageUrl = new URL(line.substring(start,end));
                    BufferedImage image = ImageIO.read(imageUrl);
                    ImageIO.write(image,"png",new File(path + item + ".png"));
                    break;
                }
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This will check if the sprite is already cached, if not, it will cache the sprite and return the file(path).

Improvements are welcome.

 

Edited by The Undefeated
  • Boge 1
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...