Skip to content

Commit

Permalink
comment waila config
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Aug 9, 2024
1 parent 61f7f02 commit be05829
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/main/java/mcp/mobius/waila/Waila.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class Waila {
.file(WailaConstants.NAMESPACE + "/" + WailaConstants.WAILA)
.version(WailaConstants.CONFIG_VERSION, WailaConfig::getConfigVersion, WailaConfig::setConfigVersion)
.json5()
.commenter(WailaConfig.COMMENTER)
.gson(new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(WailaConfig.Overlay.Color.class, new WailaConfig.Overlay.Color.Adapter())
Expand Down
77 changes: 34 additions & 43 deletions src/main/java/mcp/mobius/waila/config/PluginConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package mcp.mobius.waila.config;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -23,11 +20,10 @@
import mcp.mobius.waila.api.WailaConstants;
import mcp.mobius.waila.buildconst.Tl;
import mcp.mobius.waila.config.commenter.CommenterFactories;
import mcp.mobius.waila.config.commenter.LanguageCommenter;
import mcp.mobius.waila.mcless.config.ConfigIo;
import mcp.mobius.waila.util.Log;
import net.minecraft.locale.Language;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings("unchecked")
public enum PluginConfig implements IPluginConfig {
Expand All @@ -38,53 +34,48 @@ public enum PluginConfig implements IPluginConfig {

private static final Path PATH = Waila.CONFIG_DIR.resolve(WailaConstants.NAMESPACE + "/" + WailaConstants.WAILA + "_plugins.json5");

private static final Supplier<IJsonConfig.Commenter> COMMENTER = () -> {
var translation = new HashMap<String, String>();
try (var stream = PluginConfig.class.getResourceAsStream("/assets/waila/lang/en_us.json")) {
Language.loadFromJson(Objects.requireNonNull(stream), translation::put);
} catch (IOException e) {
throw new RuntimeException(e);
}
private static final Supplier<IJsonConfig.Commenter> COMMENTER = () -> new LanguageCommenter((translation, p) -> {
if (p.size() < 2) return null;

return p -> {
if (p.size() < 2) return null;
var namespace = p.get(0);
var path = p.get(1);
var entry = getEntry(ResourceLocation.fromNamespaceAndPath(namespace, path));
var type = entry.getType();

var namespace = p.get(0);
var path = p.get(1);
var entry = getEntry(ResourceLocation.fromNamespaceAndPath(namespace, path));
var type = entry.getType();
var sb = new StringBuilder();

var sb = new StringBuilder();
var tlKey = Tl.Config.PLUGIN_ + namespace + "." + path;
sb.append(translation.getOrDefault(tlKey, tlKey));

var tlKey = Tl.Config.PLUGIN_ + namespace + "." + path;
sb.append(translation.getOrDefault(tlKey, tlKey));
var descKey = tlKey + "_desc";
if (translation.containsKey(descKey)) sb.append('\n').append(translation.get(descKey));

var descKey = tlKey + "_desc";
if (translation.containsKey(descKey)) sb.append('\n').append(translation.get(descKey));
if (type.equals(ConfigEntry.PATH)) {
sb.append("\nCustom config, open the following file\n").append(entry.getDefaultValue());
return sb.toString();
}

if (type.equals(ConfigEntry.PATH)) {
sb.append("\nCustom config, open the following file\n").append(entry.getDefaultValue());
return sb.toString();
}
if (entry.isServerRequired()) {
sb.append("\nRequire server to have WTHIT installed, if not, will be locked to ").append(entry.getClientOnlyValue());
} else if (entry.isMerged()) {
sb.append("\nThis value will get merged with the value from the server");
} else if (entry.isSynced()) {
sb.append("\nThis value will get overridden by the server");
}

if (entry.isServerRequired()) sb.append("\nRequire server to have WTHIT installed, if not, will be locked to ").append(entry.getClientOnlyValue());
else if (entry.isMerged()) sb.append("\nThis value will get merged with the value from the server");
else if (entry.isSynced()) sb.append("\nThis value will get overridden by the server");

sb.append("\nDefault value: ").append(entry.getDefaultValue().toString());
if (type.equals(ConfigEntry.ENUM)) {
sb.append("\nAvailable values: ");
var enums = ((Enum<?>) entry.getDefaultValue()).getDeclaringClass().getEnumConstants();
sb.append(enums[0].name());
for (var i = 1; i < enums.length; i++) {
var anEnum = enums[i];
sb.append(", ").append(anEnum.name());
}
sb.append("\nDefault value: ").append(entry.getDefaultValue().toString());
if (type.equals(ConfigEntry.ENUM)) {
sb.append("\nAvailable values: ");
var enums = ((Enum<?>) entry.getDefaultValue()).getDeclaringClass().getEnumConstants();
sb.append(enums[0].name());
for (var i = 1; i < enums.length; i++) {
var anEnum = enums[i];
sb.append(", ").append(anEnum.name());
}
}

return sb.toString();
};
};
return sb.toString();
});

private static final ConfigIo<Map<String, Map<String, JsonElement>>> IO = new ConfigIo<>(
LOG::warn, LOG::error,
Expand Down
117 changes: 95 additions & 22 deletions src/main/java/mcp/mobius/waila/config/WailaConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package mcp.mobius.waila.config;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
Expand All @@ -15,14 +21,79 @@
import mcp.mobius.waila.api.IJsonConfig;
import mcp.mobius.waila.api.ITheme;
import mcp.mobius.waila.api.IWailaConfig;
import mcp.mobius.waila.buildconst.Tl;
import mcp.mobius.waila.config.commenter.LanguageCommenter;
import mcp.mobius.waila.gui.hud.theme.ThemeDefinition;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

public class WailaConfig implements IWailaConfig {

interface Nested {}

@Retention(RetentionPolicy.RUNTIME)
@interface T {

String value();

}

public static final Supplier<IJsonConfig.Commenter> COMMENTER = () -> {
var defaultValue = new WailaConfig();

return new LanguageCommenter((translation, path) -> {
if (path.isEmpty()) return null;

AnnotatedElement element = null;
Object value = defaultValue;
Class<?> parentCls = WailaConfig.class;
for (var part : path) {
try {
var field = parentCls.getDeclaredField(part);
field.setAccessible(true);
value = field.get(value);

element = field;
parentCls = field.getType();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

if (value instanceof Nested) return null;

var sb = new StringBuilder();

var tlKey = element.getAnnotation(T.class);
if (tlKey != null) {
sb.append(Objects.requireNonNull(translation.get(tlKey.value())));

var descKey = tlKey.value() + "_desc";
if (translation.containsKey(descKey)) sb.append('\n').append(translation.get(descKey));

sb.append('\n');
}

if (value instanceof Enum<?> e) {
sb.append("Default value: ").append(e.name()).append("\nAvailable values: ");
var enums = e.getDeclaringClass().getEnumConstants();
sb.append(enums[0].name());
for (var i = 1; i < enums.length; i++) {
var anEnum = enums[i];
sb.append(", ").append(anEnum.name());
}
} else if (!(value instanceof Map<?, ?> || value instanceof Collection<?>)) {
sb.append("Default value: ").append(value);
}

return sb.toString();
});
};

private final General general = new General();
private final Overlay overlay = new Overlay();

@IJsonConfig.Comment("Text Formatter")
private final Formatter formatter = new Formatter();

@IJsonConfig.Comment("Internal value, DO NOT TOUCH!")
Expand Down Expand Up @@ -51,16 +122,16 @@ public Formatter getFormatter() {
return formatter;
}

public static class General implements IWailaConfig.General {
public static class General implements IWailaConfig.General, Nested {

private boolean displayTooltip = true;
private boolean shiftForDetails = false;
private boolean hideShiftText = false;
private DisplayMode displayMode = DisplayMode.TOGGLE;
private boolean hideFromPlayerList = true;
private boolean hideFromDebug = true;
private boolean enableTextToSpeech = false;
private int rateLimit = 250;
private @T(Tl.Config.DISPLAY_TOOLTIP) boolean displayTooltip = true;
private @T(Tl.Config.SNEAKY_DETAILS) boolean shiftForDetails = false;
private @T(Tl.Config.HIDE_SNEAK_TEXT) boolean hideShiftText = false;
private @T(Tl.Config.DISPLAY_MODE) DisplayMode displayMode = DisplayMode.TOGGLE;
private @T(Tl.Config.HIDE_FROM_PLAYERS) boolean hideFromPlayerList = true;
private @T(Tl.Config.HIDE_FROM_DEBUG) boolean hideFromDebug = true;
private @T(Tl.Config.TTS) boolean enableTextToSpeech = false;
private @T(Tl.Config.RATE_LIMIT) int rateLimit = 250;

@Override
public boolean isDisplayTooltip() {
Expand Down Expand Up @@ -137,12 +208,13 @@ public void setRateLimit(int rateLimit) {

}

public static class Overlay implements IWailaConfig.Overlay {
public static class Overlay implements IWailaConfig.Overlay, Nested {

private final Position position = new Position();
private final Color color = new Color();
private float scale = 1.0F;
private int fps = 30;

private @T(Tl.Config.OVERLAY_SCALE) float scale = 1.0F;
private @T(Tl.Config.OVERLAY_FPS) int fps = 30;

@Override
public Position getPosition() {
Expand Down Expand Up @@ -171,13 +243,14 @@ public void setFps(int fps) {
this.fps = fps;
}

public static class Position implements IWailaConfig.Overlay.Position {
public static class Position implements IWailaConfig.Overlay.Position, Nested {

private final Align align = new Align();
private final Align anchor = new Align();
private int x = 0;
private int y = 0;
private boolean bossBarsOverlap = false;

private @T(Tl.Config.OVERLAY_POS_X) int x = 0;
private @T(Tl.Config.OVERLAY_POS_Y) int y = 0;
private @T(Tl.Config.BOSS_BARS_OVERLAP) boolean bossBarsOverlap = false;

@Override
public int getX() {
Expand Down Expand Up @@ -216,7 +289,7 @@ public void setBossBarsOverlap(boolean bossBarsOverlap) {
this.bossBarsOverlap = bossBarsOverlap;
}

public static class Align implements IWailaConfig.Overlay.Position.Align {
public static class Align implements IWailaConfig.Overlay.Position.Align, Nested {

X x = X.CENTER;
Y y = Y.TOP;
Expand All @@ -243,14 +316,14 @@ public void setY(Y y) {

}

public static class Color implements IWailaConfig.Overlay.Color {
public static class Color implements IWailaConfig.Overlay.Color, Nested {

private static final ResourceLocation DEFAULT = Waila.id("vanilla");
private static boolean warnDeprecatedColorGetter = true;

private int backgroundAlpha = 204;
private ResourceLocation activeTheme = DEFAULT;
private @T(Tl.Config.OVERLAY_BACKGROUND_ALPHA) int backgroundAlpha = 204;
private @T(Tl.Config.OVERLAY_THEME) ResourceLocation activeTheme = DEFAULT;

@IJsonConfig.Comment("Custom Themes")
private final Map<ResourceLocation, ThemeDefinition<?>> themes = new HashMap<>();

private ThemeDefinition<?> getThemeDef() {
Expand Down Expand Up @@ -321,7 +394,7 @@ public JsonElement serialize(Color src, Type typeOfSrc, JsonSerializationContext

}

public static class Formatter implements IWailaConfig.Formatter {
public static class Formatter implements IWailaConfig.Formatter, Nested {

private String modName = "§9§o%s";
private String blockName = "§f%s";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import mcp.mobius.waila.api.IJsonConfig;
import org.jetbrains.annotations.Nullable;

public final class AnnotationCommenter implements IJsonConfig.Commenter {
public class AnnotationCommenter implements IJsonConfig.Commenter {

private final Class<?> cls;
private final Gson gson;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mcp.mobius.waila.config.commenter;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import mcp.mobius.waila.api.IJsonConfig;
import mcp.mobius.waila.config.PluginConfig;
import net.minecraft.locale.Language;
import org.jetbrains.annotations.Nullable;

public class LanguageCommenter implements IJsonConfig.Commenter {

private final Map<String, String> translation;
private final Impl impl;

public LanguageCommenter(Impl impl) {
this.impl = impl;
translation = new HashMap<>();
try (var stream = PluginConfig.class.getResourceAsStream("/assets/waila/lang/en_us.json")) {
Language.loadFromJson(Objects.requireNonNull(stream), translation::put);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public @Nullable String getComment(List<String> path) {
return impl.getComment(translation, path);
}

public interface Impl {

@Nullable
String getComment(Map<String, String> translation, List<String> path);

}

}

0 comments on commit be05829

Please sign in to comment.