Skip to content

Commit

Permalink
chore: use lenient reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Aug 1, 2024
1 parent 69579c4 commit f7b700b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/main/kotlin/com/mineinabyss/packy/PackyGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.mineinabyss.packy.helpers.readPack
import kotlinx.coroutines.*
import team.unnamed.creative.ResourcePack
import team.unnamed.creative.base.Writable
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter
import team.unnamed.creative.sound.SoundRegistry
import kotlin.io.path.div
import kotlin.io.path.exists
Expand Down Expand Up @@ -63,7 +62,7 @@ object PackyGenerator {
cachedPack.sortItemOverrides()
PackObfuscator(cachedPack).obfuscatePack()

val builtPack = MinecraftResourcePackWriter.minecraft().build(cachedPack)
val builtPack = packy.writer.build(cachedPack)
PackyPack(builtPack, templateIds).apply {
cachedPacks[templateIds] = this
cachedPacksByteArray[templateIds] = builtPack.data().toByteArray()
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import kotlinx.coroutines.Job
import kotlinx.serialization.modules.EmptySerializersModule
import org.bukkit.plugin.java.JavaPlugin
import team.unnamed.creative.ResourcePack
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackReader
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter

class PackyPlugin : JavaPlugin() {

Expand Down Expand Up @@ -57,13 +59,15 @@ class PackyPlugin : JavaPlugin() {
override val accessToken: PackyAccessToken by config("accessToken", dataFolder.toPath(), PackyAccessToken())
override val defaultPack: ResourcePack = ResourcePack.resourcePack()
override val logger by plugin.observeLogger()
override val reader = MinecraftResourcePackReader.builder().lenient(true).build()
override val writer = MinecraftResourcePackWriter.builder().prettyPrinting(false).build()
})

PackyGenerator.activeGeneratorJob.apply { values.forEach(Job::cancel) }.clear()
PackyGenerator.cachedPacks.clear()
PackyGenerator.cachedPacksByteArray.clear()
PackyDownloader.downloadTemplates()
TemplateLoadTriggers.registerTemplateHandlers()
//TemplateLoadTriggers.registerTemplateHandlers()
PackyGenerator.setupRequiredPackTemplates()
PackyServer.registerConfigPacketHandler()
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/kotlin/com/mineinabyss/packy/PackyServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ConfigurationTask
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl
import net.minecraft.server.network.config.ServerResourcePackConfigurationTask
import org.bukkit.craftbukkit.CraftServer
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerResourcePackStatusEvent
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter
import team.unnamed.creative.server.ResourcePackServer
import team.unnamed.creative.server.handler.ResourcePackRequestHandler
import java.net.URI
Expand Down Expand Up @@ -124,7 +121,7 @@ object PackyServer {
private val handler = ResourcePackRequestHandler { _, exchange ->
val data = exchange.requestURI.parseTemplateIds()
?.let { templateIds -> PackyGenerator.cachedPacksByteArray[templateIds] }
?: MinecraftResourcePackWriter.minecraft().build(packy.defaultPack).data().toByteArray()
?: packy.writer.build(packy.defaultPack).data().toByteArray()
exchange.responseHeaders["Content-Type"] = "application/zip"
exchange.sendResponseHeaders(200, data.size.toLong())
exchange.responseBody.use { responseStream -> responseStream.write(data) }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mineinabyss/packy/PackySquash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object PackySquash {

val packName = UUID.randomUUID().toString()
val packDir = packy.plugin.dataFolder.resolve("packsquash").resolve(packName)
MinecraftResourcePackWriter.minecraft().writeToDirectory(packDir, resourcePack)
packy.writer.writeToDirectory(packDir, resourcePack)

val toml = packDir.parentFile.resolve("$packName.toml")
val tomlContent = baseToml.readText()
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/mineinabyss/packy/config/PackyContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.ComponentLogger
import com.mineinabyss.packy.PackyPlugin
import team.unnamed.creative.ResourcePack
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackReader
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter

val packy by DI.observe<PackyContext>()
interface PackyContext {
Expand All @@ -13,4 +15,6 @@ interface PackyContext {
val templates: Map<String, PackyTemplate>
val accessToken: PackyAccessToken
val logger: ComponentLogger
val reader: MinecraftResourcePackReader
val writer: MinecraftResourcePackWriter
}
7 changes: 3 additions & 4 deletions src/main/kotlin/com/mineinabyss/packy/helpers/PackyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import kotlin.io.path.deleteIfExists
import kotlin.io.path.deleteRecursively

fun File.readPack(): ResourcePack? {
val reader = MinecraftResourcePackReader.minecraft()
return runCatching {
when {
!exists() -> null
isDirectory && !listFiles().isNullOrEmpty() -> reader.readFromDirectory(this)
extension == "zip" -> reader.readFromZipFile(this)
isDirectory && !listFiles().isNullOrEmpty() -> packy.reader.readFromDirectory(this)
extension == "zip" -> packy.reader.readFromZipFile(this)
else -> null
}
}.getOrNull()
}.onFailure { packy.logger.w(this.name + ": " + it.message) }.getOrNull()
}

@OptIn(ExperimentalPathApi::class)
Expand Down

0 comments on commit f7b700b

Please sign in to comment.