diff --git a/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java b/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java index ca57ff5eb..1d3419065 100644 --- a/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java +++ b/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java @@ -84,8 +84,6 @@ public VoxyRenderSystem(WorldEngine world, ServiceManager sm) { world.acquireRef(); Logger.info("Creating Voxy render system"); - System.gc(); - if (Minecraft.getInstance().options.renderDistance().get()<3) { String msg = "Voxy: Having a vanilla render distance of 2 can cause rare culling near the edge of your screen issues, please use 3 or more"; Logger.warn(msg); @@ -100,8 +98,7 @@ public VoxyRenderSystem(WorldEngine world, ServiceManager sm) { try { //wait for opengl to be finished, this should hopefully ensure all memory allocations are free - glFinish(); - glFinish(); + glFlush(); this.worldIn = world; diff --git a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java index 0d56bcc43..ecbb63662 100644 --- a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java +++ b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java @@ -9,6 +9,7 @@ import me.cortex.voxy.common.world.WorldEngine; import me.cortex.voxy.commonImpl.VoxyCommon; import me.cortex.voxy.commonImpl.WorldIdentifier; +import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.renderer.LevelRenderer; import org.jetbrains.annotations.Nullable; @@ -24,6 +25,13 @@ public abstract class MixinLevelRenderer implements IGetVoxyRenderSystem { @Shadow private @Nullable ClientLevel level; @Unique private VoxyRenderSystem renderer; + @Unique + private static final long COOLDOWN_MS = 5000L; + @Unique + private volatile long voxy$lastRebuildTime; + @Unique + private volatile boolean voxy$pendingRebuild; + @Override public VoxyRenderSystem getVoxyRenderSystem() { return this.renderer; @@ -31,6 +39,42 @@ public VoxyRenderSystem getVoxyRenderSystem() { @Inject(method = "allChanged()V", at = @At("RETURN"), order = 900)//We want to inject before sodium private void reloadVoxyRenderer(CallbackInfo ci) { + long currentTime = System.currentTimeMillis(); + long timeSinceLastRebuild = currentTime - this.voxy$lastRebuildTime; + + if (timeSinceLastRebuild >= COOLDOWN_MS) { + // Enough time has passed, execute immediately + this.voxy$lastRebuildTime = currentTime; + this.voxy$pendingRebuild = false; + this.executeRebuild(); + } else { + // Within cooldown, schedule deferred execution using a simple daemon thread + if (!this.voxy$pendingRebuild) { + this.voxy$pendingRebuild = true; + long delay = COOLDOWN_MS - timeSinceLastRebuild; + Thread delayThread = new Thread(() -> { + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + return; + } + if (this.voxy$pendingRebuild) { + this.voxy$pendingRebuild = false; + Minecraft.getInstance().execute(() -> { + this.voxy$lastRebuildTime = System.currentTimeMillis(); + this.executeRebuild(); + }); + } + }); + delayThread.setDaemon(true); + delayThread.setName("Voxy-Debounce"); + delayThread.start(); + } + } + } + + @Unique + private void executeRebuild() { this.shutdownRenderer(); if (this.level != null) { this.createRenderer();