Skip to content

Commit

Permalink
refactor: use Bukkit#getMinecraftVersion for check, extract only when…
Browse files Browse the repository at this point in the history
… called on
  • Loading branch information
Boy0000 committed Aug 7, 2024
1 parent 0fa5659 commit 2ab4910
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ import org.bukkit.plugin.java.JavaPlugin

class IdofrontPlugin : JavaPlugin() {

override fun onEnable() {
Bukkit.getAsyncScheduler().runNow(this) {
MinecraftAssetExtractor.extractLatest()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.zip.ZipInputStream
object MinecraftAssetExtractor {

private const val VERSION_MANIFEST_URL = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
lateinit var assetPath: File
val assetPath = Bukkit.getPluginsFolder().resolve("Idofront/assetCache/${Bukkit.getMinecraftVersion()}")

fun extractLatest() {
idofrontLogger.i("Extracting latest vanilla-assets...")
Expand Down Expand Up @@ -74,21 +74,19 @@ object MinecraftAssetExtractor {
}.onFailure { it.printStackTrace() }.getOrNull() ?: error("Failed to download client JAR")

private fun findVersionInfoUrl(): String? {
val manifest = runCatching {
downloadJson(VERSION_MANIFEST_URL)
}.getOrNull() ?: error("Failed to download version manifest")

val latest = manifest.getAsJsonObject("latest").get("release").asString
assetPath = Bukkit.getPluginsFolder().resolve("Idofront/assetCache/$latest")

val version = Bukkit.getMinecraftVersion()
if (!assetPath.mkdirs()) {
idofrontLogger.i("Latest has already been extracted for $latest, skipping...")
idofrontLogger.i("Latest has already been extracted for $version, skipping...")
return null
}

val manifest = runCatching {
downloadJson(VERSION_MANIFEST_URL)
}.getOrNull() ?: error("Failed to download version manifest")

return manifest.getAsJsonArray("versions").firstOrNull {
(it as? JsonObject)?.get("id")?.asString?.equals(latest) ?: false
}?.asJsonObject?.get("url")?.asString ?: error("Failed to find version inof url for version $latest")
(it as? JsonObject)?.get("id")?.asString?.equals(version) ?: false
}?.asJsonObject?.get("url")?.asString ?: error("Failed to find version inof url for version $version")
}

private fun downloadJson(url: String) = runCatching { JsonParser.parseString(URI.create(url).toURL().readText()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ object ResourcePacks {
val resourcePackWriter = MinecraftResourcePackWriter.builder().prettyPrinting(true).build()
val resourcePackReader = MinecraftResourcePackReader.builder().lenient(true).build()

val vanillaDefaultResourcePack by lazy { readToResourcePack(MinecraftAssetExtractor.assetPath) }
/**
* Loads a copy of the vanilla resourcepack for the current version
* If it has not been yet, it will first be extracted locally
* The ResourcePack instance does not contain any of the vanilla OGG files due to filesize optimizations
*/
val defaultVanillaResourcePack by lazy {
MinecraftAssetExtractor.assetPath.apply { if (!exists()) MinecraftAssetExtractor.extractLatest() }.let(::readToResourcePack)
}

fun readToResourcePack(file: File): ResourcePack? {
return runCatching {
Expand Down

0 comments on commit 2ab4910

Please sign in to comment.