Avoid calling createCapabilities many times, mark mixin as client-only

This commit is contained in:
embeddedt 2023-08-20 20:35:20 -04:00
parent fe8d0434c5
commit 8d766a8cc8
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 8 additions and 1 deletions

View File

@ -8,12 +8,14 @@ import net.minecraftforge.fml.StartupMessageManager;
import net.minecraftforge.fml.event.IModBusEvent;
import net.minecraftforge.registries.GameData;
import net.minecraftforge.registries.RegisterEvent;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.forge.util.AsyncLoadingScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(value = GameData.class, remap = false)
@ClientOnlyMixin
public class GameDataMixin {
@Redirect(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/ModLoader;postEventWrapContainerInModOrder(Lnet/minecraftforge/eventbus/api/Event;)V"))
private static <T extends Event & IModBusEvent> void swapThreadAndPost(ModLoader loader, T event) {

View File

@ -3,6 +3,7 @@ package org.embeddedt.modernfix.forge.util;
import net.minecraftforge.fml.loading.ImmediateWindowHandler;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@ -14,9 +15,13 @@ public class AsyncLoadingScreen extends Thread implements AutoCloseable {
private static int splashThreadNum = 1;
private static GLCapabilities caps;
public AsyncLoadingScreen() {
this.setName("ModernFix splash thread " + splashThreadNum++);
this.theWindow = GLFW.glfwGetCurrentContext();
if(caps == null)
caps = GL.createCapabilities();
if(this.theWindow == 0)
throw new IllegalStateException("No context found but async loading screen was requested");
this.keepRunning = new AtomicBoolean(true);
@ -32,7 +37,7 @@ public class AsyncLoadingScreen extends Thread implements AutoCloseable {
@Override
public void run() {
GLFW.glfwMakeContextCurrent(theWindow);
GL.createCapabilities(); // seems to be needed, otherwise we get a "function not valid for context" error
GL.setCapabilities(caps);
while(keepRunning.get()) {
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(50));
ImmediateWindowHandler.renderTick();