diff --git a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt index bd0300a..b5e72e6 100644 --- a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt @@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink import matterlink.command.IMinecraftCommandSender import matterlink.command.MatterlinkCommandSender import matterlink.config.cfg +import matterlink.update.UpdateChecker import net.minecraft.util.text.TextComponentString import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.Mod @@ -51,6 +52,7 @@ object MatterLink : IMatterLink() { event.registerServerCommand(CommandMatterlink()) serverStartTime = System.currentTimeMillis() connect() + Thread(UpdateChecker()).start() } @Mod.EventHandler @@ -72,4 +74,7 @@ object MatterLink : IMatterLink() { logger.log(Level.toLevel(level, Level.INFO),formatString, *data) override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender + + override val mcVersion: String = "1.10.2" + override val modVersion: String = VERSION } diff --git a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt index f5af485..c09eec5 100644 --- a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt @@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink import matterlink.command.IMinecraftCommandSender import matterlink.command.MatterlinkCommandSender import matterlink.config.cfg +import matterlink.update.UpdateChecker import net.minecraft.util.text.TextComponentString import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.Mod @@ -51,6 +52,7 @@ object MatterLink : IMatterLink() { event.registerServerCommand(CommandMatterlink()) serverStartTime = System.currentTimeMillis() connect() + Thread(UpdateChecker()).start() } @Mod.EventHandler @@ -72,4 +74,7 @@ object MatterLink : IMatterLink() { logger.log(Level.toLevel(level, Level.INFO),formatString, *data) override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender + + override val mcVersion: String = "1.11.2" + override val modVersion: String = VERSION } diff --git a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt index bdbe720..93ea590 100644 --- a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt @@ -4,6 +4,7 @@ import matterlink.command.CommandMatterlink import matterlink.command.IMinecraftCommandSender import matterlink.command.MatterlinkCommandSender import matterlink.config.cfg +import matterlink.update.UpdateChecker import net.minecraft.util.text.TextComponentString import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.Mod @@ -51,6 +52,7 @@ object MatterLink : IMatterLink() { event.registerServerCommand(CommandMatterlink()) serverStartTime = System.currentTimeMillis() connect() + Thread(UpdateChecker()).start() } @Mod.EventHandler @@ -72,4 +74,6 @@ object MatterLink : IMatterLink() { logger.log(Level.toLevel(level, Level.INFO),formatString, *data) override var commandSender: IMinecraftCommandSender = MatterlinkCommandSender + override val mcVersion: String = "1.12.2" + override val modVersion: String = VERSION } diff --git a/core/src/main/kotlin/matterlink/IMatterLink.kt b/core/src/main/kotlin/matterlink/IMatterLink.kt index bb13d90..acae36a 100644 --- a/core/src/main/kotlin/matterlink/IMatterLink.kt +++ b/core/src/main/kotlin/matterlink/IMatterLink.kt @@ -11,9 +11,11 @@ import matterlink.config.cfg lateinit var instance: IMatterLink abstract class IMatterLink { -// var interrupted: Boolean = false - abstract var commandSender: IMinecraftCommandSender + abstract val mcVersion : String + abstract val modVersion : String + abstract var commandSender: IMinecraftCommandSender + //var interrupted: Boolean = false abstract fun wrappedSendToPlayers(msg: String) diff --git a/core/src/main/kotlin/matterlink/update/ApiUpdate.kt b/core/src/main/kotlin/matterlink/update/ApiUpdate.kt new file mode 100644 index 0000000..3752357 --- /dev/null +++ b/core/src/main/kotlin/matterlink/update/ApiUpdate.kt @@ -0,0 +1,19 @@ +package matterlink.update + +import com.google.gson.Gson + +data class ApiUpdate ( + val downloadURL: String, + val fileName: String, + val gameVersion: Array, + val releaseType: String +) { + companion object { + val gson = Gson() + + fun decode(json: String): ApiUpdate { + return gson.fromJson(json, ApiUpdate::class.java) + } + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/update/UpdateChecker.kt b/core/src/main/kotlin/matterlink/update/UpdateChecker.kt new file mode 100644 index 0000000..faff963 --- /dev/null +++ b/core/src/main/kotlin/matterlink/update/UpdateChecker.kt @@ -0,0 +1,73 @@ +package matterlink.update + +import com.google.gson.Gson +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler +import matterlink.config.cfg +import matterlink.instance +import org.apache.http.HttpResponse +import org.apache.http.client.HttpClient +import org.apache.http.client.methods.HttpGet +import org.apache.http.impl.client.HttpClients +import java.io.BufferedReader +import java.util.regex.Pattern + +class UpdateChecker : Runnable { + override fun run() { + instance.info("Checking for new versions...") + + val ApiUpdateList : Array + + val client : HttpClient = HttpClients.createDefault() + val response : HttpResponse = client.execute(HttpGet("https://cursemeta.nikky.moe/api/addon/287323/files")) + if (200 == response.statusLine.statusCode) { //HTTP 200 OK + val buffer : BufferedReader = response.entity.content.bufferedReader() + + //put all of the buffer content onto the string + var content : String = "" + var line : String? = buffer.readLine() + while (line != null) { + instance.debug(line) + content += line + line = buffer.readLine() + } + instance.debug("updateData: $content") + + val gson = Gson() + ApiUpdateList = gson.fromJson>(content,Array::class.java) + } else { + instance.error("Could not check for updates!") + return + } + + val possibleUpdates = HashMap() + var maxVersion : String = "" + ApiUpdateList.forEach { + //TODO: fix this if we ever release jars that support multiple versions + if (it.gameVersion[0] == instance.mcVersion) { + if (Pattern.matches("[mM]atter[lL]ink \\d+\\.\\d+\\.\\d+-\\d+\\.\\d+\\.?\\d*",it.fileName) ) { + val version : String = it.fileName.split("-")[1] + instance.debug(version) + possibleUpdates.set(version,it) + if (version.compareTo(maxVersion)>0 || maxVersion.equals("")) maxVersion = version + } + } + } + + if (maxVersion.isEmpty()) return + + if (maxVersion.compareTo(instance.modVersion)<0) { + val latest : ApiUpdate? = possibleUpdates[maxVersion] + if (latest != null) { + instance.warn("Mod out of date! New version available at ${latest.downloadURL}") + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = "Matterlink out of date! Please download new version from ${latest.downloadURL}" + )) + } else { + instance.fatal("Severe error in update checker!") + } + } + } + +} \ No newline at end of file