mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 03:35:51 +00:00
Add overflow protection (untested)
This commit is contained in:
parent
ddb7ebd134
commit
34905ebffa
@ -157,7 +157,9 @@ public class EnergyNet extends Network implements HologramOwner {
|
|||||||
if (connectorNodes.isEmpty() && terminusNodes.isEmpty()) {
|
if (connectorNodes.isEmpty() && terminusNodes.isEmpty()) {
|
||||||
updateHologram(b, "&4No Energy Network found");
|
updateHologram(b, "&4No Energy Network found");
|
||||||
} else {
|
} else {
|
||||||
int supply = tickAllGenerators(timestamp::getAndAdd) + tickAllCapacitors();
|
int generatorsSupply = tickAllGenerators(timestamp::getAndAdd);
|
||||||
|
int capacitorsSupply = tickAllCapacitors();
|
||||||
|
int supply = overflowSafeAddition(generatorsSupply, capacitorsSupply);
|
||||||
int remainingEnergy = supply;
|
int remainingEnergy = supply;
|
||||||
int demand = 0;
|
int demand = 0;
|
||||||
|
|
||||||
@ -245,7 +247,7 @@ public class EnergyNet extends Network implements HologramOwner {
|
|||||||
int energy = provider.getGeneratedOutput(loc, data);
|
int energy = provider.getGeneratedOutput(loc, data);
|
||||||
|
|
||||||
if (provider.isChargeable()) {
|
if (provider.isChargeable()) {
|
||||||
energy += provider.getCharge(loc, data);
|
energy = overflowSafeAddition(energy, provider.getCharge(loc, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (provider.willExplode(loc, data)) {
|
if (provider.willExplode(loc, data)) {
|
||||||
@ -257,7 +259,7 @@ public class EnergyNet extends Network implements HologramOwner {
|
|||||||
loc.getWorld().createExplosion(loc, 0F, false);
|
loc.getWorld().createExplosion(loc, 0F, false);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
supply += energy;
|
supply = overflowSafeAddition(supply, energy);
|
||||||
}
|
}
|
||||||
} catch (Exception | LinkageError throwable) {
|
} catch (Exception | LinkageError throwable) {
|
||||||
explodedBlocks.add(loc);
|
explodedBlocks.add(loc);
|
||||||
@ -280,11 +282,22 @@ public class EnergyNet extends Network implements HologramOwner {
|
|||||||
int supply = 0;
|
int supply = 0;
|
||||||
|
|
||||||
for (Map.Entry<Location, EnergyNetComponent> entry : capacitors.entrySet()) {
|
for (Map.Entry<Location, EnergyNetComponent> entry : capacitors.entrySet()) {
|
||||||
supply += entry.getValue().getCharge(entry.getKey());
|
supply = overflowSafeAddition(supply, entry.getValue().getCharge(entry.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return supply;
|
return supply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This detects if 2 integers will overflow and if they will, returns {@link Integer#MAX_VALUE}
|
||||||
|
* @param i1 the first integer
|
||||||
|
* @param i2 the second integer
|
||||||
|
* @return {@link Integer#MAX_VALUE} if overflow detected else the sum of i1 and i2
|
||||||
|
*/
|
||||||
|
private int overflowSafeAddition(int i1, int i2) {
|
||||||
|
boolean willOverflow = (i1 == Integer.MAX_VALUE || i2 == Integer.MAX_VALUE) || i1 >= 0 ? i2 > Integer.MAX_VALUE - i1 : i2 < Integer.MIN_VALUE - i1;
|
||||||
|
return willOverflow ? Integer.MAX_VALUE : i1 + i2;
|
||||||
|
}
|
||||||
|
|
||||||
private void updateHologram(@Nonnull Block b, double supply, double demand) {
|
private void updateHologram(@Nonnull Block b, double supply, double demand) {
|
||||||
if (demand > supply) {
|
if (demand > supply) {
|
||||||
|
Loading…
Reference in New Issue
Block a user