Skip to content

Commit

Permalink
update to display messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Aug 19, 2014
1 parent 29250bf commit 7fe483d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
67 changes: 38 additions & 29 deletions src/main/java/net/doubledoordev/d3core/D3Core.java
Expand Up @@ -39,12 +39,14 @@
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.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.versioning.ArtifactVersion;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
import net.doubledoordev.d3core.util.CoreHelper;
import net.doubledoordev.d3core.util.DevPerks;
import net.doubledoordev.d3core.util.ID3Mod;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
Expand All @@ -57,7 +59,6 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;

import static net.doubledoordev.d3core.util.CoreConstants.*;
Expand All @@ -80,6 +81,7 @@ public class D3Core implements ID3Mod

private boolean debug = false;
private boolean sillyness = true;
private boolean updateWarning = true;

private List<CoreHelper.ModUpdateDate> updateDateList = new ArrayList<>();

Expand All @@ -104,41 +106,34 @@ public void init(FMLInitializationEvent event)
if (modContainer instanceof FMLModContainer && modContainer.getMod() instanceof ID3Mod)
{
if (debug()) logger.info(String.format("[%s] Found a D3 Mod!", modContainer.getModId()));
Properties properties = ((FMLModContainer) modContainer).searchForVersionProperties();
if (properties != null && properties.containsKey(modContainer.getModId() + ".group") && properties.containsKey(modContainer.getModId() + ".artifactId"))
{
TreeSet<ArtifactVersion> availableVersions = new TreeSet<>();

String group = (String) properties.get(modContainer.getModId() + ".group");
String artifactId = (String) properties.get(modContainer.getModId() + ".artifactId");
if (debug()) logger.info(String.format("[%s] Group: %s ArtifactId: %s", modContainer.getModId(), group, artifactId));

URL url = new URL(MAVENURL + group.replace('.', '/') + '/' + artifactId + "/maven-metadata.xml");
if (debug()) logger.info(String.format("[%s] Maven URL: %s", modContainer.getModId(), url));
TreeSet<ArtifactVersion> availableVersions = new TreeSet<>();

DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(url.toURI().toString());
NodeList list = document.getDocumentElement().getElementsByTagName("version");
for (int i = 0; i < list.getLength(); i++)
{
String version = list.item(i).getFirstChild().getNodeValue();
if (version.startsWith(Loader.MC_VERSION + "-"))
{
availableVersions.add(new DefaultArtifactVersion(version.replace(Loader.MC_VERSION + "-", "")));
}
}
DefaultArtifactVersion current = new DefaultArtifactVersion(modContainer.getVersion().replace(Loader.MC_VERSION + "-", ""));
String group = GROUP + modContainer.getModId().toLowerCase();
String artifactId = modContainer.getModId();
if (debug()) logger.info(String.format("[%s] Group: %s ArtifactId: %s", modContainer.getModId(), group, artifactId));

if (debug()) logger.info(String.format("[%s] Current: %s Latest: %s All versions for MC %s: %s", modContainer.getModId(), current, availableVersions.last(), Loader.MC_VERSION, availableVersions));
URL url = new URL(MAVENURL + group.replace('.', '/') + '/' + artifactId + "/maven-metadata.xml");
if (debug()) logger.info(String.format("[%s] Maven URL: %s", modContainer.getModId(), url));

if (current.compareTo(availableVersions.last()) > 0)
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(url.toURI().toString());
NodeList list = document.getDocumentElement().getElementsByTagName("version");
for (int i = 0; i < list.getLength(); i++)
{
String version = list.item(i).getFirstChild().getNodeValue();
if (version.startsWith(Loader.MC_VERSION + "-"))
{
updateDateList.add(new CoreHelper.ModUpdateDate(modContainer.getName(), modContainer.getModId(), current.toString(), availableVersions.last().toString()));
availableVersions.add(new DefaultArtifactVersion(version.replace(Loader.MC_VERSION + "-", "")));
}
}
else
DefaultArtifactVersion current = new DefaultArtifactVersion(modContainer.getVersion().replace(Loader.MC_VERSION + "-", ""));

if (debug()) logger.info(String.format("[%s] Current: %s Latest: %s All versions for MC %s: %s", modContainer.getModId(), current, availableVersions.last(), Loader.MC_VERSION, availableVersions));

if (current.compareTo(availableVersions.last()) < 0)
{
logger.info("D3 Mod " + modContainer.getModId() + " doesn't have the appropriate properties for version checks.");
updateDateList.add(new CoreHelper.ModUpdateDate(modContainer.getName(), modContainer.getModId(), current.toString(), availableVersions.last().toString()));
}
}
}
Expand All @@ -151,14 +146,27 @@ public void init(FMLInitializationEvent event)
}

@Mod.EventHandler
public void init(FMLPostInitializationEvent event)
public void postInit(FMLPostInitializationEvent event)
{
for (CoreHelper.ModUpdateDate updateDate : updateDateList)
{
logger.warn(String.format("Update available for %s (%s)! Current version: %s New version: %s. Please update ASAP!", updateDate.getName(), updateDate.getModId(), updateDate.getCurrentVersion(), updateDate.getLatestVersion()));
}
}

@SubscribeEvent
public void nameFormatEvent(PlayerEvent.PlayerLoggedInEvent event)
{
if (!updateWarning) return;

event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a("{\"text\":\"\",\"extra\":[{\"text\":\"Updates available for these mods:\",\"color\":\"gold\"}]}"));
for (CoreHelper.ModUpdateDate updateDate : updateDateList)
{
event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a(String.format("{\"text\":\"\",\"extra\":[{\"text\":\"%s: %s -> %s\"}]}", updateDate.getName(), updateDate.getCurrentVersion(), updateDate.getLatestVersion())));
}
event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a("{\"text\":\"\",\"extra\":[{\"text\":\"Download here!\",\"color\":\"gold\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"http://doubledoordev.net\"}},{\"text\":\" <- That is a link btw :p\"}]}"));
}

@SubscribeEvent
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent eventArgs)
{
Expand All @@ -178,6 +186,7 @@ public void syncConfig()

debug = configuration.getBoolean("debug", MODID, debug, "Enable debug mode", "d3.core.config.debug");
sillyness = configuration.getBoolean("sillyness", MODID, sillyness, "Enable sillyness", "d3.core.config.sillyness");
updateWarning = configuration.getBoolean("updateWarning", MODID, updateWarning, "Allow update warnings on login", "d3.core.config.updateWarning");

if (sillyness) MinecraftForge.EVENT_BUS.register(getDevPerks());
else MinecraftForge.EVENT_BUS.unregister(getDevPerks());
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class CoreConstants
public static final String BASEURL = "http://doubledoordev.net/";
public static final String PERKSURL = BASEURL + "perks.json";
public static final String MAVENURL = BASEURL + "maven/";
public static final String GROUP = "net.doubledoordev.";
/**
* @see net.doubledoordev.d3core.client.ModConfigGuiFactory
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/D3Core/lang/en_US.lang
Expand Up @@ -8,3 +8,6 @@ d3.core.config.debug.tooltip=Please enable before making a bug report.

d3.core.config.sillyness=Enable sillyness
d3.core.config.sillyness.tooltip=Please don't disable, unless you want to piss off the D³ crew :p

d3.core.config.updateWarning=Update warnings
d3.core.config.updateWarning.tooltip=Please don't disable, unless you are on a modpack.
3 changes: 0 additions & 3 deletions src/main/resources/version.properties

This file was deleted.

0 comments on commit 7fe483d

Please sign in to comment.