Skip to content

Commit

Permalink
fix energy container not working right on rejoin
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Dec 21, 2023
1 parent cba78d6 commit 2350ba0
Showing 1 changed file with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MetaTileEntityQuantumStorageController extends MetaTileEntity imple

private static final int MAX_DISTANCE_RADIUS = 16;

private IEnergyContainer energyContainer;
private QuantumControllerEnergyContainer energyContainer;
/** Somewhat lazily initialized, make sure to call {@code getStorage()} before trying to access anything in this */
private Map<BlockPos, WeakReference<IQuantumStorage<?>>> storageInstances = new HashMap<>();

Expand Down Expand Up @@ -244,16 +244,12 @@ public void rebuildNetwork() {
if (storage != null) storage.setDisconnected();
}
handler.rebuildCache();
energyContainer.setMaxCapacity(energyConsumption * 16L);
markDirty();
}

private void reinitializeEnergyContainer() {
long stored = energyContainer == null ? 0L : energyContainer.getEnergyStored();

energyContainer = EnergyContainerHandler.receiverContainer(this, this.energyConsumption * 16L,
Integer.MAX_VALUE, Integer.MAX_VALUE);

if (stored > 0) energyContainer.addEnergy(stored);
energyContainer = new QuantumControllerEnergyContainer(this);
}

@Override
Expand Down Expand Up @@ -419,4 +415,34 @@ public int getSlotLimit(int slot) {
return getItemHandlers().getSlotLimit(slot);
}
}

private static class QuantumControllerEnergyContainer extends EnergyContainerHandler {

private long maxCapacity = 0;
public QuantumControllerEnergyContainer(MetaTileEntity tileEntity) {
super(tileEntity, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 0);
}

@Override
public long getEnergyCapacity() {
return this.maxCapacity;
}

protected void setMaxCapacity(long capacity) {
this.maxCapacity = capacity;
}

@Override
public @NotNull NBTTagCompound serializeNBT() {
var tag = super.serializeNBT();
tag.setLong("MaxCapacity", this.maxCapacity);
return tag;
}

@Override
public void deserializeNBT(@NotNull NBTTagCompound compound) {
this.maxCapacity = compound.getLong("MaxCapacity");
super.deserializeNBT(compound);
}
}
}

0 comments on commit 2350ba0

Please sign in to comment.