Some refactoring. Replaced all "listeners" list by instances of

CopyOnWriteArrayList, to allow listeners to unregister themselves due to
an event while preventing ConcurrentModificationExceptions.
Modified all GameDataElement.elementChanged concrete implementation to
remove the backlink from the oldOne.An element pointed by an altered
element will not show the game source element in its backlink list
anymore.
This commit is contained in:
Zukero
2017-03-02 13:53:24 +01:00
parent 2a4cfb0684
commit 940996aa30
27 changed files with 81 additions and 54 deletions

View File

@@ -349,18 +349,21 @@ public class Dialogue extends JSONElement {
@Override
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
if (switch_to_npc == oldOne) {
oldOne.removeBacklink(this);
switch_to_npc = (NPC) newOne;
if (newOne != null) newOne.addBacklink(this);
} else {
if (replies != null) {
for (Reply r : replies) {
if (r.next_phrase == oldOne) {
oldOne.removeBacklink(this);
r.next_phrase = (Dialogue) newOne;
if (newOne != null) newOne.addBacklink(this);
}
if (r.requirements != null) {
for (Requirement req : r.requirements) {
if (req.required_obj == oldOne) {
oldOne.removeBacklink(this);
req.required_obj = newOne;
if (newOne != null) newOne.addBacklink(this);
}
@@ -371,6 +374,7 @@ public class Dialogue extends JSONElement {
if (rewards != null) {
for (Reward r : rewards) {
if (r.reward_obj == oldOne) {
oldOne.removeBacklink(this);
r.reward_obj = newOne;
if (newOne != null) newOne.addBacklink(this);
}

View File

@@ -194,6 +194,7 @@ public class Droplist extends JSONElement {
if (dropped_items != null) {
for (DroppedItem di : dropped_items) {
if (di.item == oldOne) {
oldOne.removeBacklink(this);
di.item = (Item) newOne;
if (newOne != null) newOne.addBacklink(this);
}

View File

@@ -451,12 +451,14 @@ public class Item extends JSONElement {
@Override
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
if (this.category == oldOne) {
oldOne.removeBacklink(this);
this.category = (ItemCategory) newOne;
if (newOne != null) newOne.addBacklink(this);
} else {
if (this.equip_effect != null && this.equip_effect.conditions != null) {
for (ConditionEffect c : this.equip_effect.conditions) {
if (c.condition == oldOne) {
oldOne.removeBacklink(this);
c.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}
@@ -465,6 +467,7 @@ public class Item extends JSONElement {
if (this.hit_effect != null && this.hit_effect.conditions_source != null) {
for (TimedConditionEffect c : this.hit_effect.conditions_source) {
if (c.condition == oldOne) {
oldOne.removeBacklink(this);
c.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}
@@ -473,6 +476,7 @@ public class Item extends JSONElement {
if (this.hit_effect != null && this.hit_effect.conditions_target != null) {
for (TimedConditionEffect c : this.hit_effect.conditions_target) {
if (c.condition == oldOne) {
oldOne.removeBacklink(this);
c.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}
@@ -482,6 +486,7 @@ public class Item extends JSONElement {
if (this.kill_effect != null && this.kill_effect.conditions_source != null) {
for (TimedConditionEffect c : this.kill_effect.conditions_source) {
if (c.condition == oldOne) {
oldOne.removeBacklink(this);
c.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}

View File

@@ -357,16 +357,19 @@ public class NPC extends JSONElement {
@Override
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
if (dialogue == oldOne) {
oldOne.removeBacklink(this);
this.dialogue = (Dialogue) newOne;
if (newOne != null) newOne.addBacklink(this);
} else {
if (this.droplist == oldOne) {
oldOne.removeBacklink(this);
this.droplist = (Droplist) newOne;
if (newOne != null) newOne.addBacklink(this);
} else {
if (this.hit_effect != null && this.hit_effect.conditions_source != null) {
for (TimedConditionEffect tce : this.hit_effect.conditions_source) {
if (tce.condition == oldOne) {
oldOne.removeBacklink(this);
tce.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}
@@ -375,6 +378,7 @@ public class NPC extends JSONElement {
if (this.hit_effect != null && this.hit_effect.conditions_target != null) {
for (TimedConditionEffect tce : this.hit_effect.conditions_target) {
if (tce.condition == oldOne) {
oldOne.removeBacklink(this);
tce.condition = (ActorCondition) newOne;
if (newOne != null) newOne.addBacklink(this);
}

View File

@@ -12,6 +12,7 @@ import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.tree.TreeNode;
@@ -439,7 +440,7 @@ public class TMXMap extends GameDataElement {
this.link();
changedOnDisk = false;
for (MapChangedOnDiskListener l : new ArrayList<TMXMap.MapChangedOnDiskListener>(listeners)) {
for (MapChangedOnDiskListener l : listeners) {
l.mapReloaded();
}
}
@@ -449,7 +450,7 @@ public class TMXMap extends GameDataElement {
dismissNextChangeNotif--;
} else {
changedOnDisk = true;
for (MapChangedOnDiskListener l : new ArrayList<TMXMap.MapChangedOnDiskListener>(listeners)) {
for (MapChangedOnDiskListener l : listeners) {
l.mapChanged();
}
}
@@ -460,7 +461,7 @@ public class TMXMap extends GameDataElement {
public void mapReloaded();
}
private List<MapChangedOnDiskListener> listeners = new ArrayList<TMXMap.MapChangedOnDiskListener>();
private List<MapChangedOnDiskListener> listeners = new CopyOnWriteArrayList<TMXMap.MapChangedOnDiskListener>();
public void addMapChangedOnDiskListener(MapChangedOnDiskListener l) {
listeners.add(l);

View File

@@ -108,7 +108,6 @@ public class TMXMapSet implements ProjectTreeNode {
for (WatchEvent<?> event : wk.pollEvents()) {
Path changed = (Path) event.context();
String name = changed.getFileName().toString();
System.out.println("Changed: "+name);
String id = name.substring(0, name.length() - 4);
TMXMap map = getMap(id);
if (map != null) {

View File

@@ -103,7 +103,7 @@ public class WorldmapSegment extends GameDataElement {
@Override
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
oldOne.removeBacklink(this);
newOne.addBacklink(this);
if(newOne != null) newOne.addBacklink(this);
}
@Override