mirror of
https://github.com/OMGeeky/ATCS.git
synced 2025-12-28 15:27:41 +01:00
refactor: simplify link method by extracting parsing/linking logic
This commit is contained in:
@@ -189,5 +189,31 @@ public abstract class GameDataElement implements ProjectTreeNode, Serializable {
|
||||
public abstract void save();
|
||||
|
||||
public abstract List<SaveEvent> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user