55 lines
2 KiB
Java
55 lines
2 KiB
Java
package net.halfheart.ventricleengine;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static net.halfheart.ventricleengine.AidModifierHandler.STIMPAK;
|
|
|
|
public class ItemsHandler {
|
|
private static Item constructItem(String weaponName) {
|
|
String filename = "/items.json";
|
|
try (InputStream inputStream = WeaponsHandler.class.getResourceAsStream(filename)) {
|
|
String jsonText = new BufferedReader(
|
|
new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8))
|
|
.lines()
|
|
.collect(Collectors.joining("\n"));
|
|
|
|
JSONArray jsonArray = new JSONArray(jsonText);
|
|
|
|
JSONObject wantedItem = null;
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
JSONObject item = jsonArray.getJSONObject(i);
|
|
if (item.opt("name").toString().equals(weaponName)) {
|
|
wantedItem = item;
|
|
}
|
|
}
|
|
assert wantedItem != null;
|
|
String name = wantedItem.opt("name").toString();
|
|
String description = wantedItem.opt("desc").toString();
|
|
byte weight = (byte) wantedItem.opt("weight");
|
|
short cost = (short) wantedItem.opt("cost");
|
|
boolean consumable = (boolean) wantedItem.opt("consumable");
|
|
String aidModifier = wantedItem.opt("aidModifier").toString();
|
|
return new Item(name, description, weight, cost, consumable, aidModifier);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static Item findItem(String itemName) {
|
|
return switch (itemName) {
|
|
case "Stimpak" -> STIMPAK;
|
|
default -> null;
|
|
};
|
|
}
|
|
|
|
public static Item STIMPAK = constructItem("Stimpak");
|
|
}
|