Skip to content

Commit

Permalink
Don't be too ridiculous with EMC insertion, fix another server deadlo…
Browse files Browse the repository at this point in the history
…ck in extreme cases
  • Loading branch information
62832 committed May 11, 2024
1 parent 176e773 commit 15619c0
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,22 @@ public long insertItem(
}
}

var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getSellValue(what.toStack()));
var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
var itemEmc = IEMCProxy.INSTANCE.getSellValue(what.toStack());
var totalEmc = AppliedE.clampedLong(BigInteger.valueOf(itemEmc).multiply(BigInteger.valueOf(amount)));
var totalInserted = 0L;

while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
var toDeposit = AppliedE.clampedLong(totalEmc);
var canDeposit = toDeposit;
if (mode == Actionable.MODULATE) {
totalEmc = getAmountAfterPowerExpenditure(totalEmc) / itemEmc * itemEmc;
insert(EMCKey.BASE, totalEmc, Actionable.MODULATE, source);
}

var inserted = totalEmc / itemEmc;
totalInserted += inserted;
source.player().ifPresent(player -> {
if (mode == Actionable.MODULATE) {
canDeposit = getAmountAfterPowerExpenditure(canDeposit);
insert(EMCKey.BASE, canDeposit, Actionable.MODULATE, source);
AeStats.ItemsInserted.addToPlayer(player, Ints.saturatedCast(inserted));
}

var inserted = BigInteger.valueOf(canDeposit).divide(itemEmc).longValue();
totalInserted += inserted;
source.player().ifPresent(player -> {
if (mode == Actionable.MODULATE) {
AeStats.ItemsInserted.addToPlayer(player, Ints.saturatedCast(inserted));
}
});

var wouldHaveDeposited = BigInteger.valueOf(toDeposit);
totalEmc = totalEmc.subtract(wouldHaveDeposited).add(wouldHaveDeposited.remainder(itemEmc));
}
});

if (mode == Actionable.MODULATE && mayLearn && totalInserted > 0) {
source.player().ifPresent(player -> {
Expand Down

0 comments on commit 15619c0

Please sign in to comment.