Skip to content

Commit

Permalink
Massively simplify resource pack code
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Nov 3, 2024
1 parent 6957eca commit b8add48
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b8add48

Please sign in to comment.