Jump to content

Download item icons or anything from the wiki to the data folder.


Bobbey

Recommended Posts

Non-SDN scripts are troublesome with resources. You have to download the resources externally. That's why I made some code which can be used to download any image from the oldschool runescape wiki or any other website for that matter.
 

    public static CompletableFuture<BufferedImage> getImage(File imageFolder, String fileName, String url) {
        File imageFile = new File(imageFolder, fileName);

//AccessController.doPrivileged gives the new thread permission to read the image from the data directory

        return CompletableFuture.supplyAsync(() -> AccessController.doPrivileged((PrivilegedAction<BufferedImage>) () -> {
            try {
                if (!imageFile.exists()) {
                    downloadImage(url, imageFile);
                }
                return ImageIO.read(imageFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }));
    }

    private static boolean downloadImage(String urlString, File destination) throws IOException {
        URL url = new URL(urlString);
        InputStream in = url.openStream();
        FileOutputStream fos = new FileOutputStream(destination);
        ReadableByteChannel rbc = Channels.newChannel(in);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();
        in.close();
        return true;
    }

Example usage:

        //Somwhere in your class
        private BufferedImage coinIcon;
        //In your paint method
        if(coinIcon != null) {
            g.drawImage(coinsIcon, x, y, null);//Use original dimensions
            g.drawImage(coinsIcon, x, y, width, height, null);
        }

        File imageFolder = new File(getDirectoryData() + File.seperator + "scriptname", "images");
        String fileName = "coinstack.png";
        String url = "https://oldschool.runescape.wiki/images/Coins_10000.png";
        Function<Throwable, Void> errorHandler = e -> {
            script.log(e);
            return null;
        };

        ResourceManager.getImage(imageFolder, fileName, url).thenAccept(image -> {
            coinIcon = image;
            //Note: this is off the main thread, if youre inserting into lists or maps from multiple of these calls, use synchronized or concurrent collentions and maps.
        }).exceptionally(errorHandler);

The fileName is the name for how it is saved in the data folder. This code spawns a new thread that reads or downloads the image. Then the thenAccept function is called with the buffered image.

 

 

Edited by Bobbey
  • Like 1
  • Heart 3
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...