Last active
August 24, 2019 00:27
-
-
Save UpcraftLP/43e1c2979eab968cf063140f4e450324 to your computer and use it in GitHub Desktop.
Using Forge's built-in update checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core.upcraftlp.craftdev.api.util; | |
import com.google.common.collect.Lists; | |
import core.upcraftlp.craftdev.common.CraftDevCore; | |
import core.upcraftlp.craftdev.config.CoreInternalConfig; | |
import net.minecraft.util.StringUtils; | |
import net.minecraftforge.common.ForgeVersion; | |
import net.minecraftforge.fml.common.FMLCommonHandler; | |
import java.util.List; | |
/** | |
* @author UpcraftLP | |
* Check for mod updates or register them for automated update announcements | |
*/ | |
public class UpdateChecker { | |
public static final List<String> modsToCheck = Lists.newArrayList(); | |
/** | |
* used to register a mod for update notifications | |
* @param modid the modid String | |
*/ | |
public static void registerMod(String modid) { | |
modsToCheck.add(modid); | |
} | |
public static boolean hasUpdate(String modid, boolean betaVersions) { | |
ForgeVersion.CheckResult result = getResult(modid); | |
if(result.status == ForgeVersion.Status.PENDING) { | |
CraftDevCore.getLogger().warn("Cannot check for updates, found status: PENDING!"); | |
return false; | |
} | |
return betaVersions ? result.status.isAnimated() : result.status == ForgeVersion.Status.OUTDATED; | |
} | |
public static ForgeVersion.CheckResult getResult(String modid) { | |
return ForgeVersion.getResult(FMLCommonHandler.instance().findContainerFor(modid)); | |
} | |
public static String getLatestVersion(String modid) { | |
return getResult(modid).target.toString(); | |
} | |
public static void notifyServer() { | |
for (String modid : modsToCheck) { | |
if (hasUpdate(modid, CoreInternalConfig.betaUpdates)) { | |
String url = getResult(modid).url; | |
CraftDevCore.getLogger().warn("There's an update available for {}" + (StringUtils.isNullOrEmpty(url) ? "": ", download version {} here: {}"), FMLCommonHandler.instance().findContainerFor(modid).getName(), getLatestVersion(modid), url); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core.upcraftlp.craftdev.client; | |
import core.upcraftlp.craftdev.api.util.UpdateChecker; | |
import core.upcraftlp.craftdev.common.CraftDevReference; | |
import core.upcraftlp.craftdev.config.CoreInternalConfig; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.util.text.ITextComponent; | |
import net.minecraft.util.text.Style; | |
import net.minecraft.util.text.TextComponentString; | |
import net.minecraft.util.text.TextFormatting; | |
import net.minecraft.util.text.event.ClickEvent; | |
import net.minecraft.util.text.event.HoverEvent; | |
import net.minecraftforge.fml.common.FMLCommonHandler; | |
import net.minecraftforge.fml.common.Mod; | |
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
import net.minecraftforge.fml.common.gameevent.TickEvent; | |
import net.minecraftforge.fml.common.network.FMLNetworkEvent; | |
import net.minecraftforge.fml.relauncher.Side; | |
import net.minecraftforge.fml.relauncher.SideOnly; | |
/** | |
* @author UpcraftLP | |
*/ | |
@SideOnly(Side.CLIENT) | |
@Mod.EventBusSubscriber(modid = CraftDevReference.MODID, value = {Side.CLIENT}) | |
public class ClientUpdateHandler { | |
private static boolean hasShown = false; | |
@SubscribeEvent | |
public static void onPlayerTick(TickEvent.ClientTickEvent event) { | |
if(hasShown || Minecraft.getMinecraft().player == null) return; | |
for (String modid : UpdateChecker.modsToCheck) { | |
if (UpdateChecker.hasUpdate(modid, CoreInternalConfig.betaUpdates)) { | |
String url = UpdateChecker.getResult(modid).url; | |
String targetVersion = UpdateChecker.getLatestVersion(modid); | |
ITextComponent link = new TextComponentString("here").setStyle(new Style().setColor(TextFormatting.BLUE).setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)).setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(url).setStyle(new Style().setColor(TextFormatting.AQUA).setItalic(true))))); | |
Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Update available for " + FMLCommonHandler.instance().findContainerFor(modid).getName() + ", download v" + targetVersion + " ").appendSibling(link)); | |
} | |
} | |
hasShown = true; | |
} | |
@SubscribeEvent | |
public static void onPlayerJoinServer(FMLNetworkEvent.ClientConnectedToServerEvent event) { | |
hasShown = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment