mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-17 22:45:00 +00:00
added update checks for my mods, turned fplib into a regular mod, added async lib
This commit is contained in:
8 files changed
+204
-54
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package com.falsepattern.lib.internal.proxy;
|
||||
|
||||
import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.text.FormattedText;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import lombok.val;
|
||||
import net.minecraft.client.entity.EntityPlayerSP;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.event.ClickEvent;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ClientProxy extends CommonProxy {
|
||||
private Future<List<IChatComponent>> chatFuture;
|
||||
|
||||
@Override
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
super.preInit(e);
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postInit(FMLPostInitializationEvent e) {
|
||||
super.postInit(e);
|
||||
chatFuture = FalsePatternLib.getAsyncWorker().submit(() -> {
|
||||
long start = System.nanoTime();
|
||||
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);
|
||||
}
|
||||
}
|
||||
long end = System.nanoTime();
|
||||
FalsePatternLib.getLog().info("Constructed in {} ms", (end - start) / 1000000L);
|
||||
return updateText;
|
||||
});
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onSinglePlayer(EntityJoinWorldEvent e) {
|
||||
if (chatFuture == null ||
|
||||
!(e.entity instanceof EntityPlayerSP)) return;
|
||||
val player = (EntityPlayerSP) e.entity;
|
||||
try {
|
||||
for (val line: chatFuture.get()) {
|
||||
player.addChatMessage(line);
|
||||
}
|
||||
chatFuture = null;
|
||||
} catch (Exception ex) {
|
||||
FalsePatternLib.getLog().warn(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.falsepattern.lib.internal.proxy;
|
||||
|
||||
import com.falsepattern.lib.config.ConfigException;
|
||||
import com.falsepattern.lib.config.ConfigurationManager;
|
||||
import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.internal.LibraryConfig;
|
||||
import com.falsepattern.lib.internal.Tags;
|
||||
import com.falsepattern.lib.updates.ModUpdateInfo;
|
||||
import com.falsepattern.lib.updates.UpdateChecker;
|
||||
import cpw.mods.fml.common.event.FMLConstructionEvent;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class CommonProxy {
|
||||
|
||||
protected Future<List<ModUpdateInfo>> updatesFuture;
|
||||
|
||||
public void construct(FMLConstructionEvent e) {
|
||||
ConfigurationManager.init();
|
||||
}
|
||||
|
||||
public void preInit(FMLPreInitializationEvent e) {
|
||||
try {
|
||||
ConfigurationManager.registerConfig(LibraryConfig.class);
|
||||
} catch (ConfigException ex) {
|
||||
FalsePatternLib.getLog().error("Failed to register " + Tags.MODNAME + " config!", ex);
|
||||
}
|
||||
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
||||
FalsePatternLib.getLog().info("Launching asynchronous update check.");
|
||||
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json");
|
||||
updatesFuture = FalsePatternLib.getAsyncWorker().submit(() -> {
|
||||
if (!updateCheckFuture.isCancelled()) {
|
||||
try {
|
||||
var updates = updateCheckFuture.get();
|
||||
if (updates != null && updates.size() > 0) {
|
||||
for (val update:updates) {
|
||||
update.log(FalsePatternLib.getLog());
|
||||
}
|
||||
} else if (updates == null) {
|
||||
FalsePatternLib.getLog().warn("Unknown error while checking updates");
|
||||
} else {
|
||||
FalsePatternLib.getLog().info("All FalsePattern mods up to date!");
|
||||
updates = null;
|
||||
}
|
||||
return updates;
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
FalsePatternLib.getLog().warn("Error while checking updates", ex);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void postInit(FMLPostInitializationEvent e) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user