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/main/java | |
| 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/main/java')
3 files changed, 326 insertions, 3 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; + } + +} |
