Fix Long Overflows
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-08-03 15:48:30 -04:00
parent 75205ff263
commit 66a64a31ad
3 changed files with 26 additions and 8 deletions

View File

@ -52,9 +52,11 @@ public class LightningRodBaseBlockEntity extends EnergyProviderBlockEntity imple
assert getWorld() != null; assert getWorld() != null;
if (cooldown <= 0) { if (cooldown <= 0) {
boolean dirty = false;
if (energy != 0) { if (energy != 0) {
energy = 0; energy = 0;
markDirty(); dirty = true;
} }
if (getLight() > 0) { if (getLight() > 0) {
@ -68,9 +70,13 @@ public class LightningRodBaseBlockEntity extends EnergyProviderBlockEntity imple
cooldown = HardcodedConfig.LIGHTNING_ROD_COOLDOWN; cooldown = HardcodedConfig.LIGHTNING_ROD_COOLDOWN;
energy = HardcodedConfig.LIGHTNING_ROD_ENERGY_OUTPUT; energy = HardcodedConfig.LIGHTNING_ROD_ENERGY_OUTPUT;
markDirty(); dirty = true;
} }
} }
if (dirty) {
markDirty();
}
} else { } else {
energy = 0; energy = 0;
@ -85,7 +91,6 @@ public class LightningRodBaseBlockEntity extends EnergyProviderBlockEntity imple
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag); super.toTag(tag);
tag.putInt("Cooldown", cooldown); tag.putInt("Cooldown", cooldown);
tag.putLong("Energy", energy);
return tag; return tag;
} }
@ -93,7 +98,6 @@ public class LightningRodBaseBlockEntity extends EnergyProviderBlockEntity imple
public void fromTag(BlockState state, CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag); super.fromTag(state, tag);
cooldown = tag.getInt("Cooldown"); cooldown = tag.getInt("Cooldown");
energy = tag.getLong("Energy");
} }
@Override @Override

View File

@ -22,7 +22,7 @@ public class BatteryCoreBlockEntity extends BlockEntity {
@Override @Override
public void fromTag(BlockState state, CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag); super.fromTag(state, tag);
energy = tag.getLong("Energy"); energy = Math.max(0, tag.getLong("Energy"));
} }
long getEnergy() { long getEnergy() {
@ -30,6 +30,9 @@ public class BatteryCoreBlockEntity extends BlockEntity {
} }
void setEnergy(long value) { void setEnergy(long value) {
if (value < 0) {
throw new UnsupportedOperationException();
}
energy = value; energy = value;
markDirty(); markDirty();
} }

View File

@ -17,6 +17,15 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
super(type); super(type);
} }
private static long add(long a, long b) {
long diff = Long.MAX_VALUE - a;
if (b >= diff) {
return Long.MAX_VALUE;
} else {
return a + b;
}
}
private static class UnlimitedAction implements Action.PropagatedAction { private static class UnlimitedAction implements Action.PropagatedAction {
private final PassiveBatteryControllerBlockEntity battery; private final PassiveBatteryControllerBlockEntity battery;
@ -35,16 +44,15 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
@Override @Override
public void pay(long amount) { public void pay(long amount) {
battery.setEnergy(battery.getEnergy() + amount); battery.setEnergy(add(battery.getEnergy(), amount));
} }
} }
@Override @Override
protected void energyTick() { protected void energyTick() {
long charge = HardcodedConfig.BATTERY_CHARGE_RATE; long charge = HardcodedConfig.BATTERY_CHARGE_RATE;
if (!isActiveController()) { if (!isActiveController()) {
addAction(new Action(charge, (world, pos, state) -> PassiveBatteryControllerBlockEntity.this.setEnergy(PassiveBatteryControllerBlockEntity.this.getEnergy() + charge), (world, pos, state) -> {})); addAction(new Action(charge, (world, pos, state) -> PassiveBatteryControllerBlockEntity.this.setEnergy(add(PassiveBatteryControllerBlockEntity.this.getEnergy(), charge)), (world, pos, state) -> {}));
} else { } else {
propagateAction(new UnlimitedAction(this)); propagateAction(new UnlimitedAction(this));
} }
@ -60,6 +68,9 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
@Override @Override
public void setEnergy(long value) { public void setEnergy(long value) {
if (value < 0) {
throw new UnsupportedOperationException();
}
BatteryCoreBlockEntity core = getBatteryCore(); BatteryCoreBlockEntity core = getBatteryCore();
if (core != null) { if (core != null) {
core.setEnergy(value); core.setEnergy(value);