|
| 1 | +package org.embeddedt.modernfix.common.mixin.bugfix.world_leaks; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.client.multiplayer.ClientLevel; |
| 5 | +import net.minecraft.world.level.chunk.LevelChunk; |
| 6 | +import net.minecraft.world.level.lighting.LevelLightEngine; |
| 7 | +import org.embeddedt.modernfix.ModernFix; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.objectweb.asm.Opcodes; |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.Shadow; |
| 12 | +import org.spongepowered.asm.mixin.injection.At; |
| 13 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 14 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicReferenceArray; |
| 17 | + |
| 18 | +@Mixin(Minecraft.class) |
| 19 | +public class MinecraftMixin { |
| 20 | + @Shadow @Nullable public ClientLevel level; |
| 21 | + |
| 22 | + /** |
| 23 | + * To mitigate the effect of leaked client worlds, clear most of the data structures that waste memory. |
| 24 | + */ |
| 25 | + @Inject(method = "clearLevel(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At(value = "FIELD", opcode = Opcodes.PUTFIELD, target = "Lnet/minecraft/client/Minecraft;level:Lnet/minecraft/client/multiplayer/ClientLevel;")) |
| 26 | + private void clearLevelDataForLeaks(CallbackInfo ci) { |
| 27 | + if(this.level != null) { |
| 28 | + try { |
| 29 | + AtomicReferenceArray<LevelChunk> chunks = this.level.getChunkSource().storage.chunks; |
| 30 | + for(int i = 0; i < chunks.length(); i++) { |
| 31 | + chunks.set(i, null); |
| 32 | + } |
| 33 | + this.level.getChunkSource().lightEngine = new LevelLightEngine(this.level.getChunkSource(), false, false); |
| 34 | + // clear BE list otherwise they will hold chunks |
| 35 | + this.level.blockEntityList.clear(); |
| 36 | + } catch(RuntimeException e) { |
| 37 | + ModernFix.LOGGER.error("Exception clearing level data", e); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments