Skip to content
Merged
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21.4+build.8
loader_version=0.16.10

# Mod Properties
mod_version=1.3.0
mod_version=1.4.0
maven_group=dev.quickinfos
archives_base_name=quickinfos

Expand Down
90 changes: 57 additions & 33 deletions src/client/java/dev/quickinfos/QuickInfosClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package dev.quickinfos;

import dev.quickinfos.config.ConfigManager;
import dev.quickinfos.exceptions.CannotCheckTriggerConditionTrackerException;
import dev.quickinfos.exceptions.CannotRenderInfoException;
import dev.quickinfos.exceptions.CannotTriggerTrackerException;
import dev.quickinfos.infos.*;
import dev.quickinfos.screen.QuickInfosScreen;
import dev.quickinfos.trackers.DeathCoordinatesTracker;
import dev.quickinfos.trackers.Tracker;
import dev.quickinfos.utils.ScreenUtils;
import dev.quickinfos.utils.StaticUtils;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
Expand All @@ -16,6 +20,7 @@
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.text.Text;
import net.minecraft.util.Colors;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

Expand All @@ -31,46 +36,48 @@ public void onInitializeClient() {
private void onInitializeLoadStatic() {
for (Tracker tracker : new Tracker[] {new DeathCoordinatesTracker()}) {
try {
StaticVariables.TRACKERS.put(tracker.getClass().getName(), tracker);
Singleton.TRACKERS.put(tracker.getClass().getName(), tracker);
} catch (Throwable e) {
System.err.println("Failed to load a tracker at start: " + e.getMessage());
}
}

for (Info info : new Info[] {new Coordinates(), new DeathCoordinates(), new TargetedBlock(), new CurrentBiome(), new FacingDirection()}) {
for (Info info : new Info[] {new Coordinates(), new DeathCoordinates(), new TargetedBlock(), new TargetedBlockCoordinates(), new CurrentBiome(), new FacingDirection()}) {
try {
StaticVariables.INFOS_INSTANCES.put(info.getClass().getName(), info);
Singleton.INFOS_INSTANCES.put(info.getClass().getName(), info);
} catch (Throwable e) {
System.err.println("Failed to load info at start: " + e.getMessage());
}
}
}

private void onInitializeLoadConfig() {
StaticVariables.config = ConfigManager.loadConfig();
if(StaticVariables.config.isValid()){
StaticVariables.useUserConfig();
Singleton.config = ConfigManager.loadConfig();
if(Singleton.config.isValid()){
Singleton.useUserConfig();
}
else {
StaticVariables.useDefaultConfig();
StaticVariables.useDefaultOrderedInfos();
Singleton.useDefaultConfig();
Singleton.useDefaultOrderedInfos();
}
}

private void onInitializeRegisterEvents() {
// #-----------------#
// # Attach Trackers #
// #-----------------#
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
if(client == null){
return;
}

for(Tracker tracker : StaticVariables.TRACKERS.values()) {
if(tracker.shouldTrigger(client)) {
tracker.trigger(client);
ClientTickEvents.START_CLIENT_TICK.register((client) -> {
for(Tracker tracker : Singleton.TRACKERS.values()) {
try {
if (tracker.shouldTrigger(client)) {
tracker.trigger(client);
}
}
}
catch (CannotCheckTriggerConditionTrackerException | CannotTriggerTrackerException e){
// Cancel other trackers if one fails as they all depend on client
return;
}
}
});

// #--------------------#
Expand All @@ -79,19 +86,30 @@ private void onInitializeRegisterEvents() {
HudLayerRegistrationCallback.EVENT.register(
layeredDrawer -> layeredDrawer.attachLayerBefore(
IdentifiedLayer.CROSSHAIR,
StaticVariables.QUICKINFOS_LAYER,
Singleton.QUICKINFOS_LAYER,
QuickInfosClient::onCrosshairRender));

// #-------------#
// # Keybindings #
// #-------------#
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
while (Singleton.TOGGLE_INFO_KEY.wasPressed()){
Singleton.SHOW = !Singleton.SHOW;
}
while (Singleton.SHOW_MENU_KEY.wasPressed()){
ScreenUtils.openScreen(client);
}
});

// #-------------#
// # /quickinfos #
// #-------------#
ClientCommandRegistrationCallback.EVENT.register(
((commandDispatcher, registryAccess) ->
commandDispatcher.register(ClientCommandManager.literal("quickinfos").executes(
commandDispatcher.register(ClientCommandManager.literal(StaticUtils.COMMAND_LITERAL).executes(
commandContext -> {
try{
MinecraftClient client = commandContext.getSource().getClient();
client.send(() -> client.setScreen(new QuickInfosScreen(Text.empty())));
ScreenUtils.openScreen(commandContext.getSource().getClient());
return 0;
}catch (Throwable e){
commandContext.getSource().sendError(Text.of(e.toString()));
Expand All @@ -110,26 +128,32 @@ private static void onCrosshairRender(DrawContext drawContext, RenderTickCounter
client == null ||
client.options.hudHidden ||
client.getDebugHud().shouldShowDebugHud() ||
StaticVariables.ORDERED_INFOS.isEmpty() ||
Singleton.ORDERED_INFOS.isEmpty() ||
client.player == null ||
client.world == null) {
client.world == null ||
!Singleton.SHOW) {
return;
}

// Split the selected infos into separate lines
String[] rawLines = StaticVariables.ORDERED_INFOS.stream().map(info -> {
try {
return info.isOn() ? info.toHUDScreen(client) : "";
} catch (Throwable e){
return "";
}
}).toArray(String[]::new);
String[] rawLines;
try {
rawLines = Singleton.ORDERED_INFOS.stream().map(info -> info.isOn() ? info.render(client) : "").toArray(String[]::new);
}
catch (CannotRenderInfoException e){
// Abort if one info cannot be rendered as they all depend on client
return;
}

Comment on lines +139 to 147
Copy link

Copilot AI Mar 28, 2025

Choose a reason for hiding this comment

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

Catching a CannotRenderInfoException for the entire stream mapping causes all info rendering to abort if one fails; consider handling exceptions on a per-info basis to allow partial rendering.

Copilot uses AI. Check for mistakes.
ArrayList<String> lines = new ArrayList<>();
for(String line : rawLines) {
if(line != null && !line.isEmpty()) lines.add(line) ;
}

renderLines(client, lines, drawContext);
}

private static void renderLines(@NotNull MinecraftClient client, @NotNull ArrayList<String> lines, @NotNull DrawContext drawContext) {
// Calculate the screen width and set a y_margin
int screenWidth = client.getWindow().getScaledWidth();
int y_margin = 2;
Expand All @@ -139,8 +163,8 @@ private static void onCrosshairRender(DrawContext drawContext, RenderTickCounter
for (String line : lines) {
int textWidth = client.textRenderer.getWidth(line);
int bottom_top = lines.size() * (client.textRenderer.fontHeight + y_margin);
int x = y_margin;
switch (StaticVariables.POSITION){
int x = 2;
switch (Singleton.POSITION){
case TOP_RIGHT:
x = screenWidth - textWidth - y_margin;
drawContext.drawText(client.textRenderer, line, x, y, Colors.WHITE, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,66 @@
import dev.quickinfos.infos.FacingDirection;
import dev.quickinfos.infos.Info;
import dev.quickinfos.trackers.Tracker;
import dev.quickinfos.utils.DefaultConfigUtils;
import dev.quickinfos.utils.KeyUtils;
import dev.quickinfos.utils.StaticUtils;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class StaticVariables {
public static final Identifier QUICKINFOS_LAYER = Identifier.of("quickinfos");
public class Singleton {
public static final Identifier QUICKINFOS_LAYER = Identifier.of(StaticUtils.LAYER_ID);
public static final HashMap<String, Info> INFOS_INSTANCES = new HashMap<>();
public static final HashMap<String, Tracker> TRACKERS = new HashMap<>();
public static final ArrayList<Info> ORDERED_INFOS = new ArrayList<>();
public static Positions POSITION;
public static KeyBinding TOGGLE_INFO_KEY;
public static KeyBinding SHOW_MENU_KEY;
public static boolean SHOW;
public static Config config;

private StaticVariables() {}

public static void useDefaultOrderedInfos() {
ORDERED_INFOS.addAll(INFOS_INSTANCES.values());
}
private Singleton() {}

public static void useDefaultConfig(){
INFOS_INSTANCES.get(Coordinates.class.getName()).setOn(true);
INFOS_INSTANCES.get(CurrentBiome.class.getName()).setOn(true);
INFOS_INSTANCES.get(FacingDirection.class.getName()).setOn(true);

POSITION = Positions.TOP_RIGHT;
SHOW = true;

TOGGLE_INFO_KEY = KeyUtils.registerToggleInfo(DefaultConfigUtils.TOGGLE_INFO_KEYCODE);
SHOW_MENU_KEY = KeyUtils.registerShowMenu(DefaultConfigUtils.SHOW_MENU_KEYCODE);
}

public static void useUserConfig(){
POSITION = config.getPosition();
public static void useDefaultOrderedInfos() {
ORDERED_INFOS.addAll(INFOS_INSTANCES.values());
}

public static void useUserConfig(){
for (Map.Entry<String, Boolean> entry : config.getEnabledModules().entrySet()) {
Info info = INFOS_INSTANCES.getOrDefault(entry.getKey(), null);
if(info != null){
info.setOn(entry.getValue());
ORDERED_INFOS.add(info);
}
}

// For new infos in mod updates
for (Info info : INFOS_INSTANCES.values()) {
if(!ORDERED_INFOS.contains(info)){
ORDERED_INFOS.add(info);
info.setOn(false);
}
}

POSITION = config.getPosition();
SHOW = config.getShow();

TOGGLE_INFO_KEY = KeyUtils.registerToggleInfo(config.getToggleKeyCode());
SHOW_MENU_KEY = KeyUtils.registerShowMenu(config.getShowMenuKeyCode());
}
}
72 changes: 63 additions & 9 deletions src/client/java/dev/quickinfos/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package dev.quickinfos.config;

import dev.quickinfos.enums.Positions;
import org.lwjgl.glfw.GLFW;

import java.util.LinkedHashMap;
import java.util.Map;

public class Config {

//#region Fields

private final Map<String, Boolean> enabledModules = new LinkedHashMap<>();
private Positions position;
private int toggleKeyCode;
private int showMenuKeyCode;
private boolean show;

public boolean isValid(){
return position != null && !enabledModules.isEmpty();
}
//#endregion Fields

//#region Getters and setters

public Map<String, Boolean> getEnabledModules() {
return enabledModules;
Expand All @@ -21,19 +28,66 @@ public void addEnabledModule(String enabledModule, boolean isOn) {
this.enabledModules.put(enabledModule, isOn);
}

public void clearEnabledModules() {
this.enabledModules.clear();
}

public Positions getPosition() {
return this.position;
}

public void setPosition(Positions position) {
this.position = position;
}

public int getToggleKeyCode() {
return toggleKeyCode;
}

public void setToggleKeyCode(int toggleKeyCode) {
this.toggleKeyCode = toggleKeyCode;
}

public boolean getShow() {
return show;
}

public void setShow(boolean show) {
this.show = show;
}

public int getShowMenuKeyCode() {
return showMenuKeyCode;
}

public void setShowMenuKeyCode(int showMenuKeyCode) {
this.showMenuKeyCode = showMenuKeyCode;
}

//#endregion Getters and setters

//#region Methods

public boolean isValid() {
return position != null && !enabledModules.isEmpty();
}

public void clearEnabledModules() {
this.enabledModules.clear();
}

public void clearPosition() {
this.position = null;
}

public void setPosition(Positions position) {
this.position = position;
public void clearToggleKeyCode() {
this.toggleKeyCode = GLFW.GLFW_KEY_K;
}

public void clearShow() {
this.show = true;
}

public void clearShowMenuKeyCode() {
this.showMenuKeyCode = GLFW.GLFW_KEY_M;
}

//#endregion Methods

}
Loading