Skip to content

Commit

Permalink
Do not form CraftingCPU if storage overflow detected (#443)
Browse files Browse the repository at this point in the history
Only one `SingularityCraftingStorage` per CPU allowed

Closes: GTNewHorizons/GT-New-Horizons-Modpack#15138
  • Loading branch information
Laiff authored Dec 19, 2023
1 parent 14110e6 commit 6723c4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public IAECluster createCluster(final World w, final WorldCoord min, final World

@Override
public boolean verifyInternalStructure(final World w, final WorldCoord min, final WorldCoord max) {
boolean storage = false;
boolean isValid = true;
boolean hasStorage = false;
long totalBytes = 0L;

for (int x = min.x; x <= max.x; x++) {
for (int y = min.y; y <= max.y; y++) {
Expand All @@ -66,14 +68,20 @@ public boolean verifyInternalStructure(final World w, final WorldCoord min, fina
return false;
}

if (!storage && te instanceof TileCraftingTile) {
storage = ((TileCraftingTile) te).getStorageBytes() > 0;
if (isValid && te instanceof TileCraftingTile craftingTile) {
long storageBytes = craftingTile.getStorageBytes();
hasStorage |= storageBytes > 0;
if (Long.MAX_VALUE - storageBytes >= totalBytes) {
totalBytes += storageBytes;
} else {
isValid = false;
}
}
}
}
}

return storage;
return hasStorage && isValid;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,15 @@ void addTile(final TileCraftingTile te) {
this.tiles.push(te);

if (te.isStorage()) {
this.availableStorage += te.getStorageBytes();
this.storage.add(te);
long additionalStorage = te.getStorageBytes();
if (Long.MAX_VALUE - additionalStorage >= this.availableStorage) {
// Safe to add as it does not cause overflow
this.availableStorage += additionalStorage;
this.storage.add(te);
} else {
// Prevent form CPU if storage overflowed
this.tiles.remove(te);
}
} else if (te.isStatus()) {
this.status.add((TileCraftingMonitorTile) te);
} else if (te.isAccelerator()) {
Expand Down

0 comments on commit 6723c4c

Please sign in to comment.