From b8add48c8606c7d332d347bceaa5ecf2b20e38d5 Mon Sep 17 00:00:00 2001 From: Esoteric Enderman Date: Sun, 3 Nov 2024 13:59:35 +0000 Subject: [PATCH] Massively simplify resource pack code --- .../pack/resource/ResourcePackListener.kt | 2 +- .../pack/resource/ResourcePackManager.kt | 17 +++---- .../pack/resource/ResourcePackPlugin.kt | 15 ------ .../pack/resource/ResourcePackServer.kt | 49 ++++++++----------- .../minecraft/plugins/library/TestPlugin.kt | 6 +-- 5 files changed, 32 insertions(+), 57 deletions(-) delete mode 100644 lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackPlugin.kt diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackListener.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackListener.kt index dd41b32..039b8a6 100644 --- a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackListener.kt +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackListener.kt @@ -15,7 +15,7 @@ import java.net.URI */ class ResourcePackListener(resourcePackServer: ResourcePackServer) : Listener { private val resourcePackInfo = ResourcePackInfo.resourcePackInfo() - .hash(resourcePackServer.resourcePackManager.resourcePackZipFile!!.sha1()) + .hash(resourcePackServer.resourcePackManager.zipFile.sha1()) .uri(URI.create("http://" + resourcePackServer.socketAddress + "/")).build() @EventHandler diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackManager.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackManager.kt index 6c9e085..dae30c6 100644 --- a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackManager.kt +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackManager.kt @@ -3,6 +3,7 @@ package foundation.esoteric.minecraft.plugins.library.pack.resource import foundation.esoteric.minecraft.plugins.library.utility.plugin.saveResources import foundation.esoteric.utility.file.zip import org.apache.commons.io.FileUtils +import org.bukkit.plugin.java.JavaPlugin import java.io.File import kotlin.io.path.Path @@ -15,24 +16,22 @@ import kotlin.io.path.Path * For example, if your plugin's name (the name that appears in-game when running `/plugins`) is SCPPlugin, then the resource pack * must be named `SCPPluginResourcePack`. */ -class ResourcePackManager(internal val plugin: ResourcePackPlugin) { +class ResourcePackManager(internal val plugin: JavaPlugin) { - val resourcePackResourceFolderName = Path(plugin.name + "ResourcePack") - - var resourcePackZipFile: File? = null + internal val resourcePath = Path(plugin.name + "ResourcePack") + internal val zipFile = File(plugin.dataFolder, "$resourcePath.zip") init { - val resourcePackFolder = plugin.saveResources(resourcePackResourceFolderName) + val resourcePackFolder = plugin.saveResources(resourcePath) try { - resourcePackZipFile = File(plugin.dataFolder, "$resourcePackResourceFolderName.zip") - resourcePackFolder.zip(resourcePackZipFile!!) + resourcePackFolder.zip(zipFile) FileUtils.deleteDirectory(resourcePackFolder) + + ResourcePackServer(this) } catch (exception: Exception) { exception.printStackTrace() } - - ResourcePackServer(this) } } diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackPlugin.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackPlugin.kt deleted file mode 100644 index da0bce3..0000000 --- a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackPlugin.kt +++ /dev/null @@ -1,15 +0,0 @@ -package foundation.esoteric.minecraft.plugins.library.pack.resource - -import org.bukkit.plugin.Plugin - -/** - * Represents a plugin that uses the resource pack features of this library. - */ -interface ResourcePackPlugin : Plugin { - /** - * The resource pack manager of this plugin. - * - * The plugin must implement ResourcePackPlugin to use the manager. - */ - val resourcePackManager : ResourcePackManager -} diff --git a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackServer.kt b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackServer.kt index bdb4985..e3fbe78 100644 --- a/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackServer.kt +++ b/lib/src/main/kotlin/foundation/esoteric/minecraft/plugins/library/pack/resource/ResourcePackServer.kt @@ -4,7 +4,6 @@ import com.sun.net.httpserver.HttpExchange import com.sun.net.httpserver.HttpHandler import com.sun.net.httpserver.HttpServer import org.bukkit.Bukkit -import org.bukkit.plugin.java.JavaPlugin import java.io.FileInputStream import java.io.IOException import java.net.InetSocketAddress @@ -14,54 +13,48 @@ import java.net.InetSocketAddress * * @param plugin The plugin that implements this resource pack. */ -class ResourcePackServer(val resourcePackManager: ResourcePackManager) { - - private val plugin = resourcePackManager.plugin - - private val hostName: String = Bukkit.getServer().ip - private val port: Int = plugin.config.getInt("http-server.port") +class ResourcePackServer(internal val resourcePackManager: ResourcePackManager) { private val successResponseCode = 200 private val notFoundResponseCode = 404 - private var server: HttpServer? = null + private lateinit var server: HttpServer - fun getPort(): Int? { - return server?.address?.port - } + private val serverPort: Int + get() = server.address.port - fun getHostName(): String? { - return server?.address?.hostName - } + private val serverHostName: String + get() = server.address.hostName - val socketAddress: String - get() = getHostName() + ":" + getPort() + internal val socketAddress: String + get() = "$serverHostName:$serverPort" init { try { + val hostName = Bukkit.getServer().ip + val port = resourcePackManager.plugin.config.getInt("http-server.port") + server = HttpServer.create(InetSocketAddress(hostName, port), 0) - } catch (exception: IOException) { - exception.printStackTrace() - } - server!!.createContext("/", ResourcePackDownloadHandler()) + server.createContext("/", ResourcePackDownloadHandler()) - server!!.executor = null - server!!.start() + server.executor = null + server.start() - Bukkit.getPluginManager().registerEvents(ResourcePackListener(this), plugin) + Bukkit.getPluginManager().registerEvents(ResourcePackListener(this), resourcePackManager.plugin) + } catch (exception: IOException) { + exception.printStackTrace() + } } - internal inner class ResourcePackDownloadHandler : HttpHandler { + private inner class ResourcePackDownloadHandler : HttpHandler { @Throws(IOException::class) override fun handle(exchange: HttpExchange) { - val resourcePackManager = plugin.resourcePackManager - - val file = resourcePackManager.resourcePackZipFile!! + val file = resourcePackManager.zipFile if (file.exists()) { exchange.responseHeaders["Content-Type"] = "application/zip" - exchange.responseHeaders["Content-Disposition"] = "attachment; filename=\"" + resourcePackManager.resourcePackResourceFolderName + ".zip" + "\"" + exchange.responseHeaders["Content-Disposition"] = "attachment; filename=\"" + resourcePackManager.resourcePath + ".zip" + "\"" exchange.sendResponseHeaders(successResponseCode, file.length()) diff --git a/lib/src/test/kotlin/foundation/esoteric/minecraft/plugins/library/TestPlugin.kt b/lib/src/test/kotlin/foundation/esoteric/minecraft/plugins/library/TestPlugin.kt index 440fb91..6e8ef93 100644 --- a/lib/src/test/kotlin/foundation/esoteric/minecraft/plugins/library/TestPlugin.kt +++ b/lib/src/test/kotlin/foundation/esoteric/minecraft/plugins/library/TestPlugin.kt @@ -3,16 +3,14 @@ package foundation.esoteric.minecraft.plugins.library import foundation.esoteric.minecraft.plugins.library.item.CustomItemManager import foundation.esoteric.minecraft.plugins.library.item.CustomItemPlugin import foundation.esoteric.minecraft.plugins.library.pack.resource.ResourcePackManager -import foundation.esoteric.minecraft.plugins.library.pack.resource.ResourcePackPlugin import org.bukkit.plugin.java.JavaPlugin -open class TestPlugin : JavaPlugin(), CustomItemPlugin, ResourcePackPlugin { +open class TestPlugin : JavaPlugin(), CustomItemPlugin { - override lateinit var resourcePackManager: ResourcePackManager override lateinit var customItemManager: CustomItemManager override fun onEnable() { customItemManager = CustomItemManager(this) - resourcePackManager = ResourcePackManager(this) + ResourcePackManager(this) } }