Skip to content

Conversation

@MCTian-mi
Copy link
Contributor

@MCTian-mi MCTian-mi commented Sep 27, 2025

What

At the moment the Mods#Optifine checks whether shaderpack is active by reflection, the result is cached and never changed afterward.

// Special Optifine handler, but consolidated here for simplicity
Optifine(null) {
@Override
public boolean isModLoaded() {
if (this.modLoaded == null) {
try {
Class<?> c = Class.forName("net.optifine.shaders.Shaders");
Field f = c.getDeclaredField("shaderPackLoaded");
f.setAccessible(true);
this.modLoaded = f.getBoolean(null);
} catch (Exception ignored) {
this.modLoaded = false;
}
}
return this.modLoaded;
}
};

This results in issues when players turn shaders on/off after the cache is created. In the case where players disabled shaders, the bloom effect won't come back (since they still think the shader is on from the cached value); and in the other case where the shader is turned off afterward, the game will come to a crash.

This PR make it so the shaderpack load state is checked at real-time, which should potentially fix the issues mentioned above.

Implementation Details

This PR moves the shaderpack checker to a separate enum constant, named ShadersMod (Where the OptiFine shader code originally come from), and @Overrides the isModLoaded method with a reference to Shaders#shaderPackLoaded

ShadersMod(null) {

    @Override
    public boolean isModLoaded() {
        // Check shader pack state at real time instead of caching it
        return OptiFine.isModLoaded() && Shaders.shaderPackLoaded;
    }
}

Where the class net.optifine.shaders.Shaders created in api package is adapted and minimized from OptiFine itself. The OptiFine.isModLoaded() method call serves as a guard to avoid ClassNotFoundExceptions.

And the original enum constant is renamed from Optifine to OptiFine, under the consideration of both fixing the spelling mistake and preventing unexpected compatibility issues. The OptiFine checker is now delegating the forge-provided method for checking OptiFine's existence.

OptiFine(null) {

    @Override
    public boolean isModLoaded() {
       if (this.modLoaded == null) {
            this.modLoaded = FMLCommonHandler.instance().getSide().isClient() &&
                    FMLClientHandler.instance().hasOptifine();
        }
        return this.modLoaded;
    }
}

Outcome

  • Above-mentioned issues should (hopefully) get fixed. (Note: Now when you turn shaders on afterward, the game won't crash. Instead, it results in some rendering issues, whose behavior depends on the shader pack you choose. I'm still investigating this issue. But I think it shouldn't be a blocker for this PR.)
  • Addons with direct reference to Mods#Optifine will break.

@MCTian-mi MCTian-mi requested a review from a team as a code owner September 27, 2025 09:46
Copy link
Contributor

@brachy84 brachy84 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@ALongStringOfNumbers ALongStringOfNumbers added the type: feature New feature or request label Sep 29, 2025
@ALongStringOfNumbers ALongStringOfNumbers merged commit 9571ba0 into GregTechCEu:master Oct 26, 2025
3 of 4 checks passed
@MCTian-mi MCTian-mi deleted the real-time-shader-check branch October 28, 2025 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants