Skip to content

Commit 3d6e70c

Browse files
committed
put update chatification code in public library, plus misc cleanup
1 parent 24396f1 commit 3d6e70c

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
acceptedMinecraftVersions = "[1.7.10]",
4848
acceptableRemoteVersions = "*")
4949
public class FalsePatternLib {
50+
public static final String UPDATE_URL = "https://falsepattern.com/mc/versions.json";
51+
5052
@Getter private static final Logger log = LogManager.getLogger(Tags.MODNAME);
5153

5254
@Getter private static final boolean developerEnvironment =

src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.falsepattern.lib.internal.proxy;
22

33
import com.falsepattern.lib.internal.FalsePatternLib;
4+
import com.falsepattern.lib.internal.Tags;
45
import com.falsepattern.lib.text.FormattedText;
6+
import com.falsepattern.lib.updates.UpdateChecker;
57
import com.falsepattern.lib.util.Async;
68
import cpw.mods.fml.common.Loader;
79
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
@@ -49,32 +51,7 @@ public List<IChatComponent> call() throws Exception {
4951
return null;
5052
}
5153
val updates = updatesFuture.get();
52-
if (updates == null || updates.size() == 0)
53-
return null;
54-
val updateText = new ArrayList<IChatComponent>(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable")).toChatText());
55-
val mods = Loader.instance().getIndexedModList();
56-
for (val update : updates) {
57-
val mod = mods.get(update.modID);
58-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText());
59-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText());
60-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText());
61-
if (!update.updateURL.isEmpty()) {
62-
val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText();
63-
val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText();
64-
val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText();
65-
pre.get(pre.size() - 1).appendSibling(link.get(0));
66-
link.get(link.size() - 1).appendSibling(post.get(0));
67-
for (val l : link) {
68-
l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL));
69-
}
70-
link.remove(0);
71-
post.remove(0);
72-
updateText.addAll(pre);
73-
updateText.addAll(link);
74-
updateText.addAll(post);
75-
}
76-
}
77-
return updateText;
54+
return UpdateChecker.updateListToChatMessages(Tags.MODNAME, updates);
7855
}
7956
});
8057
}

src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public void preInit(FMLPreInitializationEvent e) {
3232
try {
3333
ConfigurationManager.registerConfig(LibraryConfig.class);
3434
} catch (ConfigException ex) {
35-
FalsePatternLib.getLog().error("Failed to register " + Tags.MODNAME + " config!", ex);
35+
FalsePatternLib.getLog().error("Failed to register config!", ex);
3636
}
3737
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
3838
FalsePatternLib.getLog().info("Launching asynchronous update check.");
39-
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json");
39+
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL);
4040
updatesFuture = Async.asyncWorker.submit(new Callable<List<ModUpdateInfo>>() {
4141
@Override
4242
public List<ModUpdateInfo> call() {
@@ -56,9 +56,9 @@ public List<ModUpdateInfo> call() {
5656
update.log(FalsePatternLib.getLog());
5757
}
5858
} else if (updates == null) {
59-
FalsePatternLib.getLog().warn("Unknown error while checking updates");
59+
FalsePatternLib.getLog().warn("Unknown error while checking updates.");
6060
} else {
61-
FalsePatternLib.getLog().info("All FalsePattern mods up to date!");
61+
FalsePatternLib.getLog().info("All checked mods up to date!");
6262
updates = null;
6363
}
6464
return updates;

src/main/java/com/falsepattern/lib/updates/UpdateChecker.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
import com.falsepattern.lib.internal.Internet;
99
import com.falsepattern.lib.internal.LibraryConfig;
1010
import com.falsepattern.lib.internal.Tags;
11+
import com.falsepattern.lib.text.FormattedText;
1112
import com.falsepattern.lib.util.Async;
1213
import cpw.mods.fml.common.Loader;
1314
import lombok.val;
15+
import net.minecraft.client.resources.I18n;
16+
import net.minecraft.event.ClickEvent;
17+
import net.minecraft.util.IChatComponent;
1418

1519
import java.io.ByteArrayOutputStream;
1620
import java.io.IOException;
@@ -138,4 +142,39 @@ public static List<ModUpdateInfo> fetchUpdates(String url) {
138142
}
139143
return result;
140144
}
145+
146+
/**
147+
* Formats the raw list of updates into lines of chat messages you can send to players.
148+
* @param initiator Who/what/which mod did this update check
149+
* @param updates The list of updates to convert
150+
* @return A list of chat messages that can be sent to players
151+
*/
152+
public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
153+
if (updates == null || updates.size() == 0)
154+
return null;
155+
val updateText = new ArrayList<IChatComponent>(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable", initiator)).toChatText());
156+
val mods = Loader.instance().getIndexedModList();
157+
for (val update : updates) {
158+
val mod = mods.get(update.modID);
159+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText());
160+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText());
161+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText());
162+
if (!update.updateURL.isEmpty()) {
163+
val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText();
164+
val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText();
165+
val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText();
166+
pre.get(pre.size() - 1).appendSibling(link.get(0));
167+
link.get(link.size() - 1).appendSibling(post.get(0));
168+
for (val l : link) {
169+
l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL));
170+
}
171+
link.remove(0);
172+
post.remove(0);
173+
updateText.addAll(pre);
174+
updateText.addAll(link);
175+
updateText.addAll(post);
176+
}
177+
}
178+
return updateText;
179+
}
141180
}

src/main/resources/assets/falsepatternlib/lang/en_US.lang

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ config.falsepatternlib.updatecheck=Enable update checks
22
config.falsepatternlib.updatecheck.tooltip=Used to control whether FalsePatternLib should check for outdated mods.
33
config.falsepatternlib.disableinternet=FalsePatternLib Offline Mode
44
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.
5-
falsepatternlib.chat.updatesavailable=§4Outdated mods detected! §r
5+
falsepatternlib.chat.updatesavailable=§4Outdated mods detected by %s! §r
66
falsepatternlib.chat.modname=§b§l[%s]§r
77
falsepatternlib.chat.currentversion= Current Version: §6%s§r
88
falsepatternlib.chat.latestversion= Latest Version: §2%s§r

0 commit comments

Comments
 (0)