<commit_before>/*
 * Copyright (c) 2017, Adam <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.client.game;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.runelite.client.RuneLite;
import net.runelite.http.api.item.ItemClient;
import net.runelite.http.api.item.ItemPrice;

public class ItemManager
{
	/**
	 * not yet looked up
	 */

	static final ItemPrice EMPTY = new ItemPrice();
	/**
	 * has no price
	 */
	static final ItemPrice NONE = new ItemPrice();

	private final ItemClient client = new ItemClient();
	private final LoadingCache<Integer, ItemPrice> itemPrices;

	public ItemManager(RuneLite runelite)
	{
		itemPrices = CacheBuilder.newBuilder()
			.maximumSize(512L)
			.expireAfterAccess(1, TimeUnit.HOURS)
			.build(new ItemPriceLoader(runelite, client));
	}

	/**
	 * Look up an item's price asynchronously.
	 *
	 * @param itemId
	 * @return the price, or null if the price is not yet loaded
	 */
	public ItemPrice get(int itemId)
	{
		ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
		if (itemPrice != null && itemPrice != EMPTY)
		{
			return itemPrice == NONE ? null : itemPrice;
		}

		itemPrices.refresh(itemId);
		return null;
	}

	/**
	 * Look up an item's price synchronously
	 *
	 * @param itemId
	 * @return
	 * @throws IOException
	 */
	public ItemPrice getItemPrice(int itemId) throws IOException
	{
		ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
		if (itemPrice != null && itemPrice != EMPTY)
		{
			return itemPrice == NONE ? null : itemPrice;
		}

		itemPrice = client.lookupItemPrice(itemId);
		itemPrices.put(itemId, itemPrice);
		return itemPrice;
	}

	/**
	 * Convert a quantity to stack size
	 *
	 * @param quantity
	 * @return
	 */
	public static String quantityToStackSize(int quantity)
	{
		if (quantity >= 10_000_000)
		{
			return quantity / 1_000_000 + "M";
		}

		if (quantity >= 100_000)
		{
			return quantity / 1_000 + "K";
		}

		return "" + quantity;
	}
}<commit_msg>runelite-client: add getItemImage to ItemManager to get item sprites<commit_after>/*
 * Copyright (c) 2017, Adam <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.client.game;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import net.runelite.api.Client;
import net.runelite.api.SpritePixels;
import net.runelite.client.RuneLite;
import net.runelite.http.api.item.ItemClient;
import net.runelite.http.api.item.ItemPrice;

public class ItemManager
{
	/**
	 * not yet looked up
	 */

	static final ItemPrice EMPTY = new ItemPrice();
	/**
	 * has no price
	 */
	static final ItemPrice NONE = new ItemPrice();


	private final ItemClient itemClient = new ItemClient();
	private final LoadingCache<Integer, ItemPrice> itemPrices;

	private static LoadingCache<Integer, BufferedImage> itemImages;

	public ItemManager(RuneLite runelite)
	{
		itemPrices = CacheBuilder.newBuilder()
			.maximumSize(512L)
			.expireAfterAccess(1, TimeUnit.HOURS)
			.build(new ItemPriceLoader(runelite, itemClient));

		itemImages = CacheBuilder.newBuilder()
			.maximumSize(200)
			.expireAfterAccess(1, TimeUnit.HOURS)
			.build(new CacheLoader<Integer, BufferedImage>()
			{
				@Override
				public BufferedImage load(Integer itemId) throws Exception
				{
					return loadImage(itemId);
				}
			});
	}

	/**
	 * Look up an item's price asynchronously.
	 *
	 * @param itemId
	 * @return the price, or null if the price is not yet loaded
	 */
	public ItemPrice get(int itemId)
	{
		ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
		if (itemPrice != null && itemPrice != EMPTY)
		{
			return itemPrice == NONE ? null : itemPrice;
		}

		itemPrices.refresh(itemId);
		return null;
	}

	/**
	 * Look up an item's price synchronously
	 *
	 * @param itemId
	 * @return
	 * @throws IOException
	 */
	public ItemPrice getItemPrice(int itemId) throws IOException
	{
		ItemPrice itemPrice = itemPrices.getIfPresent(itemId);
		if (itemPrice != null && itemPrice != EMPTY)
		{
			return itemPrice == NONE ? null : itemPrice;
		}

		itemPrice = itemClient.lookupItemPrice(itemId);
		itemPrices.put(itemId, itemPrice);
		return itemPrice;
	}

	/**
	 * Convert a quantity to stack size
	 *
	 * @param quantity
	 * @return
	 */
	public static String quantityToStackSize(int quantity)
	{
		if (quantity >= 10_000_000)
		{
			return quantity / 1_000_000 + "M";
		}

		if (quantity >= 100_000)
		{
			return quantity / 1_000 + "K";
		}

		return "" + quantity;
	}

	/**
	 * Loads item sprite from game, makes transparent, and generates image
	 * @param itemId
	 * @return
	 */
	private BufferedImage loadImage(int itemId)
	{
		Client client = RuneLite.getClient();
		SpritePixels sprite = client.createItemSprite(itemId, 1, 1, SpritePixels.DEFAULT_SHADOW_COLOR, 0, false);
		int[] pixels = sprite.getPixels();
		int[] transPixels = new int[pixels.length];
		BufferedImage img = new BufferedImage(sprite.getWidth(), sprite.getHeight(), BufferedImage.TYPE_INT_ARGB);

		for (int i = 0; i < pixels.length; i++)
		{
			if (pixels[i] != 0)
			{
				transPixels[i] = pixels[i] | 0xff000000;
			}
		}

		img.setRGB(0, 0, sprite.getWidth(), sprite.getHeight(), transPixels, 0, sprite.getWidth());

		return img;
	}

	/**
	 * Get item sprite image as BufferedImage
	 *
	 * @param itemId
	 * @return
	 */
	public static BufferedImage getImage(int itemId)
	{
		try
		{
			return itemImages.get(itemId);
		}
		catch (ExecutionException ex)
		{
			return null;
		}
	}
}
{'fraction_non_alphanumeric': 0.09369001919385797, 'fraction_numerical': 0.01067658349328215, 'mean_word_length': 3.9214876033057853, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3957952', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2440, 'n_words': 845}