Skip to content

Commit

Permalink
info hud
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnVN committed Jul 10, 2024
1 parent 0e34114 commit db788e1
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/client/java/autumnvn/autumn/AutumnClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AutumnClient implements ClientModInitializer {
public static KeyBinding ignorePlayerKey;
public static KeyBinding settingKey;
public static KeyBinding zoomKey;
public static double tps;

@Override
public void onInitializeClient() {
Expand Down
3 changes: 3 additions & 0 deletions src/client/java/autumnvn/autumn/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Options {
public SimpleOption<Boolean> betterChat;
public SimpleOption<Boolean> fullBright;
public SimpleOption<Boolean> horseSwim;
public SimpleOption<Boolean> infoHud;
public SimpleOption<Boolean> instantSneak;
public SimpleOption<Boolean> noFade;
public SimpleOption<Boolean> noFishingBobber;
Expand All @@ -53,6 +54,8 @@ public Options() {
options.put("fullBright", fullBright);
horseSwim = SimpleOption.ofBoolean("Horse Swim", value -> Tooltip.of(Text.of("Make riding horse swim in water")), true);
options.put("horseSwim", horseSwim);
infoHud = SimpleOption.ofBoolean("Info Hud", value -> Tooltip.of(Text.of("Show fps, coordinates, direction, tps, targeted entity health & horse stats on screen, show armor above hotbar, show hunger & xp bar when riding")), true);
options.put("infoHud", infoHud);
instantSneak = SimpleOption.ofBoolean("Instant Sneak", value -> Tooltip.of(Text.of("Instantly sneak when holding shift, no animation")), true);
options.put("instantSneak", instantSneak);
noFade = SimpleOption.ofBoolean("No Fade", value -> Tooltip.of(Text.of("Remove fade animation on splash & title screen")), true);
Expand Down
1 change: 1 addition & 0 deletions src/client/java/autumnvn/autumn/SettingsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SettingsScreen extends Screen {
AutumnClient.options.betterChat,
AutumnClient.options.fullBright,
AutumnClient.options.horseSwim,
AutumnClient.options.infoHud,
AutumnClient.options.instantSneak,
AutumnClient.options.noFade,
AutumnClient.options.noFishingBobber,
Expand Down
45 changes: 45 additions & 0 deletions src/client/java/autumnvn/autumn/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package autumnvn.autumn;

import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;

public class Utils {
private static class TimedEntity {
long time;
Entity entity;

TimedEntity(Entity entity) {
this.entity = entity;
this.time = System.currentTimeMillis();
}
}

private static TimedEntity recentTargetedEntity;

public static Entity getTargetedEntity(float tickDelta) {
Entity entity2 = AutumnClient.client.getCameraEntity();
Vec3d vec3d = entity2.getCameraPosVec(tickDelta);
Vec3d vec3d2 = entity2.getRotationVec(1);
Box box = entity2.getBoundingBox().expand(32767, 32767, 32767);
EntityHitResult entityHitResult = ProjectileUtil.raycast(entity2, vec3d, vec3d.add(vec3d2.multiply(32767)), box, entity -> !entity.isSpectator() && entity.canHit(), 32767);

if (entityHitResult != null) {
recentTargetedEntity = new TimedEntity(entityHitResult.getEntity());
return recentTargetedEntity.entity;
}

if (recentTargetedEntity != null && (System.currentTimeMillis() - recentTargetedEntity.time) > 3000) {
recentTargetedEntity = null;
}

return recentTargetedEntity == null ? null : recentTargetedEntity.entity;
}

public static String color(double value, double min, double max) {
double third = (max - min) / 3;
return value < min + third ? "§c" : value < min + third * 2 ? "§e" : "§a";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package autumnvn.autumn.mixin.client;

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;
import autumnvn.autumn.AutumnClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket;

@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin {
private long lastTime;
private long lastTick;

// TPS
@Inject(method = "onWorldTimeUpdate", at = @At("HEAD"))
private void onWorldTimeUpdate(WorldTimeUpdateS2CPacket packet, CallbackInfo ci) {
long tick = packet.getTime();
long time = System.nanoTime();
if (lastTick != 0 && lastTime != 0) {
int passedTick = (int) (tick - lastTick);
if (passedTick > 0) {
AutumnClient.tps = Math.min(1000 / ((time - lastTime) / 1000000 / passedTick), 20);
}
}
lastTick = tick;
lastTime = time;
}
}
118 changes: 118 additions & 0 deletions src/client/java/autumnvn/autumn/mixin/client/InGameHudMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package autumnvn.autumn.mixin.client;

import java.util.ArrayList;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import autumnvn.autumn.AutumnClient;
import autumnvn.autumn.Utils;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.JumpingMount;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.passive.AbstractHorseEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;

@Mixin(InGameHud.class)
public class InGameHudMixin {
private String[] direction = new String[] {"+Z", "-X+Z", "-X", "-X-Z", "-Z", "+X-Z", "+X", "+X+Z"};

@Shadow
private LivingEntity getRiddenEntity() {
return null;
}

@Shadow
private int getHeartCount(LivingEntity entity) {
return 0;
}

@Shadow
private int getHeartRows(int heartCount) {
return 0;
}

@Shadow
private void renderHotbarItem(DrawContext context, int x, int y, float f, PlayerEntity player, ItemStack stack, int seed) {}

// InfoHud
@Inject(method = "render", at = @At("TAIL"))
private void render(DrawContext context, float tickDelta, CallbackInfo ci) {
if (AutumnClient.options.infoHud.getValue() && !AutumnClient.client.options.hudHidden && !AutumnClient.client.getDebugHud().shouldShowDebugHud()) {
ArrayList<String> lines = new ArrayList<String>();
lines.add(String.format("%d fps", AutumnClient.client.getCurrentFps()));
lines.add(String.format("%d %d %d %s",
AutumnClient.client.player.getBlockX(),
AutumnClient.client.player.getBlockY(),
AutumnClient.client.player.getBlockZ(),
direction[(int) (AutumnClient.client.player.getYaw() / 45 + 0.5) & 7]));
lines.add(String.format("%.1f tps", AutumnClient.tps));
if (Utils.getTargetedEntity(tickDelta) instanceof LivingEntity livingEntity) {
float health = livingEntity.getHealth() + livingEntity.getAbsorptionAmount();
String healthLine = String.format("%s %s%.0f",
livingEntity.getDisplayName().getString(),
Utils.color(health, 0, livingEntity.getMaxHealth()),
health);
lines.add(healthLine);
int x = 2 + AutumnClient.client.textRenderer.getWidth(healthLine) + 2;
int y = 2 + (AutumnClient.client.textRenderer.fontHeight + 2) * (lines.size() - 1);
context.drawGuiTexture(new Identifier("hud/heart/container"), x, y, 9, 9);
context.drawGuiTexture(new Identifier("hud/heart/full"), x, y, 9, 9);
}
if (Utils.getTargetedEntity(tickDelta) instanceof AbstractHorseEntity abstractHorseEntity) {
double speed = abstractHorseEntity.getAttributes().getValue(EntityAttributes.GENERIC_MOVEMENT_SPEED) * 42.157787584d;
double jump = abstractHorseEntity.getJumpStrength();
double jumpHeight = -0.1817584952d * jump * jump * jump + 3.689713992d * jump * jump + 2.128599134d * jump + -0.343930367d;
lines.add(String.format("Speed %s%.1fm/s §rJump %s%.1fm",
Utils.color(speed, 4.742751103d, 14.228253309d), speed,
Utils.color(jumpHeight, 1.08623d, 5.29262d), jumpHeight));
}
lines.forEach(line -> {
context.drawText(AutumnClient.client.textRenderer, line, 2, 2 + (AutumnClient.client.textRenderer.fontHeight + 2) * lines.indexOf(line), 0xffffff, true);
});
}

// ArmorHud
if (AutumnClient.options.infoHud.getValue() && AutumnClient.client.interactionManager.hasStatusBars()) {
int y = context.getScaledWindowHeight() - 55;
if (AutumnClient.client.player.getAir() < AutumnClient.client.player.getMaxAir()) {
y -= 10;
}
if (getRiddenEntity() != null) {
y -= getHeartRows(getHeartCount(getRiddenEntity())) * 10;
}
for (int i = 0, x = 68; i < 4; i++, x -= 15) {
renderHotbarItem(context, context.getScaledWindowWidth() / 2 + x, y, tickDelta, AutumnClient.client.player, AutumnClient.client.player.getInventory().getArmorStack(i), 1);
}
}
}

// MountHud
@ModifyVariable(method = "renderMountHealth", at = @At("STORE"), ordinal = 2)
private int k(int original) {
return AutumnClient.options.infoHud.getValue() && AutumnClient.client.interactionManager.hasStatusBars() ? original - 10 : original;
}

@ModifyVariable(method = "renderStatusBars", at = @At(value = "STORE", ordinal = 1), ordinal = 13)
private int x(int original) {
return AutumnClient.options.infoHud.getValue() ? 0 : original;
}

@ModifyVariable(method = "renderStatusBars", at = @At(value = "STORE", ordinal = 1), ordinal = 10)
private int t(int original) {
return original -= AutumnClient.options.infoHud.getValue() && getRiddenEntity() != null ? getHeartRows(getHeartCount(getRiddenEntity())) * 10 : 0;
}

@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getJumpingMount()Lnet/minecraft/entity/JumpingMount;"))
private JumpingMount getJumpingMount(ClientPlayerEntity player) {
return AutumnClient.options.infoHud.getValue() && (!AutumnClient.client.interactionManager.hasExperienceBar() || AutumnClient.client.options.jumpKey.isPressed() || player.getMountJumpStrength() > 0) ? player.getJumpingMount() : null;
}
}
2 changes: 2 additions & 0 deletions src/client/resources/autumn.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"CameraMixin",
"ChatHudMixin",
"ClientPlayerInteractionManagerMixin",
"ClientPlayNetworkHandlerMixin",
"EntityMixin",
"FishingBobberEntityRendererMixin",
"GameRendererMixin",
"InGameHudMixin",
"LightmapTextureManagerMixin",
"LivingEntityMixin",
"MinecraftClientMixin",
Expand Down

0 comments on commit db788e1

Please sign in to comment.