diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/LightningRodBaseBlockEntity.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/LightningRodBaseBlockEntity.java index 9dec6f5..3b09fdd 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/entity/LightningRodBaseBlockEntity.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/LightningRodBaseBlockEntity.java @@ -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 diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/BatteryCoreBlockEntity.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/BatteryCoreBlockEntity.java index 9043f79..3f122bd 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/BatteryCoreBlockEntity.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/BatteryCoreBlockEntity.java @@ -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(); } diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/PassiveBatteryControllerBlockEntity.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/PassiveBatteryControllerBlockEntity.java index 5d2965e..cedcd4c 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/PassiveBatteryControllerBlockEntity.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/battery/PassiveBatteryControllerBlockEntity.java @@ -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);