Skip to content

Commit

Permalink
feat(game.widget): debug screen speed meter
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Oct 11, 2022
1 parent b9fc05e commit fcd8f6b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Expand Up @@ -2,6 +2,7 @@

import org.auioc.mcmod.arnicalib.game.config.ConfigUtils;
import org.auioc.mcmod.arnicalib.mod.client.widget.AdvancedItemTooltip;
import org.auioc.mcmod.arnicalib.mod.client.widget.DebugScreenInformation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ForgeConfigSpec;
Expand All @@ -15,6 +16,7 @@ public class AHClientConfig {
final ForgeConfigSpec.Builder b = new ForgeConfigSpec.Builder();

ConfigUtils.push(b, "advanced_item_tooltip", AdvancedItemTooltip.Config::build);
ConfigUtils.push(b, "additional_debug_information", DebugScreenInformation.Config::build);

CONFIG = b.build();
}
Expand Down
Expand Up @@ -2,15 +2,20 @@

import org.auioc.mcmod.arnicalib.mod.client.command.AHClientCommands;
import org.auioc.mcmod.arnicalib.mod.client.widget.AdvancedItemTooltip;
import org.auioc.mcmod.arnicalib.mod.client.widget.DebugScreenInformation;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

@OnlyIn(Dist.CLIENT)
public final class AHClientEventHandler {

private static final Minecraft MC = Minecraft.getInstance();

@SubscribeEvent
public static void registerCommands(final RegisterClientCommandsEvent event) {
AHClientCommands.register(event.getDispatcher());
Expand All @@ -21,4 +26,9 @@ public static void onItemTooltip(ItemTooltipEvent event) {
AdvancedItemTooltip.handle(event);
}

@SubscribeEvent
public static void onRenderTextOverlay(final RenderGameOverlayEvent.Text event) {
if (MC.options.renderDebug) DebugScreenInformation.handle(event.getLeft(), event.getRight());
}

}
@@ -0,0 +1,69 @@
package org.auioc.mcmod.arnicalib.mod.client.widget;

import java.util.ArrayList;
import org.auioc.mcmod.arnicalib.base.collection.ListUtils;
import org.auioc.mcmod.arnicalib.game.world.position.SpeedUnit;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;

public class DebugScreenInformation {

private static final Minecraft MC = Minecraft.getInstance();

public static void handle(ArrayList<String> gameInfo, ArrayList<String> systemInfo) {
if (Config.enabled.get()) {
if (!MC.options.reducedDebugInfo) handleGameInformation(gameInfo);
handleSystemInformation(systemInfo);
}
}

private static void handleGameInformation(ArrayList<String> info) {
if (Config.speedmeterEnabled.get()) {
var unit = Config.speedmeterUnit.get();
var symbol = Config.speedmeterShowUnitSymbol.get() ? String.format(" (%s)", unit.getSymbol()) : "";

var entity = MC.cameraEntity;
double vX = unit.convertFrom(entity.getX() - entity.xOld);
double vY = unit.convertFrom(entity.getY() - entity.yOld);
double vZ = unit.convertFrom(entity.getZ() - entity.zOld);
double vA = Math.sqrt(vX * vX + vY * vY + vZ * vZ);

var velocity = String.format("Velocity: %.3f / %.3f / %.3f%s", vX, vY, vZ, symbol);
var speed = String.format("Speed: %.3f%s", vA, symbol);

int at = ListUtils.indexOf(info, (l) -> l.startsWith("XYZ")) + 1;
ListUtils.add(info, at, speed);
ListUtils.add(info, at, velocity);
}
}

private static void handleSystemInformation(ArrayList<String> info) {

}


public static class Config {

public static BooleanValue enabled;

public static BooleanValue speedmeterEnabled;
public static EnumValue<SpeedUnit> speedmeterUnit;
public static BooleanValue speedmeterShowUnitSymbol;

public static void build(final ForgeConfigSpec.Builder b) {
enabled = b.define("enabled", true);

{
b.push("speedmeter");
speedmeterEnabled = b.define("enabled", true);
speedmeterUnit = b.defineEnum("unit", SpeedUnit.METRES_PER_SECOND);
speedmeterShowUnitSymbol = b.define("show_unit_symbol", true);
b.pop();
}
}

}

}

0 comments on commit fcd8f6b

Please sign in to comment.