From d1862ea106f34030991550f786bb6e50c97ee51a Mon Sep 17 00:00:00 2001 From: XiYang6666 <1782356858@qq.com> Date: Fri, 9 Aug 2024 16:24:43 +0800 Subject: [PATCH] :art: optimize format --- .../hashbook/service/NBTItemDataServices.kt | 29 ++++---- .../hashbook/service/PDCItemDataServices.kt | 56 +++++++-------- .../xyz/xasmc/hashbook/util/MarkUtil.kt | 71 +++++++++++-------- 3 files changed, 83 insertions(+), 73 deletions(-) diff --git a/src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt b/src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt index 6f88ad9..509f7e4 100644 --- a/src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt +++ b/src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt @@ -6,10 +6,7 @@ import org.bukkit.inventory.ItemStack class NBTItemDataServices : ItemDataServices { override fun setItemData( - item: ItemStack, - path: String, - dataType: ItemDataServices.DataType, - value: T + item: ItemStack, path: String, dataType: ItemDataServices.DataType, value: T ): ItemStack? { val nbtItem = NBTItem(item) val (nbtPath, key) = splitPathAndKey(path) @@ -29,22 +26,23 @@ class NBTItemDataServices : ItemDataServices { return nbtItem.item } + @Suppress("UNCHECKED_CAST") override fun getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType): T? { val nbtItem = NBTItem(item) val (nbtPath, key) = splitPathAndKey(path) val compound = getNbtCompoundByPath(nbtItem, nbtPath) ?: return null - - return if (compound.hasKey(path)) when (dataType) { - ItemDataServices.DataType.Byte -> compound.getByte(path) as T - ItemDataServices.DataType.Short -> compound.getShort(path) as T - ItemDataServices.DataType.Long -> compound.getLong(path) as T - ItemDataServices.DataType.ByteArray -> compound.getByteArray(path) as T - ItemDataServices.DataType.Double -> compound.getDouble(path) as T - ItemDataServices.DataType.Float -> compound.getFloat(path) as T - ItemDataServices.DataType.Boolean -> compound.getBoolean(path) as T - ItemDataServices.DataType.String -> compound.getString(path) as T + if (!compound.hasKey(key)) return null + return when (dataType) { + ItemDataServices.DataType.Byte -> compound.getByte(key) + ItemDataServices.DataType.Short -> compound.getShort(key) + ItemDataServices.DataType.Long -> compound.getLong(key) + ItemDataServices.DataType.ByteArray -> compound.getByteArray(key) + ItemDataServices.DataType.Double -> compound.getDouble(key) + ItemDataServices.DataType.Float -> compound.getFloat(key) + ItemDataServices.DataType.Boolean -> compound.getBoolean(key) + ItemDataServices.DataType.String -> compound.getString(key) else -> null - } else null + } as T? } override fun hasItemData(item: ItemStack, path: String): Boolean { @@ -65,5 +63,4 @@ class NBTItemDataServices : ItemDataServices { } return current } - } \ No newline at end of file diff --git a/src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt b/src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt index 419f9ca..176db25 100644 --- a/src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt +++ b/src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt @@ -6,35 +6,34 @@ import org.bukkit.persistence.PersistentDataType import xyz.xasmc.hashbook.HashBook class PDCItemDataServices : ItemDataServices { + @Suppress("UNCHECKED_CAST") override fun setItemData( - item: ItemStack, - path: String, - dataType: ItemDataServices.DataType, - value: T + item: ItemStack, path: String, dataType: ItemDataServices.DataType, value: T ): ItemStack? { val itemMeta = item.itemMeta - itemMeta.persistentDataContainer.set( - NamespacedKey(HashBook.instance, path), - DataTypeToPdcType(dataType) ?: return null, - when (dataType) { - ItemDataServices.DataType.Boolean -> (if (value as Boolean) 1 else 0).toByte() - else -> value - } as (T & Any) - ) + val namespacedKey = NamespacedKey(HashBook.instance, path) + val type = dataTypeToPdcType(dataType) ?: return null + val data = when (dataType) { + ItemDataServices.DataType.Boolean -> + if (value is Boolean) (if (value) 1 else 0).toByte() as T + else return null + + else -> value + } as (T & Any) + itemMeta.persistentDataContainer.set(namespacedKey, type, data) item.itemMeta = itemMeta return item } + @Suppress("UNCHECKED_CAST") override fun getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType): T? { val itemMeta = item.itemMeta - val result = - itemMeta.persistentDataContainer.get( - NamespacedKey(HashBook.instance, path), - DataTypeToPdcType(dataType) ?: return null - ) + val namespace = NamespacedKey(HashBook.instance, path) + val type = dataTypeToPdcType(dataType) ?: return null + val result = itemMeta.persistentDataContainer.get(namespace, type) return when (dataType) { - ItemDataServices.DataType.Boolean -> (result != 0) as T + ItemDataServices.DataType.Boolean -> ((result is Byte) && (result.toInt() != 0)) as T else -> result } } @@ -44,17 +43,18 @@ class PDCItemDataServices : ItemDataServices { return itemMeta.persistentDataContainer.has(NamespacedKey(HashBook.instance, path)) } - private fun DataTypeToPdcType(type: ItemDataServices.DataType): PersistentDataType? { + @Suppress("UNCHECKED_CAST") + private fun dataTypeToPdcType(type: ItemDataServices.DataType): PersistentDataType? { return when (type) { - ItemDataServices.DataType.Byte -> PersistentDataType.BYTE as PersistentDataType - ItemDataServices.DataType.Short -> PersistentDataType.SHORT as PersistentDataType - ItemDataServices.DataType.Long -> PersistentDataType.LONG as PersistentDataType - ItemDataServices.DataType.Float -> PersistentDataType.FLOAT as PersistentDataType - ItemDataServices.DataType.Double -> PersistentDataType.DOUBLE as PersistentDataType - ItemDataServices.DataType.ByteArray -> PersistentDataType.BYTE_ARRAY as PersistentDataType - ItemDataServices.DataType.Boolean -> PersistentDataType.BYTE as PersistentDataType - ItemDataServices.DataType.String -> PersistentDataType.STRING as PersistentDataType + ItemDataServices.DataType.Byte -> PersistentDataType.BYTE + ItemDataServices.DataType.Short -> PersistentDataType.SHORT + ItemDataServices.DataType.Long -> PersistentDataType.LONG + ItemDataServices.DataType.Float -> PersistentDataType.FLOAT + ItemDataServices.DataType.Double -> PersistentDataType.DOUBLE + ItemDataServices.DataType.ByteArray -> PersistentDataType.BYTE_ARRAY + ItemDataServices.DataType.Boolean -> PersistentDataType.BYTE + ItemDataServices.DataType.String -> PersistentDataType.STRING else -> return null - } + } as PersistentDataType } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt b/src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt index 91c45a7..90f30d7 100644 --- a/src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt +++ b/src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt @@ -9,44 +9,57 @@ import org.bukkit.entity.Player object MarkUtil { private val mm = MiniMessage.miniMessage() - private val playerMark = mutableMapOf>() - - fun updateMark(player: Player, location: Location, name: String) { - val world = player.world - val textList = name.split("\n") - val count = textList.size - val interval = 0.2 - val offset = -0.4 - val marks = playerMark[player] ?: mutableListOf().also { playerMark[player] = it } - val currentCount = marks.size - when { - count > currentCount -> repeat(count - currentCount) { marks.add(createArmorStand(world)) } - count < currentCount -> repeat(currentCount - count) { marks.removeLast()?.remove() } + private val playerMark = mutableMapOf() + + class Mark { + private val armorStands = mutableListOf() + + fun update(location: Location, text: String) { + val world = location.world + val textList = text.lines() + val count = textList.size + val interval = 0.2 + val offset = -0.4 + val currentCount = armorStands.size + when { + count > currentCount -> repeat(count - currentCount) { armorStands.add(createArmorStand(world)) } + count < currentCount -> repeat(currentCount - count) { armorStands.removeLast()?.remove() } + } + val top = location.clone().add(0.0, interval * count / 2 + offset, 0.0) + textList.forEachIndexed { i, it -> + val mark = armorStands[i] + mark.teleport(top.clone().add(0.0, -i * interval, 0.0)) + mark.customName(mm.deserialize(it)) + } } - val top = location.clone().add(0.0, interval * count / 2 + offset, 0.0) - textList.forEachIndexed { i, it -> - val mark = playerMark[player]!![i] - mark.teleport(top.clone().add(0.0, -i * interval, 0.0)) - mark.customName(mm.deserialize(it)) + + fun remove() { + armorStands.forEach { it.remove() } + armorStands.clear() + } + + private fun createArmorStand(world: World): ArmorStand { + return (world.spawnEntity(Location(world, .0, .0, .0), EntityType.ARMOR_STAND) as ArmorStand).apply { + isVisible = false + isMarker = true + isCustomNameVisible = true + setGravity(false) + } } } + fun updateMark(player: Player, location: Location, text: String) { + val mark = playerMark[player] ?: Mark().also { playerMark[player] = it } + mark.update(location, text) + } + fun removeMark(player: Player) { - playerMark[player]?.forEach { it.remove() } + playerMark[player]?.remove() playerMark.remove(player) } fun clearAllMark() { - playerMark.forEach { it.value.forEach { it.remove() } } + playerMark.forEach { it.value.remove() } playerMark.clear() } - - private fun createArmorStand(world: World): ArmorStand { - return (world.spawnEntity(Location(world, .0, .0, .0), EntityType.ARMOR_STAND) as ArmorStand).apply { - isVisible = false - isMarker = true - isCustomNameVisible = true - setGravity(false) - } - } } \ No newline at end of file