From 839be1f28f7abfd4b25aa20c75b7deb62224e6cb Mon Sep 17 00:00:00 2001 From: MagicBot Date: Tue, 27 Feb 2024 13:31:11 -0500 Subject: [PATCH] Always check for empty arrays. For loop processes them. --- src/engine/objects/ItemTemplate.java | 123 ++++++++++++++------------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/src/engine/objects/ItemTemplate.java b/src/engine/objects/ItemTemplate.java index d352bf8d..54bf12ad 100644 --- a/src/engine/objects/ItemTemplate.java +++ b/src/engine/objects/ItemTemplate.java @@ -78,11 +78,12 @@ public class ItemTemplate { obj_name = (String) jsonObject.get("obj_name"); - // Reading floats from an array + // Reading floats from an array (note always check for empty arrays) JSONArray scaleData = (JSONArray) jsonObject.get("obj_scale"); - obj_scale = new Vector3fImmutable(((Double) scaleData.get(0)).floatValue(), ((Double) scaleData.get(1)).floatValue(), - ((Double) scaleData.get(2)).floatValue()); + if (scaleData.isEmpty() == false) + obj_scale = new Vector3fImmutable(((Double) scaleData.get(0)).floatValue(), ((Double) scaleData.get(1)).floatValue(), + ((Double) scaleData.get(2)).floatValue()); // Reading an integer value @@ -131,10 +132,10 @@ public class ItemTemplate { item_render_object_female = ((Long) jsonObject.get("item_render_object_female")).intValue(); item_health_full = ((Double) jsonObject.get("item_health_full")).floatValue(); - Object skills_used = jsonObject.get("item_skill_used"); + Object skill_used = jsonObject.get("item_skill_used"); - if (skills_used instanceof String) { - String skilString = (String) skills_used; + if (skill_used instanceof String) { + String skilString = (String) skill_used; skilString = skilString.replaceAll("\\s", ""); skilString = skilString.replaceAll(",", ""); item_skill_used.add(Enum.CharacterSkills.valueOf(skilString)); @@ -171,14 +172,15 @@ public class ItemTemplate { JSONArray weapon_damage = (JSONArray) item_weapon.get("weapon_damage"); - for (Object o : weapon_damage) { - JSONObject damage_entry = (JSONObject) o; - Enum.SourceType sourceType = Enum.SourceType.valueOf(((String) damage_entry.get("damage_type")).toUpperCase()); - int min = ((Long) damage_entry.get("damage_min")).intValue(); - int max = ((Long) damage_entry.get("damage_max")).intValue(); - int[] minMax = {min, max}; - item_weapon_damage.put(sourceType, minMax); - } + if (weapon_damage.isEmpty() == false) + for (Object o : weapon_damage) { + JSONObject damage_entry = (JSONObject) o; + Enum.SourceType sourceType = Enum.SourceType.valueOf(((String) damage_entry.get("damage_type")).toUpperCase()); + int min = ((Long) damage_entry.get("damage_min")).intValue(); + int max = ((Long) damage_entry.get("damage_max")).intValue(); + int[] minMax = {min, max}; + item_weapon_damage.put(sourceType, minMax); + } } JSONArray itemflags = (JSONArray) jsonObject.get("item_flags"); @@ -200,73 +202,78 @@ public class ItemTemplate { JSONArray skill_required = (JSONArray) jsonObject.get("item_skill_req"); - for (Object o : skill_required) { - JSONObject skill_req = (JSONObject) o; - String skill_type = (String) skill_req.get("skill_type"); - skill_type = skill_type.replaceAll("\\s", ""); - skill_type = skill_type.replaceAll(",", ""); - int skill_level = ((Long) skill_req.get("skill_level")).intValue(); - item_skill_required.put(Enum.CharacterSkills.valueOf(skill_type), skill_level); - } + if (skill_required.isEmpty() == false) + for (Object o : skill_required) { + JSONObject skill_req = (JSONObject) o; + String skill_type = (String) skill_req.get("skill_type"); + skill_type = skill_type.replaceAll("\\s", ""); + skill_type = skill_type.replaceAll(",", ""); + int skill_level = ((Long) skill_req.get("skill_level")).intValue(); + item_skill_required.put(Enum.CharacterSkills.valueOf(skill_type), skill_level); + } JSONObject race_required = (JSONObject) jsonObject.get("item_race_req"); boolean restrict = ((Boolean) race_required.get("restrict")); JSONArray races = (JSONArray) race_required.get("races"); - for (Object o : races) { - String race = (String) o; - race = race.replaceAll("\\s", ""); - race = race.replaceAll(",", ""); - race = race.replaceAll("-", ""); - - if (restrict) - item_race_res.add(Enum.MonsterType.valueOf(race)); - else - item_race_req.add(Enum.MonsterType.valueOf(race)); - } + if (races.isEmpty() == false) + for (Object o : races) { + String race = (String) o; + race = race.replaceAll("\\s", ""); + race = race.replaceAll(",", ""); + race = race.replaceAll("-", ""); + + if (restrict) + item_race_res.add(Enum.MonsterType.valueOf(race)); + else + item_race_req.add(Enum.MonsterType.valueOf(race)); + } JSONObject class_required = (JSONObject) jsonObject.get("item_class_req"); restrict = ((Boolean) class_required.get("restrict")); JSONArray classes = (JSONArray) class_required.get("classes"); - for (Object o : classes) { - String classEntry = (String) o; - classEntry = classEntry.replaceAll("\\s", ""); - classEntry = classEntry.replaceAll(",", ""); + if (classes.isEmpty() == false) + for (Object o : classes) { + String classEntry = (String) o; + classEntry = classEntry.replaceAll("\\s", ""); + classEntry = classEntry.replaceAll(",", ""); - if (restrict) - item_class_res.add(Enum.ClassType.valueOf(classEntry)); - else - item_class_req.add(Enum.ClassType.valueOf(classEntry)); - } + if (restrict) + item_class_res.add(Enum.ClassType.valueOf(classEntry)); + else + item_class_req.add(Enum.ClassType.valueOf(classEntry)); + } JSONObject disc_required = (JSONObject) jsonObject.get("item_disc_req"); restrict = ((Boolean) disc_required.get("restrict")); JSONArray discs = (JSONArray) disc_required.get("discs"); - for (Object o : discs) { - String disc = (String) o; - disc = disc.replaceAll("\\s", ""); - disc = disc.replaceAll(",", ""); + if (discs.isEmpty() == false) + for (Object o : discs) { + String disc = (String) o; + disc = disc.replaceAll("\\s", ""); + disc = disc.replaceAll(",", ""); - if (restrict) - item_disc_res.add(Enum.DisciplineType.valueOf(disc)); - else - item_disc_req.add(Enum.DisciplineType.valueOf(disc)); - } + if (restrict) + item_disc_res.add(Enum.DisciplineType.valueOf(disc)); + else + item_disc_req.add(Enum.DisciplineType.valueOf(disc)); + } item_level_req = ((Long) jsonObject.get("item_level_req")).intValue(); item_sex_req = Enum.SexType.valueOf((String) jsonObject.get("item_sex_req")); JSONArray powerActions = (JSONArray) jsonObject.get("item_user_power_action"); - for (Object o : powerActions) { - JSONObject powerActionEntry = (JSONObject) o; - String power = (String) powerActionEntry.get("power"); - JSONArray args = (JSONArray) powerActionEntry.get("arguments"); - int[] arguments = {((Long) args.get(0)).intValue(), ((Long) args.get(1)).intValue()}; - item_user_power_action.put(power, arguments); - } + if (powerActions.isEmpty() == false) + for (Object o : powerActions) { + JSONObject powerActionEntry = (JSONObject) o; + String power = (String) powerActionEntry.get("power"); + JSONArray args = (JSONArray) powerActionEntry.get("arguments"); + int[] arguments = {((Long) args.get(0)).intValue(), ((Long) args.get(1)).intValue()}; + item_user_power_action.put(power, arguments); + } } catch (Exception e) { Logger.error(e); }