Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GT++ Energy Buffer NPE #3296

Merged
merged 3 commits into from
Sep 28, 2024
Merged

Fix GT++ Energy Buffer NPE #3296

merged 3 commits into from
Sep 28, 2024

Conversation

reobf
Copy link
Contributor

@reobf reobf commented Sep 28, 2024

Fix: GTNewHorizons/GT-New-Horizons-Modpack#16565

The bug is: Charge a GT++ Energy Buffer to full energy, save the game and load it again, there will be ERROR in log:

expand

[13:47:28] [Server thread/INFO] [FML]: Loading dimension 180 (New World) (net.minecraft.server.integrated.IntegratedServer@5e21656e)
[13:47:28] [Server thread/ERROR] [GregTech GTNH]: Encountered Exception while loading MetaTileEntity.
[13:47:28] [Server thread/ERROR] [GregTech GTNH]: java.lang.NullPointerException
	at gregtech.api.metatileentity.BaseTileEntity.func_70296_d(BaseTileEntity.java:567)
	at gregtech.api.metatileentity.CommonMetaTileEntity.func_70296_d(CommonMetaTileEntity.java:118)
	at gregtech.api.metatileentity.MetaTileEntity.func_70296_d(MetaTileEntity.java:1049)
	at gregtech.api.metatileentity.MetaTileEntity.setEUVar(MetaTileEntity.java:593)
	at gtPlusPlus.xmod.gregtech.common.tileentities.storage.MTEEnergyBuffer.loadNBTData(MTEEnergyBuffer.java:269)
	at gregtech.api.metatileentity.CommonMetaTileEntity.loadMetaTileNBT(CommonMetaTileEntity.java:100)
	at gregtech.api.metatileentity.BaseMetaTileEntity.setInitialValuesAsNBT(BaseMetaTileEntity.java:221)
	at gregtech.api.metatileentity.BaseMetaTileEntity.func_145839_a(BaseMetaTileEntity.java:173)
	at net.minecraft.tileentity.TileEntity.func_145827_c(TileEntity.java:116)
	at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:496)
	at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:41)
	at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:12)
	at net.minecraftforge.common.util.AsynchronousExecutor.skipQueue(AsynchronousExecutor.java:344)
This is caused by 'loadNBTData' invoking some method on 'worldObj' indirectly(which is still null when chunk is loading).

This happens if:

  1. NBT tag 'aStoredEU' is present.
  2. 'aStoredEU' != internal EU value

internal EU value might exceed the maximum, but 'aStoredEU' is strictly truncated to max EU storage
That's why the bug only happens when its chraged to full energy

This PR removes unnecessary saving of 'aStoredEU' in method 'saveNBTData', to prevent accessing 'worldObj' too early.
After fix, Energy Buffer will not lose its energy after save-n-load because internal EU value is already persisted in BaseMetaTileEntity.java.

nbt.setLong("mStoredEnergy", mStoredEnergy);

mStoredEnergy = aNBT.getLong("mStoredEnergy");

The function of keeping EU after harvested won't be affected, because 'loadNBTData' and 'setItemNBT' will still work.

@boubou19 boubou19 added the bug fix Fix a bug. Please link it in the PR. label Sep 28, 2024
@boubou19 boubou19 enabled auto-merge (squash) September 28, 2024 11:21
@boubou19 boubou19 merged commit b8eac28 into GTNewHorizons:master Sep 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix Fix a bug. Please link it in the PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A fully charged GT++ Energy Buffer generates NPE on chunk load
3 participants