From 3d6e70c4f3ed21d640d7355e73dc7b5fc80d1a65 Mon Sep 17 00:00:00 2001 From: FalsePattern <30945458+FalsePattern@users.noreply.github.com> Date: Fri, 10 Jun 2022 12:43:03 +0200 Subject: [PATCH] put update chatification code in public library, plus misc cleanup --- .../lib/internal/FalsePatternLib.java | 2 + .../lib/internal/proxy/ClientProxy.java | 29 ++------------ .../lib/internal/proxy/CommonProxy.java | 8 ++-- .../lib/updates/UpdateChecker.java | 39 +++++++++++++++++++ .../assets/falsepatternlib/lang/en_US.lang | 2 +- 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java b/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java index 65a0b8e..78f8f31 100644 --- a/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java +++ b/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java @@ -47,6 +47,8 @@ import java.util.concurrent.atomic.AtomicBoolean; acceptedMinecraftVersions = "[1.7.10]", acceptableRemoteVersions = "*") public class FalsePatternLib { + public static final String UPDATE_URL = "https://falsepattern.com/mc/versions.json"; + @Getter private static final Logger log = LogManager.getLogger(Tags.MODNAME); @Getter private static final boolean developerEnvironment = diff --git a/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java b/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java index a334361..95c65f8 100644 --- a/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java +++ b/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java @@ -1,7 +1,9 @@ package com.falsepattern.lib.internal.proxy; import com.falsepattern.lib.internal.FalsePatternLib; +import com.falsepattern.lib.internal.Tags; import com.falsepattern.lib.text.FormattedText; +import com.falsepattern.lib.updates.UpdateChecker; import com.falsepattern.lib.util.Async; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.event.FMLPostInitializationEvent; @@ -49,32 +51,7 @@ public class ClientProxy extends CommonProxy { return null; } val updates = updatesFuture.get(); - if (updates == null || updates.size() == 0) - return null; - val updateText = new ArrayList(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable")).toChatText()); - val mods = Loader.instance().getIndexedModList(); - for (val update : updates) { - val mod = mods.get(update.modID); - updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText()); - updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText()); - updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText()); - if (!update.updateURL.isEmpty()) { - val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText(); - val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText(); - val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText(); - pre.get(pre.size() - 1).appendSibling(link.get(0)); - link.get(link.size() - 1).appendSibling(post.get(0)); - for (val l : link) { - l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL)); - } - link.remove(0); - post.remove(0); - updateText.addAll(pre); - updateText.addAll(link); - updateText.addAll(post); - } - } - return updateText; + return UpdateChecker.updateListToChatMessages(Tags.MODNAME, updates); } }); } diff --git a/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java b/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java index 5bb0d3f..09bd9c7 100644 --- a/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java +++ b/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java @@ -32,11 +32,11 @@ public class CommonProxy { try { ConfigurationManager.registerConfig(LibraryConfig.class); } catch (ConfigException ex) { - FalsePatternLib.getLog().error("Failed to register " + Tags.MODNAME + " config!", ex); + FalsePatternLib.getLog().error("Failed to register config!", ex); } if (LibraryConfig.ENABLE_UPDATE_CHECKER) { FalsePatternLib.getLog().info("Launching asynchronous update check."); - val updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json"); + val updateCheckFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL); updatesFuture = Async.asyncWorker.submit(new Callable>() { @Override public List call() { @@ -56,9 +56,9 @@ public class CommonProxy { update.log(FalsePatternLib.getLog()); } } else if (updates == null) { - FalsePatternLib.getLog().warn("Unknown error while checking updates"); + FalsePatternLib.getLog().warn("Unknown error while checking updates."); } else { - FalsePatternLib.getLog().info("All FalsePattern mods up to date!"); + FalsePatternLib.getLog().info("All checked mods up to date!"); updates = null; } return updates; diff --git a/src/main/java/com/falsepattern/lib/updates/UpdateChecker.java b/src/main/java/com/falsepattern/lib/updates/UpdateChecker.java index 1b10185..e6e58c2 100644 --- a/src/main/java/com/falsepattern/lib/updates/UpdateChecker.java +++ b/src/main/java/com/falsepattern/lib/updates/UpdateChecker.java @@ -8,9 +8,13 @@ import com.falsepattern.lib.internal.FalsePatternLib; import com.falsepattern.lib.internal.Internet; import com.falsepattern.lib.internal.LibraryConfig; import com.falsepattern.lib.internal.Tags; +import com.falsepattern.lib.text.FormattedText; import com.falsepattern.lib.util.Async; import cpw.mods.fml.common.Loader; import lombok.val; +import net.minecraft.client.resources.I18n; +import net.minecraft.event.ClickEvent; +import net.minecraft.util.IChatComponent; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -138,4 +142,39 @@ public class UpdateChecker { } return result; } + + /** + * Formats the raw list of updates into lines of chat messages you can send to players. + * @param initiator Who/what/which mod did this update check + * @param updates The list of updates to convert + * @return A list of chat messages that can be sent to players + */ + public static List updateListToChatMessages(String initiator, List updates) { + if (updates == null || updates.size() == 0) + return null; + val updateText = new ArrayList(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable", initiator)).toChatText()); + val mods = Loader.instance().getIndexedModList(); + for (val update : updates) { + val mod = mods.get(update.modID); + updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText()); + updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText()); + updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText()); + if (!update.updateURL.isEmpty()) { + val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText(); + val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText(); + val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText(); + pre.get(pre.size() - 1).appendSibling(link.get(0)); + link.get(link.size() - 1).appendSibling(post.get(0)); + for (val l : link) { + l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL)); + } + link.remove(0); + post.remove(0); + updateText.addAll(pre); + updateText.addAll(link); + updateText.addAll(post); + } + } + return updateText; + } } diff --git a/src/main/resources/assets/falsepatternlib/lang/en_US.lang b/src/main/resources/assets/falsepatternlib/lang/en_US.lang index 31079e4..0ba9ee4 100644 --- a/src/main/resources/assets/falsepatternlib/lang/en_US.lang +++ b/src/main/resources/assets/falsepatternlib/lang/en_US.lang @@ -2,7 +2,7 @@ config.falsepatternlib.updatecheck=Enable update checks config.falsepatternlib.updatecheck.tooltip=Used to control whether FalsePatternLib should check for outdated mods. config.falsepatternlib.disableinternet=FalsePatternLib Offline Mode config.falsepatternlib.disableinternet.tooltip=Used to control whether FalsePatternLib should be allowed to use the internet. If this is enabled, update checks and library downloads will be completely blocked. -falsepatternlib.chat.updatesavailable=§4Outdated mods detected! §r +falsepatternlib.chat.updatesavailable=§4Outdated mods detected by %s! §r falsepatternlib.chat.modname=§b§l[%s]§r falsepatternlib.chat.currentversion= Current Version: §6%s§r falsepatternlib.chat.latestversion= Latest Version: §2%s§r