56 lines
No EOL
2.1 KiB
Java
56 lines
No EOL
2.1 KiB
Java
package net.halfheart.ventricleengine;
|
|
|
|
import net.halfheart.ventricleengine.objects.Armor;
|
|
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.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class ArmorsHandler {
|
|
|
|
public static Armor constructArmor(String armorName) {
|
|
String filename = "/armors.json";
|
|
try (InputStream inputStream = ItemsHandler.class.getResourceAsStream(filename); BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8))) {
|
|
|
|
String jsonText = reader.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;
|
|
}
|
|
}
|
|
|
|
if (wantedItem == null) return null;
|
|
|
|
String name = wantedItem.getString("name");
|
|
String description = wantedItem.getString("description");
|
|
byte weight = (byte) wantedItem.getInt("weight");
|
|
short cost = (short) wantedItem.getInt("cost");
|
|
|
|
String type = wantedItem.getString("type");
|
|
String resUnit = wantedItem.getString("resUnit");
|
|
|
|
JSONArray resArray = wantedItem.getJSONArray("resistances");
|
|
List<Byte> resistances = new ArrayList<>();
|
|
for (int i = 0; i < resArray.length(); i++) {
|
|
resistances.add((byte) resArray.getInt(i));
|
|
}
|
|
|
|
return new Armor(name, description, weight, cost, type, resistances, resUnit);
|
|
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Could not load armor data", e);
|
|
}
|
|
}
|
|
} |