feat: use categories in config

This commit is contained in:
Johannes Frohnmeyer 2023-07-18 21:13:28 +02:00
parent b85cd1ee94
commit 08d9c0633b
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 95 additions and 71 deletions

View File

@ -10,7 +10,7 @@ jfMod {
minecraftVersion = "1.20.1" minecraftVersion = "1.20.1"
yarn("build.9") yarn("build.9")
loaderVersion = "0.14.21" loaderVersion = "0.14.21"
libJfVersion = "3.10.1" libJfVersion = "3.10.2"
modrinth { modrinth {
projectId = "google-chat" projectId = "google-chat"

View File

@ -27,7 +27,7 @@ public class GoogleChat implements ModInitializer {
String sourceString = toString(source); String sourceString = toString(source);
if (respectRegex && failsRegex(sourceString, direction)) return source; if (respectRegex && failsRegex(sourceString, direction)) return source;
MutableText translated; MutableText translated;
if (GoogleChatConfig.desugar) { if (GoogleChatConfig.Processing.desugar) {
translated = Text.literal(translateIfNeeded(sourceString, direction, true)); translated = Text.literal(translateIfNeeded(sourceString, direction, true));
} else { } else {
translated = MutableText.of(translateIfNeeded(source.getContent(), direction, false)) translated = MutableText.of(translateIfNeeded(source.getContent(), direction, false))
@ -36,8 +36,8 @@ public class GoogleChat implements ModInitializer {
translated.append(translateIfNeeded(sibling, direction, false)); translated.append(translateIfNeeded(sibling, direction, false));
} }
} }
if (GoogleChatConfig.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translated)); if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translated));
if (GoogleChatConfig.translationTooltip) { if (GoogleChatConfig.General.translationTooltip) {
return source.copy().styled(style -> addHover(style, Text.literal("Translated: ").append(translated))); return source.copy().styled(style -> addHover(style, Text.literal("Translated: ").append(translated)));
} else if (translated.getStyle().getHoverEvent() == null) { } else if (translated.getStyle().getHoverEvent() == null) {
return translated.styled(style -> addHover(style, Text.literal("Original: ").append(source))); return translated.styled(style -> addHover(style, Text.literal("Original: ").append(source)));
@ -111,12 +111,12 @@ public class GoogleChat implements ModInitializer {
private static boolean failsRegex(String text, Direction direction) { private static boolean failsRegex(String text, Direction direction) {
boolean isSender = (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) == (direction == Direction.C2S); boolean isSender = (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) == (direction == Direction.C2S);
if (isSender) return text.matches(GoogleChatConfig.sendingRegex) == GoogleChatConfig.sendingRegexIsBlacklist; if (isSender) return text.matches(GoogleChatConfig.Processing.sendingRegex) == GoogleChatConfig.Processing.sendingRegexIsBlacklist;
else return text.matches(GoogleChatConfig.receivingRegex) == GoogleChatConfig.receivingRegexIsBlacklist; else return text.matches(GoogleChatConfig.Processing.receivingRegex) == GoogleChatConfig.Processing.receivingRegexIsBlacklist;
} }
private static boolean shouldSkipOutright(Direction direction) { private static boolean shouldSkipOutright(Direction direction) {
return !GoogleChatConfig.enabled || !hasTarget(direction); return !GoogleChatConfig.General.enabled || !hasTarget(direction);
} }
public static boolean hasTarget(Direction direction) { public static boolean hasTarget(Direction direction) {
@ -129,15 +129,15 @@ public class GoogleChat implements ModInitializer {
public String source() { public String source() {
return switch (this) { return switch (this) {
case C2S -> GoogleChatConfig.clientLanguage; case C2S -> GoogleChatConfig.General.clientLanguage;
case S2C -> GoogleChatConfig.serverLanguage; case S2C -> GoogleChatConfig.General.serverLanguage;
}; };
} }
public String target() { public String target() {
return switch (this) { return switch (this) {
case C2S -> GoogleChatConfig.serverLanguage; case C2S -> GoogleChatConfig.General.serverLanguage;
case S2C -> GoogleChatConfig.clientLanguage; case S2C -> GoogleChatConfig.General.clientLanguage;
}; };
} }
} }

View File

@ -6,10 +6,10 @@ import net.minecraft.text.Text;
import java.util.Map; import java.util.Map;
public class GoogleChatCache { public class GoogleChatCache {
private static Map<Text, Text> s2ct = new FixedSizeMap<>(GoogleChatConfig.cacheSize); private static Map<Text, Text> s2ct = new FixedSizeMap<>(GoogleChatConfig.Advanced.cacheSize);
private static Map<Text, Text> c2st = new FixedSizeMap<>(GoogleChatConfig.cacheSize); private static Map<Text, Text> c2st = new FixedSizeMap<>(GoogleChatConfig.Advanced.cacheSize);
private static Map<String, String> s2cs = new FixedSizeMap<>(GoogleChatConfig.cacheSize); private static Map<String, String> s2cs = new FixedSizeMap<>(GoogleChatConfig.Advanced.cacheSize);
private static Map<String, String> c2ss = new FixedSizeMap<>(GoogleChatConfig.cacheSize); private static Map<String, String> c2ss = new FixedSizeMap<>(GoogleChatConfig.Advanced.cacheSize);
public static void clear() { public static void clear() {
s2ct.clear(); s2ct.clear();

View File

@ -2,51 +2,57 @@ package io.gitlab.jfronny.googlechat;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.Ignore; import io.gitlab.jfronny.commons.serialize.gson.api.v1.Ignore;
import io.gitlab.jfronny.libjf.config.api.v1.*; import io.gitlab.jfronny.libjf.config.api.v1.*;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder;
import net.fabricmc.api.*; import net.fabricmc.api.*;
import net.fabricmc.loader.api.*; import net.fabricmc.loader.api.*;
@JfConfig(referencedConfigs = "libjf-translate-v1") @JfConfig(tweaker = GoogleChatConfig.class)
public class GoogleChatConfig { public class GoogleChatConfig {
@Entry public static boolean enabled = true; @Category(referencedConfigs = "libjf-translate-v1")
@Entry public static String serverLanguage = "auto"; public static class General {
@Entry public static String clientLanguage = "en"; @Entry public static boolean enabled = true;
@Entry public static boolean translationTooltip = false; @Entry public static String serverLanguage = "auto";
@Entry public static boolean desugar = false; @Entry public static String clientLanguage = "en";
@Entry public static String receivingRegex = ""; @Entry public static boolean translationTooltip = false;
@Entry public static boolean receivingRegexIsBlacklist = true;
@Entry public static String sendingRegex = "";
@Entry public static boolean sendingRegexIsBlacklist = true;
@Entry(min = 1, max = 1024) public static int cacheSize = 256;
@Entry public static boolean debugLogs = FabricLoader.getInstance().isDevelopmentEnvironment();
@Preset @Preset
public static void client() { public static void client() {
enabled = true; enabled = true;
if (!serverLanguage.equals("auto")) { if (!serverLanguage.equals("auto")) {
serverLanguage = "auto"; serverLanguage = "auto";
clientLanguage = "en"; clientLanguage = "en";
String tmp = receivingRegex; }
receivingRegex = sendingRegex; }
sendingRegex = tmp;
@Preset
public static void server() {
enabled = true;
if (!clientLanguage.equals("auto")) {
clientLanguage = "auto";
serverLanguage = "en";
}
} }
} }
@Preset @Category
public static void server() { public static class Processing {
enabled = true; @Entry public static boolean desugar = false;
if (!clientLanguage.equals("auto")) { @Entry public static String receivingRegex = "";
clientLanguage = "auto"; @Entry public static boolean receivingRegexIsBlacklist = true;
serverLanguage = "en"; @Entry public static String sendingRegex = "";
String tmp = receivingRegex; @Entry public static boolean sendingRegexIsBlacklist = true;
receivingRegex = sendingRegex; }
sendingRegex = tmp;
} @Category
public static class Advanced {
@Entry(min = 1, max = 1024) public static int cacheSize = 256;
@Entry public static boolean debugLogs = FabricLoader.getInstance().isDevelopmentEnvironment();
} }
@Ignore private static boolean initial = true; @Ignore private static boolean initial = true;
@Verifier @Verifier
public static void verify() { public static void verify() {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER && !clientLanguage.equals("auto")) { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER && !General.clientLanguage.equals("auto")) {
System.err.println(""" System.err.println("""
Your client language is not set to "auto" and you are using a server. Your client language is not set to "auto" and you are using a server.
This setup is not recommended! Please set up GoogleChat according to its documentation!"""); This setup is not recommended! Please set up GoogleChat according to its documentation!""");
@ -55,6 +61,21 @@ public class GoogleChatConfig {
initial = false; initial = false;
} }
public static ConfigBuilder<?> tweak(ConfigBuilder<?> builder) {
return builder
.addMigration("enabled", reader -> General.enabled = reader.nextBoolean())
.addMigration("serverLanguage", reader -> General.serverLanguage = reader.nextString())
.addMigration("clientLanguage", reader -> General.clientLanguage = reader.nextString())
.addMigration("translationTooltip", reader -> General.translationTooltip = reader.nextBoolean())
.addMigration("desugar", reader -> Processing.desugar = reader.nextBoolean())
.addMigration("receivingRegex", reader -> Processing.receivingRegex = reader.nextString())
.addMigration("receivingRegexIsBlacklist", reader -> Processing.receivingRegexIsBlacklist = reader.nextBoolean())
.addMigration("sendingRegex", reader -> Processing.sendingRegex = reader.nextString())
.addMigration("sendingRegexIsBlacklist", reader -> Processing.sendingRegexIsBlacklist = reader.nextBoolean())
.addMigration("cacheSize", reader -> Advanced.cacheSize = reader.nextInt())
.addMigration("debugLogs", reader -> Advanced.debugLogs = reader.nextBoolean());
}
static { static {
JFC_GoogleChatConfig.ensureInitialized(); JFC_GoogleChatConfig.ensureInitialized();
} }

View File

@ -28,12 +28,12 @@ public class GoogleChatServer implements DedicatedServerModInitializer {
return CompletableFuture.completedFuture(message); return CompletableFuture.completedFuture(message);
original = message; original = message;
message = GoogleChatCache.c2s(message); message = GoogleChatCache.c2s(message);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied C2S translation from " + original + " to " + message); if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied C2S translation from " + original + " to " + message);
} }
// All messages should be translated to the client language before sending // All messages should be translated to the client language before sending
original = message; original = message;
message = GoogleChatCache.s2c(message); message = GoogleChatCache.s2c(message);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied S2C translation from " + original + " to " + message); if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied S2C translation from " + original + " to " + message);
return CompletableFuture.completedFuture(message); return CompletableFuture.completedFuture(message);
}); });
} }

View File

@ -1,27 +1,30 @@
{ {
"google-chat.jfconfig.title": "GoogleChat", "google-chat.jfconfig.title": "GoogleChat",
"google-chat.jfconfig.enabled": "Enabled", "google-chat.jfconfig.general.title": "General",
"google-chat.jfconfig.enabled.tooltip": "Whether translations should be applied", "google-chat.jfconfig.general.enabled": "Enabled",
"google-chat.jfconfig.serverLanguage": "Server Language", "google-chat.jfconfig.general.enabled.tooltip": "Whether translations should be applied",
"google-chat.jfconfig.serverLanguage.tooltip": "The language of the server used in translations. \"auto\" will disable translating your own messages", "google-chat.jfconfig.general.serverLanguage": "Server Language",
"google-chat.jfconfig.clientLanguage": "Client Language", "google-chat.jfconfig.general.serverLanguage.tooltip": "The language of the server used in translations. \"auto\" will disable translating your own messages",
"google-chat.jfconfig.clientLanguage.tooltip": "Your own language used in translations. \"auto\" will disable translating messages by other server members", "google-chat.jfconfig.general.clientLanguage": "Client Language",
"google-chat.jfconfig.translationTooltip": "Translation Tooltip", "google-chat.jfconfig.general.clientLanguage.tooltip": "Your own language used in translations. \"auto\" will disable translating messages by other server members",
"google-chat.jfconfig.translationTooltip.tooltip": "Display translations as a tooltip (on hover) and keep the original message. This will overwrite other tooltips", "google-chat.jfconfig.general.translationTooltip": "Translation Tooltip",
"google-chat.jfconfig.desugar": "Desugar", "google-chat.jfconfig.general.translationTooltip.tooltip": "Display translations as a tooltip (on hover) and keep the original message. This will overwrite other tooltips",
"google-chat.jfconfig.desugar.tooltip": "Translate all messages as plain strings. This will remove formatting but may be more accurate", "google-chat.jfconfig.processing.title": "Processing",
"google-chat.jfconfig.receivingRegex": "Receiving Regex", "google-chat.jfconfig.processing.desugar": "Desugar",
"google-chat.jfconfig.receivingRegex.tooltip": "A Regex pattern to check whether a received message should be translated", "google-chat.jfconfig.processing.desugar.tooltip": "Translate all messages as plain strings. This improves translation quality but removes all message styling",
"google-chat.jfconfig.receivingRegexIsBlacklist": "Receiving Regex Is Blacklist", "google-chat.jfconfig.processing.receivingRegex": "Receiving Regex",
"google-chat.jfconfig.receivingRegexIsBlacklist.tooltip": "Whether the relevant regex should blacklist messages from translation instead of whitelisting", "google-chat.jfconfig.processing.receivingRegex.tooltip": "A Regex pattern to check whether a received message should be translated",
"google-chat.jfconfig.sendingRegex": "Sending Regex", "google-chat.jfconfig.processing.receivingRegexIsBlacklist": "Receiving Regex Is Blacklist",
"google-chat.jfconfig.sendingRegex.tooltip": "A Regex pattern to check whether a sent message should be translated", "google-chat.jfconfig.processing.receivingRegexIsBlacklist.tooltip": "Whether the relevant regex should blacklist messages from translation instead of whitelisting",
"google-chat.jfconfig.sendingRegexIsBlacklist": "Sending Regex Is Blacklist", "google-chat.jfconfig.processing.sendingRegex": "Sending Regex",
"google-chat.jfconfig.sendingRegexIsBlacklist.tooltip": "Whether the relevant regex should blacklist messages from translation instead of whitelisting", "google-chat.jfconfig.processing.sendingRegex.tooltip": "A Regex pattern to check whether a sent message should be translated",
"google-chat.jfconfig.cacheSize": "Cache Size", "google-chat.jfconfig.processing.sendingRegexIsBlacklist": "Sending Regex Is Blacklist",
"google-chat.jfconfig.cacheSize.tooltips": "The size of each message cache. Since there are four caches, the actual size will be four times this.", "google-chat.jfconfig.processing.sendingRegexIsBlacklist.tooltip": "Whether the relevant regex should blacklist messages from translation instead of whitelisting",
"google-chat.jfconfig.debugLogs": "Debug Logs", "google-chat.jfconfig.advanced.title": "Advanced",
"google-chat.jfconfig.debugLogs.tooltips": "Log additional information about message processing. Useful for debugging", "google-chat.jfconfig.advanced.cacheSize": "Cache Size",
"google-chat.jfconfig.advanced.cacheSize.tooltips": "The size of each message cache. Since there are four caches, the actual size will be four times this.",
"google-chat.jfconfig.advanced.debugLogs": "Debug Logs",
"google-chat.jfconfig.advanced.debugLogs.tooltips": "Log additional information about message processing. Useful for debugging",
"google-chat.jfconfig.client": "Client", "google-chat.jfconfig.client": "Client",
"google-chat.jfconfig.server": "Server" "google-chat.jfconfig.server": "Server"