diff --git a/src/main/java/evilcraft/EvilCraft.java b/src/main/java/evilcraft/EvilCraft.java index 332f2bb661..29851cb25d 100644 --- a/src/main/java/evilcraft/EvilCraft.java +++ b/src/main/java/evilcraft/EvilCraft.java @@ -95,6 +95,9 @@ public void preInit(FMLPreInitializationEvent event) { // Mod compatibility loading. ModCompatLoader.preInit(); + + // Start fetching the version info + VersionStats.load(); } /** diff --git a/src/main/java/evilcraft/Reference.java b/src/main/java/evilcraft/Reference.java index dfa6101ef0..6d7a29d4e0 100644 --- a/src/main/java/evilcraft/Reference.java +++ b/src/main/java/evilcraft/Reference.java @@ -64,6 +64,7 @@ public class Reference { public static final String MOD_FMP = "ForgeMultipart"; public static final String MOD_FORESTRY = "Forestry"; public static final String MOD_TCONSTRUCT = "TConstruct"; + public static final String MOD_VERSION_CHECKER = "VersionChecker"; // Dependencies public static final String MOD_DEPENDENCIES = "" // This is not required anymore (and never was?) "required-after:" + MOD_FORGE + "@[@FORGE_VERSION@,)" diff --git a/src/main/java/evilcraft/VersionStats.java b/src/main/java/evilcraft/VersionStats.java index 72f7f75485..05fe6b6df2 100644 --- a/src/main/java/evilcraft/VersionStats.java +++ b/src/main/java/evilcraft/VersionStats.java @@ -4,6 +4,7 @@ import java.net.URL; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; @@ -12,6 +13,8 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; /** @@ -21,6 +24,8 @@ */ public class VersionStats { + private static VersionStats VERSION_STATS = null; + private static boolean CHECKED = false; /** @@ -44,6 +49,20 @@ public static String getVersion() { return Reference.MOD_VERSION + " for Minecraft " + Reference.MOD_MC_VERSION; } + /** + * Fetch the latest version. Make sure this method is only loaded once! + */ + public static synchronized void load() { + new Thread(new Runnable() { + + @Override + public void run() { + VersionStats versionStats = getVersionStats(); + sendIMCOutdatedMessage(versionStats); + } + }).start(); + } + /** * Check the latest version. * @param event The tick event. @@ -68,6 +87,28 @@ public void run() { }).start(); } + /** + * Send a message to the Version Checker mod with the update info. + * This is an integration with Dynious Version Checker See + * http://www.minecraftforum.net/topic/2721902- + * @param versionStats The version info holder. + */ + public static synchronized void sendIMCOutdatedMessage(VersionStats versionStats) { + if(Loader.isModLoaded(Reference.MOD_VERSION_CHECKER)) { + NBTTagCompound compound = new NBTTagCompound(); + compound.setString("modDisplayName", Reference.MOD_NAME); + compound.setString("oldVersion", Reference.MOD_VERSION); + compound.setString("newVersion", versionStats.mod_version); + + compound.setString("updateUrl", versionStats.update_link); + compound.setBoolean("isDirectLink", true); + compound.setString("changeLog", ""); + + FMLInterModComms.sendRuntimeMessage(Reference.MOD_ID, + Reference.MOD_VERSION_CHECKER, "addUpdate", compound); + } + } + private static boolean needsUpdate(VersionStats versionStats) { if(versionStats != null) { if(!Reference.MOD_VERSION.equals(versionStats.mod_version)) @@ -80,7 +121,7 @@ private static void sendMessage(EntityPlayer player, String message) { player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + message)); } - private static VersionStats getVersionStats() { + private static VersionStats fetchVersionStats() { VersionStats versionStats = null; try { Gson gson = new Gson(); @@ -95,4 +136,11 @@ private static VersionStats getVersionStats() { return versionStats; } + private static synchronized VersionStats getVersionStats() { + if(VERSION_STATS == null) { + VERSION_STATS = fetchVersionStats(); + } + return VERSION_STATS; + } + }