mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 03:39:37 +00:00 
			
		
		
		
	Add basic update checking.
This commit is contained in:
		@ -83,6 +83,7 @@ subprojects {
 | 
			
		||||
 | 
			
		||||
    // Serialization
 | 
			
		||||
    implementation("com.charleskorn.kaml:kaml:0.38.0")
 | 
			
		||||
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")
 | 
			
		||||
 | 
			
		||||
    // Paper API
 | 
			
		||||
    compileOnly("io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT")
 | 
			
		||||
 | 
			
		||||
@ -1,2 +1,3 @@
 | 
			
		||||
dependencies {
 | 
			
		||||
  // TODO: might be able to ship all dependencies in core? are we duplicating classes in JARs?
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ package cloud.kubelet.foundation.core
 | 
			
		||||
import cloud.kubelet.foundation.core.command.BackupCommand
 | 
			
		||||
import cloud.kubelet.foundation.core.command.GamemodeCommand
 | 
			
		||||
import cloud.kubelet.foundation.core.command.LeaderboardCommand
 | 
			
		||||
import cloud.kubelet.foundation.core.command.UpdateCommand
 | 
			
		||||
import net.kyori.adventure.text.Component
 | 
			
		||||
import org.bukkit.GameMode
 | 
			
		||||
import org.bukkit.command.CommandExecutor
 | 
			
		||||
@ -41,6 +42,7 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
 | 
			
		||||
 | 
			
		||||
    // Register commands.
 | 
			
		||||
    registerCommandExecutor("fbackup", BackupCommand(this, backupPath))
 | 
			
		||||
    registerCommandExecutor("fupdate", UpdateCommand())
 | 
			
		||||
    registerCommandExecutor(listOf("survival", "s"), GamemodeCommand(GameMode.SURVIVAL))
 | 
			
		||||
    registerCommandExecutor(listOf("creative", "c"), GamemodeCommand(GameMode.CREATIVE))
 | 
			
		||||
    registerCommandExecutor(listOf("adventure", "a"), GamemodeCommand(GameMode.ADVENTURE))
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
package cloud.kubelet.foundation.core.command
 | 
			
		||||
 | 
			
		||||
import cloud.kubelet.foundation.core.update.UpdateUtil
 | 
			
		||||
import org.bukkit.command.Command
 | 
			
		||||
import org.bukkit.command.CommandExecutor
 | 
			
		||||
import org.bukkit.command.CommandSender
 | 
			
		||||
 | 
			
		||||
class UpdateCommand : CommandExecutor {
 | 
			
		||||
  override fun onCommand(
 | 
			
		||||
    sender: CommandSender,
 | 
			
		||||
    command: Command,
 | 
			
		||||
    label: String,
 | 
			
		||||
    args: Array<out String>
 | 
			
		||||
  ): Boolean {
 | 
			
		||||
    // TODO: Move to separate thread?
 | 
			
		||||
    val modules = UpdateUtil.fetchManifest()
 | 
			
		||||
    val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() }
 | 
			
		||||
 | 
			
		||||
    sender.sendMessage("Updates:")
 | 
			
		||||
    modules.forEach { (name, manifest) ->
 | 
			
		||||
      // Dumb naming problem. Don't want to fix it right now.
 | 
			
		||||
      val plugin = if (name == "foundation-core") {
 | 
			
		||||
        plugins["foundation"]
 | 
			
		||||
      } else {
 | 
			
		||||
        plugins[name.lowercase()]
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (plugin == null) {
 | 
			
		||||
        sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})")
 | 
			
		||||
      } else {
 | 
			
		||||
        sender.sendMessage("${plugin.name}: ${manifest.version} (have ${plugin.description.version})")
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return true
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,9 @@
 | 
			
		||||
package cloud.kubelet.foundation.core.update
 | 
			
		||||
 | 
			
		||||
import kotlinx.serialization.Serializable
 | 
			
		||||
 | 
			
		||||
@Serializable
 | 
			
		||||
data class ModuleManifest(
 | 
			
		||||
  val version: String,
 | 
			
		||||
  val artifacts: List<String>,
 | 
			
		||||
)
 | 
			
		||||
@ -0,0 +1,40 @@
 | 
			
		||||
package cloud.kubelet.foundation.core.update
 | 
			
		||||
 | 
			
		||||
import kotlinx.serialization.DeserializationStrategy
 | 
			
		||||
import kotlinx.serialization.builtins.MapSerializer
 | 
			
		||||
import kotlinx.serialization.builtins.serializer
 | 
			
		||||
import kotlinx.serialization.json.Json
 | 
			
		||||
import java.net.URI
 | 
			
		||||
import java.net.http.HttpClient
 | 
			
		||||
import java.net.http.HttpRequest
 | 
			
		||||
import java.net.http.HttpResponse
 | 
			
		||||
 | 
			
		||||
object UpdateUtil {
 | 
			
		||||
  private val client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build()
 | 
			
		||||
 | 
			
		||||
  // TODO: Add environment variable override. Document it.
 | 
			
		||||
  private const val manifestUrl =
 | 
			
		||||
    "https://git.gorence.io/lgorence/foundation/-/jobs/artifacts/main/raw/build/manifests/update.json?job=build"
 | 
			
		||||
 | 
			
		||||
  fun fetchManifest() = fetchFile(
 | 
			
		||||
    manifestUrl, MapSerializer(String.serializer(), ModuleManifest.serializer()),
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
  private inline fun <reified T> fetchFile(url: String, strategy: DeserializationStrategy<T>): T {
 | 
			
		||||
    val request = HttpRequest
 | 
			
		||||
      .newBuilder()
 | 
			
		||||
      .GET()
 | 
			
		||||
      .uri(URI.create(url))
 | 
			
		||||
      .build()
 | 
			
		||||
 | 
			
		||||
    val response = client.send(
 | 
			
		||||
      request,
 | 
			
		||||
      HttpResponse.BodyHandlers.ofString()
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    return Json.decodeFromString(
 | 
			
		||||
      strategy,
 | 
			
		||||
      response.body()
 | 
			
		||||
    )
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -8,9 +8,13 @@ authors:
 | 
			
		||||
  - kubelet
 | 
			
		||||
commands:
 | 
			
		||||
  fbackup:
 | 
			
		||||
    description: Foundation Backup
 | 
			
		||||
    description: Back the server up now
 | 
			
		||||
    usage: /fbackup
 | 
			
		||||
    permission: foundation.backup
 | 
			
		||||
    permission: foundation.command.backup
 | 
			
		||||
  fupdate:
 | 
			
		||||
    description: List updates to the Foundation plugin
 | 
			
		||||
    usage: /fupdate
 | 
			
		||||
    permission: foundation.command.update
 | 
			
		||||
  survival:
 | 
			
		||||
    description: Switch to survival gamemode
 | 
			
		||||
    usage: /survival
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user