MrCrayfish / Controllable

Adds in the ability to use a controller to play Minecraft Java Edition
https://mrcrayfish.com/mods?id=controllable
GNU General Public License v3.0
271 stars 78 forks source link

🐛 Fixed crash with OptiFine #465

Open KaiAF opened 11 months ago

KaiAF commented 11 months ago

The cause of the crash was when trying to inject the "controllableLastRender" method, it was looking for a specific set of arguments while without OptiFine it worked, but with it, it would crash. This is because in OptiFine it adds two new fields inside the render method. Seen below:

@@ -899,24 +1041,33 @@
                RenderSystem.disableBlend();
                RenderSystem.disableDepthTest();
                RenderSystem.resetTextureMatrix();
                this.postEffect.process(p_109094_);
+               RenderSystem.enableTexture();
             }

             this.minecraft.getMainRenderTarget().bindWrite(true);
+         } else {
+            RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
          }

          Window window = this.minecraft.getWindow();
          RenderSystem.clear(256, Minecraft.ON_OSX);
-         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, 21000.0F);
+         float guiFarPlane = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? Reflector.ForgeHooksClient_getGuiFarPlane.callFloat() : 21000.0F; // optifine
+         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, guiFarPlane);
          RenderSystem.setProjectionMatrix(matrix4f, VertexSorting.ORTHOGRAPHIC_Z);
          PoseStack posestack = RenderSystem.getModelViewStack();
          posestack.pushPose();
          posestack.setIdentity();
-         posestack.translate(0.0F, 0.0F, -11000.0F);
+         float guiOffsetZ = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? 1000.0F - guiFarPlane : -11000.0F;
+         posestack.translate(0.0, 0.0, guiOffsetZ); // optifine
          RenderSystem.applyModelViewMatrix();
          Lighting.setupFor3DItems();
          GuiGraphics guigraphics = new GuiGraphics(this.minecraft, this.renderBuffers.bufferSource());
+         if (this.lightTexture.isCustom()) {
+            this.lightTexture.setAllowed(false);
+         }
+
          if (p_109096_ && this.minecraft.level != null) {
             this.minecraft.getProfiler().popPush("gui");
             if (this.minecraft.player != null) {
                float f = Mth.lerp(p_109094_, this.minecraft.player.oSpinningEffectIntensity, this.minecraft.player.spinningEffectIntensity);

image

Fixes #433, fixes #418, fixes #472, fixes #477

1nd1r4 commented 8 months ago

@MrCrayfish can you approve this request?

nightinnn1 commented 7 months ago

The cause of the crash was when trying to inject the "controllableLastRender" method, it was looking for a specific set of arguments while without OptiFine it worked, but with it, it would crash. This is because in OptiFine it adds two new fields inside the render method. Seen below:

@@ -899,24 +1041,33 @@
                RenderSystem.disableBlend();
                RenderSystem.disableDepthTest();
                RenderSystem.resetTextureMatrix();
                this.postEffect.process(p_109094_);
+               RenderSystem.enableTexture();
             }

             this.minecraft.getMainRenderTarget().bindWrite(true);
+         } else {
+            RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
          }

          Window window = this.minecraft.getWindow();
          RenderSystem.clear(256, Minecraft.ON_OSX);
-         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, 21000.0F);
+         float guiFarPlane = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? Reflector.ForgeHooksClient_getGuiFarPlane.callFloat() : 21000.0F; // optifine
+         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, guiFarPlane);
          RenderSystem.setProjectionMatrix(matrix4f, VertexSorting.ORTHOGRAPHIC_Z);
          PoseStack posestack = RenderSystem.getModelViewStack();
          posestack.pushPose();
          posestack.setIdentity();
-         posestack.translate(0.0F, 0.0F, -11000.0F);
+         float guiOffsetZ = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? 1000.0F - guiFarPlane : -11000.0F;
+         posestack.translate(0.0, 0.0, guiOffsetZ); // optifine
          RenderSystem.applyModelViewMatrix();
          Lighting.setupFor3DItems();
          GuiGraphics guigraphics = new GuiGraphics(this.minecraft, this.renderBuffers.bufferSource());
+         if (this.lightTexture.isCustom()) {
+            this.lightTexture.setAllowed(false);
+         }
+
          if (p_109096_ && this.minecraft.level != null) {
             this.minecraft.getProfiler().popPush("gui");
             if (this.minecraft.player != null) {
                float f = Mth.lerp(p_109094_, this.minecraft.player.oSpinningEffectIntensity, this.minecraft.player.spinningEffectIntensity);

image

Fixes #433, fixes #418, fixes #472

Can you just upload your version somewhere?

KaiAF commented 7 months ago

@nightinnn1, sure. I could not figure out to publish it on GitHub since the build script is using a lot of secret variables. So, I just uploaded it to my file server.

Forge download, 1.20.1 -> https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3.jar

nightinnn1 commented 7 months ago

@nightinnn1, sure. I could not figure out to publish it on GitHub since the build script is using a lot of secret variables. So, I just uploaded it to my file server.

Forge download, 1.20.1 -> https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3.jar

Your mode version crashing even without optifine :(

KaiAF commented 7 months ago

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

Musky420 commented 7 months ago

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

It works. kudos.

While trying other setting mods. I found out embeddium+oculus works better and faster than optifine (for me at least). I tried running your hotfix but unfortunately it crashes with embeddium+oculus.

Do you think you could look into the argument for me? It only crashes when the setting mod has a shader loader. I tried with other setting mod (without shader loader) and it didn't crash.

PrometherioNPL commented 5 months ago

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

It works. kudos.

While trying other setting mods. I found out embeddium+oculus works better and faster than optifine (for me at least). I tried running your hotfix but unfortunately it crashes with embeddium+oculus.

Do you think you could look into the argument for me? It only crashes when the setting mod has a shader loader. I tried with other setting mod (without shader loader) and it didn't crash.

Hi, is there any way to use Optifine (with all my shaders, texture packs etc) with controllable?