Skip to content

Commit

Permalink
Merge pull request #1 from Leonerd-04/1.19.x
Browse files Browse the repository at this point in the history
1.19.x
  • Loading branch information
Leonerd-04 authored Jun 8, 2023
2 parents 9e34416 + 8a6c95b commit f88f67b
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 98 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ It is recommended to use this mod with TerraformersMC's ModMenu to gain access t
## The HUD
SimpleHUD, as of now, displays the following:
- Average framerate and minimum framerate
- Server Ping (1.19.3)
- Coordinates
- Time

Besides just displaying this information, the color of the HUD itself acts as an indicator:
- The fps color changes to indicate low framerates
- The ping color changes similarly to indicate high ping
- The color of the clock changes to indicate when the player can sleep

## Compatibility
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
id 'fabric-loom' version '0.10-SNAPSHOT'
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'maven-publish'
}

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

archivesBaseName = project.archives_base_name
version = project.mod_version
Expand Down Expand Up @@ -47,7 +47,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.17 (21w19a) upwards uses Java 16.
it.options.release = 16
it.options.release = 17
}

java {
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.18.2
yarn_mappings=1.18.2+build.3
loader_version=0.14.8
minecraft_version=1.19.3
yarn_mappings=1.19.3+build.5
loader_version=0.14.12

# Mod Properties
mod_version = 0.2.0
mod_version = 0.3.1
maven_group = me.Ieonerd
archives_base_name = simplehud

# Dependencies
fabric_version=0.48.0+1.18.2
modmenu_version=3.0.0
fabric_version=0.72.0+1.19.3
modmenu_version=5.0.2
130 changes: 76 additions & 54 deletions src/main/java/me/Ieonerd/simplehud/config/SimpleHUDConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.Ieonerd.simplehud.config;

import com.mojang.serialization.Codec;
import me.Ieonerd.simplehud.SimpleHUD;
import me.Ieonerd.simplehud.gui.SimpleHUDDisplay;

Expand All @@ -10,40 +11,39 @@

import net.fabricmc.loader.api.FabricLoader;

import net.minecraft.client.option.CyclingOption;
import net.minecraft.client.option.DoubleOption;
import net.minecraft.client.option.Option;
import net.minecraft.text.TranslatableText;
import net.minecraft.text.Text;
import net.minecraft.client.option.SimpleOption;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

// Handles config, including storage, for this mod
// I figured some of this code by looking at the implementation in Mod Menu
// Credit to TerraformersMC, though I didn't use their code verbatim
public class SimpleHUDConfig {
private static CyclingOption<SimpleHUDDisplay.Clock> clockMode;
private static CyclingOption<SimpleHUDDisplay.Compass> compassMode;
private static CyclingOption<Boolean> indicateCanSleep;
private static CyclingOption<Boolean> indicateLowFps;
private static CyclingOption<Boolean> displayMinFps;
private static CyclingOption<Boolean> respectReducedF3;

// Using DoubleOption lets me use a slider
private static DoubleOption coordsRounding;
public static SimpleOption<SimpleHUDDisplay.Clock> clockMode;
public static SimpleOption<SimpleHUDDisplay.Compass> compassMode;
public static SimpleOption<Boolean> indicateCanSleep;
public static SimpleOption<Boolean> indicateLowFps;
public static SimpleOption<Boolean> displayMinFps;
public static SimpleOption<Boolean> indicateHighPing;
public static SimpleOption<Boolean> respectReducedF3;
public static SimpleOption<Integer> coordsRounding;

// An array with all the configs, specifically for rendering them in the settings screen
public static ArrayList<Option> OPTIONS;
public static ArrayList<SimpleOption<?>> OPTIONS;

// Hashmaps storing the config values
private final static HashMap<String, String> STRING_MAP = new HashMap<>();
private final static HashMap<String, Integer> INT_MAP = new HashMap<>();
private final static HashMap<String, Boolean> BOOLEAN_MAP = new HashMap<>();
private final static HashMap<String, SimpleOption<Enum<?>>> ENUM_MAP = new HashMap<>();
private final static HashMap<String, SimpleOption<Integer>> INT_MAP = new HashMap<>();
private final static HashMap<String, SimpleOption<Boolean>> BOOLEAN_MAP = new HashMap<>();

private final static Type MAP_TYPE = new TypeToken<HashMap<String, Object>>() {}.getType();
private final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
Expand All @@ -56,8 +56,6 @@ private static void prepareConfigFile(){
file = new File(FabricLoader.getInstance().getConfigDir().toFile(), SimpleHUD.MOD_ID + ".json");
}

// The config stores different config types in three hashmaps and combines them into one for storage

// Tries to load a config file; reverts to default values if not found
public static void load(){
OPTIONS = new ArrayList<>();
Expand All @@ -67,6 +65,7 @@ public static void load(){
compassMode = createEnumOption("compassMode","simplehud.config.compass", SimpleHUDDisplay.Compass.INITIALS_ONLY);
indicateCanSleep = createBoolOption("indicateCanSleep", "simplehud.config.sleep_indicator", true);
indicateLowFps = createBoolOption("indicateLowFps","simplehud.config.low_fps", true);
indicateHighPing = createBoolOption("indicateHighPing","simplehud.config.high_ping", true);
displayMinFps = createBoolOption("displayMinFps", "simplehud.config.fps_min", true);
respectReducedF3 = createBoolOption("respectReducedF3", "simplehud.config.respect_reduced_f3", false);
coordsRounding = createIntOption("coordRounding", "simplehud.config.coords", 3, 0, 6);
Expand All @@ -83,15 +82,18 @@ public static void load(){

for(String key : map.keySet()){
if(map.get(key) instanceof Double){
INT_MAP.put(key, ((Double) map.get(key)).intValue());
INT_MAP.get(key).setValue(((Double) map.get(key)).intValue());
continue;
}
if(map.get(key) instanceof Boolean){
BOOLEAN_MAP.put(key, (Boolean) map.get(key));
BOOLEAN_MAP.get(key).setValue((Boolean) map.get(key));
continue;
}

STRING_MAP.put(key, (String) map.get(key));
if(map.get(key) instanceof String){
ENUM_MAP.get(key).setValue(getEnum(key, (String) map.get(key)));
continue;
}
LOGGER.info("Setting \"{}\" could not be read", key);
}

} catch (FileNotFoundException | JsonSyntaxException e) {
Expand All @@ -104,9 +106,9 @@ public static void load(){
public static void save(){
prepareConfigFile();

HashMap<String, Object> map = new HashMap<>(STRING_MAP);
map.putAll(BOOLEAN_MAP);
map.putAll(INT_MAP);
HashMap<String, Object> map = new HashMap<>(getValues(ENUM_MAP));
map.putAll(getValues(BOOLEAN_MAP));
map.putAll(getValues(INT_MAP));

String json = GSON.toJson(map, MAP_TYPE);

Expand All @@ -120,60 +122,80 @@ public static void save(){
}
}

// Finds the class and enum value of a hashmap key's string value
// Used for converting String entries to their proper Enum types to properly read the config file
private static Enum<?> getEnum(String enumKey, String value){
return switch(enumKey){
case "clockMode" -> Enum.valueOf(SimpleHUDDisplay.Clock.class, value);
case "compassMode" -> Enum.valueOf(SimpleHUDDisplay.Compass.class, value);
default -> null;
};
}

//Maps a hashmap of SimpleOptions to a hashmap of values
private static <T> HashMap<String, T> getValues(HashMap<String, SimpleOption<T>> optionMap){
HashMap<String, T> valMap = new HashMap<>();
for(String key : optionMap.keySet()){
valMap.put(key, optionMap.get(key).getValue());
}

return valMap;
}


public static boolean getBoolConfigValue(String key){
return BOOLEAN_MAP.get(key);
return BOOLEAN_MAP.get(key).getValue();
}

public static int getIntConfigValue(String key){
return INT_MAP.get(key);
return INT_MAP.get(key).getValue();
}

public static String getConfigValue(String key){
return STRING_MAP.get(key);
return ENUM_MAP.get(key).getValue().toString();
}


private static CyclingOption<Boolean> createBoolOption(String hashKey, String translationKey, boolean defaultVal){
BOOLEAN_MAP.put(hashKey, defaultVal);

CyclingOption<Boolean> option = CyclingOption.create(
translationKey,
ignored -> BOOLEAN_MAP.get(hashKey),
(ignored, ignored2, newVal) -> BOOLEAN_MAP.put(hashKey, newVal)
);
private static SimpleOption<Boolean> createBoolOption(String hashKey, String translationKey, boolean defaultVal){
SimpleOption<Boolean> option = SimpleOption.ofBoolean(translationKey, defaultVal);

BOOLEAN_MAP.put(hashKey, option);
OPTIONS.add(option);
return option;
}

private static DoubleOption createIntOption(String hashKey, String translationKey, int defaultVal, int min, int max){
INT_MAP.put(hashKey, defaultVal);

DoubleOption option = new DoubleOption(translationKey, min, max, 1.0F,
ignored -> (double) INT_MAP.get(hashKey),
(ignored, newVal) -> INT_MAP.put(hashKey, newVal.intValue()),
(ignored, ignored2) -> {
TranslatableText valueKey = new TranslatableText(String.format(translationKey + ".%d", INT_MAP.get(hashKey)));
return new TranslatableText(translationKey, valueKey);
}
private static SimpleOption<Integer> createIntOption(String hashKey, String translationKey, int defaultVal, int min, int max){
SimpleOption<Integer> option = new SimpleOption<>(
translationKey,
SimpleOption.emptyTooltip(),
(optionText, value) -> {
Text valueKey = Text.translatable(String.format(translationKey + ".%d", value));
return Text.translatable(translationKey, valueKey);
},
new SimpleOption.ValidatingIntSliderCallbacks(min, max),
defaultVal,
value -> {}
);

INT_MAP.put(hashKey, option);
OPTIONS.add(option);
return option;
}

private static <E extends Enum<E>> CyclingOption<E> createEnumOption(String hashKey, String translationKey, E defaultVal){
STRING_MAP.put(hashKey, defaultVal.name());
private static <E extends Enum<E>> SimpleOption<E> createEnumOption(String hashKey, String translationKey, E defaultVal){
List<E> values = Arrays.asList(defaultVal.getDeclaringClass().getEnumConstants());

CyclingOption<E> option = CyclingOption.create(
SimpleOption<E> option = new SimpleOption<>(
translationKey,
defaultVal.getDeclaringClass().getEnumConstants(),
value -> new TranslatableText(translationKey + "." + value.name().toLowerCase()),
ignored -> Enum.valueOf(defaultVal.getDeclaringClass(), STRING_MAP.get(hashKey)),
(ignored, ignored2, value) -> STRING_MAP.put(hashKey, value.name())
SimpleOption.emptyTooltip(),
(optionText, value) -> Text.translatable(translationKey + "." + value.name().toLowerCase()),
new SimpleOption.PotentialValuesBasedCallbacks<>(values,
Codec.INT.xmap(values::get, values::indexOf)),
defaultVal,
value -> {}
);

ENUM_MAP.put(hashKey, (SimpleOption<Enum<?>>) option);
OPTIONS.add(option);
return option;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.screen.option.GameOptionsScreen;
import net.minecraft.client.gui.widget.ButtonListWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.option.Option;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.text.Text;

import static me.Ieonerd.simplehud.config.SimpleHUDConfig.OPTIONS;
Expand All @@ -26,12 +25,15 @@ public SimpleHUDConfigScreen(Screen parent) {

protected void init(){
buttonList = new ButtonListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
for(Option option : OPTIONS) buttonList.addSingleOptionEntry(option);
for(SimpleOption<?> option : OPTIONS) buttonList.addSingleOptionEntry(option);
this.addDrawableChild(buttonList);

this.addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height - 27, 200, 20, ScreenTexts.DONE, (button) -> {
this.client.setScreen(this.parent);
}));
this.addDrawableChild(
ButtonWidget.builder(
Text.of("Done"),
(button) -> this.client.setScreen(this.parent)
).dimensions(this.width / 2 - 100, this.height - 27, 200, 20).build()
);
}

public void removed(){
Expand Down
Loading

0 comments on commit f88f67b

Please sign in to comment.