Skip to content

Commit

Permalink
Changed Config system
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-makes committed Jan 31, 2024
1 parent e5f7fac commit 568808c
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 181 deletions.
4 changes: 2 additions & 2 deletions common/src/main/java/dev/schmarrn/lighty/ModeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void loadMode(ResourceLocation id) {
}

ModeLoader.mode = modeToLoad;
Config.setLastUsedMode(id);
Config.LAST_USED_MODE.setValue(id);
Compute.clear();
enable();
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public static LightyMode getCurrentMode() {
* If the requested mode isn't loaded, default to the first registered mode.
*/
public static void setLastUsedMode() {
mode = MODES.getOrDefault(Config.getLastUsedMode(), MODES.values().iterator().next());
mode = MODES.getOrDefault(Config.LAST_USED_MODE.getValue(), MODES.values().iterator().next());
}

private ModeLoader() {}
Expand Down
42 changes: 8 additions & 34 deletions common/src/main/java/dev/schmarrn/lighty/api/LightyColors.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,23 @@
import dev.schmarrn.lighty.config.Config;

public class LightyColors {
private static int GREEN = 0x00FF00;
private static int ORANGE = 0xFF6600;
private static int RED = 0xFF0000;

static {
onConfigUpdate();
}

public static void onConfigUpdate() {
GREEN = Config.getOverlayGreen();
ORANGE = Config.getOverlayOrange();
RED = Config.getOverlayRed();
}

public static int getSafe() {
return GREEN;
}

public static int getSafeARGB() {
return GREEN | 0xFF000000;
}

public static int getWarning() {
return ORANGE;
}

public static int getWarningARGB() {
return ORANGE | 0xFF000000;
private static int getSafeARGB() {
return Config.OVERLAY_GREEN.getValue() | 0xFF000000;
}

public static int getDanger() {
return RED;
private static int getWarningARGB() {
return Config.OVERLAY_ORANGE.getValue() | 0xFF000000;
}

public static int getDangerARGB() {
return RED | 0xFF000000;
private static int getDangerARGB() {
return Config.OVERLAY_RED.getValue() | 0xFF000000;
}

public static int getARGB(int blockLightLevel, int skyLightLevel) {
int color = LightyColors.getSafeARGB();

if (blockLightLevel <= Config.getBlockThreshold()) {
if (skyLightLevel <= Config.getSkyThreshold()) {
if (blockLightLevel <= Config.BLOCK_THRESHOLD.getValue()) {
if (skyLightLevel <= Config.SKY_THRESHOLD.getValue()) {
color = LightyColors.getDangerARGB();
} else {
color = LightyColors.getWarningARGB();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public static boolean isBlocked(BlockState block, BlockState up, ClientLevel wor
}

public static boolean isSafe(int blockLightLevel) {
return !(blockLightLevel <= Config.getBlockThreshold());
return !(blockLightLevel <= Config.BLOCK_THRESHOLD.getValue());
}
}
19 changes: 19 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/config/BooleanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.schmarrn.lighty.config;

public class BooleanConfig extends ConfigType<Boolean> {
BooleanConfig(String key, Boolean defaultValue) {
super(key, defaultValue);

Config.register(key, this);
}

@Override
String serialize() {
return Boolean.toString(getValue());
}

@Override
void deserialize(String value) {
setValue(Boolean.valueOf(value));
}
}
25 changes: 25 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/config/ColorConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.schmarrn.lighty.config;

public class ColorConfig extends ConfigType<Integer> {
public ColorConfig(String key, Integer defaultValue) {
super(key, defaultValue);

Config.register(key, this);
}

@Override
String serialize() {
int color = getValue();
StringBuilder ret = new StringBuilder("0x");
for (int ii = 5; ii >= 0; --ii) {
int nibble = (color & (0xF << (4*ii))) >> (4*ii);
ret.append(Integer.toHexString(nibble));
}
return ret.toString();
}

@Override
void deserialize(String color) {
setValue(Integer.parseUnsignedInt(color.replace("0x", ""), 16));
}
}
186 changes: 60 additions & 126 deletions common/src/main/java/dev/schmarrn/lighty/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,155 +16,89 @@

import dev.schmarrn.lighty.Lighty;
import dev.schmarrn.lighty.UtilDefinition;
import dev.schmarrn.lighty.api.LightyColors;
import net.minecraft.resources.ResourceLocation;

import java.io.*;
import java.util.Properties;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class Config {
private static final String PATH = UtilDefinition.INSTANCE.getConfigDir().toString() + "/lighty.config";

private final Properties properties;

private static Config config;

private static final String LAST_USED_MODE = "lighty.last_used_mode";
private static final String SKY_THRESHOLD = "lighty.sky_threshold";
private static final String BLOCK_THRESHOLD = "lighty.block_threshold";
private static final String OVERLAY_DISTANCE = "lighty.overlay_distance";
private static final String OVERLAY_BRIGHTNESS = "lighty.overlay_brightness";
private static final String SHOW_SAFE = "lighty.show_safe";

private static final String OVERLAY_GREEN = "lighty.overlay_green";
private static final String OVERLAY_ORANGE = "lighty.overlay_orange";
private static final String OVERLAY_RED = "lighty.overlay_red";

private Config() {
properties = new Properties();

try (Reader reader = new FileReader(PATH)) {
properties.load(reader);

// Set Defaults if no values are set
properties.putIfAbsent(LAST_USED_MODE, "lighty:carpet_mode");
properties.putIfAbsent(SKY_THRESHOLD, "0");
properties.putIfAbsent(BLOCK_THRESHOLD, "0");
properties.putIfAbsent(OVERLAY_DISTANCE, "2");
properties.putIfAbsent(OVERLAY_BRIGHTNESS, "10");
properties.putIfAbsent(SHOW_SAFE, String.valueOf(true));
properties.putIfAbsent(OVERLAY_GREEN, Integer.toHexString(0x00FF00));
properties.putIfAbsent(OVERLAY_ORANGE, Integer.toHexString(0xFF6600));
properties.putIfAbsent(OVERLAY_RED, Integer.toHexString(0xFF0000));
} catch (FileNotFoundException e) {
Lighty.LOGGER.warn("No Lighty config found at {}, loading defaults and saving config file.", PATH);

// Add Defaults here
properties.setProperty(LAST_USED_MODE, "lighty:carpet_mode");
properties.setProperty(SKY_THRESHOLD, "0");
properties.setProperty(BLOCK_THRESHOLD, "0");
properties.setProperty(OVERLAY_DISTANCE, "2");
properties.setProperty(OVERLAY_BRIGHTNESS, "10");
properties.setProperty(SHOW_SAFE, String.valueOf(true));

properties.setProperty(OVERLAY_GREEN, Integer.toHexString(0x00FF00));
properties.setProperty(OVERLAY_ORANGE, Integer.toHexString(0xFF6600));
properties.setProperty(OVERLAY_RED, Integer.toHexString(0xFF0000));
} catch (IOException e) {
Lighty.LOGGER.error("Error while reading from Lighty config at {}: {}", PATH, e);
}

this.write();
}

private void write() {
try (FileWriter writer = new FileWriter(PATH)){
properties.store(writer, null);
} catch (IOException e) {
Lighty.LOGGER.error("Error while writing to Lighty config at {}: {}", PATH, e);
}
}
private static final String PATH = UtilDefinition.INSTANCE.getConfigDir().toString() + "/lighty3.config";
private static final Map<String, String> fileState = new HashMap<>();
private static final Map<String, ConfigSerDe> configValues = new HashMap<>();

public static int getSkyThreshold() {
return Integer.parseInt(config.properties.getProperty(SKY_THRESHOLD, "0"));
}
public static final ResourceLocationConfig LAST_USED_MODE = new ResourceLocationConfig("lighty.last_used_mode", new ResourceLocation("lighty:carpet_mode"));

public static int getBlockThreshold() {
return Integer.parseInt(config.properties.getProperty(BLOCK_THRESHOLD, "0"));
}
public static final IntegerConfig SKY_THRESHOLD = new IntegerConfig("lighty.sky_threshold", 0);
public static final IntegerConfig BLOCK_THRESHOLD = new IntegerConfig("lighty.block_threshold", 0);
public static final IntegerConfig OVERLAY_DISTANCE = new IntegerConfig("lighty.overlay_distance", 2);
public static final IntegerConfig OVERLAY_BRIGHTNESS = new IntegerConfig("lighty.overlay_brightness", 10);

public static int getOverlayDistance() {
return Integer.parseInt(config.properties.getProperty(OVERLAY_DISTANCE, "2"));
}
public static final BooleanConfig SHOW_SAFE = new BooleanConfig("lighty.show_safe", true);

public static int getOverlayBrightness() {
return Integer.parseInt(config.properties.getProperty(OVERLAY_BRIGHTNESS, "10"));
}
public static boolean getShowSafe() {
return Boolean.parseBoolean(config.properties.getProperty(SHOW_SAFE, String.valueOf(true)));
}
public static final ColorConfig OVERLAY_GREEN = new ColorConfig("lighty.overlay_green", 0x00FF00);
public static final ColorConfig OVERLAY_ORANGE = new ColorConfig("lighty.overlay_orange", 0xFF6600);
public static final ColorConfig OVERLAY_RED = new ColorConfig("lighty.overlay_red", 0xFF0000);

public static ResourceLocation getLastUsedMode() {
return new ResourceLocation(config.properties.getProperty(LAST_USED_MODE, "lighty:carpet_mode"));
private static void afterRegister(String key, ConfigSerDe type) {
String value = fileState.getOrDefault(key, null);
if (value != null) {
type.deserialize(value);
}
}

public static void setLastUsedMode(ResourceLocation id) {
config.properties.setProperty(LAST_USED_MODE, id.toString());
config.write();
public static void register(String key, ConfigSerDe type) {
configValues.put(key, type);
afterRegister(key, type);
}

public static void setSkyThreshold(int i) {
config.properties.setProperty(SKY_THRESHOLD, String.valueOf(i));
config.write();
}
public static void reloadFromDisk() {
File file = new File(PATH);
try {
BufferedReader bf = new BufferedReader(new FileReader(file));

public static void setBlockThreshold(int i) {
config.properties.setProperty(BLOCK_THRESHOLD, String.valueOf(i));
config.write();
}
bf.lines().forEach((line) -> {
var splits = line.split("=");
if (splits.length == 2) {
fileState.putIfAbsent(splits[0].trim(), splits[1].trim());
} else {
Lighty.LOGGER.warn("Too many = signs on some line in the config, good luck finding them.");
}
});
bf.close();
} catch (FileNotFoundException e) {
Lighty.LOGGER.warn("No Lighty config found at {}, using defaults.", PATH);
} catch (IOException e) {
Lighty.LOGGER.error("Could not close Lighty config at {}. This should not happen, please report on GitHub. Abort.", PATH);
throw new RuntimeException(e);
}

public static void setOverlayDistance(int i) {
config.properties.setProperty(OVERLAY_DISTANCE, String.valueOf(i));
config.write();
for (var entry : configValues.entrySet()) {
afterRegister(entry.getKey(), entry.getValue());
}
}

public static void setOverlayBrightness(int i) {
config.properties.setProperty(OVERLAY_BRIGHTNESS, String.valueOf(i));
config.write();
}
public static void save() {
StringBuilder content = new StringBuilder();

public static void setShowSafe(boolean b) {
config.properties.setProperty(SHOW_SAFE, String.valueOf(b));
config.write();
}
for (var pair : configValues.entrySet()) {
content.append(pair.getKey()).append("=").append(pair.getValue().serialize()).append("\n");
}

public static void setOverlayGreen(int color) {
config.properties.setProperty(OVERLAY_GREEN, Integer.toHexString(color));
config.write();
LightyColors.onConfigUpdate();
}
public static void setOverlayOrange(int color) {
config.properties.setProperty(OVERLAY_ORANGE, Integer.toHexString(color));
config.write();
LightyColors.onConfigUpdate();
}
public static void setOverlayRed(int color) {
config.properties.setProperty(OVERLAY_RED, Integer.toHexString(color));
config.write();
LightyColors.onConfigUpdate();
}
File file = new File(PATH);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(content.toString());
bw.flush();
bw.close();
} catch (IOException e) {
Lighty.LOGGER.warn("Could not write Lighty config file at {}. Config changes are not saved.", PATH);
}

public static int getOverlayGreen() {
return Integer.parseUnsignedInt(config.properties.getProperty(OVERLAY_GREEN, Integer.toHexString(0x00FF00)), 16);
}
public static int getOverlayOrange() {
return Integer.parseUnsignedInt(config.properties.getProperty(OVERLAY_ORANGE, Integer.toHexString(0xFF6600)), 16);
}
public static int getOverlayRed() {
return Integer.parseUnsignedInt(config.properties.getProperty(OVERLAY_RED, Integer.toHexString(0xFF0000)), 16);
}

public static void init() {
config = new Config();
reloadFromDisk();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.schmarrn.lighty.config;

public abstract class ConfigSerDe {
abstract String serialize();
abstract void deserialize(String value);
}
34 changes: 34 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/config/ConfigType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.schmarrn.lighty.config;

abstract class ConfigType<T> extends ConfigSerDe{
public void onChange(T value){}

private final T DEFAULT_VALUE;
private final String KEY;

private T value;

public T getValue() {
return value;
}

public void setValue(T newValue) {
value = newValue;
onChange(newValue);
Config.save();
}

public void resetToDefault() {
value = DEFAULT_VALUE;
}

public String getKey() {
return KEY;
}

ConfigType(String key, T defaultValue) {
this.KEY = key;
this.DEFAULT_VALUE = defaultValue;
this.value = defaultValue;
}
}
Loading

0 comments on commit 568808c

Please sign in to comment.