From 0a1fef41981b30f5d0c8be891b1f253dc9eede76 Mon Sep 17 00:00:00 2001 From: OMGeeky <> Date: Mon, 16 Jun 2025 08:23:43 +0200 Subject: [PATCH] refactor: simplify link method by extracting parsing/linking logic --- .../model/GameDataElement.java | 28 ++++++++++++++++++- .../model/gamedata/ActorCondition.java | 11 ++------ .../model/gamedata/Dialogue.java | 11 ++------ .../model/gamedata/Droplist.java | 11 ++------ .../atcontentstudio/model/gamedata/Item.java | 11 ++------ .../model/gamedata/ItemCategory.java | 13 ++------- .../model/gamedata/JSONElement.java | 3 +- .../atcontentstudio/model/gamedata/NPC.java | 11 ++------ .../atcontentstudio/model/gamedata/Quest.java | 13 ++------- .../model/gamedata/QuestStage.java | 13 ++------- 10 files changed, 47 insertions(+), 78 deletions(-) diff --git a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java index a5b1438..abf57ef 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java @@ -189,5 +189,31 @@ public abstract class GameDataElement implements ProjectTreeNode, Serializable { public abstract void save(); public abstract List attemptSave(); - + + /** + * Checks if the current state indicates that parsing/linking should be skipped. + * @return true if the operation should be skipped, false otherwise + */ + protected boolean shouldSkipParseOrLink() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return true; + } + if (this.state == State.linked) { + //Already linked. + return true; + } + return false; + } + + /** + * Ensures the element is parsed if needed based on its current state. + */ + protected void ensureParseIfNeeded() { + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } + } + } diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java index d07d701..d023387 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java @@ -224,17 +224,10 @@ public class ActorCondition extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. + if (shouldSkipParseOrLink()) { return; } + ensureParseIfNeeded(); if (this.icon_id != null) { String spritesheetId = this.icon_id.split(":")[0]; if (getProject().getSpritesheet(spritesheetId) == null) { diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java index bfe2611..4ad477c 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java @@ -206,17 +206,10 @@ public class Dialogue extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. + if (shouldSkipParseOrLink()) { return; } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking dialogue "+id+". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java index 7eaa377..9afa5fb 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java @@ -129,17 +129,10 @@ public class Droplist extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. + if (shouldSkipParseOrLink()) { return; } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking droplist "+id+". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java index 4f58823..1684174 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java @@ -206,17 +206,10 @@ public class Item extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. + if (shouldSkipParseOrLink()) { return; } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking item "+id+". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java index f6365fa..7d539df 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java @@ -171,18 +171,11 @@ public class ItemCategory extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. + if (shouldSkipParseOrLink()) { return; } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - + ensureParseIfNeeded(); + //Nothing to link to :D this.state = State.linked; } diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java index acb780f..95c1415 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java @@ -26,8 +26,7 @@ public abstract class JSONElement extends GameDataElement { @SuppressWarnings("rawtypes") public void parse() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. + if (shouldSkipParseOrLink()) { return; } JSONParser parser = new JSONParser(); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java index c1fb6cf..b4ef553 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java @@ -208,17 +208,10 @@ public class NPC extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. + if (shouldSkipParseOrLink()) { return; } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking item "+id+". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java index 8c7b650..bcf03c6 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java @@ -113,18 +113,11 @@ public class Quest extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. + if (shouldSkipParseOrLink()) { return; } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - + ensureParseIfNeeded(); + for (QuestStage stage : stages) { stage.link(); } diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java index 55edbfa..086a2e8 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java @@ -59,18 +59,11 @@ public class QuestStage extends JSONElement { @Override public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. + if (shouldSkipParseOrLink()) { return; } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - + ensureParseIfNeeded(); + //Nothing to link to :D this.state = State.linked; }