package net.halfheart.ventricleengine; import org.json.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; public class ArmorsHandler { private static Armor constructArmor(String armorName) { String filename = "/armors.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(armorName)) { wantedItem = item; } } assert wantedItem != null; JSONObject resistances = wantedItem.getJSONObject("resistances"); String name = wantedItem.opt("name").toString(); String description = wantedItem.opt("desc").toString(); String armorType = wantedItem.opt("type").toString(); String resUnit = wantedItem.opt("unit").toString(); List resList = new ArrayList<>(); for (String key : resistances.keySet()) { byte res = (byte) resistances.opt(key); resList.add(res); } byte weight = (byte) wantedItem.opt("weight"); short cost = (short) wantedItem.opt("cost"); return new Armor(name, description, armorType, resList, resUnit, cost, weight); } catch (IOException e) { throw new RuntimeException(e); } } public static Armor findArmor(String armorName) { return switch (armorName) { case "Nothing" -> NOTHING; case "Leather Armor" -> LEATHERARMOR; case "Sturdy Leather Armor" -> STURDYLEATHERARMOR; case "Raider Armor" -> RAIDERARMOR; case "Metal Armor" -> METALARMOR; case "Combat Armor" -> COMBATARMOR; case "Centurion Armor" -> CENTURIONARMOR; case "NCR Ranger Armor" -> NCRRANGERARMOR; case "T-45 Power Armor" -> T45POWERARMOR; case "T-60 Power Armor" -> T60POWERARMOR; case "T-65 Power Armor" -> T65POWERARMOR; case "X-01 Power Armor" -> X01POWERARMOR; case "APA MkI Power Armor" -> APAMKIORPOWERARMOR; case "APA MkII Power Armor" -> APAMKIIPOWERARMOR; case "Raider Power Armor" -> RAIDERPOWERARMOR; case "Ultracite Power Armor" -> ULTRACITEPOWERARMOR; case "T-51 Power Armor" -> T51POWERARMOR; case "Enclave Uniform" -> ENCLAVEUNIFORM; case "Excavator Power Armor" -> EXCAVATORPOWERARMOR; default -> null; }; } public static Armor NOTHING = constructArmor("Nothing"); public static Armor LEATHERARMOR = constructArmor("Leather Armor"); public static Armor STURDYLEATHERARMOR = constructArmor("Sturdy Leather Armor"); public static Armor RAIDERARMOR = constructArmor("Raider Armor"); public static Armor METALARMOR = constructArmor("Metal Armor"); public static Armor COMBATARMOR = constructArmor("Combat Armor"); public static Armor CENTURIONARMOR = constructArmor("Centurion Armor"); public static Armor NCRRANGERARMOR = constructArmor("NCR Ranger Armor"); public static Armor T45POWERARMOR = constructArmor("T-45 Power Armor"); public static Armor T60POWERARMOR = constructArmor("T-60 Power Armor"); public static Armor T65POWERARMOR = constructArmor("T-65 Power Armor"); public static Armor X01POWERARMOR = constructArmor("X-01 Power Armor"); public static Armor APAMKIORPOWERARMOR = constructArmor("APA MkI Power Armor"); public static Armor APAMKIIPOWERARMOR = constructArmor("APA MkII Power Armor"); public static Armor RAIDERPOWERARMOR = constructArmor("Raider Power Armor"); public static Armor ULTRACITEPOWERARMOR = constructArmor("Ultracite Power Armor"); public static Armor T51POWERARMOR = constructArmor("T-51 Power Armor"); public static Armor ENCLAVEUNIFORM = constructArmor("Enclave Uniform"); public static Armor EXCAVATORPOWERARMOR = constructArmor("Excavator Power Armor"); }