Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Aug 4, 2024
1 parent 3859227 commit 9eb22b6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.polyfrost.crosshair.mixin;

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

@Inject(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiIngame;renderGameOverlay(F)V"))
private void draw(float partialTicks, long nanoTime, CallbackInfo ci) {
CrosshairRenderer.INSTANCE.drawCrosshair((EntityRenderer) (Object) this);
if (ModConfig.INSTANCE.enabled) CrosshairRenderer.INSTANCE.drawCrosshair((EntityRenderer) (Object) this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraftforge.client.GuiIngameForge;
import org.polyfrost.crosshair.config.ModConfig;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GuiIngameForge.class)
public class GuiIngameForgeMixin {

@Inject(method = "renderCrosshairs", at = @At("HEAD"), cancellable = true, remap = false)
private void cancelVanillaCrosshair(int width, int height, CallbackInfo ci) {
if (ModConfig.INSTANCE.enabled) {
ci.cancel();
GlStateManager.enableAlpha();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/polyfrost/crosshair/mixin/MinecraftAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.Minecraft;
import net.minecraft.util.Timer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(Minecraft.class)
public interface MinecraftAccessor {
@Accessor
Timer getTimer();
}
88 changes: 43 additions & 45 deletions src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import cc.polyfrost.oneconfig.images.OneImage
import cc.polyfrost.oneconfig.libs.universal.UResolution
import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.Gui
import net.minecraft.client.gui.ScaledResolution
import net.minecraft.client.renderer.EntityRenderer
import net.minecraft.client.renderer.GlStateManager as GL
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraft.client.renderer.texture.TextureUtil
import net.minecraft.entity.monster.IMob
Expand All @@ -18,12 +18,15 @@ import net.minecraft.entity.passive.EntityWaterMob
import net.minecraft.entity.player.EntityPlayer
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.client.event.TextureStitchEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11
import org.polyfrost.crosshair.config.ModConfig
import org.polyfrost.crosshair.mixin.GuiIngameAccessor
import org.polyfrost.crosshair.mixin.MinecraftAccessor
import java.awt.image.BufferedImage
import kotlin.math.ceil
import net.minecraft.client.renderer.GlStateManager as GL

object CrosshairRenderer {
private var drawingImage = BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB)
Expand Down Expand Up @@ -65,60 +68,55 @@ object CrosshairRenderer {
vanillaLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", vanilla)
}

@SubscribeEvent
fun cancel(event: RenderGameOverlayEvent.Pre) {
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS || !ModConfig.enabled) return
GL.enableAlpha()
event.isCanceled = true
}

@SubscribeEvent
fun onPackSwitch(event: TextureStitchEvent) {
updateVanilla()
}

fun drawCrosshair(entityRenderer: EntityRenderer) {
if (!ModConfig.enabled) return
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return

entityRenderer.setupOverlayRendering()
GL.pushMatrix()
GL.tryBlendFuncSeparate(770, 771, 1, 0)
GL.enableBlend()
val renderConfig = ModConfig.renderConfig
if (renderConfig.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
}
GL.enableAlpha()
val parent = RenderGameOverlayEvent((mc as MinecraftAccessor).timer.renderPartialTicks, ScaledResolution(mc))
MinecraftForge.EVENT_BUS.post(RenderGameOverlayEvent.Pre(parent, RenderGameOverlayEvent.ElementType.CROSSHAIRS))
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == true) {
entityRenderer.setupOverlayRendering()
GL.pushMatrix()
GL.tryBlendFuncSeparate(770, 771, 1, 0)
GL.enableBlend()
val renderConfig = ModConfig.renderConfig
if (renderConfig.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
}
GL.enableAlpha()

GL11.glColor4f(1f, 1f, 1f, 1f)
GL11.glColor4f(1f, 1f, 1f, 1f)

(if (ModConfig.mode) textureLocation else vanillaLocation).let { mc.textureManager.bindTexture(it) }
val mcScale = UResolution.scaleFactor.toFloat()
GL.scale(1 / mcScale, 1 / mcScale, 1f)
val crosshair = ModConfig.newCurrentCrosshair
GL.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
val scale = crosshair.scale / 100f
val textureSize = 16
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
GL.translate(translation, translation, 0f)
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
val c = getColor()
if (c.rgb != -1) {
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
(if (ModConfig.mode) textureLocation else vanillaLocation).let { mc.textureManager.bindTexture(it) }
val mcScale = UResolution.scaleFactor.toFloat()
GL.scale(1 / mcScale, 1 / mcScale, 1f)
val crosshair = ModConfig.newCurrentCrosshair
GL.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
val scale = crosshair.scale / 100f
val textureSize = 16
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
GL.translate(translation, translation, 0f)
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
val c = getColor()
if (c.rgb != -1) {
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
}
if (renderConfig.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
}
GL11.glColor4f(1f, 1f, 1f, 1f)
GL.disableBlend()
GL.popMatrix()
}
if (renderConfig.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
}
GL11.glColor4f(1f, 1f, 1f, 1f)
GL.disableBlend()
GL.popMatrix()
MinecraftForge.EVENT_BUS.post(RenderGameOverlayEvent.Post(parent, RenderGameOverlayEvent.ElementType.CROSSHAIRS))
}

val WHITE = OneColor(-1)
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/mixins.polycrosshair.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"client": [
"EntityRendererMixin",
"GuiIngameAccessor",
"GuiIngameMixin"
"GuiIngameForgeMixin",
"GuiIngameMixin",
"MinecraftAccessor"
],
"verbose": true
}

0 comments on commit 9eb22b6

Please sign in to comment.