diff options
| author | Aizistral | 2022-08-18 00:56:09 +0200 |
|---|---|---|
| committer | GitHub | 2022-08-18 00:56:09 +0200 |
| commit | d7dbf9426d9a84683927a18030f894e59de87e17 (patch) | |
| tree | ec6969554f056c8a67db43badee55e4c0fe30d44 /src | |
| parent | daf343aa7ec013103b793d539d7d798960c8477c (diff) | |
| parent | 0a609d698ff69ee77c7601e69f29ad80ef677c70 (diff) | |
Merge pull request #146 from MODKILLER1001/1.19.2-ModMenu-Cloth
Adds ModMenu support for 1.19.2
Diffstat (limited to 'src')
5 files changed, 377 insertions, 7 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/core/ModMenuIntegration.java b/src/main/java/com/aizistral/nochatreports/core/ModMenuIntegration.java new file mode 100644 index 0000000..c7724bb --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/core/ModMenuIntegration.java @@ -0,0 +1,170 @@ +package com.aizistral.nochatreports.core; + +import java.util.List; + +import com.aizistral.nochatreports.gui.FontHelper; +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.locale.Language; +import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.HoverEvent; +import net.minecraft.network.chat.Style; +import net.minecraft.util.FormattedCharSequence; +import net.minecraft.network.chat.Component; + +/** + * Implementation of ModMenu and ClothConfig support for the mod. + * + * @author MODKILLER1001 + */ + +public class ModMenuIntegration implements ModMenuApi { + + private Component[] makeTooltip(String key) { + String localized = Language.getInstance().getOrDefault(key); + List<String> list = FontHelper.wrap(Minecraft.getInstance().font, localized, 250); + Component[] tooltip = new Component[list.size()]; + + for (int i = 0; i < list.size(); i++) { + tooltip[i] = Component.literal(list.get(i)); + } + + return tooltip; + } + + @Override + public ConfigScreenFactory<?> getModConfigScreenFactory() { + return screen -> { + + // Get the previous screen + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(Minecraft.getInstance().screen) + .setTitle(Component.translatable("configuration.NoChatReports.config")); + + // Set category + ConfigCategory visual = builder.getOrCreateCategory(Component.translatable("configuration.NoChatReports.category.visual")); + ConfigCategory technical = builder.getOrCreateCategory(Component.translatable("configuration.NoChatReports.category.technical")); + ConfigCategory whitelistedServers = builder.getOrCreateCategory(Component.translatable("configuration.NoChatReports.category.whitelistedServers")); + + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + // Set an option for showServerSafety + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.showServerSafety"), NoReportsConfig.getInstance().showServerSafety) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.showServerSafety.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().showServerSafety = newValue) + .build()); + + // Add link for documentation for showServerSafety + visual.addEntry(entryBuilder.startTextDescription(Component.translatable("option.NoChatReports.showServerSafety.moreInfo") + .withStyle(s -> s + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.translatable("https://github.com/Aizistral-Studios/No-Chat-Reports/wiki/Configuration-Files/#option-showserversafety"))).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/Aizistral-Studios/No-Chat-Reports/wiki/Configuration-Files/#option-showserversafety")))) + .build()); + + // Set an option for hideRedChatIndicators + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.hideRedChatIndicators"), NoReportsConfig.getInstance().hideRedChatIndicators) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.hideRedChatIndicators.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().hideRedChatIndicators = newValue) + .build()); + + // Set an option for hideYellowChatIndicators + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.hideYellowChatIndicators"), NoReportsConfig.getInstance().hideYellowChatIndicators) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.hideYellowChatIndicators.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().hideYellowChatIndicators = newValue) + .build()); + + // Set an option for hideGrayChatIndicators + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.hideGrayChatIndicators"), NoReportsConfig.getInstance().hideGrayChatIndicators) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.hideGrayChatIndicators.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().hideGrayChatIndicators = newValue) + .build()); + + // Set an option for hideWarningToast + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.hideWarningToast"), NoReportsConfig.getInstance().hideWarningToast) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.hideWarningToast.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().hideWarningToast = newValue) + .build()); + + // Set an option for alwaysHideReportButton + visual.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.alwaysHideReportButton"), NoReportsConfig.getInstance().alwaysHideReportButton) + .setDefaultValue(false) + .setTooltip(this.makeTooltip("option.NoChatReports.alwaysHideReportButton.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().alwaysHideReportButton = newValue) + .build()); + + // Set an option for demandOnServer + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.demandOnServer"), NoReportsConfig.getInstance().demandOnServer) + .setDefaultValue(false) + .setTooltip(this.makeTooltip("option.NoChatReports.demandOnServer.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().demandOnServer = newValue) + .build()); + + // Set an option for enableDebugLog + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.enableDebugLog"), NoReportsConfig.getInstance().enableDebugLog) + .setDefaultValue(false) + .setTooltip(this.makeTooltip("option.NoChatReports.enableDebugLog.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().enableDebugLog = newValue) + .build()); + + // Set an option for convertToGameMessage + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.convertToGameMessage"), NoReportsConfig.getInstance().convertToGameMessage) + .setDefaultValue(false) + .setTooltip(this.makeTooltip("option.NoChatReports.convertToGameMessage.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().convertToGameMessage = newValue) + .build()); + + // Set an option for disableTelemetry + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.disableTelemetry"), NoReportsConfig.getInstance().disableTelemetry) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.disableTelemetry.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().disableTelemetry = newValue) + .build()); + + // Set an option for showReloadButton + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.showReloadButton"), NoReportsConfig.getInstance().showReloadButton) + .setDefaultValue(true) + .setTooltip(this.makeTooltip("option.NoChatReports.showReloadButton.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().showReloadButton = newValue) + .build()); + + // Set an option for whitelistAllServers + technical.addEntry(entryBuilder.startBooleanToggle(Component.translatable("option.NoChatReports.whitelistAllServers"), NoReportsConfig.getInstance().whitelistAllServers) + .setDefaultValue(false) + .setTooltip(this.makeTooltip("option.NoChatReports.whitelistAllServers.tooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().whitelistAllServers = newValue) + .build()); + + // Instructions for adding servers + whitelistedServers.addEntry(entryBuilder.startTextDescription(Component.translatable("option.NoChatReports.whitelistedServers.instructions")) + .build()); + + // Add or remove whitelisted servers + whitelistedServers.addEntry(entryBuilder.startStrList(Component.translatable("option.NoChatReports.whitelistedServers"), NoReportsConfig.getInstance().whitelistedServers) + .setExpanded(true) + .setInsertInFront(true) + .setTooltip(this.makeTooltip("option.NoChatReports.whitelistedServers.tooltip")) + .setAddButtonTooltip(Component.translatable("option.NoChatReports.whitelistedServers.addButtonTooltip")) + .setRemoveButtonTooltip(Component.translatable("option.NoChatReports.whitelistedServers.removeButtonTooltip")) + .setSaveConsumer(newValue -> NoReportsConfig.getInstance().whitelistedServers = newValue) + .build()); + + // Save config + builder.setSavingRunnable(() -> { + NoReportsConfig.saveConfig(); + NoReportsConfig.loadConfig(); + }); + return builder.build(); + }; + } +}
\ No newline at end of file diff --git a/src/main/java/com/aizistral/nochatreports/core/NoReportsConfig.java b/src/main/java/com/aizistral/nochatreports/core/NoReportsConfig.java index c0bd6e8..97c8ade 100644 --- a/src/main/java/com/aizistral/nochatreports/core/NoReportsConfig.java +++ b/src/main/java/com/aizistral/nochatreports/core/NoReportsConfig.java @@ -39,14 +39,14 @@ public class NoReportsConfig { private static final Path CONFIG_FILE = FabricLoader.getInstance().getConfigDir().resolve("NoChatReports.json"); private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static NoReportsConfig INSTANCE; - private boolean demandOnClient = true, demandOnServer = false, enableDebugLog = false, + protected boolean demandOnClient = true, demandOnServer = false, enableDebugLog = false, convertToGameMessage = false, showServerSafety = true, hideRedChatIndicators = true, hideYellowChatIndicators = true, hideGrayChatIndicators = true, hideWarningToast = true, alwaysHideReportButton = false, disableTelemetry = true, showReloadButton = true, whitelistAllServers = false; - private List<String> whitelistedServers; + protected List<String> whitelistedServers; - private static NoReportsConfig getInstance() { + protected static NoReportsConfig getInstance() { if (INSTANCE == null) { loadConfig(); } diff --git a/src/main/java/com/aizistral/nochatreports/gui/FontHelper.java b/src/main/java/com/aizistral/nochatreports/gui/FontHelper.java new file mode 100644 index 0000000..838749b --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/gui/FontHelper.java @@ -0,0 +1,153 @@ +package com.aizistral.nochatreports.gui; + +import java.util.Arrays; +import java.util.List; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.util.FormattedCharSequence; + +/** + * I couldn't find a way to properly convert {@link FormattedCharSequence} back into + * normal string. Lol. + * + * @author Aizistral + */ + +@Environment(EnvType.CLIENT) +public final class FontHelper { + + private FontHelper() { + throw new IllegalStateException("Can't touch this"); + } + + /** + * Breaks a string into a list of pieces that will fit a specified width. + */ + + public static List<String> wrap(Font font, String str, int wrapWidth) { + return Arrays.asList(wrapFormattedStringToWidth(font, str, wrapWidth).split("\n")); + } + + /** + * Inserts newline and formatting into a string to wrap it within the specified + * width. + */ + + private static String wrapFormattedStringToWidth(Font font, String str, int wrapWidth) { + int j = sizeStringToWidth(font, str, wrapWidth); + + if (str.length() <= j) + return str; + else { + String s1 = str.substring(0, j); + char c0 = str.charAt(j); + boolean flag = c0 == 32 || c0 == 10; + String s2 = getFormatFromString(s1) + str.substring(j + (flag ? 1 : 0)); + return s1 + "\n" + wrapFormattedStringToWidth(font, s2, wrapWidth); + } + } + + /** + * Determines how many characters from the string will fit into the specified + * width. + */ + + private static int sizeStringToWidth(Font font, String str, int wrapWidth) { + int j = str.length(); + int k = 0; + int l = 0; + int i1 = -1; + + for (boolean flag = false; l < j; ++l) { + char c0 = str.charAt(l); + + switch (c0) { + case 10: + --l; + break; + case 167: + if (l < j - 1) { + ++l; + char c1 = str.charAt(l); + + if (c1 != 108 && c1 != 76) { + if (c1 == 114 || c1 == 82 || isFormatColor(c1)) { + flag = false; + } + } else { + flag = true; + } + } + + break; + case 32: + i1 = l; + default: + k += font.width(String.valueOf(c0)); + + if (flag) { + ++k; + } + } + + if (c0 == 10) { + ++l; + i1 = l; + break; + } + + if (k > wrapWidth) { + break; + } + } + + return l != j && i1 != -1 && i1 < l ? i1 : l; + } + + /** + * Checks if the char code is a hexadecimal character, used to set colour. + */ + + private static boolean isFormatColor(char colorChar) { + return colorChar >= 48 && colorChar <= 57 || colorChar >= 97 && colorChar <= 102 + || colorChar >= 65 && colorChar <= 70; + } + + /** + * Checks if the char code is O-K...lLrRk-o... used to set special formatting. + */ + + private static boolean isFormatSpecial(char formatChar) { + return formatChar >= 107 && formatChar <= 111 || formatChar >= 75 && formatChar <= 79 || formatChar == 114 + || formatChar == 82; + } + + /** + * Digests a string for nonprinting formatting characters then returns a string + * containing only that formatting. + */ + + private static String getFormatFromString(String text) { + String s1 = ""; + int i = -1; + int j = text.length(); + + while ((i = text.indexOf(167, i + 1)) != -1) { + if (i < j - 1) { + char c0 = text.charAt(i + 1); + + if (isFormatColor(c0)) { + s1 = "\u00a7" + c0; + } else if (isFormatSpecial(c0)) { + s1 = s1 + "\u00a7" + c0; + } + } + } + + return s1; + } + +} diff --git a/src/main/resources/assets/nochatreports/lang/en_us.json b/src/main/resources/assets/nochatreports/lang/en_us.json index b881a0d..3c037d7 100644 --- a/src/main/resources/assets/nochatreports/lang/en_us.json +++ b/src/main/resources/assets/nochatreports/lang/en_us.json @@ -10,5 +10,40 @@ "gui.nochatreports.unsafe_server.header": "Caution: Unsafe Server", "gui.nochatreports.unsafe_server.contents": "This server requires all chat messages to be signed, which means they can be reported to Mojang, which may result in a ban from all multiplayer servers. This likely means that the server owner kept \"enforce-secure-profile\" enabled in the server.properties, which is the default since 1.19.1. You can choose to play anyway, but No Chat Reports will not be able to protect you.", "gui.nochatreports.unsafe_server.check": "Do not show again for this server", - "gui.nochatrepords.reload_config_tooltip": "Reload No Chat Reports config. Use carefully." -} + "gui.nochatrepords.reload_config_tooltip": "Reload No Chat Reports config. Use carefully.", + + "configuration.NoChatReports.config": "No Chat Reports", + "configuration.NoChatReports.category.visual": "Visual", + "configuration.NoChatReports.category.technical": "Technical", + "configuration.NoChatReports.category.whitelistedServers": "Whitelisted Servers", + "option.NoChatReports.showServerSafety": "Show server safety", + "option.NoChatReports.showServerSafety.tooltip": "Allows the client to display server safety status in bottom-right corner of chat screen. The icon is hoverable for additional information.", + "option.NoChatReports.showServerSafety.moreInfo": "§bClick here to read more about server safety levels", + "option.NoChatReports.hideRedChatIndicators": "Hide red chat indicators", + "option.NoChatReports.hideRedChatIndicators.tooltip": "Prevents the client from displaying \"Not Secure\" indicators, in the form of red bar to the left and §c§l(§r§c!§c§l)§r icon to the right of chat message.", + "option.NoChatReports.hideYellowChatIndicators": "Hide yellow chat indicators", + "option.NoChatReports.hideYellowChatIndicators.tooltip": "Prevents the client from displaying \"Modified\" indicators, in the form of yellow bar to the left and §e§l(§r§e?§e§l)§r icon to the right of chat message.", + "option.NoChatReports.hideGrayChatIndicators": "Hide gray chat indicators", + "option.NoChatReports.hideGrayChatIndicators.tooltip": "Prevents the client from displaying special indicators for system messages, in the form of gray bar to the left.", + "option.NoChatReports.hideWarningToast": "Hide warning toast", + "option.NoChatReports.hideWarningToast.tooltip": "Removes a warning toast that is displayed when entering servers with disabled §oenforce-secure-profile§r.", + "option.NoChatReports.alwaysHideReportButton": "Always hide report button", + "option.NoChatReports.alwaysHideReportButton.tooltip": "Prevents the client from ever showing report button in Social Interactions screen. This is disabled by default, and mod simply makes button inactive on servers that have No Chat Reports installed, adding an explanation to the tooltip of the button.", + "option.NoChatReports.demandOnServer": "Demand on server", + "option.NoChatReports.demandOnServer.tooltip": "Prevents the client from joining servers that don't have No Chat Reports installed.", + "option.NoChatReports.enableDebugLog": "Enable debug log", + "option.NoChatReports.enableDebugLog.tooltip": "Enables logging of certain debug information. This is generally useless for non-developers.", + "option.NoChatReports.convertToGameMessage": "Convert to game message", + "option.NoChatReports.convertToGameMessage.tooltip": "Makes the server convert all player messages into system messages. System messages don't have any signatures attached and cannot be reported; on unmodified clients they will display with gray bar to the left.", + "option.NoChatReports.disableTelemetry": "Disable telemetry", + "option.NoChatReports.disableTelemetry.tooltip": "Allows the mod to disable built-in telemetry, similar to how No Telemetry does.", + "option.NoChatReports.showReloadButton": "Show reload button", + "option.NoChatReports.showReloadButton.tooltip": "Adds a button for config reloading to Multiplayer menu.", + "option.NoChatReports.whitelistAllServers": "Whitelist all servers", + "option.NoChatReports.whitelistAllServers.tooltip": "Makes the mod behave as if all servers were added to §oWhitelisted Servers§r list.", + "option.NoChatReports.whitelistedServers": "Whitelisted Servers", + "option.NoChatReports.whitelistedServers.tooltip": "Contains servers for which \"Unsafe Server\" warning screen should never display. Servers are added to this list automatically when you check \"Do not show again for this server\" box in that screen. Each entry is a string that is formatted as §l<server-ip>:<port>§r.", + "option.NoChatReports.whitelistedServers.addButtonTooltip": "Add server", + "option.NoChatReports.whitelistedServers.removeButtonTooltip": "Remove server", + "option.NoChatReports.whitelistedServers.instructions": "When adding a server click below Whitelisted Servers to type in the field." +}
\ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 71989d3..ddc4eac 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -22,8 +22,11 @@ "main": [ "com.aizistral.nochatreports.NoChatReports" ], - "client": [ + "client": [ "com.aizistral.nochatreports.NoChatReportsClient" + ], + "modmenu": [ + "com.aizistral.nochatreports.core.ModMenuIntegration" ] }, "mixins": [ @@ -40,5 +43,14 @@ "java": ">=17" }, "suggests": {}, - "accessWidener" : "nochatreports.accesswidener" + "accessWidener" : "nochatreports.accesswidener", + + "custom": { + "modmenu": { + "links": { + "modmenu.modrinth": "https://modrinth.com/mod/no-chat-reports", + "modmenu.curseforge": "https://www.curseforge.com/minecraft/mc-mods/no-chat-reports" + } + } + } } |
