Skip to content

Commit

Permalink
Fix integer overflow in reporting max Mana (#49)
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
ramidzkh authored Sep 7, 2023
1 parent eba3d0c commit 4e4b342
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/main/java/appbot/ae2/ManaGenericStackInvStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,22 @@ public boolean areIncomingTranfersDone() {
}

public int insert(int amount, Actionable actionable) {
var inserted = 0;
var inserted = 0L;

for (var i = 0; i < inv.size() && inserted < amount; ++i) {
inserted += (int) inv.insert(i, ManaKey.KEY, amount - inserted, actionable);
for (var i = 0; i < inv.size() && inserted < amount; i++) {
inserted += inv.insert(i, ManaKey.KEY, amount - inserted, actionable);
}

return inserted;
return (int) inserted;
}

private int extract(int amount, Actionable actionable) {
var extracted = 0;
var extracted = 0L;

for (var i = 0; i < inv.size() && extracted < amount; ++i) {
extracted += (int) inv.extract(i, ManaKey.KEY, amount - extracted, actionable);
for (var i = 0; i < inv.size() && extracted < amount; i++) {
extracted += inv.extract(i, ManaKey.KEY, amount - extracted, actionable);
}

return extracted;
return (int) extracted;
}
}
5 changes: 3 additions & 2 deletions src/main/java/appbot/ae2/ManaStorageImportStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public boolean transfer(StackTransferContext context) {
receiver.receiveMana(-amount);
}

var inserted = inv.getInventory().insert(ManaKey.KEY, amount, Actionable.MODULATE, context.getActionSource());
var inserted = (int) inv.getInventory().insert(ManaKey.KEY, amount, Actionable.MODULATE,
context.getActionSource());

if (inserted < amount) {
var leftover = amount - inserted;
var backfill = (int) Math.min(leftover,
var backfill = Math.min(leftover,
ManaHelper.getCapacity(receiver) - receiver.getCurrentMana());

if (backfill > 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/appbot/block/FluixPoolBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.EnumSet;
import java.util.List;

import com.google.common.primitives.Ints;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -131,12 +133,12 @@ public int getMaxMana() {

var oldMana = mana.getMana();
var oldManaCap = super.getMaxMana();
int manaCap;
long manaCap;

if (getMainNode().isActive()) {
var storage = grid.getStorageService().getInventory();
mana.setMana((int) storage.extract(ManaKey.KEY, Integer.MAX_VALUE, Actionable.SIMULATE, actionSource));
manaCap = (int) (storage.extract(ManaKey.KEY, Integer.MAX_VALUE, Actionable.SIMULATE, actionSource)
manaCap = (storage.extract(ManaKey.KEY, Integer.MAX_VALUE, Actionable.SIMULATE, actionSource)
+ storage.insert(ManaKey.KEY, Integer.MAX_VALUE, Actionable.SIMULATE, actionSource));
} else {
mana.setMana(0);
Expand All @@ -148,7 +150,7 @@ public int getMaxMana() {
markDispatchable();
}

return manaCap;
return Ints.saturatedCast(manaCap);
}

@Override
Expand Down

0 comments on commit 4e4b342

Please sign in to comment.