Skip to content

Commit

Permalink
fixes and make it not have a seizure when rendering anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Dec 16, 2024
1 parent f9aa043 commit 8247677
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 376 deletions.
11 changes: 0 additions & 11 deletions src/main/java/org/polyfrost/crosshair/mixin/GuiIngameAccessor.java

This file was deleted.

33 changes: 24 additions & 9 deletions src/main/java/org/polyfrost/crosshair/mixin/GuiIngameMixin.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.gui.GuiIngame;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.GuiIngameForge;
import org.polyfrost.crosshair.CrosshairHUD;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(GuiIngame.class)
public class GuiIngameMixin {
@Inject(method = "showCrosshair", at = @At("HEAD"))
private void check(CallbackInfoReturnable<Boolean> cir) {
//cir.setReturnValue(false);
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(GuiIngameForge.class)
public abstract class GuiIngameMixin {
@Redirect(method = "renderCrosshairs", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;bind(Lnet/minecraft/util/ResourceLocation;)V"), remap = false)
private void bindTexture(GuiIngameForge it, ResourceLocation res) {
if (CrosshairHUD.INSTANCE.getUseVanilla()) bind(res);
else GlStateManager.bindTexture(CrosshairHUD.INSTANCE.getId());
}

@Redirect(method = "renderCrosshairs", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;drawTexturedModalRect(IIIIII)V"))
private void drawCrosshair(GuiIngameForge it, int x, int y, int u, int v, int w, int h) {
final float tex = CrosshairHUD.INSTANCE.getUseVanilla() ? 256f : CrosshairHUD.INSTANCE.getWidth();
// #optimize-fold-zeroes
Gui.drawModalRectWithCustomSizedTexture(x, y, 0f, 0f, w, h, tex, tex);
}

@Shadow(remap = false)
protected abstract void bind(ResourceLocation res);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiIngame;
import org.polyfrost.crosshair.CrosshairHUD;
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.CallbackInfoReturnable;

@Mixin(GuiIngame.class)
public abstract class GuiIngameShowCrosshairMixin {
@Inject(method = "showCrosshair", at = @At(value = "RETURN", ordinal = 0), cancellable = true)
private void showWhenDebug(CallbackInfoReturnable<Boolean> cir) {
if (CrosshairHUD.INSTANCE.getShowInDebug()) cir.setReturnValue(true);
}

@Inject(method = "showCrosshair", at = @At("HEAD"), cancellable = true)
private void showWhenPerspective(CallbackInfoReturnable<Boolean> cir) {
if (!CrosshairHUD.INSTANCE.getShowInThirdPerson() && Minecraft.getMinecraft().gameSettings.thirdPersonView != 0) {
cir.setReturnValue(false);
}
}

@Inject(method = "showCrosshair", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;pointedEntity:Lnet/minecraft/entity/Entity;"), cancellable = true)
private void showWhenSpectator(CallbackInfoReturnable<Boolean> cir) {
if (CrosshairHUD.INSTANCE.getShowInSpectator()) cir.setReturnValue(true);
}
}
68 changes: 24 additions & 44 deletions src/main/kotlin/org/polyfrost/crosshair/CrosshairHUD.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package org.polyfrost.crosshair

import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraft.client.renderer.texture.TextureUtil
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import org.lwjgl.opengl.GL11.*
import org.polyfrost.crosshair.mixin.GuiIngameAccessor
import org.polyfrost.oneconfig.api.config.v1.annotations.Button
import org.polyfrost.oneconfig.api.config.v1.annotations.Include
import org.polyfrost.oneconfig.api.config.v1.annotations.Switch
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud
import org.polyfrost.polyui.unit.Vec2
import org.polyfrost.polyui.utils.getResourceStream
import org.polyfrost.universal.UMatrixStack
import org.polyfrost.universal.UResolution
import java.nio.file.Paths
import javax.imageio.ImageIO
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import net.minecraft.client.renderer.GlStateManager as GL

object CrosshairHUD : LegacyHud() {
@Switch(title = "Show in F3")
private var showInDebug = false

@Switch(title = "Show in GUIs")
private var showInGUIs = true
@Switch(title = "Use Vanilla")
var useVanilla = false

@Switch(title = "Show in F3")
var showInDebug = false

@Switch(title = "Show in Spectator")
private var showInSpectator = true
var showInSpectator = true

@Switch(title = "Show in 3rd Person")
private var showInThirdPerson = true
var showInThirdPerson = true

@Include
var currentCrosshair: String? = null

private var id = GL.generateTexture()
@Button(title = "Open Editor")
private val openEditor = Runnable {
PolyCrosshairUI.open()
}

val id = GL.generateTexture()
private var texSize = 15f

override var width: Float
Expand All @@ -47,44 +47,24 @@ object CrosshairHUD : LegacyHud() {
get() = texSize
set(value) {}

private val target = Paths.get("polycrosshair.png")

override fun category() = Category.COMBAT

override fun initialize() {
val img = if (currentCrosshair.isNullOrEmpty() || !target.exists()) ImageIO.read(getResourceStream("assets/polycrosshair/default.png"))
else ImageIO.read(target.inputStream())
val currentCrosshair = currentCrosshair
val stream = when {
currentCrosshair.isNullOrEmpty() -> getResourceStream("assets/polycrosshair/default.png")
else -> {
val p = Paths.get(currentCrosshair)
if (p.exists()) p.inputStream() else getResourceStream("assets/polycrosshair/default.png")
}
}
val img = ImageIO.read(stream)
setCrosshair(img.getRGB(0, 0, img.width, img.height, null, 0, img.width), img.width)
DynamicTexture(img)
}

override fun hasBackground() = false

override fun render(stack: UMatrixStack, x: Float, y: Float, scaleX: Float, scaleY: Float) {
val mc = Minecraft.getMinecraft()
if (!(mc.ingameGUI as GuiIngameAccessor).shouldShowCrosshair()) return

GL.enableAlpha()
GL.enableBlend()
GL.bindTexture(id)
GL.color(1f, 1f, 1f, 1f)
GL.tryBlendFuncSeparate(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR, 1, 0)

val tesellator = Tessellator.getInstance()
val renderer = tesellator.worldRenderer
renderer.begin(GL_QUADS, DefaultVertexFormats.POSITION_TEX)
val mcScale = UResolution.scaleFactor
val left = x / mcScale
val top = y / mcScale
val right = (left + (texSize * scaleX))
val bottom = (top + (texSize * scaleY))
renderer.pos(left, bottom, 0.0).tex(0.0, 1.0).endVertex()
renderer.pos(right, bottom, 0.0).tex(1.0, 1.0).endVertex()
renderer.pos(right, top, 0.0).tex(1.0, 0.0).endVertex()
renderer.pos(left, top, 0.0).tex(0.0, 0.0).endVertex()
tesellator.draw()
GL.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1, 0)
GL.disableBlend()
}


Expand All @@ -94,7 +74,7 @@ object CrosshairHUD : LegacyHud() {
TextureUtil.uploadTexture(id, cdata, size, size)
}

override fun defaultPosition() = Vec2(1920f / 2f - 7f, 1080f / 2f - 7f)
override fun defaultPosition() = Vec2(1920f / 2f, 1080f / 2f)

override fun multipleInstancesAllowed() = false

Expand Down
Loading

0 comments on commit 8247677

Please sign in to comment.