Jump to content

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


Recommended Posts

Posted (edited)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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