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

View File

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

View File

@ -17,6 +17,15 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
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 final PassiveBatteryControllerBlockEntity battery;
@ -35,16 +44,15 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
@Override
public void pay(long amount) {
battery.setEnergy(battery.getEnergy() + amount);
battery.setEnergy(add(battery.getEnergy(), amount));
}
}
@Override
protected void energyTick() {
long charge = HardcodedConfig.BATTERY_CHARGE_RATE;
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 {
propagateAction(new UnlimitedAction(this));
}
@ -60,6 +68,9 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
@Override
public void setEnergy(long value) {
if (value < 0) {
throw new UnsupportedOperationException();
}
BatteryCoreBlockEntity core = getBatteryCore();
if (core != null) {
core.setEnergy(value);