Skip to content

Commit

Permalink
recalculate usage on load
Browse files Browse the repository at this point in the history
move energy calc into own method
don't save energy consumption on load
  • Loading branch information
ghzdude committed Dec 22, 2023
1 parent 21dad72 commit c29909a
Showing 1 changed file with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public void onPlacement() {
rebuildNetwork();
}

@Override
public void onLoad() {
calculateEnergyUsage();
super.onLoad();
}

// Used when this controller is initially placed. Try to find all possible
// storage instances that are connected and within our distance radius
// todo rework this to use neighbor cache somehow
Expand Down Expand Up @@ -237,16 +243,6 @@ public void rebuildNetwork() {
oldInstances.remove(pos);
oldPositions.remove(pos);

energyConsumption += switch (storage.getType()) {
case ITEM, FLUID -> {
int tier = storage instanceof ITieredMetaTileEntity tieredMTE ? tieredMTE.getTier() : 1;
yield GTValues.V[tier] / 2;
}
case PROXY -> 2L;
case EXTENDER -> 1L;
default -> 0L;
};

// check against already check posses so we don't recheck a checked pos
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos offsetPos = pos.offset(facing);
Expand Down Expand Up @@ -278,11 +274,29 @@ public void rebuildNetwork() {
if (storage != null) storage.setDisconnected();
}
handler.rebuildCache();
calculateEnergyUsage();
energyContainer.setMaxCapacity(energyConsumption * 16L);
writeCustomData(GregtechDataCodes.UPDATE_ENERGY_PER, buf -> buf.writeLong(energyConsumption));
markDirty();
}

private void calculateEnergyUsage() {
for (var pos : storagePositions) {
var storage = getStorage(pos, false);
if (storage == null) continue;

energyConsumption += switch (storage.getType()) {
case ITEM, FLUID -> {
int tier = storage instanceof ITieredMetaTileEntity tieredMTE ? tieredMTE.getTier() : 1;
yield tier > 4 ? 256L : 16L;
}
case PROXY -> 8L;
case EXTENDER -> 2L;
default -> 0L;
};
}
writeCustomData(GregtechDataCodes.UPDATE_ENERGY_PER, buf -> buf.writeLong(energyConsumption));
}

private void reinitializeEnergyContainer() {
energyContainer = new QuantumControllerEnergyContainer(this);
}
Expand All @@ -309,7 +323,6 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
list.appendTag(new NBTTagLong(pos.toLong()));
}
tagCompound.setTag("StorageInstances", list);
tagCompound.setLong("EnergyConsumption", energyConsumption);
return tagCompound;
}

Expand All @@ -320,7 +333,6 @@ public void readFromNBT(NBTTagCompound data) {
for (int i = 0; i < list.tagCount(); i++) {
storagePositions.add(BlockPos.fromLong(((NBTTagLong) list.get(i)).getLong()));
}
energyConsumption = data.getLong("EnergyConsumption");
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit c29909a

Please sign in to comment.