Skip to content

Instantly share code, notes, and snippets.

@LexManos
Created December 25, 2013 08:32
Show Gist options
  • Save LexManos/8121391 to your computer and use it in GitHub Desktop.
Save LexManos/8121391 to your computer and use it in GitHub Desktop.
Forge Potential Update Check System
/**
* This software is provided under the terms of the Minecraft Forge Public
* License v1.0.
*/
package net.minecraftforge.common;
import static net.minecraftforge.common.ForgeVersion.Status.*;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import com.google.common.io.ByteStreams;
import com.google.gson.Gson;
import cpw.mods.fml.common.versioning.ArtifactVersion;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
public class ForgeVersion
{
//This number is incremented every time we remove deprecated code/major API changes, never reset
public static final int majorVersion = 10;
//This number is incremented every minecraft release, never reset
public static final int minorVersion = 12;
//This number is incremented every time a interface changes or new major feature is added, and reset every Minecraft version
public static final int revisionVersion = 0;
//This number is incremented every time Jenkins builds Forge, and never reset. Should always be 0 in the repo code.
public static final int buildVersion = 0;
private static Status status = PENDING;
private static String target = null;
public static int getMajorVersion()
{
return majorVersion;
}
public static int getMinorVersion()
{
return minorVersion;
}
public static int getRevisionVersion()
{
return revisionVersion;
}
public static int getBuildVersion()
{
return buildVersion;
}
public static Status getStatus()
{
return status;
}
public static String getTarget()
{
return target;
}
public static String getVersion()
{
return String.format("%d.%d.%d.%d", majorVersion, minorVersion, revisionVersion, buildVersion);
}
public static enum Status
{
PENDING,
FAILED,
UP_TO_DATE,
OUTDATED,
AHEAD,
BETA,
BETA_OUTDATED
}
public static void startVersionCheck()
{
new Thread("Forge Version Check")
{
@Override
public void run()
{
try
{
URL url = new URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json");
InputStream con = url.openStream();
String data = new String(ByteStreams.toByteArray(con));
con.close();
Map<String, Object> json = new Gson().fromJson(data, Map.class);
String homepage = (String)json.get("homepage");
Map<String, String> promos = (Map<String, String>)json.get("promos");
String rec = promos.get(MinecraftForge.MC_VERSION + "-recommended");
String lat = promos.get(MinecraftForge.MC_VERSION + "-latest");
ArtifactVersion current = new DefaultArtifactVersion(getVersion());
if (rec != null)
{
ArtifactVersion recommended = new DefaultArtifactVersion(rec);
int diff = recommended.compareTo(current);
if (diff == 0)
status = UP_TO_DATE;
else if (diff < 0)
{
status = AHEAD;
if (lat != null)
{
if (current.compareTo(new DefaultArtifactVersion(lat)) < 0)
{
status = OUTDATED;
target = lat;
}
}
}
else
{
status = OUTDATED;
target = rec;
}
}
else if (lat != null)
{
if (current.compareTo(new DefaultArtifactVersion(lat)) < 0)
{
status = BETA_OUTDATED;
target = lat;
}
else
status = BETA;
}
else
status = BETA;
}
catch (Exception e)
{
e.printStackTrace();
status = FAILED;
}
}
}.start();
}
}
--- ../src-base/minecraft/net/minecraft/client/gui/GuiMainMenu.java
+++ ../src-work/minecraft/net/minecraft/client/gui/GuiMainMenu.java
@@ -30,6 +30,7 @@
import net.minecraft.world.demo.DemoWorldServer;
import net.minecraft.world.storage.ISaveFormat;
import net.minecraft.world.storage.WorldInfo;
+import net.minecraftforge.client.ForgeHooksClient;
import org.apache.commons.io.Charsets;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -630,6 +631,7 @@
this.drawString(this.field_146289_q, brd, 2, this.field_146295_m - ( 10 + i * (this.field_146289_q.FONT_HEIGHT + 1)), 16777215);
}
}
+ ForgeHooksClient.renderMainMenu(this, field_146289_q, field_146294_l, field_146295_m);
String s1 = "Copyright Mojang AB. Do not distribute!";
this.drawString(this.field_146289_q, s1, this.field_146294_l - this.field_146289_q.getStringWidth(s1) - 2, this.field_146295_m - 10, -1);
public static void renderMainMenu(GuiMainMenu gui, FontRenderer font, int width, int height)
{
Status status = ForgeVersion.getStatus();
if (status == BETA || status == BETA_OUTDATED)
{
// render a warning at the top of the screen,
String line = EnumChatFormatting.RED + "WARNING:" + EnumChatFormatting.RESET + " Forge Beta,";
gui.drawString(font, line, (width - font.getStringWidth(line)) / 2, 4 + (0 * (font.FONT_HEIGHT + 1)), -1);
line = "Major issues may arise, verify before reporting.";
gui.drawString(font, line, (width - font.getStringWidth(line)) / 2, 4 + (1 * (font.FONT_HEIGHT + 1)), -1);
}
String line = null;
switch(status)
{
//case FAILED: line = " Version check failed"; break;
//case UP_TO_DATE: line = "Forge up to date"}; break;
//case AHEAD: line = "Using non-recommended Forge build, issues may arise."}; break;
case OUTDATED:
case BETA_OUTDATED: line = "New Forge version avalible: " + ForgeVersion.getTarget(); break;
default: break;
}
if (line != null)
{
// if we have a line, render it in the bottom right, above Mojang's copyright line
gui.drawString(font, line, width - font.getStringWidth(line) - 2, height - (2 * (font.FONT_HEIGHT + 1)), -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment