put update chatification code in public library, plus misc cleanup

This commit is contained in:
FalsePattern committed 2022-06-10 12:43:03 +02:00
1 parent 24396f1766
commit 3d6e70c4f3
5 files changed
+49 -31

No files matched your search

@@ -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 =
@@ -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<IChatComponent>(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);
}
});
}
@@ -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<List<ModUpdateInfo>>() {
@Override
public List<ModUpdateInfo> 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;
@@ -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<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
if (updates == null || updates.size() == 0)
return null;
val updateText = new ArrayList<IChatComponent>(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;
}
}