diff --git a/src/main/java/gg/archipelago/client/parts/DataPackage.java b/src/main/java/gg/archipelago/client/parts/DataPackage.java index 4f7da0b..27a6af3 100644 --- a/src/main/java/gg/archipelago/client/parts/DataPackage.java +++ b/src/main/java/gg/archipelago/client/parts/DataPackage.java @@ -23,27 +23,18 @@ public class DataPackage implements Serializable { public String uuid = UUID.randomUUID().toString(); public String getItem(long itemID) { - for (Map.Entry game : games.entrySet()) { - for (Map.Entry item : game.getValue().itemNameToId.entrySet()) { - if(item.getValue() == itemID) - return item.getKey(); - } + if(!itemIdToName.containsKey(itemID)) { + return String.format("Unknown Item [%d]", itemID); } - return String.format("Unknown Item [%d]", itemID); + + return itemIdToName.get(itemID); } public String getLocation(long locationID) { - if(locationIdToName.containsKey(locationID)) - return locationIdToName.get(locationID); - for (Map.Entry game : games.entrySet()) { - for (Map.Entry location : game.getValue().locationNameToId.entrySet()) { - if(location.getValue() == locationID) { - locationIdToName.put(locationID, location.getKey()); - return location.getKey(); - } - } - } - return String.format("Unknown Location [%d]", locationID); + if(!locationIdToName.containsKey(locationID)) + return String.format("Unknown Location [%d]", locationID); + + return locationIdToName.get(locationID); } public Map getVersions() { @@ -57,13 +48,6 @@ public HashMap getGames() { } public HashMap getItems() { - if(itemIdToName.isEmpty()) { - for (Map.Entry gameEntry : games.entrySet()) { - for (Map.Entry items : gameEntry.getValue().itemNameToId.entrySet()) { - itemIdToName.put(items.getValue(), items.getKey()); - } - } - } return itemIdToName; } @@ -79,14 +63,7 @@ public HashMap getItemsForGame(String game) { } public HashMap getLocations() { - if(locationIdToName.isEmpty()) { - for (Map.Entry gameEntry : games.entrySet()) { - for (Map.Entry locations : gameEntry.getValue().locationNameToId.entrySet()) { - itemIdToName.put(locations.getValue(), locations.getKey()); - } - } - } - return itemIdToName; + return locationIdToName; } public HashMap getLocationsForGame(String game) { @@ -110,6 +87,26 @@ public String getUUID() { public void update(DataPackage newData) { games.putAll(newData.getGames()); + buildItemsMap(); + buildLocationsMap(); version = newData.version; } + + private void buildItemsMap() { + for (Map.Entry gameEntry : games.entrySet()) { + for (Map.Entry items : gameEntry.getValue().itemNameToId.entrySet()) { + itemIdToName.put(items.getValue(), items.getKey()); + } + } + } + + private void buildLocationsMap() { + locationIdToName.clear(); + + for (Map.Entry gameEntry : games.entrySet()) { + for (Map.Entry locations : gameEntry.getValue().locationNameToId.entrySet()) { + locationIdToName.put(locations.getValue(), locations.getKey()); + } + } + } }