Skip to content

Commit

Permalink
🎨 optimize format
Browse files Browse the repository at this point in the history
  • Loading branch information
XiYang6666 committed Aug 9, 2024
1 parent bfaf7b3 commit d1862ea
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 73 deletions.
29 changes: 13 additions & 16 deletions src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import org.bukkit.inventory.ItemStack

class NBTItemDataServices : ItemDataServices {
override fun <T> setItemData(
item: ItemStack,
path: String,
dataType: ItemDataServices.DataType<T>,
value: T
item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>, value: T
): ItemStack? {
val nbtItem = NBTItem(item)
val (nbtPath, key) = splitPathAndKey(path)
Expand All @@ -29,22 +26,23 @@ class NBTItemDataServices : ItemDataServices {
return nbtItem.item
}

@Suppress("UNCHECKED_CAST")
override fun <T> getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>): 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 {
Expand All @@ -65,5 +63,4 @@ class NBTItemDataServices : ItemDataServices {
}
return current
}

}
56 changes: 28 additions & 28 deletions src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,34 @@ import org.bukkit.persistence.PersistentDataType
import xyz.xasmc.hashbook.HashBook

class PDCItemDataServices : ItemDataServices {
@Suppress("UNCHECKED_CAST")
override fun <T> setItemData(
item: ItemStack,
path: String,
dataType: ItemDataServices.DataType<T>,
value: T
item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>, 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 <T> getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>): 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
}
}
Expand All @@ -44,17 +43,18 @@ class PDCItemDataServices : ItemDataServices {
return itemMeta.persistentDataContainer.has(NamespacedKey(HashBook.instance, path))
}

private fun <T> DataTypeToPdcType(type: ItemDataServices.DataType<T>): PersistentDataType<T, T>? {
@Suppress("UNCHECKED_CAST")
private fun <T> dataTypeToPdcType(type: ItemDataServices.DataType<T>): PersistentDataType<T, T>? {
return when (type) {
ItemDataServices.DataType.Byte -> PersistentDataType.BYTE as PersistentDataType<T, T>
ItemDataServices.DataType.Short -> PersistentDataType.SHORT as PersistentDataType<T, T>
ItemDataServices.DataType.Long -> PersistentDataType.LONG as PersistentDataType<T, T>
ItemDataServices.DataType.Float -> PersistentDataType.FLOAT as PersistentDataType<T, T>
ItemDataServices.DataType.Double -> PersistentDataType.DOUBLE as PersistentDataType<T, T>
ItemDataServices.DataType.ByteArray -> PersistentDataType.BYTE_ARRAY as PersistentDataType<T, T>
ItemDataServices.DataType.Boolean -> PersistentDataType.BYTE as PersistentDataType<T, T>
ItemDataServices.DataType.String -> PersistentDataType.STRING as PersistentDataType<T, T>
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<T, T>
}
}
71 changes: 42 additions & 29 deletions src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,57 @@ import org.bukkit.entity.Player

object MarkUtil {
private val mm = MiniMessage.miniMessage()
private val playerMark = mutableMapOf<Player, MutableList<ArmorStand>>()

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<ArmorStand>().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<Player, Mark>()

class Mark {
private val armorStands = mutableListOf<ArmorStand>()

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)
}
}
}

0 comments on commit d1862ea

Please sign in to comment.