From 4e4b34231b085f6ba7eb49ac6b190017086d7988 Mon Sep 17 00:00:00 2001 From: ramidzkh Date: Thu, 7 Sep 2023 13:30:06 +1000 Subject: [PATCH] Fix integer overflow in reporting max Mana (#49) Closes #44 --- .../appbot/ae2/ManaGenericStackInvStorage.java | 16 ++++++++-------- .../appbot/ae2/ManaStorageImportStrategy.java | 5 +++-- .../java/appbot/block/FluixPoolBlockEntity.java | 8 +++++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/appbot/ae2/ManaGenericStackInvStorage.java b/src/main/java/appbot/ae2/ManaGenericStackInvStorage.java index 69648ac..8214c0a 100644 --- a/src/main/java/appbot/ae2/ManaGenericStackInvStorage.java +++ b/src/main/java/appbot/ae2/ManaGenericStackInvStorage.java @@ -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; } } diff --git a/src/main/java/appbot/ae2/ManaStorageImportStrategy.java b/src/main/java/appbot/ae2/ManaStorageImportStrategy.java index c78cf38..be03338 100644 --- a/src/main/java/appbot/ae2/ManaStorageImportStrategy.java +++ b/src/main/java/appbot/ae2/ManaStorageImportStrategy.java @@ -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) { diff --git a/src/main/java/appbot/block/FluixPoolBlockEntity.java b/src/main/java/appbot/block/FluixPoolBlockEntity.java index 3d3899f..6e8ada6 100644 --- a/src/main/java/appbot/block/FluixPoolBlockEntity.java +++ b/src/main/java/appbot/block/FluixPoolBlockEntity.java @@ -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; @@ -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); @@ -148,7 +150,7 @@ public int getMaxMana() { markDispatchable(); } - return manaCap; + return Ints.saturatedCast(manaCap); } @Override