Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
11 452
src/client/java/com/kodari/customcrosshair/mixin/HudMixin.java
package com.kodari.customcrosshair.mixin;
import com.kodari.customcrosshair.CrosshairConfig;
import net.minecraft.client.AttackIndicatorStatus;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.debug.DebugScreenEntries;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.component.AttackRange;
import net.minecraft.world.phys.HitResult;
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(targets = "net.minecraft.client.gui.Hud")
public abstract class HudMixin {
private static final Identifier ATTACK_INDICATOR_FULL = Identifier.withDefaultNamespace("hud/crosshair_attack_indicator_full");
private static final Identifier ATTACK_INDICATOR_BACKGROUND = Identifier.withDefaultNamespace("hud/crosshair_attack_indicator_background");
private static final Identifier ATTACK_INDICATOR_PROGRESS = Identifier.withDefaultNamespace("hud/crosshair_attack_indicator_progress");
@Inject(
method = "extractCrosshair(Lnet/minecraft/client/gui/GuiGraphicsExtractor;Lnet/minecraft/client/DeltaTracker;)V",
at = @At("HEAD"),
cancellable = true
)
private void customcrosshair$render(GuiGraphicsExtractor graphics, DeltaTracker deltaTracker, CallbackInfo ci) {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft.player == null
|| !minecraft.options.getCameraType().isFirstPerson()
|| minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.THREE_DIMENSIONAL_CROSSHAIR)) {
return;
}
ci.cancel();
graphics.nextStratum();
int centerX = graphics.guiWidth() / 2;
int centerY = graphics.guiHeight() / 2;
int size = Math.clamp(CrosshairConfig.size, 1, 20);
int thickness = Math.clamp(CrosshairConfig.thickness, 1, 10);
int color = CrosshairConfig.color;
switch (CrosshairConfig.shape) {
case CROSS -> drawCrosshairCross(graphics, centerX, centerY, size, thickness, color);
case DOT -> drawDot(graphics, centerX, centerY, size, thickness, color);
case CIRCLE -> drawCircle(graphics, centerX, centerY, size, thickness, color);
}
if (minecraft.options.attackIndicator().get() == AttackIndicatorStatus.CROSSHAIR) {
drawAttackIndicator(graphics, minecraft, centerY + size + thickness + 2);
}
}
private static void drawCrosshairCross(GuiGraphicsExtractor graphics, int centerX, int centerY, int size, int thickness, int color) {
int gap = thickness + 1;
int halfThickness = thickness / 2;
drawRect(graphics, centerX - halfThickness, centerY - gap - size, thickness, size, color);
drawRect(graphics, centerX - halfThickness, centerY + gap, thickness, size, color);
drawRect(graphics, centerX - gap - size, centerY - halfThickness, size, thickness, color);
drawRect(graphics, centerX + gap, centerY - halfThickness, size, thickness, color);
}
private static void drawDot(GuiGraphicsExtractor graphics, int centerX, int centerY, int size, int thickness, int color) {
int diameter = Math.max(1, Math.min(size, thickness * 2));
drawRect(graphics, centerX - diameter / 2, centerY - diameter / 2, diameter, diameter, color);
}
private static void drawCircle(GuiGraphicsExtractor graphics, int centerX, int centerY, int radius, int thickness, int color) {
int innerRadius = Math.max(0, radius - thickness);
int outerSquared = radius * radius;
int innerSquared = innerRadius * innerRadius;
for (int y = -radius; y <= radius; y++) {
int outerX = (int) Math.floor(Math.sqrt(outerSquared - y * y));
if (innerRadius == 0 || y * y > innerSquared) {
drawRect(graphics, centerX - outerX, centerY + y, outerX * 2 + 1, 1, color);
} else {
int innerX = (int) Math.floor(Math.sqrt(innerSquared - y * y));
int sideWidth = outerX - innerX;
if (sideWidth > 0) {
drawRect(graphics, centerX - outerX, centerY + y, sideWidth, 1, color);
drawRect(graphics, centerX + innerX + 1, centerY + y, sideWidth, 1, color);
}
}
}
}
private static void drawRect(GuiGraphicsExtractor graphics, int x, int y, int width, int height, int color) {
if ((color & 0x00FFFFFF) == 0) {
graphics.fill(RenderPipelines.GUI_INVERT, x, y, x + width, y + height, 0xFFFFFFFF);
} else {
graphics.fill(x, y, x + width, y + height, color);
}
}
private static void drawAttackIndicator(GuiGraphicsExtractor graphics, Minecraft minecraft, int y) {
float attackStrength = minecraft.player.getAttackStrengthScale(0.0F);
boolean renderFullIndicator = false;
if (minecraft.crosshairPickEntity instanceof LivingEntity target && attackStrength >= 1.0F) {
renderFullIndicator = minecraft.player.getCurrentItemAttackStrengthDelay() > 5.0F;
renderFullIndicator &= target.isAlive();
AttackRange attackRange = minecraft.player.getActiveItem().get(DataComponents.ATTACK_RANGE);
HitResult hitResult = minecraft.hitResult;
renderFullIndicator &= attackRange == null || hitResult != null && attackRange.isInRange(minecraft.player, hitResult.getLocation());
}
int x = graphics.guiWidth() / 2 - 8;
if (renderFullIndicator) {
graphics.blitSprite(RenderPipelines.CROSSHAIR, ATTACK_INDICATOR_FULL, x, y, 16, 16);
} else if (attackStrength < 1.0F) {
int progress = (int) (attackStrength * 17.0F);
graphics.blitSprite(RenderPipelines.CROSSHAIR, ATTACK_INDICATOR_BACKGROUND, x, y, 16, 4);
graphics.blitSprite(RenderPipelines.CROSSHAIR, ATTACK_INDICATOR_PROGRESS, 16, 4, 0, 0, x, y, progress, 4);
}
}
}