1010import mekanism .api .energy .IEnergyContainer ;
1111import mekanism .api .energy .IStrictEnergyHandler ;
1212import mekanism .api .recipes .ItemStackToEnergyRecipe ;
13+ import mekanism .common .Mekanism ;
1314import mekanism .common .integration .energy .EnergyCompatUtils ;
1415import mekanism .common .inventory .container .slot .ContainerSlotType ;
1516import 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