Code:
package io.ruin.api.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public enum GrandExchangeAPI {
BOFA(25862, 25865, 25867, 25869, 25884, 25886, 25888, 25890, 25892, 25894, 25896),
BLADE_OF_SAELDOR(23997, 23995, 24551, 24553, 25870, 25872, 25874, 25876, 25878, 25880, 25882),
BRACELET_ETHERUEM(21817, 21816),
SANGUINESTI_STAFF(22481, 22323, 25731, 25733, 25739),
SCYTHE_OF_VITUR(22486, 22325, 22664, 25736, 25738, 25739, 25741),
SERPENTINE_HELM(12929, 12931, 13198, 13199, 13196, 13197),
TOXIC_STAFF(12902, 12904),
TOXIC_BLOWPIPE(12924, 12926),
TOME_OF_FIRE(20716, 20714),
CRAWS_BOW(22547, 22550),
THAMMARONS_SCEPTRE(22552, 22555),
VIGGORAS_CHAINMACE(22542, 22545),
TRIDENT(11905, 11907),
TRIDENT_E(22290, 22288),
TOXIC_TRIDENT(12900, 12899),
TOXIC_TRIDENT_E(22294, 22292);
int unchargedId;
int[] chargedIds;
GrandExchangeAPI(int unchargedId, int... chargedIds) {
this.unchargedId = unchargedId;
this.chargedIds = chargedIds;
}
public static void main(String[] args) throws IOException {
URL url = new URL("https://prices.runescape.wiki/api/v1/osrs/latest");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0");
// Read the response from the API
ObjectMapper apiMapper = new ObjectMapper();
JsonNode apiRoot = apiMapper.readTree(conn.getInputStream());
// Parse the JSON file
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("./boomscape-game-server/data/items/item_info.json"));
// Iterate through the list of items
for (JsonNode item : root) {
int id = item.path("id").asInt();
int currentPrice = item.path("protect_value").asInt();
for (GrandExchangeAPI grandExchangeAPI : GrandExchangeAPI.values())
for (int chargedIds : grandExchangeAPI.chargedIds)
if (chargedIds == id)
id = grandExchangeAPI.unchargedId;
JsonNode lowNumber = apiRoot.path("data").path(String.valueOf(id)).path("low");
JsonNode highNumber = apiRoot.path("data").path(String.valueOf(id)).path("high");
int price = (highNumber.asInt() + lowNumber.asInt()) / 2;
if (price == 0)
continue;
// Update the 'protect_value' for the current item
((ObjectNode) item).put("protect_value", price);
String currentPriceComparison = currentPrice > 0 ? String.valueOf(currentPrice) : "Unset";
//System.out.println("ID:" + id + ", Current price: " + currentPriceComparison + ", New price: " + low);
}
// Write the updated JSON data to a new file
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("./boomscape-game-server/data/items/item_info_updated.json"), root);
System.out.println("Successfully updated Protection Values for Item Info");
}
}