Skip to content

Commit d1862ea

Browse files
committed
🎨 optimize format
1 parent bfaf7b3 commit d1862ea

File tree

3 files changed

+83
-73
lines changed

3 files changed

+83
-73
lines changed

src/main/kotlin/xyz/xasmc/hashbook/service/NBTItemDataServices.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import org.bukkit.inventory.ItemStack
66

77
class NBTItemDataServices : ItemDataServices {
88
override fun <T> setItemData(
9-
item: ItemStack,
10-
path: String,
11-
dataType: ItemDataServices.DataType<T>,
12-
value: T
9+
item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>, value: T
1310
): ItemStack? {
1411
val nbtItem = NBTItem(item)
1512
val (nbtPath, key) = splitPathAndKey(path)
@@ -29,22 +26,23 @@ class NBTItemDataServices : ItemDataServices {
2926
return nbtItem.item
3027
}
3128

29+
@Suppress("UNCHECKED_CAST")
3230
override fun <T> getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>): T? {
3331
val nbtItem = NBTItem(item)
3432
val (nbtPath, key) = splitPathAndKey(path)
3533
val compound = getNbtCompoundByPath(nbtItem, nbtPath) ?: return null
36-
37-
return if (compound.hasKey(path)) when (dataType) {
38-
ItemDataServices.DataType.Byte -> compound.getByte(path) as T
39-
ItemDataServices.DataType.Short -> compound.getShort(path) as T
40-
ItemDataServices.DataType.Long -> compound.getLong(path) as T
41-
ItemDataServices.DataType.ByteArray -> compound.getByteArray(path) as T
42-
ItemDataServices.DataType.Double -> compound.getDouble(path) as T
43-
ItemDataServices.DataType.Float -> compound.getFloat(path) as T
44-
ItemDataServices.DataType.Boolean -> compound.getBoolean(path) as T
45-
ItemDataServices.DataType.String -> compound.getString(path) as T
34+
if (!compound.hasKey(key)) return null
35+
return when (dataType) {
36+
ItemDataServices.DataType.Byte -> compound.getByte(key)
37+
ItemDataServices.DataType.Short -> compound.getShort(key)
38+
ItemDataServices.DataType.Long -> compound.getLong(key)
39+
ItemDataServices.DataType.ByteArray -> compound.getByteArray(key)
40+
ItemDataServices.DataType.Double -> compound.getDouble(key)
41+
ItemDataServices.DataType.Float -> compound.getFloat(key)
42+
ItemDataServices.DataType.Boolean -> compound.getBoolean(key)
43+
ItemDataServices.DataType.String -> compound.getString(key)
4644
else -> null
47-
} else null
45+
} as T?
4846
}
4947

5048
override fun hasItemData(item: ItemStack, path: String): Boolean {
@@ -65,5 +63,4 @@ class NBTItemDataServices : ItemDataServices {
6563
}
6664
return current
6765
}
68-
6966
}

src/main/kotlin/xyz/xasmc/hashbook/service/PDCItemDataServices.kt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,34 @@ import org.bukkit.persistence.PersistentDataType
66
import xyz.xasmc.hashbook.HashBook
77

88
class PDCItemDataServices : ItemDataServices {
9+
@Suppress("UNCHECKED_CAST")
910
override fun <T> setItemData(
10-
item: ItemStack,
11-
path: String,
12-
dataType: ItemDataServices.DataType<T>,
13-
value: T
11+
item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>, value: T
1412
): ItemStack? {
1513
val itemMeta = item.itemMeta
16-
itemMeta.persistentDataContainer.set(
17-
NamespacedKey(HashBook.instance, path),
18-
DataTypeToPdcType(dataType) ?: return null,
19-
when (dataType) {
20-
ItemDataServices.DataType.Boolean -> (if (value as Boolean) 1 else 0).toByte()
21-
else -> value
22-
} as (T & Any)
23-
)
14+
val namespacedKey = NamespacedKey(HashBook.instance, path)
15+
val type = dataTypeToPdcType(dataType) ?: return null
16+
val data = when (dataType) {
17+
ItemDataServices.DataType.Boolean ->
18+
if (value is Boolean) (if (value) 1 else 0).toByte() as T
19+
else return null
20+
21+
else -> value
22+
} as (T & Any)
23+
itemMeta.persistentDataContainer.set(namespacedKey, type, data)
2424
item.itemMeta = itemMeta
2525
return item
2626
}
2727

28+
@Suppress("UNCHECKED_CAST")
2829
override fun <T> getItemData(item: ItemStack, path: String, dataType: ItemDataServices.DataType<T>): T? {
2930
val itemMeta = item.itemMeta
30-
val result =
31-
itemMeta.persistentDataContainer.get(
32-
NamespacedKey(HashBook.instance, path),
33-
DataTypeToPdcType(dataType) ?: return null
34-
)
31+
val namespace = NamespacedKey(HashBook.instance, path)
32+
val type = dataTypeToPdcType(dataType) ?: return null
33+
val result = itemMeta.persistentDataContainer.get(namespace, type)
3534

3635
return when (dataType) {
37-
ItemDataServices.DataType.Boolean -> (result != 0) as T
36+
ItemDataServices.DataType.Boolean -> ((result is Byte) && (result.toInt() != 0)) as T
3837
else -> result
3938
}
4039
}
@@ -44,17 +43,18 @@ class PDCItemDataServices : ItemDataServices {
4443
return itemMeta.persistentDataContainer.has(NamespacedKey(HashBook.instance, path))
4544
}
4645

47-
private fun <T> DataTypeToPdcType(type: ItemDataServices.DataType<T>): PersistentDataType<T, T>? {
46+
@Suppress("UNCHECKED_CAST")
47+
private fun <T> dataTypeToPdcType(type: ItemDataServices.DataType<T>): PersistentDataType<T, T>? {
4848
return when (type) {
49-
ItemDataServices.DataType.Byte -> PersistentDataType.BYTE as PersistentDataType<T, T>
50-
ItemDataServices.DataType.Short -> PersistentDataType.SHORT as PersistentDataType<T, T>
51-
ItemDataServices.DataType.Long -> PersistentDataType.LONG as PersistentDataType<T, T>
52-
ItemDataServices.DataType.Float -> PersistentDataType.FLOAT as PersistentDataType<T, T>
53-
ItemDataServices.DataType.Double -> PersistentDataType.DOUBLE as PersistentDataType<T, T>
54-
ItemDataServices.DataType.ByteArray -> PersistentDataType.BYTE_ARRAY as PersistentDataType<T, T>
55-
ItemDataServices.DataType.Boolean -> PersistentDataType.BYTE as PersistentDataType<T, T>
56-
ItemDataServices.DataType.String -> PersistentDataType.STRING as PersistentDataType<T, T>
49+
ItemDataServices.DataType.Byte -> PersistentDataType.BYTE
50+
ItemDataServices.DataType.Short -> PersistentDataType.SHORT
51+
ItemDataServices.DataType.Long -> PersistentDataType.LONG
52+
ItemDataServices.DataType.Float -> PersistentDataType.FLOAT
53+
ItemDataServices.DataType.Double -> PersistentDataType.DOUBLE
54+
ItemDataServices.DataType.ByteArray -> PersistentDataType.BYTE_ARRAY
55+
ItemDataServices.DataType.Boolean -> PersistentDataType.BYTE
56+
ItemDataServices.DataType.String -> PersistentDataType.STRING
5757
else -> return null
58-
}
58+
} as PersistentDataType<T, T>
5959
}
6060
}

src/main/kotlin/xyz/xasmc/hashbook/util/MarkUtil.kt

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,57 @@ import org.bukkit.entity.Player
99

1010
object MarkUtil {
1111
private val mm = MiniMessage.miniMessage()
12-
private val playerMark = mutableMapOf<Player, MutableList<ArmorStand>>()
13-
14-
fun updateMark(player: Player, location: Location, name: String) {
15-
val world = player.world
16-
val textList = name.split("\n")
17-
val count = textList.size
18-
val interval = 0.2
19-
val offset = -0.4
20-
val marks = playerMark[player] ?: mutableListOf<ArmorStand>().also { playerMark[player] = it }
21-
val currentCount = marks.size
22-
when {
23-
count > currentCount -> repeat(count - currentCount) { marks.add(createArmorStand(world)) }
24-
count < currentCount -> repeat(currentCount - count) { marks.removeLast()?.remove() }
12+
private val playerMark = mutableMapOf<Player, Mark>()
13+
14+
class Mark {
15+
private val armorStands = mutableListOf<ArmorStand>()
16+
17+
fun update(location: Location, text: String) {
18+
val world = location.world
19+
val textList = text.lines()
20+
val count = textList.size
21+
val interval = 0.2
22+
val offset = -0.4
23+
val currentCount = armorStands.size
24+
when {
25+
count > currentCount -> repeat(count - currentCount) { armorStands.add(createArmorStand(world)) }
26+
count < currentCount -> repeat(currentCount - count) { armorStands.removeLast()?.remove() }
27+
}
28+
val top = location.clone().add(0.0, interval * count / 2 + offset, 0.0)
29+
textList.forEachIndexed { i, it ->
30+
val mark = armorStands[i]
31+
mark.teleport(top.clone().add(0.0, -i * interval, 0.0))
32+
mark.customName(mm.deserialize(it))
33+
}
2534
}
26-
val top = location.clone().add(0.0, interval * count / 2 + offset, 0.0)
27-
textList.forEachIndexed { i, it ->
28-
val mark = playerMark[player]!![i]
29-
mark.teleport(top.clone().add(0.0, -i * interval, 0.0))
30-
mark.customName(mm.deserialize(it))
35+
36+
fun remove() {
37+
armorStands.forEach { it.remove() }
38+
armorStands.clear()
39+
}
40+
41+
private fun createArmorStand(world: World): ArmorStand {
42+
return (world.spawnEntity(Location(world, .0, .0, .0), EntityType.ARMOR_STAND) as ArmorStand).apply {
43+
isVisible = false
44+
isMarker = true
45+
isCustomNameVisible = true
46+
setGravity(false)
47+
}
3148
}
3249
}
3350

51+
fun updateMark(player: Player, location: Location, text: String) {
52+
val mark = playerMark[player] ?: Mark().also { playerMark[player] = it }
53+
mark.update(location, text)
54+
}
55+
3456
fun removeMark(player: Player) {
35-
playerMark[player]?.forEach { it.remove() }
57+
playerMark[player]?.remove()
3658
playerMark.remove(player)
3759
}
3860

3961
fun clearAllMark() {
40-
playerMark.forEach { it.value.forEach { it.remove() } }
62+
playerMark.forEach { it.value.remove() }
4163
playerMark.clear()
4264
}
43-
44-
private fun createArmorStand(world: World): ArmorStand {
45-
return (world.spawnEntity(Location(world, .0, .0, .0), EntityType.ARMOR_STAND) as ArmorStand).apply {
46-
isVisible = false
47-
isMarker = true
48-
isCustomNameVisible = true
49-
setGravity(false)
50-
}
51-
}
5265
}

0 commit comments

Comments
 (0)