Skip to content

Commit 1636b48

Browse files
committed
perform an extra simulation for safety and log if it failed
1 parent 7638e35 commit 1636b48

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/main/java/mekanism/common/inventory/slot/EnergyInventorySlot.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import mekanism.api.energy.IEnergyContainer;
1111
import mekanism.api.energy.IStrictEnergyHandler;
1212
import mekanism.api.recipes.ItemStackToEnergyRecipe;
13+
import mekanism.common.Mekanism;
1314
import mekanism.common.integration.energy.EnergyCompatUtils;
1415
import mekanism.common.inventory.container.slot.ContainerSlotType;
1516
import mekanism.common.inventory.container.slot.SlotOverlay;
@@ -169,13 +170,19 @@ private boolean fillContainerFromItem() {
169170
if (simulatedRemainder < energyInItem) {
170171
//If we were simulated that we could actually insert any, then
171172
// extract up to as much energy as we were able to accept from the item
172-
long extractedEnergy = itemEnergyHandler.extractEnergy(energyInItem - simulatedRemainder, Action.EXECUTE);
173-
if (extractedEnergy > 0L) {
174-
//If we were able to actually extract it from the item, then insert it into our energy container
175-
MekanismUtils.logExpectedZero(energyContainer.insert(extractedEnergy, Action.EXECUTE, AutomationType.INTERNAL));
176-
//and mark that we were able to transfer at least some of it
177-
onContentsChanged();
178-
return true;
173+
long toPull = energyInItem - simulatedRemainder;
174+
simulatedRemainder = energyContainer.insert(toPull, Action.SIMULATE, AutomationType.INTERNAL);
175+
if (simulatedRemainder == 0L) {
176+
long extractedEnergy = itemEnergyHandler.extractEnergy(toPull, Action.EXECUTE);
177+
if (extractedEnergy > 0L) {
178+
//If we were able to actually extract it from the item, then insert it into our energy container
179+
MekanismUtils.logExpectedZero(energyContainer.insert(extractedEnergy, Action.EXECUTE, AutomationType.INTERNAL));
180+
//and mark that we were able to transfer at least some of it
181+
onContentsChanged();
182+
return true;
183+
}
184+
} else {
185+
Mekanism.logger.error("EnergyInventorySlot#fillContainerFromItem: Simulation after extraction calculation had a remainder. Tried pulling {}, remainder {}", toPull, simulatedRemainder);
179186
}
180187
}
181188
}
@@ -195,11 +202,17 @@ public void drainContainer() {
195202
long simulatedRemainder = itemEnergyHandler.insertEnergy(storedEnergy, Action.SIMULATE);
196203
if (simulatedRemainder < storedEnergy) {
197204
//We are able to fit at least some energy from our container into the item
198-
long extractedEnergy = energyContainer.extract(storedEnergy - simulatedRemainder, Action.EXECUTE, AutomationType.INTERNAL);
199-
if (extractedEnergy > 0L) {
200-
//If we were able to actually extract it from our energy container, then insert it into the item
201-
MekanismUtils.logExpectedZero(itemEnergyHandler.insertEnergy(extractedEnergy, Action.EXECUTE));
202-
onContentsChanged();
205+
long toOffer = storedEnergy - simulatedRemainder;
206+
simulatedRemainder = itemEnergyHandler.insertEnergy(toOffer, Action.SIMULATE);
207+
if (simulatedRemainder == 0L) {
208+
long extractedEnergy = energyContainer.extract(toOffer, Action.EXECUTE, AutomationType.INTERNAL);
209+
if (extractedEnergy > 0L) {
210+
//If we were able to actually extract it from our energy container, then insert it into the item
211+
MekanismUtils.logExpectedZero(itemEnergyHandler.insertEnergy(extractedEnergy, Action.EXECUTE));
212+
onContentsChanged();
213+
}
214+
} else {
215+
Mekanism.logger.error("EnergyInventorySlot#drainContainer: Simulation after insertion calculation had a remainder. Offered {}, remainder {}", toOffer, simulatedRemainder);
203216
}
204217
}
205218
}

0 commit comments

Comments
 (0)