Skip to content

Commit

Permalink
When loading saves from past version, fix up null player references. (#…
Browse files Browse the repository at this point in the history
…10518)

* When loading saves from past version, fix up null player references.

* Do it in a different place.

* Check for null data and use a method so it can be marked for removal.
  • Loading branch information
asvitkine authored May 27, 2022
1 parent 70ae733 commit 13308d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ public void postDeSerialize() {
territoryListeners = new CopyOnWriteArrayList<>();
dataChangeListeners = new CopyOnWriteArrayList<>();
delegates = new HashMap<>();
fixUpNullPlayers();
}

@RemoveOnNextMajorRelease
private void fixUpNullPlayers() {
// Old save games may have territories and units owned by a different null player object that
// has a null data. Update them to the new null player, so that code can rely on getData()
// not being null.
for (Territory t : getMap().getTerritories()) {
if (t.getOwner().getData() == null) {
t.setOwner(playerList.getNullPlayer());
}
}
for (Unit u : getUnits()) {
if (u.getOwner().getData() == null) {
u.setOwner(playerList.getNullPlayer());
}
}
}

public interface Unlocker extends Closeable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean isEmpty() {

/** Returns a Set of all players who have units in this collection. */
public Set<GamePlayer> getPlayersWithUnits() {
// note nulls are handled by PlayerId.NULL_PLAYERID
// note nulls are handled by PlayerList.getNullPlayer()
return units.stream().map(Unit::getOwner).collect(Collectors.toSet());
}

Expand Down

0 comments on commit 13308d3

Please sign in to comment.