From f04f062723c37f16c1340b0e8b952bfb47d3c696 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Mon, 23 Jun 2025 16:24:47 +0200 Subject: [PATCH] extract some more toJson stuff to reduce duplication --- .../model/gamedata/Common.java | 45 +++-- .../atcontentstudio/model/gamedata/Item.java | 55 +----- .../atcontentstudio/model/gamedata/NPC.java | 183 +----------------- 3 files changed, 35 insertions(+), 248 deletions(-) diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Common.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Common.java index 1488bee..b8ee41e 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Common.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Common.java @@ -41,7 +41,30 @@ public final class Common { //endregion //region write common stuff + public static void writeMinMaxToMap(Map parent, Integer min, Integer max, int defaultValue) { + if (min != null || max != null) { + if (min != null) + parent.put("min", min); + else parent.put("min", defaultValue); + if (max != null) + parent.put("max", max); + else parent.put("max", defaultValue); + } + } + public static void writeMinMaxToMap(Map parent, String key, Integer min, Integer max, int defaultValue) { + if (min != null || max != null) { + Map minMaxMap = new LinkedHashMap(); + parent.put(key, minMaxMap); + writeMinMaxToMap(parent, min, max, defaultValue); + } + } + public static void writeDescriptionToMap(Map parent, String description) { + if (description != null) parent.put("description", description); + } + public static void writeIconToMap(Map parent, String icon_id) { + if (icon_id != null) parent.put("iconID", icon_id); + } public static void writeHitReceivedEffectToMap(Map parent, HitReceivedEffect effect) { if(effect != null){ writeHitEffectToMap(parent, effect); @@ -81,27 +104,9 @@ public final class Common { } public static void writeBasicEffectObjectToMap(BasicEffect effect, Map parent, String keyHP, String keyAP) { - if (effect.hp_boost_min != null || effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - parent.put(keyHP, hpJson); - if (effect.hp_boost_min != null) - hpJson.put("min", effect.hp_boost_min); - else hpJson.put("min", 0); - if (effect.hp_boost_max != null) - hpJson.put("max", effect.hp_boost_max); - else hpJson.put("max", 0); - } + writeMinMaxToMap(parent, keyHP, effect.hp_boost_min, effect.hp_boost_max, 0); - if (effect.ap_boost_min != null || effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - parent.put(keyAP, apJson); - if (effect.ap_boost_min != null) - apJson.put("min", effect.ap_boost_min); - else apJson.put("min", 0); - if (effect.ap_boost_max != null) - apJson.put("max", effect.ap_boost_max); - else apJson.put("max", 0); - } + writeMinMaxToMap(parent, keyAP, effect.ap_boost_min, effect.ap_boost_max, 0); } public static void writeTimedActorConditionEffectObjectToMap(List list, Map parent, String key) { diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java index 22d3f1d..5f44f59 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java @@ -353,7 +353,7 @@ public class Item extends JSONElement { public Map toJson() { Map itemJson = new LinkedHashMap(); itemJson.put("id", this.id); - if (this.icon_id != null) itemJson.put("iconID", this.icon_id); + writeIconToMap(itemJson, this.icon_id); if (this.name != null) itemJson.put("name", this.name); if (this.display_type != null) itemJson.put("displaytype", this.display_type.toString()); @@ -364,20 +364,11 @@ public class Item extends JSONElement { } else if (this.category_id != null) { itemJson.put("category", this.category_id); } - if (this.description != null) itemJson.put("description", this.description); + writeDescriptionToMap(itemJson, this.description); if (this.equip_effect != null) { Map equipEffectJson = new LinkedHashMap(); itemJson.put("equipEffect", equipEffectJson); - if (this.equip_effect.damage_boost_min != null || this.equip_effect.damage_boost_max != null) { - Map damageJson = new LinkedHashMap(); - equipEffectJson.put("increaseAttackDamage", damageJson); - if (this.equip_effect.damage_boost_min != null) - damageJson.put("min", this.equip_effect.damage_boost_min); - else damageJson.put("min", 0); - if (this.equip_effect.damage_boost_max != null) - damageJson.put("max", this.equip_effect.damage_boost_max); - else damageJson.put("max", 0); - } + writeMinMaxToMap(equipEffectJson, "increaseAttackDamage", this.equip_effect.damage_boost_min, this.equip_effect.damage_boost_max, 0); if (this.equip_effect.max_hp_boost != null) equipEffectJson.put("increaseMaxHP", this.equip_effect.max_hp_boost); if (this.equip_effect.max_ap_boost != null) @@ -412,45 +403,9 @@ public class Item extends JSONElement { } } } - if (this.hit_effect != null) { - Map hitEffectJson = new LinkedHashMap(); - itemJson.put("hitEffect", hitEffectJson); - if (this.hit_effect.hp_boost_min != null || this.hit_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentHP", hpJson); - if (this.hit_effect.hp_boost_min != null) hpJson.put("min", this.hit_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_effect.hp_boost_max != null) hpJson.put("max", this.hit_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_effect.ap_boost_min != null || this.hit_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentAP", apJson); - if (this.hit_effect.ap_boost_min != null) apJson.put("min", this.hit_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_effect.ap_boost_max != null) apJson.put("max", this.hit_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - hitEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedActorConditionEffect condition : this.hit_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - writeTimedConditionEffectToMap(condition, conditionJson); - } - } - if (this.hit_effect.conditions_target != null) { - List conditionsTargetJson = new ArrayList(); - hitEffectJson.put("conditionsTarget", conditionsTargetJson); - for (TimedActorConditionEffect condition : this.hit_effect.conditions_target) { - Map conditionJson = new LinkedHashMap(); - conditionsTargetJson.add(conditionJson); - writeTimedConditionEffectToMap(condition, conditionJson); - } - } - } + writeHitEffectToMap(itemJson, this.hit_effect, "hitEffect"); writeHitReceivedEffectToMap(itemJson, this.hit_received_effect, "hitReceivedEffect"); + String key; if (this.category != null && this.category.action_type != null && this.category.action_type == ItemCategory.ActionType.equip) { key = "killEffect"; diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java index f63598b..4d9e424 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java @@ -330,21 +330,14 @@ public class NPC extends JSONElement { Map npcJson = new LinkedHashMap(); npcJson.put("id", this.id); if (this.name != null) npcJson.put("name", this.name); - if (this.icon_id != null) npcJson.put("iconID", this.icon_id); + writeIconToMap(npcJson, this.icon_id); if (this.max_hp != null) npcJson.put("maxHP", this.max_hp); if (this.max_ap != null) npcJson.put("maxAP", this.max_ap); if (this.move_cost != null) npcJson.put("moveCost", this.move_cost); if (this.unique != null) npcJson.put("unique", this.unique); if (this.monster_class != null) npcJson.put("monsterClass", this.monster_class.toString()); if (this.movement_type != null) npcJson.put("movementAggressionType", this.movement_type.toString()); - if (this.attack_damage_min != null || this.attack_damage_max != null) { - Map adJson = new LinkedHashMap(); - npcJson.put("attackDamage", adJson); - if (this.attack_damage_min != null) adJson.put("min", this.attack_damage_min); - else adJson.put("min", 0); - if (this.attack_damage_max != null) adJson.put("max", this.attack_damage_max); - else adJson.put("max", 0); - } + writeMinMaxToMap(npcJson, "attackDamage", this.attack_damage_min, attack_damage_max, 0); if (this.spawngroup_id != null) npcJson.put("spawnGroup", this.spawngroup_id); if (this.faction_id != null) npcJson.put("faction", this.faction_id); if (this.dialogue != null) { @@ -363,175 +356,9 @@ public class NPC extends JSONElement { if (this.critical_multiplier != null) npcJson.put("criticalMultiplier", this.critical_multiplier); if (this.block_chance != null) npcJson.put("blockChance", this.block_chance); if (this.damage_resistance != null) npcJson.put("damageResistance", this.damage_resistance); - if (this.hit_effect != null) { - Map hitEffectJson = new LinkedHashMap(); - npcJson.put("hitEffect", hitEffectJson); - if (this.hit_effect.hp_boost_min != null || this.hit_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentHP", hpJson); - if (this.hit_effect.hp_boost_min != null) hpJson.put("min", this.hit_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_effect.hp_boost_max != null) hpJson.put("max", this.hit_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_effect.ap_boost_min != null || this.hit_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentAP", apJson); - if (this.hit_effect.ap_boost_min != null) apJson.put("min", this.hit_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_effect.ap_boost_max != null) apJson.put("max", this.hit_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - hitEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedActorConditionEffect condition : this.hit_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) - conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - if (this.hit_effect.conditions_target != null) { - List conditionsTargetJson = new ArrayList(); - hitEffectJson.put("conditionsTarget", conditionsTargetJson); - for (TimedActorConditionEffect condition : this.hit_effect.conditions_target) { - Map conditionJson = new LinkedHashMap(); - conditionsTargetJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) - conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } - if (this.hit_received_effect != null) { - Map hitReceivedEffectJson = new LinkedHashMap(); - npcJson.put("hitReceivedEffect", hitReceivedEffectJson); - if (this.hit_received_effect.hp_boost_min != null || this.hit_received_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseCurrentHP", hpJson); - if (this.hit_received_effect.hp_boost_min != null) - hpJson.put("min", this.hit_received_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_received_effect.hp_boost_max != null) - hpJson.put("max", this.hit_received_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_received_effect.ap_boost_min != null || this.hit_received_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseCurrentAP", apJson); - if (this.hit_received_effect.ap_boost_min != null) - apJson.put("min", this.hit_received_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_received_effect.ap_boost_max != null) - apJson.put("max", this.hit_received_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_received_effect.target.hp_boost_min != null || this.hit_received_effect.target.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseAttackerCurrentHP", hpJson); - if (this.hit_received_effect.target.hp_boost_min != null) - hpJson.put("min", this.hit_received_effect.target.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_received_effect.target.hp_boost_max != null) - hpJson.put("max", this.hit_received_effect.target.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_received_effect.target.ap_boost_min != null || this.hit_received_effect.target.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseAttackerCurrentAP", apJson); - if (this.hit_received_effect.target.ap_boost_min != null) - apJson.put("min", this.hit_received_effect.target.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_received_effect.target.ap_boost_max != null) - apJson.put("max", this.hit_received_effect.target.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_received_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - hitReceivedEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedActorConditionEffect condition : this.hit_received_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) - conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - if (this.hit_received_effect.conditions_target != null) { - List conditionsTargetJson = new ArrayList(); - hitReceivedEffectJson.put("conditionsTarget", conditionsTargetJson); - for (TimedActorConditionEffect condition : this.hit_received_effect.conditions_target) { - Map conditionJson = new LinkedHashMap(); - conditionsTargetJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) - conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } - if (this.death_effect != null) { - Map deathEffectJson = new LinkedHashMap(); - npcJson.put("deathEffect", deathEffectJson); - if (this.death_effect.hp_boost_min != null || this.death_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - deathEffectJson.put("increaseCurrentHP", hpJson); - if (this.death_effect.hp_boost_min != null) hpJson.put("min", this.death_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.death_effect.hp_boost_max != null) hpJson.put("max", this.death_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.death_effect.ap_boost_min != null || this.death_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - deathEffectJson.put("increaseCurrentAP", apJson); - if (this.death_effect.ap_boost_min != null) apJson.put("min", this.death_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.death_effect.ap_boost_max != null) apJson.put("max", this.death_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.death_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - deathEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedActorConditionEffect condition : this.death_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) - conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } + writeHitEffectToMap(npcJson, this.hit_effect, "hitEffect"); + writeHitReceivedEffectToMap(npcJson, this.hit_received_effect, "hitReceivedEffect"); + writeDeathEffectToMap(npcJson, this.death_effect, "deathEffect"); return npcJson; }