Skip to content

Commit

Permalink
Added support for VersionChecker mod.
Browse files Browse the repository at this point in the history
Also includes some small improvements in how the version stats are
fetched.
  • Loading branch information
rubensworks committed Jun 16, 2014
1 parent f36b187 commit 15d589d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/evilcraft/EvilCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public void preInit(FMLPreInitializationEvent event) {

// Mod compatibility loading.
ModCompatLoader.preInit();

// Start fetching the version info
VersionStats.load();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/evilcraft/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -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@,)"
Expand Down
50 changes: 49 additions & 1 deletion src/main/java/evilcraft/VersionStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand All @@ -21,6 +24,8 @@
*/
public class VersionStats {

private static VersionStats VERSION_STATS = null;

private static boolean CHECKED = false;

/**
Expand All @@ -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.
Expand All @@ -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))
Expand All @@ -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();
Expand All @@ -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;
}

}

0 comments on commit 15d589d

Please sign in to comment.