Async ItemVariationMapping loading

This commit is contained in:
Lucwousin
2019-10-15 15:41:11 +02:00
parent 95076cfc05
commit 44a36cd8ce
4 changed files with 54 additions and 32 deletions

View File

@@ -28,6 +28,7 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import java.awt.Color; import java.awt.Color;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -192,6 +193,13 @@ public class ItemManager
eventbus.subscribe(GameStateChanged.class, this, this::onGameStateChanged); eventbus.subscribe(GameStateChanged.class, this, this::onGameStateChanged);
eventbus.subscribe(PostItemDefinition.class, this, this::onPostItemDefinition); eventbus.subscribe(PostItemDefinition.class, this, this::onPostItemDefinition);
Completable.fromAction(ItemVariationMapping::load)
.subscribeOn(Schedulers.computation())
.subscribe(
() -> log.debug("Loaded {} item variations", ItemVariationMapping.getSize()),
ex -> log.warn("Error loading item variations", ex)
);
} }
private void loadPrices() private void loadPrices()

View File

@@ -26,44 +26,20 @@
package net.runelite.client.game; package net.runelite.client.game;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson; import com.google.gson.stream.JsonReader;
import com.google.gson.reflect.TypeToken; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Collection; import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import lombok.extern.slf4j.Slf4j;
/** /**
* Converts variation items to it's base item counterparts * Converts variation items to it's base item counterparts
*/ */
@Slf4j
public class ItemVariationMapping public class ItemVariationMapping
{ {
private static final Map<Integer, Integer> MAPPINGS; private static Map<Integer, Integer> MAPPINGS;
static
{
final Gson gson = new Gson();
final TypeToken<Map<String, Collection<Integer>>> typeToken = new TypeToken<Map<String, Collection<Integer>>>()
{
};
final InputStream geLimitData = ItemVariationMapping.class.getResourceAsStream("/item_variations.json");
final Map<String, Collection<Integer>> itemVariations = gson.fromJson(new InputStreamReader(geLimitData), typeToken.getType());
ImmutableMap.Builder<Integer, Integer> builder = new ImmutableMap.Builder<>();
for (Collection<Integer> value : itemVariations.values())
{
final Iterator<Integer> iterator = value.iterator();
final int base = iterator.next();
while (iterator.hasNext())
{
builder.put(iterator.next(), base);
}
}
MAPPINGS = builder.build();
}
/** /**
* Get base item id for provided variation item id. * Get base item id for provided variation item id.
@@ -75,4 +51,39 @@ public class ItemVariationMapping
{ {
return MAPPINGS.getOrDefault(itemId, itemId); return MAPPINGS.getOrDefault(itemId, itemId);
} }
static void load() throws IOException
{
try (JsonReader reader = new JsonReader(new InputStreamReader(ItemVariationMapping.class.getResourceAsStream("/item_variations.json"), StandardCharsets.UTF_8)))
{
ImmutableMap.Builder<Integer, Integer> builder = ImmutableMap.builderWithExpectedSize(5039);
reader.beginObject();
while (reader.hasNext())
{
// Names are useless
reader.skipValue();
reader.beginArray();
int base = reader.nextInt();
while (reader.hasNext())
{
builder.put(
reader.nextInt(),
base
);
}
reader.endArray();
}
reader.endObject();
MAPPINGS = builder.build();
}
}
static int getSize()
{
return MAPPINGS.size();
}
} }

View File

@@ -56,9 +56,9 @@ public class HighAlchemyOverlay extends WidgetItemOverlay
this.itemManager = itemManager; this.itemManager = itemManager;
this.plugin = plugin; this.plugin = plugin;
int natPrice = itemManager.getItemPrice(ItemID.NATURE_RUNE); int natPrice = itemManager.getItemPrice(ItemID.NATURE_RUNE, true);
this.alchPrice = natPrice; this.alchPrice = natPrice;
this.alchPriceNoStaff = natPrice + 5 * itemManager.getItemPrice(ItemID.FIRE_RUNE); this.alchPriceNoStaff = natPrice + 5 * itemManager.getItemPrice(ItemID.FIRE_RUNE, true);
showOnBank(); showOnBank();
showOnInventory(); showOnInventory();

View File

@@ -77,10 +77,13 @@ public class StonedTrackerPlugin extends Plugin
@Inject @Inject
private ClientToolbar clientToolbar; private ClientToolbar clientToolbar;
@Inject @Inject
public StonedTrackerConfig config; public StonedTrackerConfig config;
@Inject @Inject
private Client client; private Client client;
@Inject @Inject
private ItemManager itemManager; private ItemManager itemManager;