Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

ElytaHud #147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/github/darkkronicle/kronhud/KronHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
174 changes: 174 additions & 0 deletions src/main/java/io/github/darkkronicle/kronhud/gui/hud/ElytraHud.java
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add translations for these keys.

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<AnchorPoint> 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: ");
Comment on lines +79 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These strings would benefit of translations as well


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;
Comment on lines +110 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These as well

}

@Override
public List<KronConfig<?>> getConfigurationOptions() {
List<KronConfig<?>> 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;
}
Comment on lines +155 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods are very likely just the same as in the superclass and therefore unnecessary

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}