Skip to content

Commit 9eb22b6

Browse files
committed
events
1 parent 3859227 commit 9eb22b6

File tree

5 files changed

+81
-47
lines changed

5 files changed

+81
-47
lines changed

src/main/java/org/polyfrost/crosshair/mixin/EntityRendererMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.polyfrost.crosshair.mixin;
22

33
import net.minecraft.client.renderer.EntityRenderer;
4+
import org.polyfrost.crosshair.config.ModConfig;
45
import org.polyfrost.crosshair.render.CrosshairRenderer;
56
import org.spongepowered.asm.mixin.Mixin;
67
import org.spongepowered.asm.mixin.injection.At;
@@ -12,6 +13,6 @@ public abstract class EntityRendererMixin {
1213

1314
@Inject(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiIngame;renderGameOverlay(F)V"))
1415
private void draw(float partialTicks, long nanoTime, CallbackInfo ci) {
15-
CrosshairRenderer.INSTANCE.drawCrosshair((EntityRenderer) (Object) this);
16+
if (ModConfig.INSTANCE.enabled) CrosshairRenderer.INSTANCE.drawCrosshair((EntityRenderer) (Object) this);
1617
}
1718
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.polyfrost.crosshair.mixin;
2+
3+
import net.minecraft.client.renderer.GlStateManager;
4+
import net.minecraftforge.client.GuiIngameForge;
5+
import org.polyfrost.crosshair.config.ModConfig;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(GuiIngameForge.class)
12+
public class GuiIngameForgeMixin {
13+
14+
@Inject(method = "renderCrosshairs", at = @At("HEAD"), cancellable = true, remap = false)
15+
private void cancelVanillaCrosshair(int width, int height, CallbackInfo ci) {
16+
if (ModConfig.INSTANCE.enabled) {
17+
ci.cancel();
18+
GlStateManager.enableAlpha();
19+
}
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.polyfrost.crosshair.mixin;
2+
3+
import net.minecraft.client.Minecraft;
4+
import net.minecraft.util.Timer;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
@Mixin(Minecraft.class)
9+
public interface MinecraftAccessor {
10+
@Accessor
11+
Timer getTimer();
12+
}

src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import cc.polyfrost.oneconfig.images.OneImage
66
import cc.polyfrost.oneconfig.libs.universal.UResolution
77
import cc.polyfrost.oneconfig.utils.dsl.mc
88
import net.minecraft.client.gui.Gui
9+
import net.minecraft.client.gui.ScaledResolution
910
import net.minecraft.client.renderer.EntityRenderer
10-
import net.minecraft.client.renderer.GlStateManager as GL
1111
import net.minecraft.client.renderer.texture.DynamicTexture
1212
import net.minecraft.client.renderer.texture.TextureUtil
1313
import net.minecraft.entity.monster.IMob
@@ -18,12 +18,15 @@ import net.minecraft.entity.passive.EntityWaterMob
1818
import net.minecraft.entity.player.EntityPlayer
1919
import net.minecraftforge.client.event.RenderGameOverlayEvent
2020
import net.minecraftforge.client.event.TextureStitchEvent
21+
import net.minecraftforge.common.MinecraftForge
2122
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
2223
import org.lwjgl.opengl.GL11
2324
import org.polyfrost.crosshair.config.ModConfig
2425
import org.polyfrost.crosshair.mixin.GuiIngameAccessor
26+
import org.polyfrost.crosshair.mixin.MinecraftAccessor
2527
import java.awt.image.BufferedImage
2628
import kotlin.math.ceil
29+
import net.minecraft.client.renderer.GlStateManager as GL
2730

2831
object CrosshairRenderer {
2932
private var drawingImage = BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB)
@@ -65,60 +68,55 @@ object CrosshairRenderer {
6568
vanillaLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", vanilla)
6669
}
6770

68-
@SubscribeEvent
69-
fun cancel(event: RenderGameOverlayEvent.Pre) {
70-
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS || !ModConfig.enabled) return
71-
GL.enableAlpha()
72-
event.isCanceled = true
73-
}
74-
7571
@SubscribeEvent
7672
fun onPackSwitch(event: TextureStitchEvent) {
7773
updateVanilla()
7874
}
7975

8076
fun drawCrosshair(entityRenderer: EntityRenderer) {
81-
if (!ModConfig.enabled) return
82-
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return
83-
84-
entityRenderer.setupOverlayRendering()
85-
GL.pushMatrix()
86-
GL.tryBlendFuncSeparate(770, 771, 1, 0)
87-
GL.enableBlend()
88-
val renderConfig = ModConfig.renderConfig
89-
if (renderConfig.invertColor) {
90-
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
91-
}
92-
GL.enableAlpha()
77+
val parent = RenderGameOverlayEvent((mc as MinecraftAccessor).timer.renderPartialTicks, ScaledResolution(mc))
78+
MinecraftForge.EVENT_BUS.post(RenderGameOverlayEvent.Pre(parent, RenderGameOverlayEvent.ElementType.CROSSHAIRS))
79+
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == true) {
80+
entityRenderer.setupOverlayRendering()
81+
GL.pushMatrix()
82+
GL.tryBlendFuncSeparate(770, 771, 1, 0)
83+
GL.enableBlend()
84+
val renderConfig = ModConfig.renderConfig
85+
if (renderConfig.invertColor) {
86+
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
87+
}
88+
GL.enableAlpha()
9389

94-
GL11.glColor4f(1f, 1f, 1f, 1f)
90+
GL11.glColor4f(1f, 1f, 1f, 1f)
9591

96-
(if (ModConfig.mode) textureLocation else vanillaLocation).let { mc.textureManager.bindTexture(it) }
97-
val mcScale = UResolution.scaleFactor.toFloat()
98-
GL.scale(1 / mcScale, 1 / mcScale, 1f)
99-
val crosshair = ModConfig.newCurrentCrosshair
100-
GL.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
101-
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
102-
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
103-
val scale = crosshair.scale / 100f
104-
val textureSize = 16
105-
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
106-
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
107-
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
108-
GL.translate(translation, translation, 0f)
109-
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
110-
val c = getColor()
111-
if (c.rgb != -1) {
112-
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
113-
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
92+
(if (ModConfig.mode) textureLocation else vanillaLocation).let { mc.textureManager.bindTexture(it) }
93+
val mcScale = UResolution.scaleFactor.toFloat()
94+
GL.scale(1 / mcScale, 1 / mcScale, 1f)
95+
val crosshair = ModConfig.newCurrentCrosshair
96+
GL.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
97+
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
98+
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
99+
val scale = crosshair.scale / 100f
100+
val textureSize = 16
101+
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
102+
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
103+
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
104+
GL.translate(translation, translation, 0f)
114105
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
106+
val c = getColor()
107+
if (c.rgb != -1) {
108+
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
109+
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
110+
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
111+
}
112+
if (renderConfig.invertColor) {
113+
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
114+
}
115+
GL11.glColor4f(1f, 1f, 1f, 1f)
116+
GL.disableBlend()
117+
GL.popMatrix()
115118
}
116-
if (renderConfig.invertColor) {
117-
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
118-
}
119-
GL11.glColor4f(1f, 1f, 1f, 1f)
120-
GL.disableBlend()
121-
GL.popMatrix()
119+
MinecraftForge.EVENT_BUS.post(RenderGameOverlayEvent.Post(parent, RenderGameOverlayEvent.ElementType.CROSSHAIRS))
122120
}
123121

124122
val WHITE = OneColor(-1)

src/main/resources/mixins.polycrosshair.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"client": [
1010
"EntityRendererMixin",
1111
"GuiIngameAccessor",
12-
"GuiIngameMixin"
12+
"GuiIngameForgeMixin",
13+
"GuiIngameMixin",
14+
"MinecraftAccessor"
1315
],
1416
"verbose": true
1517
}

0 commit comments

Comments
 (0)