Skip to content

Commit 9571ba0

Browse files
authored
Check OptiFine Shader at real time (#2871)
1 parent 4555104 commit 9571ba0

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.optifine.shaders;
2+
3+
/// Adapted and minimized from OptiFine
4+
public class Shaders {
5+
public static boolean shaderPackLoaded;
6+
}

src/main/java/gregtech/api/util/Mods.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
import net.minecraft.item.ItemStack;
66
import net.minecraft.util.text.TextFormatting;
7+
import net.minecraftforge.fml.client.FMLClientHandler;
8+
import net.minecraftforge.fml.common.FMLCommonHandler;
79
import net.minecraftforge.fml.common.Loader;
810
import net.minecraftforge.fml.common.ModContainer;
911
import net.minecraftforge.fml.common.registry.GameRegistry;
1012
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
1113
import net.minecraftforge.fml.relauncher.Side;
1214
import net.minecraftforge.fml.relauncher.SideOnly;
15+
import net.optifine.shaders.Shaders;
1316

1417
import org.jetbrains.annotations.NotNull;
1518
import org.jetbrains.annotations.Nullable;
1619

17-
import java.lang.reflect.Field;
1820
import java.util.ArrayList;
1921
import java.util.Arrays;
2022
import java.util.List;
@@ -79,23 +81,26 @@ public enum Mods {
7981
Vintagium(Names.VINTAGIUM),
8082
Alfheim(Names.ALFHEIM),
8183

82-
// Special Optifine handler, but consolidated here for simplicity
83-
Optifine(null) {
84+
OptiFine(null) {
8485

8586
@Override
8687
public boolean isModLoaded() {
8788
if (this.modLoaded == null) {
88-
try {
89-
Class<?> c = Class.forName("net.optifine.shaders.Shaders");
90-
Field f = c.getDeclaredField("shaderPackLoaded");
91-
f.setAccessible(true);
92-
this.modLoaded = f.getBoolean(null);
93-
} catch (Exception ignored) {
94-
this.modLoaded = false;
95-
}
89+
this.modLoaded = FMLCommonHandler.instance().getSide().isClient() &&
90+
FMLClientHandler.instance().hasOptifine();
9691
}
9792
return this.modLoaded;
9893
}
94+
},
95+
96+
// Special Optifine shader handler, but consolidated here for simplicity
97+
ShadersMod(null) {
98+
99+
@Override
100+
public boolean isModLoaded() {
101+
// Check shader pack state at real time instead of caching it
102+
return OptiFine.isModLoaded() && Shaders.shaderPackLoaded;
103+
}
99104
};
100105

101106
public static class Names {

src/main/java/gregtech/client/model/pipeline/VertexLighterFlatSpecial.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected void processQuad() {
134134
updateColor(normal[v], color[v], x, y, z, tint, multiplier);
135135

136136
// When enabled this causes the rendering to be black with Optifine
137-
if (!Mods.Optifine.isModLoaded() && diffuse) {
137+
if (!Mods.ShadersMod.isModLoaded() && diffuse) {
138138
float d = LightUtil.diffuseLight(normal[v][0], normal[v][1], normal[v][2]);
139139
for (int i = 0; i < 3; i++) {
140140
color[v][i] *= d;

src/main/java/gregtech/client/model/pipeline/VertexLighterSmoothAoSpecial.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public VertexLighterSmoothAoSpecial(BlockColors colors) {
1515

1616
@Override
1717
protected void updateLightmap(float[] normal, float[] lightmap, float x, float y, float z) {
18-
if (Mods.Optifine.isModLoaded()) {
18+
if (Mods.ShadersMod.isModLoaded()) {
1919
super.updateLightmap(normal, lightmap, x, y, z);
2020
return;
2121
}
@@ -28,7 +28,7 @@ protected void updateLightmap(float[] normal, float[] lightmap, float x, float y
2828
protected void updateColor(float[] normal, float[] color, float x, float y, float z, float tint, int multiplier) {
2929
super.updateColor(normal, color, x, y, z, tint, multiplier);
3030

31-
if (Mods.Optifine.isModLoaded()) {
31+
if (Mods.ShadersMod.isModLoaded()) {
3232
return;
3333
}
3434

@@ -146,7 +146,7 @@ protected float getAo(float x, float y, float z) {
146146

147147
@Override
148148
public void updateBlockInfo() {
149-
if (Mods.Optifine.isModLoaded()) {
149+
if (Mods.ShadersMod.isModLoaded()) {
150150
super.updateBlockInfo();
151151
return;
152152
}

src/main/java/gregtech/client/utils/BloomEffectUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static BlockRenderLayer getEffectiveBloomLayer() {
8383
*/
8484
@Contract("null -> _; !null -> !null")
8585
public static BlockRenderLayer getEffectiveBloomLayer(BlockRenderLayer fallback) {
86-
return Mods.Optifine.isModLoaded() ? fallback : bloom;
86+
return Mods.ShadersMod.isModLoaded() ? fallback : bloom;
8787
}
8888

8989
/**
@@ -115,7 +115,7 @@ public static BlockRenderLayer getEffectiveBloomLayer(boolean isBloomActive) {
115115
*/
116116
@Contract("_, null -> _; _, !null -> !null")
117117
public static BlockRenderLayer getEffectiveBloomLayer(boolean isBloomActive, BlockRenderLayer fallback) {
118-
return Mods.Optifine.isModLoaded() || !isBloomActive ? fallback : bloom;
118+
return Mods.ShadersMod.isModLoaded() || !isBloomActive ? fallback : bloom;
119119
}
120120

121121
/**
@@ -253,7 +253,7 @@ public static BloomRenderTicket registerBloomRender(@Nullable IRenderSetup setup
253253
@NotNull IBloomEffect render,
254254
@Nullable Predicate<BloomRenderTicket> validityChecker,
255255
@Nullable Supplier<World> worldContext) {
256-
if (Mods.Optifine.isModLoaded()) return BloomRenderTicket.INVALID;
256+
if (Mods.ShadersMod.isModLoaded()) return BloomRenderTicket.INVALID;
257257
BloomRenderTicket ticket = new BloomRenderTicket(setup, bloomType, render, validityChecker, worldContext);
258258
BLOOM_RENDER_LOCK.lock();
259259
try {
@@ -302,7 +302,7 @@ public static int renderBloomBlockLayer(RenderGlobal renderGlobal,
302302
@NotNull Entity entity) {
303303
Minecraft.getMinecraft().profiler.endStartSection("BTLayer");
304304

305-
if (Mods.Optifine.isModLoaded()) {
305+
if (Mods.ShadersMod.isModLoaded()) {
306306
return renderGlobal.renderBlockLayer(blockRenderLayer, partialTicks, pass, entity);
307307
}
308308

src/main/java/gregtech/client/utils/CTMHooks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CTMHooks {
3636
public static ThreadLocal<Boolean> ENABLE = new ThreadLocal<>();
3737

3838
public static boolean checkLayerWithOptiFine(boolean canRenderInLayer, byte layers, BlockRenderLayer layer) {
39-
if (Mods.Optifine.isModLoaded()) {
39+
if (Mods.ShadersMod.isModLoaded()) {
4040
if (canRenderInLayer) {
4141
if (layer == BloomEffectUtil.getBloomLayer()) return false;
4242
} else if ((layers >> BloomEffectUtil.getBloomLayer().ordinal() & 1) == 1 &&
@@ -50,7 +50,7 @@ public static boolean checkLayerWithOptiFine(boolean canRenderInLayer, byte laye
5050
public static List<BakedQuad> getQuadsWithOptiFine(List<BakedQuad> ret, BlockRenderLayer layer,
5151
IBakedModel bakedModel, IBlockState state, EnumFacing side,
5252
long rand) {
53-
if (Mods.Optifine.isModLoaded() && CTMHooks.ENABLE.get() == null) {
53+
if (Mods.ShadersMod.isModLoaded() && CTMHooks.ENABLE.get() == null) {
5454
if (layer == BloomEffectUtil.getBloomLayer()) {
5555
return Collections.emptyList();
5656
} else if (layer == BloomEffectUtil.getEffectiveBloomLayer()) {

src/main/java/gregtech/client/utils/DepthTextureUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class DepthTextureUtil {
4040
private static int lastWidth, lastHeight;
4141

4242
private static boolean shouldRenderDepthTexture() {
43-
return lastBind && !Mods.Optifine.isModLoaded() && ConfigHolder.client.hookDepthTexture &&
43+
return lastBind && !Mods.ShadersMod.isModLoaded() && ConfigHolder.client.hookDepthTexture &&
4444
OpenGlHelper.isFramebufferEnabled();
4545
}
4646

0 commit comments

Comments
 (0)