Skip to content

Commit

Permalink
BR#3356 - Fixes an issue where moving goods into/out from a colony me…
Browse files Browse the repository at this point in the history
…sses up the production. The most noticable effect of this bug was colonists starving to death while a new colonist was born. The issue was caused by the ProductionCache being out-of-sync since the GoodsContainer was directly called instead of using the accessor methods through GoodsLocation. For now, the problem is fixed by just calling the invalidateCache-method if the parent is a GoodsLocation.
  • Loading branch information
stiangre committed Jun 25, 2024
1 parent 139e95f commit 66c1fa5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/net/sf/freecol/common/model/GoodsContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ public boolean addGoods(GoodsType type, int amount) {
this.storedGoods.put(type, newAmount);
}
}

invalidateCache();

return true;
}

Expand Down Expand Up @@ -291,9 +294,18 @@ public Goods removeGoods(GoodsType type, int amount) {
this.storedGoods.remove(type);
}
}

invalidateCache();

return removedGoods;
}

private void invalidateCache() {
if (parent != null && (parent instanceof GoodsLocation)) {
((GoodsLocation) parent).invalidateCache();
}
}

/**
* Set the amount of goods in this container.
*
Expand All @@ -310,6 +322,7 @@ public void setAmount(GoodsType goodsType, int newAmount) {
this.storedGoods.put(goodsType, newAmount);
}
}
invalidateCache();
}

/**
Expand All @@ -319,6 +332,7 @@ public void removeAll() {
synchronized (this.storedGoods) {
this.storedGoods.clear();
}
invalidateCache();
}

/**
Expand Down Expand Up @@ -353,6 +367,7 @@ public void removeAbove(final int newAmount) {
forEachMapEntry(this.storedGoods, hiPred,
e -> this.storedGoods.put(e.getKey(), newAmount));
}
invalidateCache();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/net/sf/freecol/server/model/ServerColony.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ public void csNewTurn(Random random, LogBuilder lb, ChangeSet cs) {
boolean newUnitBorn = false;
GoodsContainer container = getGoodsContainer();
container.saveState();

invalidateCache();

// Check for learning by experience
for (WorkLocation wl : getCurrentWorkLocationsList()) {
Expand Down

0 comments on commit 66c1fa5

Please sign in to comment.