From 3f19ca959bd342af2667343616802ab97c9b3749 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Tue, 24 Jun 2025 16:51:29 +0200 Subject: [PATCH] improve code a bit --- .../ui/gamedataeditors/ItemEditor.java | 60 +++++++++---------- .../ui/gamedataeditors/NPCEditor.java | 44 +++++++------- 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/ItemEditor.java b/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/ItemEditor.java index 6610e8b..d334664 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/ItemEditor.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/ItemEditor.java @@ -18,6 +18,9 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; +import java.util.Objects; + +import static com.gpl.rpg.atcontentstudio.model.gamedata.Common.*; public class ItemEditor extends JSONElementEditor { @@ -30,7 +33,7 @@ public class ItemEditor extends JSONElementEditor { private static final String useLabel = "Effect on use: "; - private Common.ActorConditionEffect selectedEquipEffectCondition; + private ActorConditionEffect selectedEquipEffectCondition; private JButton itemIcon; @@ -68,9 +71,9 @@ public class ItemEditor extends JSONElementEditor { private JRadioButton equipConditionImmunity; private JSpinner equipConditionMagnitude; - private CommonEditor.HitEffectPane hitEffectPane = new CommonEditor.HitEffectPane("Effect on every hit: ", Common.TimedActorConditionEffect::new, this, null, null); - private CommonEditor.DeathEffectPane killEffectPane = new CommonEditor.DeathEffectPane(killLabel, Common.TimedActorConditionEffect::new, this, null); - private CommonEditor.HitRecievedEffectPane hitReceivedEffectPane = new CommonEditor.HitRecievedEffectPane("Effect on every hit received: ", Common.TimedActorConditionEffect::new, this, null, null); + private CommonEditor.HitEffectPane hitEffectPane = new CommonEditor.HitEffectPane("Effect on every hit: ", TimedActorConditionEffect::new, this, null, null); + private CommonEditor.DeathEffectPane killEffectPane = new CommonEditor.DeathEffectPane(killLabel, TimedActorConditionEffect::new, this, null); + private CommonEditor.HitRecievedEffectPane hitReceivedEffectPane = new CommonEditor.HitRecievedEffectPane("Effect on every hit received: ", TimedActorConditionEffect::new, this, null, null); public ItemEditor(Item item) { super(item, item.getDesc(), item.getIcon()); @@ -125,8 +128,8 @@ public class ItemEditor extends JSONElementEditor { String titleEquipConditions = "Actor Conditions applied when equipped: "; equipConditionsModel = new EquipConditionsListModel(equipEffect); CommonEditor.ConditionsCellRenderer cellRendererEquipConditions = new CommonEditor.ConditionsCellRenderer(); - BasicLambdaWithArg selectedSetEquipConditions = (value)->selectedEquipEffectCondition = value; - BasicLambdaWithReturn selectedGetEquipConditions = ()->selectedEquipEffectCondition ; + BasicLambdaWithArg selectedSetEquipConditions = (value)->selectedEquipEffectCondition = value; + BasicLambdaWithReturn selectedGetEquipConditions = ()->selectedEquipEffectCondition ; BasicLambda selectedResetEquipConditions = ()->selectedEquipEffectCondition = null; BasicLambdaWithArg updatePaneEquipConditions = (editorPane) -> updateEquipConditionEditorPane(editorPane, selectedEquipEffectCondition, listener); var resultEquipConditions = UiUtils.getCollapsibleItemList(listener, @@ -137,7 +140,7 @@ public class ItemEditor extends JSONElementEditor { (x) -> {}, updatePaneEquipConditions, item.writable, - Common.ActorConditionEffect::new, + ActorConditionEffect::new, cellRendererEquipConditions, titleEquipConditions, (x) -> null); @@ -153,31 +156,24 @@ public class ItemEditor extends JSONElementEditor { equipEffectPane.collapse(); } - Common.HitEffect hitEffect; - if (item.hit_effect == null) { - hitEffect = new Common.HitEffect(); - } else { - hitEffect = item.hit_effect; - } - hitEffectPane.createHitEffectPaneContent(listener, item.writable, hitEffect, new CommonEditor.SourceTimedConditionsListModel(hitEffect), new CommonEditor.TargetTimedConditionsListModel(hitEffect)); + HitEffect hitEffect = Objects.requireNonNullElseGet(item.hit_effect, HitEffect::new); + hitEffectPane.createHitEffectPaneContent(listener, item.writable, hitEffect, + new CommonEditor.SourceTimedConditionsListModel(hitEffect), + new CommonEditor.TargetTimedConditionsListModel(hitEffect)); pane.add(hitEffectPane.effectPane, JideBoxLayout.FIX); - Common.DeathEffect killEffect; - if (item.kill_effect == null) { - killEffect = new Common.DeathEffect(); - } else { - killEffect = item.kill_effect; - } - killEffectPane.createDeathEffectPaneContent(listener, item.writable, killEffect, new CommonEditor.SourceTimedConditionsListModel(killEffect)); + DeathEffect killEffect = Objects.requireNonNullElseGet(item.kill_effect, DeathEffect::new); + killEffectPane.createDeathEffectPaneContent(listener, item.writable, killEffect, + new CommonEditor.SourceTimedConditionsListModel(killEffect)); pane.add(killEffectPane.effectPane, JideBoxLayout.FIX); - Common.HitReceivedEffect hitReceivedEffect; - if (item.hit_received_effect == null) { - hitReceivedEffect = new Common.HitReceivedEffect(); - } else { - hitReceivedEffect = item.hit_received_effect; - } - hitReceivedEffectPane.createHitReceivedEffectPaneContent(listener, item.writable, hitReceivedEffect, new CommonEditor.SourceTimedConditionsListModel(hitReceivedEffect), new CommonEditor.TargetTimedConditionsListModel(hitReceivedEffect)); + HitReceivedEffect hitReceivedEffect = Objects.requireNonNullElseGet(item.hit_received_effect, + HitReceivedEffect::new); + hitReceivedEffectPane.createHitReceivedEffectPaneContent(listener, item.writable, hitReceivedEffect, + new CommonEditor.SourceTimedConditionsListModel( + hitReceivedEffect), + new CommonEditor.TargetTimedConditionsListModel( + hitReceivedEffect)); pane.add(killEffectPane.effectPane, JideBoxLayout.FIX); @@ -203,7 +199,7 @@ public class ItemEditor extends JSONElementEditor { } - public void updateEquipConditionEditorPane(JPanel pane, Common.ActorConditionEffect condition, final FieldUpdateListener listener) { + public void updateEquipConditionEditorPane(JPanel pane, ActorConditionEffect condition, final FieldUpdateListener listener) { pane.removeAll(); if (equipConditionBox != null) { removeElementListener(equipConditionBox); @@ -251,18 +247,18 @@ public class ItemEditor extends JSONElementEditor { pane.repaint(); } - public static class EquipConditionsListModel extends OrderedListenerListModel { + public static class EquipConditionsListModel extends OrderedListenerListModel { public EquipConditionsListModel(Item.EquipEffect equipEffect) { super(equipEffect); } @Override - protected List getItems() { + protected List getItems() { return source.conditions; } @Override - protected void setItems(List conditions) { + protected void setItems(List conditions) { source.conditions = conditions; } } diff --git a/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/NPCEditor.java b/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/NPCEditor.java index 1f74ec5..724a3e1 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/NPCEditor.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/gamedataeditors/NPCEditor.java @@ -16,6 +16,9 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; +import java.util.Objects; + +import static com.gpl.rpg.atcontentstudio.model.gamedata.Common.*; public class NPCEditor extends JSONElementEditor { @@ -53,9 +56,9 @@ public class NPCEditor extends JSONElementEditor { private JSpinner blockChance; private JSpinner dmgRes; - private CommonEditor.HitEffectPane hitEffectPane = new CommonEditor.HitEffectPane("Effect on every hit: ", Common.TimedActorConditionEffect::new, this, null, null); - private CommonEditor.HitRecievedEffectPane hitReceivedEffectPane = new CommonEditor.HitRecievedEffectPane("Effect on every hit received: ", Common.TimedActorConditionEffect::new, this, "NPC", "Attacker"); - private CommonEditor.DeathEffectPane deathEffectPane = new CommonEditor.DeathEffectPane("Effect when killed: ", Common.TimedActorConditionEffect::new, this, "Killer"); + private CommonEditor.HitEffectPane hitEffectPane = new CommonEditor.HitEffectPane("Effect on every hit: ", TimedActorConditionEffect::new, this, null, null); + private CommonEditor.HitRecievedEffectPane hitReceivedEffectPane = new CommonEditor.HitRecievedEffectPane("Effect on every hit received: ", TimedActorConditionEffect::new, this, "NPC", "Attacker"); + private CommonEditor.DeathEffectPane deathEffectPane = new CommonEditor.DeathEffectPane("Effect when killed: ", TimedActorConditionEffect::new, this, "Killer"); private JPanel dialogueGraphPane; private DialogueGraphView dialogueGraphView; @@ -143,31 +146,24 @@ public class NPCEditor extends JSONElementEditor { blockChance = addIntegerField(combatTraitPane, "Block chance: ", npc.block_chance, false, npc.writable, listener); dmgRes = addIntegerField(combatTraitPane, "Damage resistance: ", npc.damage_resistance, false, npc.writable, listener); - Common.HitEffect hitEffect; - if (npc.hit_effect == null) { - hitEffect = new Common.HitEffect(); - } else { - hitEffect = npc.hit_effect; - } - hitEffectPane.createHitEffectPaneContent(listener, npc.writable, hitEffect, new CommonEditor.SourceTimedConditionsListModel(hitEffect), new CommonEditor.TargetTimedConditionsListModel(hitEffect)); + HitEffect hitEffect = Objects.requireNonNullElseGet(npc.hit_effect, HitEffect::new); + hitEffectPane.createHitEffectPaneContent(listener, npc.writable, hitEffect, + new CommonEditor.SourceTimedConditionsListModel(hitEffect), + new CommonEditor.TargetTimedConditionsListModel(hitEffect)); combatTraitPane.add(hitEffectPane.effectPane, JideBoxLayout.FIX); - Common.HitReceivedEffect hitReceivedEffect; - if (npc.hit_received_effect == null) { - hitReceivedEffect = new Common.HitReceivedEffect(); - } else { - hitReceivedEffect = npc.hit_received_effect; - } - hitReceivedEffectPane.createHitReceivedEffectPaneContent(listener, npc.writable, hitReceivedEffect, new CommonEditor.SourceTimedConditionsListModel(hitReceivedEffect), new CommonEditor.TargetTimedConditionsListModel(hitReceivedEffect)); + HitReceivedEffect hitReceivedEffect = Objects.requireNonNullElseGet(npc.hit_received_effect, + HitReceivedEffect::new); + hitReceivedEffectPane.createHitReceivedEffectPaneContent(listener, npc.writable, hitReceivedEffect, + new CommonEditor.SourceTimedConditionsListModel( + hitReceivedEffect), + new CommonEditor.TargetTimedConditionsListModel( + hitReceivedEffect)); combatTraitPane.add(hitReceivedEffectPane.effectPane, JideBoxLayout.FIX); - Common.DeathEffect deathEffect; - if (npc.death_effect == null) { - deathEffect = new Common.DeathEffect(); - } else { - deathEffect = npc.death_effect; - } - deathEffectPane.createDeathEffectPaneContent(listener, npc.writable, deathEffect, new CommonEditor.SourceTimedConditionsListModel(deathEffect)); + DeathEffect deathEffect = Objects.requireNonNullElseGet(npc.death_effect, DeathEffect::new); + deathEffectPane.createDeathEffectPaneContent(listener, npc.writable, deathEffect, + new CommonEditor.SourceTimedConditionsListModel(deathEffect)); combatTraitPane.add(deathEffectPane.effectPane, JideBoxLayout.FIX); pane.add(combatTraitPane, JideBoxLayout.FIX);