feat: NTBAPI非依存シリアライズ・デシリアライズ
This commit is contained in:
@@ -46,18 +46,15 @@ public object EventListener : Listener {
|
||||
| not | confirm register |
|
||||
+------------+------------------*/
|
||||
if (isRegistered(event.player.uniqueId)) fetch(event.player)
|
||||
else if (event.player.hasPlayedBefore()) {
|
||||
register(event.player)
|
||||
} else {
|
||||
event.player.sendMessage(
|
||||
MiniMessage.miniMessage()
|
||||
.deserialize(
|
||||
"<gray>[SMCDB] Welcome, ${event.player.name}.<newline>" +
|
||||
"SMCDB is active but you have already played before.<newline>" +
|
||||
"Run <green>/smcdb register<gray> to register yourself."
|
||||
)
|
||||
else if (event.player.hasPlayedBefore()) register(event.player)
|
||||
else event.player.sendMessage(
|
||||
MiniMessage.miniMessage()
|
||||
.deserialize(
|
||||
"<gray>[SMCDB] Welcome, ${event.player.name}.<newline>" +
|
||||
"SMCDB is active but you have already played before.<newline>" +
|
||||
"Run <green>/smcdb register<gray> to register yourself."
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
||||
@@ -1,61 +1,168 @@
|
||||
package net.hareworks.simplymcdb
|
||||
|
||||
import de.tr7zw.nbtapi.NBT
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.Base64
|
||||
import java.util.function.Function
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.bukkit.entity.Player as BukkitPlayer
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.potion.PotionEffect
|
||||
import org.bukkit.potion.PotionEffectType
|
||||
import org.bukkit.util.io.BukkitObjectInputStream
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream
|
||||
|
||||
data class PlayerData(
|
||||
val health: Float,
|
||||
val hunger: Int,
|
||||
val exp: Float,
|
||||
val effects: String,
|
||||
val inv: String,
|
||||
val enderchest: String
|
||||
@Serializable
|
||||
data class PlayerSnapshot(
|
||||
val version: Int = CURRENT_VERSION,
|
||||
val health: Double,
|
||||
val foodLevel: Int,
|
||||
val xpProgress: Float,
|
||||
val selectedItemSlot: Int,
|
||||
val potionEffects: List<PotionEffectSnapshot>,
|
||||
val inventory: List<ItemStackSnapshot?>,
|
||||
val enderChest: List<ItemStackSnapshot?>
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class PotionEffectSnapshot(
|
||||
val type: String,
|
||||
val amplifier: Int,
|
||||
val duration: Int,
|
||||
val ambient: Boolean,
|
||||
val particles: Boolean,
|
||||
val icon: Boolean
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ItemStackSnapshot(@SerialName("value") val encodedItem: String)
|
||||
|
||||
private const val CURRENT_VERSION = 1
|
||||
private val json =
|
||||
Json {
|
||||
encodeDefaults = true
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
object PlayerSerializer {
|
||||
fun serialize(player: BukkitPlayer): String {
|
||||
val result =
|
||||
NBT.get(
|
||||
player,
|
||||
Function { nbt ->
|
||||
var output = NBT.createNBTObject()
|
||||
|
||||
output.setFloat("Health", player.health.toFloat())
|
||||
output.setInteger("foodLevel", player.foodLevel)
|
||||
output.setFloat("XpP", player.exp)
|
||||
output.setInteger("SelectedItemSlot", player.inventory.heldItemSlot)
|
||||
var active_effects = output.getCompoundList("active_effects")
|
||||
nbt.getCompoundList("active_effects").forEach {
|
||||
active_effects.addCompound(it)
|
||||
}
|
||||
var inventory = output.getCompoundList("Inventory")
|
||||
nbt.getCompoundList("Inventory").forEach { inventory.addCompound(it) }
|
||||
var enderchest = output.getCompoundList("EnderItems")
|
||||
nbt.getCompoundList("EnderItems").forEach { enderchest.addCompound(it) }
|
||||
|
||||
output.toString()
|
||||
}
|
||||
val snapshot =
|
||||
PlayerSnapshot(
|
||||
health = player.health,
|
||||
foodLevel = player.foodLevel,
|
||||
xpProgress = player.exp,
|
||||
selectedItemSlot = player.inventory.heldItemSlot,
|
||||
potionEffects = player.activePotionEffects.map(::serializePotionEffect),
|
||||
inventory = player.inventory.contents.map { it?.let(::serializeItemStack) },
|
||||
enderChest = player.enderChest.contents.map { it?.let(::serializeItemStack) }
|
||||
)
|
||||
return result
|
||||
return json.encodeToString(PlayerSnapshot.serializer(), snapshot)
|
||||
}
|
||||
|
||||
fun deserialize(player: BukkitPlayer, data: String) {
|
||||
val snapshot =
|
||||
try {
|
||||
json.decodeFromString(PlayerSnapshot.serializer(), data)
|
||||
} catch (_: SerializationException) {
|
||||
return LegacySerializer.deserialize(player, data)
|
||||
} catch (_: IllegalArgumentException) {
|
||||
return LegacySerializer.deserialize(player, data)
|
||||
}
|
||||
applySnapshot(player, migrateIfNeeded(snapshot))
|
||||
}
|
||||
|
||||
private fun migrateIfNeeded(snapshot: PlayerSnapshot): PlayerSnapshot {
|
||||
var current = snapshot
|
||||
var version = snapshot.version
|
||||
while (version < CURRENT_VERSION) {
|
||||
current = migrateOnce(version, current)
|
||||
version++
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
private fun migrateOnce(fromVersion: Int, snapshot: PlayerSnapshot): PlayerSnapshot {
|
||||
return when (fromVersion) {
|
||||
1 -> snapshot
|
||||
else -> snapshot
|
||||
}
|
||||
}
|
||||
|
||||
private fun applySnapshot(player: BukkitPlayer, snapshot: PlayerSnapshot) {
|
||||
player.health = snapshot.health.coerceIn(0.0, player.maxHealth)
|
||||
player.foodLevel = snapshot.foodLevel.coerceIn(0, 20)
|
||||
player.exp = snapshot.xpProgress.coerceIn(0f, 1f)
|
||||
player.inventory.heldItemSlot =
|
||||
snapshot.selectedItemSlot.coerceIn(0, player.inventory.contents.size - 1)
|
||||
|
||||
val appliedTypes = player.activePotionEffects.map { it.type }.toSet()
|
||||
appliedTypes.forEach { it?.let(player::removePotionEffect) }
|
||||
snapshot.potionEffects.forEach { eff ->
|
||||
val type = PotionEffectType.getByName(eff.type)
|
||||
if (type == null) {
|
||||
App.instance.logger.warning("Unknown potion effect type during restore: ${eff.type}")
|
||||
return@forEach
|
||||
}
|
||||
val potion = PotionEffect(type, eff.duration, eff.amplifier, eff.ambient, eff.particles, eff.icon)
|
||||
player.addPotionEffect(potion)
|
||||
}
|
||||
|
||||
player.inventory.clear()
|
||||
snapshot.inventory.forEachIndexed { index, item ->
|
||||
player.inventory.setItem(index, item?.let(::deserializeItemStack))
|
||||
}
|
||||
|
||||
player.enderChest.clear()
|
||||
snapshot.enderChest.forEachIndexed { index, item ->
|
||||
player.enderChest.setItem(index, item?.let(::deserializeItemStack))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun serializePotionEffect(effect: PotionEffect): PotionEffectSnapshot {
|
||||
return PotionEffectSnapshot(
|
||||
type = effect.type.name ?: "",
|
||||
amplifier = effect.amplifier,
|
||||
duration = effect.duration,
|
||||
ambient = effect.isAmbient,
|
||||
particles = effect.hasParticles(),
|
||||
icon = effect.hasIcon()
|
||||
)
|
||||
}
|
||||
|
||||
private fun serializeItemStack(item: ItemStack): ItemStackSnapshot {
|
||||
val byteArray =
|
||||
ByteArrayOutputStream().use { byteStream ->
|
||||
BukkitObjectOutputStream(byteStream).use { out -> out.writeObject(item) }
|
||||
byteStream.toByteArray()
|
||||
}
|
||||
return ItemStackSnapshot(Base64.getEncoder().encodeToString(byteArray))
|
||||
}
|
||||
|
||||
private fun deserializeItemStack(snapshot: ItemStackSnapshot): ItemStack {
|
||||
val data = Base64.getDecoder().decode(snapshot.encodedItem)
|
||||
return ByteArrayInputStream(data).use { byteStream ->
|
||||
BukkitObjectInputStream(byteStream).use { input -> input.readObject() as ItemStack }
|
||||
}
|
||||
}
|
||||
|
||||
private object LegacySerializer {
|
||||
fun deserialize(player: BukkitPlayer, data: String) {
|
||||
NBT.modify(
|
||||
player,
|
||||
Function { nbt ->
|
||||
val input = NBT.parseNBT(data)
|
||||
|
||||
App.instance.logger.info("Deserializing player data: $data")
|
||||
App.instance.logger.info("deserialized: ${input.toString()}")
|
||||
|
||||
nbt.setFloat("Health", input.getFloat("Health"))
|
||||
nbt.setInteger("foodLevel", input.getInteger("foodLevel"))
|
||||
nbt.setFloat("XpP", input.getFloat("XpP"))
|
||||
nbt.setInteger("SelectedItemSlot", input.getInteger("SelectedItemSlot"))
|
||||
val active_effects = nbt.getCompoundList("active_effects")
|
||||
active_effects.clear()
|
||||
input.getCompoundList("active_effects").forEach { active_effects.addCompound(it) }
|
||||
val activeEffects = nbt.getCompoundList("active_effects")
|
||||
activeEffects.clear()
|
||||
input.getCompoundList("active_effects").forEach { activeEffects.addCompound(it) }
|
||||
val inventory = nbt.getCompoundList("Inventory")
|
||||
inventory.clear()
|
||||
input.getCompoundList("Inventory").forEach { inventory.addCompound(it) }
|
||||
@@ -64,5 +171,6 @@ object PlayerSerializer {
|
||||
input.getCompoundList("EnderItems").forEach { enderchest.addCompound(it) }
|
||||
}
|
||||
)
|
||||
App.instance.logger.info("Legacy player data applied; will be migrated on next save.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user