mirror of
https://github.com/lukasabbe/SimpleTransportHud.git
synced 2026-04-30 10:50:53 +00:00
Fixed speed, pitch & coords
This commit is contained in:
@@ -3,7 +3,9 @@ package com.lukasabbe.simplehud;
|
|||||||
import com.lukasabbe.simplehud.config.Config;
|
import com.lukasabbe.simplehud.config.Config;
|
||||||
import com.lukasabbe.simplehud.huds.ElytraHud;
|
import com.lukasabbe.simplehud.huds.ElytraHud;
|
||||||
import com.lukasabbe.simplehud.huds.SimpleHud;
|
import com.lukasabbe.simplehud.huds.SimpleHud;
|
||||||
|
import com.lukasabbe.simplehud.tools.ElytraTools;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
|
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -22,5 +24,6 @@ public class SimpleHudMod implements ClientModInitializer {
|
|||||||
Config.HANDLER.load();
|
Config.HANDLER.load();
|
||||||
configInstance = Config.HANDLER.instance();
|
configInstance = Config.HANDLER.instance();
|
||||||
HUD_LIST.forEach(simpleHud -> HudElementRegistry.addFirst(simpleHud.getIdentifier(), simpleHud::render));
|
HUD_LIST.forEach(simpleHud -> HudElementRegistry.addFirst(simpleHud.getIdentifier(), simpleHud::render));
|
||||||
|
ClientTickEvents.END_CLIENT_TICK.register(client -> ElytraTools.tickElytraTools());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ public class Config {
|
|||||||
@SerialEntry
|
@SerialEntry
|
||||||
public HudPosition hudPosition = HudPosition.CENTER;
|
public HudPosition hudPosition = HudPosition.CENTER;
|
||||||
|
|
||||||
|
@SerialEntry
|
||||||
|
public SpeedEnum speedEnum = SpeedEnum.kmh;
|
||||||
|
|
||||||
@SerialEntry
|
@SerialEntry
|
||||||
public boolean ignoreSafeArea = false;
|
public boolean ignoreSafeArea = false;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.lukasabbe.simplehud.config;
|
||||||
|
|
||||||
|
import dev.isxander.yacl3.api.NameableEnum;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
|
||||||
|
public enum SpeedEnum implements NameableEnum {
|
||||||
|
kmh,
|
||||||
|
mph,
|
||||||
|
ms;
|
||||||
|
@Override
|
||||||
|
public Component getDisplayName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.lukasabbe.simplehud.huds;
|
package com.lukasabbe.simplehud.huds;
|
||||||
|
|
||||||
import com.lukasabbe.simplehud.Constants;
|
import com.lukasabbe.simplehud.Constants;
|
||||||
|
import com.lukasabbe.simplehud.SimpleHudMod;
|
||||||
|
import com.lukasabbe.simplehud.tools.ElytraTools;
|
||||||
import net.minecraft.client.DeltaTracker;
|
import net.minecraft.client.DeltaTracker;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.resources.Identifier;
|
import net.minecraft.resources.Identifier;
|
||||||
@@ -11,9 +13,42 @@ public class ElytraHud implements SimpleHud {
|
|||||||
if(!isHudActivated()) return;
|
if(!isHudActivated()) return;
|
||||||
//if(!ElytraTools.isFlying()) return;
|
//if(!ElytraTools.isFlying()) return;
|
||||||
if(client.noRender) return;
|
if(client.noRender) return;
|
||||||
|
if(client.player == null) return;
|
||||||
|
|
||||||
|
int[] pos = getCornerPos();
|
||||||
|
int x = pos[0];
|
||||||
|
int y = pos[1];
|
||||||
|
|
||||||
renderBackPlate(graphics);
|
renderBackPlate(graphics);
|
||||||
|
|
||||||
|
int textX = 5;
|
||||||
|
float textScale = 0.6f;
|
||||||
|
int whiteColor = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
int pitchTextY = 5;
|
||||||
|
renderCenteredScaledText(graphics, String.format("%d°", ElytraTools.getPitch()), x + textX, y + pitchTextY, whiteColor, textScale);
|
||||||
|
|
||||||
|
int speedTextY = 15;
|
||||||
|
renderCenteredScaledText(graphics, getSpeed(), x + textX, y + speedTextY, whiteColor, textScale);
|
||||||
|
|
||||||
|
int coordinatesTextY = 25;
|
||||||
|
String coordinatesText = String.format("%.0f:%.0f:%.0f", client.player.getX(), client.player.getY(), client.player.getZ());
|
||||||
|
|
||||||
|
int maxAvailableWidth = 40;
|
||||||
|
int textWidth = client.font.width(coordinatesText);
|
||||||
|
|
||||||
|
float maxScale = (float) maxAvailableWidth / textWidth;
|
||||||
|
float finalScale = Math.min(textScale, maxScale);
|
||||||
|
|
||||||
|
renderCenteredScaledText(graphics, coordinatesText, x + textX, y + coordinatesTextY, whiteColor, finalScale);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getSpeed(){
|
||||||
|
return switch (SimpleHudMod.configInstance.speedEnum){
|
||||||
|
case kmh -> String.format("%.1f km/h", ElytraTools.getSpeedKmh());
|
||||||
|
case mph -> String.format("%.1f mph", ElytraTools.getSpeedMph());
|
||||||
|
case ms -> String.format("%.1f m/s", ElytraTools.getSpeedMs());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ public interface SimpleHud {
|
|||||||
Minecraft client = Minecraft.getInstance();
|
Minecraft client = Minecraft.getInstance();
|
||||||
Identifier backPlateAsset = Identifier.fromNamespaceAndPath(Constants.MOD_ID, "textures/backplate.png");
|
Identifier backPlateAsset = Identifier.fromNamespaceAndPath(Constants.MOD_ID, "textures/backplate.png");
|
||||||
|
|
||||||
|
|
||||||
void render(GuiGraphics graphics, DeltaTracker tracker);
|
void render(GuiGraphics graphics, DeltaTracker tracker);
|
||||||
|
|
||||||
Identifier getIdentifier();
|
Identifier getIdentifier();
|
||||||
@@ -23,15 +22,9 @@ public interface SimpleHud {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default void renderBackPlate(GuiGraphics graphics){
|
default void renderBackPlate(GuiGraphics graphics){
|
||||||
int screenWidth = client.getWindow().getGuiScaledWidth();
|
int[] pos = getCornerPos();
|
||||||
int screenHeight = client.getWindow().getGuiScaledHeight();
|
|
||||||
|
|
||||||
//Creates pos for HUD based on config
|
|
||||||
int[] pos = calculateHudPosition(screenWidth, screenHeight, SimpleHudMod.configInstance.hudPosition);
|
|
||||||
int x = pos[0];
|
int x = pos[0];
|
||||||
int y = pos[1];
|
int y = pos[1];
|
||||||
int centeredX = 50;
|
|
||||||
int centeredY = 60;
|
|
||||||
|
|
||||||
int backPlateCornerX = 0;
|
int backPlateCornerX = 0;
|
||||||
int backPlateCornerY = 0;
|
int backPlateCornerY = 0;
|
||||||
@@ -42,7 +35,7 @@ public interface SimpleHud {
|
|||||||
graphics.blit(
|
graphics.blit(
|
||||||
RenderPipelines.GUI_TEXTURED,
|
RenderPipelines.GUI_TEXTURED,
|
||||||
backPlateAsset,
|
backPlateAsset,
|
||||||
x - centeredX, y - centeredY,
|
x, y,
|
||||||
backPlateCornerX, backPlateCornerY,
|
backPlateCornerX, backPlateCornerY,
|
||||||
backPlateWidth, backPlateHeight,
|
backPlateWidth, backPlateHeight,
|
||||||
backPlateWidth, backPlateHeight
|
backPlateWidth, backPlateHeight
|
||||||
@@ -71,4 +64,26 @@ public interface SimpleHud {
|
|||||||
default -> new int[]{screenWidth / 2, screenHeight - 25}; // CENTER
|
default -> new int[]{screenWidth / 2, screenHeight - 25}; // CENTER
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void renderCenteredScaledText(GuiGraphics graphics, String text, int centerX, int y, int color, float scale){
|
||||||
|
var stack = graphics.pose();
|
||||||
|
stack.pushMatrix();
|
||||||
|
stack.translate(centerX, y);
|
||||||
|
stack.scale(scale, scale);
|
||||||
|
stack.translate(-centerX, -y);
|
||||||
|
graphics.drawString(client.font, text, centerX, y, color);
|
||||||
|
stack.popMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
default int[] getCornerPos(){
|
||||||
|
int backPlateCenteredX = 50;
|
||||||
|
int backPlateCenteredY = 60;
|
||||||
|
|
||||||
|
int screenWidth = client.getWindow().getGuiScaledWidth();
|
||||||
|
int screenHeight = client.getWindow().getGuiScaledHeight();
|
||||||
|
int[] pos = calculateHudPosition(screenWidth, screenHeight, SimpleHudMod.configInstance.hudPosition);
|
||||||
|
int x = pos[0] - backPlateCenteredX;
|
||||||
|
int y = pos[1] - backPlateCenteredY;
|
||||||
|
return new int[]{x, y};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,65 @@ package com.lukasabbe.simplehud.tools;
|
|||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.player.LocalPlayer;
|
import net.minecraft.client.player.LocalPlayer;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
import org.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
public class ElytraTools {
|
public class ElytraTools {
|
||||||
private static final LocalPlayer player = Minecraft.getInstance().player;
|
private static Vec3 oldPos = null;
|
||||||
|
|
||||||
|
private static double speed = 0;
|
||||||
|
|
||||||
|
public static void tickElytraTools(){
|
||||||
|
calculateSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void calculateSpeed(){
|
||||||
|
var player = getLocalPlayer();
|
||||||
|
if(player == null) return;
|
||||||
|
if(oldPos == null){
|
||||||
|
oldPos = player.getPosition(0f);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vec3 newPos = player.getPosition(0f);
|
||||||
|
double distance = newPos.distanceTo(oldPos);
|
||||||
|
oldPos = newPos;
|
||||||
|
speed = distance;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isFlying(){
|
public static boolean isFlying(){
|
||||||
|
var player = getLocalPlayer();
|
||||||
if(player == null) return false;
|
if(player == null) return false;
|
||||||
return player.isFallFlying();
|
return player.isFallFlying();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getPitch(){
|
||||||
|
var player = getLocalPlayer();
|
||||||
|
if(player == null) return 0;
|
||||||
|
return (int)player.getXRot();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getYaw(){
|
||||||
|
var player = getLocalPlayer();
|
||||||
|
if(player == null) return 0;
|
||||||
|
return (int)player.getYRot();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static @Nullable LocalPlayer getLocalPlayer() {
|
||||||
|
return Minecraft.getInstance().player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getRawSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
public static double getSpeedMs(){
|
||||||
|
return speed * 20;
|
||||||
|
}
|
||||||
|
public static double getSpeedKmh(){
|
||||||
|
return getSpeedMs() * 3.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getSpeedMph(){
|
||||||
|
return getSpeedMs() * 2.23694;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user