From 358e96c51a587ad84fb41ff05e226f46b031af89 Mon Sep 17 00:00:00 2001 From: INotSleep <77581131+INotSleep@users.noreply.github.com> Date: Wed, 19 Jul 2023 21:15:26 +0300 Subject: [PATCH 1/2] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd35470..6363870 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A mod to add clean and useful HUD widgets. Download this mod on [CurseForge](https://www.curseforge.com/minecraft/mc-mods/kronhud). -Requires [DarkKore](https://www.curseforge.com/minecraft/mc-mods/darkkore) and Minecraft 1.19. +Requires [DarkKore](https://www.curseforge.com/minecraft/mc-mods/darkkore) and Minecraft 1.20.1. ## Widgets @@ -33,6 +33,7 @@ Currenty KronHUD has: - Player Model - Game Clock - IRL Clock +- Elytra helper All modules can be moved around freely, and have snapable movement so you can get everything in line! From ef00a66620d80499603d60163731ceeccc2cee66 Mon Sep 17 00:00:00 2001 From: INotSleep <77581131+INotSleep@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:31:07 +0300 Subject: [PATCH 2/2] Added ElytraHud, which allows to calculate distance, that will be traveled on elytra --- .../github/darkkronicle/kronhud/KronHUD.java | 1 + .../kronhud/gui/hud/ElytraHud.java | 174 ++++++++++++++++++ .../mixins/MixinClientPlayerEntity.java | 24 +++ 3 files changed, 199 insertions(+) create mode 100644 src/main/java/io/github/darkkronicle/kronhud/gui/hud/ElytraHud.java diff --git a/src/main/java/io/github/darkkronicle/kronhud/KronHUD.java b/src/main/java/io/github/darkkronicle/kronhud/KronHUD.java index 9b88507..e13904a 100644 --- a/src/main/java/io/github/darkkronicle/kronhud/KronHUD.java +++ b/src/main/java/io/github/darkkronicle/kronhud/KronHUD.java @@ -60,6 +60,7 @@ public void initHuds() { hudManager.add(new CompassHud()); hudManager.add(new TPSHud()); hudManager.add(new ComboHud()); + hudManager.add(new ElytraHud()); HudRenderCallback.EVENT.register(hudManager::render); hudManager.getEntries().forEach(HudEntry::init); } diff --git a/src/main/java/io/github/darkkronicle/kronhud/gui/hud/ElytraHud.java b/src/main/java/io/github/darkkronicle/kronhud/gui/hud/ElytraHud.java new file mode 100644 index 0000000..acc37a7 --- /dev/null +++ b/src/main/java/io/github/darkkronicle/kronhud/gui/hud/ElytraHud.java @@ -0,0 +1,174 @@ +package io.github.darkkronicle.kronhud.gui.hud; + +import io.github.darkkronicle.kronhud.config.*; +import io.github.darkkronicle.kronhud.gui.component.DynamicallyPositionable; +import io.github.darkkronicle.kronhud.gui.entry.TextHudEntry; +import io.github.darkkronicle.kronhud.gui.layout.AnchorPoint; +import io.github.darkkronicle.kronhud.util.ColorUtil; +import io.github.darkkronicle.kronhud.util.DrawPosition; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.Vec3d; + +import java.util.List; + +public class ElytraHud extends TextHudEntry implements DynamicallyPositionable { + + public static final Identifier ID = new Identifier("kronhud", "elytrahud"); + + private final KronColor firstColor = new KronColor("positivespeed", ID.getPath(), ColorUtil.SELECTOR_GREEN); + private final KronColor secondColor = new KronColor("negativespeed", ID.getPath(), ColorUtil.SELECTOR_RED); + private final KronColor fourthColor = new KronColor("zerospeed", ID.getPath(), ColorUtil.GRAY); + private final KronColor thirdColor = new KronColor("maintextcolor", ID.getPath(), ColorUtil.WHITE); + private final KronColor fifthColor = new KronColor("distanceColor", ID.getPath(), ColorUtil.SELECTOR_BLUE); + + private Vec3d speed; + private boolean fallFlying; + private double bottomBlockY; + + private final KronOptionList anchor = DefaultOptions.getAnchorPoint(AnchorPoint.MIDDLE_MIDDLE); + + public ElytraHud() { + super(71, 31, false); + speed = new Vec3d(0, 0, 0); + fallFlying = false; + } + + public void updateSpeed(Vec3d velocity, boolean fallFlying, double bottomBlockY) { + speed = velocity; + this.fallFlying = fallFlying; + this.bottomBlockY = bottomBlockY; + } + + + @Override + public void renderComponent(DrawContext context, float delta) { + TextRenderer textRenderer = client.textRenderer; + + DrawPosition pos = getPos(); + + double verticalSpeed = (double) Math.round(speed.getY() * 2000) /100; + double horizontalSpeed = (double) Math.round(Math.sqrt(Math.pow(speed.getX(), 2) + Math.pow(speed.getZ(), 2)) * 2000) /100; + + String distanceText = "---"; + if (verticalSpeed < 0) { + double distancePassed = (double) Math.round(bottomBlockY / Math.abs(verticalSpeed) * horizontalSpeed * 100) /100; + + StringBuilder temp = new StringBuilder(String.valueOf(distancePassed)); + String[] splitted = temp.toString().split("\\."); + String[] splitted1 = splitted[0].split(""); + + int startingSpace = splitted[0].length() % 3; + temp = new StringBuilder(); + for (int i = 0; i < splitted[0].length(); i++) { + if (startingSpace == 0) { + temp.append(" "); + startingSpace = 3; + } else startingSpace --; + temp.append(splitted1[i]); + } + distanceText = temp.toString()+"."+splitted[1]; + } + + KronColor vspeedColor = verticalSpeed == 0 ? fourthColor : verticalSpeed > 0 ? firstColor : secondColor; + KronColor hspeedColor = horizontalSpeed > 0 ? firstColor : fourthColor; + + drawString(context, textRenderer, Text.of("VSpeed: "), pos.x()+3, pos.y()+3, thirdColor.getValue().color(), true); + int currPos = textRenderer.getWidth("VSpeed: "); + + drawString(context, textRenderer, Text.of(String.valueOf(verticalSpeed)), pos.x()+3+currPos, pos.y()+3, vspeedColor.getValue().color(), true); + currPos += textRenderer.getWidth(String.valueOf(verticalSpeed)); + + drawString(context, textRenderer, Text.of(" BPS"), pos.x()+3+currPos, pos.y()+3, thirdColor.getValue().color(), true); + + drawString(context, textRenderer, Text.of("HSpeed: "), pos.x()+3, pos.y()+13, thirdColor.getValue().color(), true); + currPos = textRenderer.getWidth("HSpeed: "); + + drawString(context, textRenderer, Text.of(String.valueOf(horizontalSpeed)), pos.x()+3+currPos, pos.y()+13, hspeedColor.getValue().color(), true); + currPos += textRenderer.getWidth(String.valueOf(horizontalSpeed)); + + drawString(context, textRenderer, Text.of(" BPS"), pos.x()+3+currPos, pos.y()+13, thirdColor.getValue().color(), true); + currPos = 0; + + drawString(context, textRenderer, Text.of("Distance: "), pos.x()+3, pos.y()+23, thirdColor.getValue().color(), true); + currPos = textRenderer.getWidth("Distance: "); + + drawString(context, textRenderer, Text.of(distanceText), pos.x()+3+currPos, pos.y()+23, fifthColor.getValue().color(), true); + currPos += textRenderer.getWidth(distanceText); + + drawString(context, textRenderer, Text.of(" m"), pos.x()+3+currPos, pos.y()+23, thirdColor.getValue().color(), true); + + } + + @Override + public void renderPlaceholderComponent(DrawContext context, float delta) { + TextRenderer textRenderer = client.textRenderer; + DrawPosition pos = getPos(); + drawString(context, textRenderer, Text.of("VSpeed: "), pos.x()+3, pos.y()+3, thirdColor.getValue().color(), true); + int currPos = textRenderer.getWidth("VSpeed: "); + + drawString(context, textRenderer, Text.of(String.valueOf(0d)), pos.x()+3+currPos, pos.y()+3, fourthColor.getValue().color(), true); + currPos += textRenderer.getWidth(String.valueOf(0d)); + + drawString(context, textRenderer, Text.of(" BPS"), pos.x()+3+currPos, pos.y()+3, thirdColor.getValue().color(), true); + + drawString(context, textRenderer, Text.of("HSpeed: "), pos.x()+3, pos.y()+13, thirdColor.getValue().color(), true); + currPos = textRenderer.getWidth("HSpeed: "); + + drawString(context, textRenderer, Text.of(String.valueOf(0d)), pos.x()+3+currPos, pos.y()+13, fourthColor.getValue().color(), true); + currPos += textRenderer.getWidth(String.valueOf(0d)); + + drawString(context, textRenderer, Text.of(" BPS"), pos.x()+3+currPos, pos.y()+13, thirdColor.getValue().color(), true); + currPos = 0; + } + + @Override + public List> getConfigurationOptions() { + List> options = super.getConfigurationOptions(); + options.add(firstColor); + options.add(secondColor); + options.add(thirdColor); + options.add(fourthColor); + options.add(fifthColor); + + return options; + } + + @Override + public Identifier getId() { + return ID; + } + + @Override + public AnchorPoint getAnchor() { + return anchor.getValue(); + } + + @Override + public void setHovered(boolean hovered) { + this.hovered = hovered; + } + + @Override + public int getWidth() { + return this.width; + } + + @Override + public int getHeight() { + return this.height; + } + + @Override + public void setWidth(int width) { + this.width = width; + } + + @Override + public void setHeight(int height) { + this.height = height; + } +} diff --git a/src/main/java/io/github/darkkronicle/kronhud/mixins/MixinClientPlayerEntity.java b/src/main/java/io/github/darkkronicle/kronhud/mixins/MixinClientPlayerEntity.java index 63bc670..3fd6b2d 100644 --- a/src/main/java/io/github/darkkronicle/kronhud/mixins/MixinClientPlayerEntity.java +++ b/src/main/java/io/github/darkkronicle/kronhud/mixins/MixinClientPlayerEntity.java @@ -1,9 +1,13 @@ package io.github.darkkronicle.kronhud.mixins; import io.github.darkkronicle.kronhud.gui.HudManager; +import io.github.darkkronicle.kronhud.gui.hud.ElytraHud; import io.github.darkkronicle.kronhud.gui.hud.ToggleSprintHud; +import net.minecraft.block.AirBlock; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -17,4 +21,24 @@ private void alwaysPressed(CallbackInfo info) { MinecraftClient mc = MinecraftClient.getInstance(); mc.player.setSprinting(hud.getSprintToggled().getValue() || mc.options.sprintKey.isPressed()); } + + @Inject(method = "tickMovement", at = @At(value = "HEAD")) + private void speedGetter(CallbackInfo ci) { + ElytraHud elytraHud = (ElytraHud) HudManager.getInstance().get(ElytraHud.ID); + MinecraftClient mc = MinecraftClient.getInstance(); + assert mc.player != null; + assert mc.world != null; + Vec3d playerVel = mc.player.getVelocity(); + Vec3d speed = new Vec3d(playerVel.x, mc.player.isOnGround() && playerVel.y<0 ? 0 : playerVel.y, playerVel.z); + BlockPos playerBlockPos = mc.player.getBlockPos(); + + + for (int i = playerBlockPos.getY(); i>mc.world.getBottomY(); i--) { + if (!(mc.world.getBlockState(new BlockPos(playerBlockPos.getX(), i, playerBlockPos.getZ())).getBlock() instanceof AirBlock)) { + elytraHud.updateSpeed(speed, mc.player.isFallFlying(), mc.player.getPos().getY() - i); + return; + } + } + elytraHud.updateSpeed(speed, mc.player.isFallFlying(), mc.player.getPos().getY() - mc.world.getBottomY()); + } }