Skip to content

Commit

Permalink
feat: editLore を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Apr 9, 2022
1 parent a1d62ff commit b8eb569
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/v1_8/src/main/kotlin/dev/s7a/ktspigot/item/extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ inline fun <reified T : ItemMeta> ItemStack.editItemMeta(action: T.() -> Unit):
}
}

/**
* アイテムの説明文を変更する
*
* @param altColorChar [color] に使う文字 / '&'
* @param action 説明文の変更処理
* @since 1.0.0
*/
inline fun ItemMeta.editLore(altColorChar: Char? = '&', action: MutableList<String>.() -> Unit) {
lore = loreOrNull.orEmpty().toMutableList().apply(action).color(altColorChar)
}

/**
* アイテムの説明文が存在するならば変更する
*
* @param altColorChar [color] に使う文字 / '&'
* @param action 説明文の変更処理
* @return 説明文を変更できたら true
* @since 1.0.0
*/
inline fun ItemMeta.editLoreIfHas(altColorChar: Char? = '&', action: MutableList<String>.() -> Unit): Boolean {
lore = loreOrNull?.toMutableList()?.apply(action)?.color(altColorChar) ?: return false
return true
}

/**
* [ItemStack] を生成する
*
Expand Down
62 changes: 62 additions & 0 deletions src/test/kotlin/util/ItemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.s7a.ktspigot.KtSpigotTest
import dev.s7a.ktspigot.item.customModelDataOrNull
import dev.s7a.ktspigot.item.displayNameOrNull
import dev.s7a.ktspigot.item.editItemMeta
import dev.s7a.ktspigot.item.editLore
import dev.s7a.ktspigot.item.editLoreIfHas
import dev.s7a.ktspigot.item.itemStack
import dev.s7a.ktspigot.item.localizedNameOrNull
import dev.s7a.ktspigot.item.loreOrNull
Expand Down Expand Up @@ -122,6 +124,66 @@ class ItemTest {
assertEquals(expected, meta.loreOrNull)
}

@Test
fun `lore can be edit`() {
val expected = List(5) { randomString() }
val itemStack = ItemStack(Material.STONE)
assertNull(itemStack.loreOrNull)
itemStack.editItemMeta {
lore = expected.take(2)
}
assertNotEquals(expected, itemStack.loreOrNull)
itemStack.editItemMeta {
editLore {
addAll(expected.takeLast(3))
}
}
assertEquals(expected, itemStack.loreOrNull)
}

@Test
fun `lore can be edit using editLoreIfHas`() {
val expected = List(5) { randomString() }
val itemStack = ItemStack(Material.STONE)
assertNull(itemStack.loreOrNull)
itemStack.editItemMeta {
lore = expected.take(2)
}
assertNotEquals(expected, itemStack.loreOrNull)
itemStack.editItemMeta {
editLoreIfHas {
addAll(expected.takeLast(3))
}.let(::assertTrue)
}
assertEquals(expected, itemStack.loreOrNull)
}

@Test
fun `lore cannot be edit using editLoreIfHas`() {
val expected = List(5) { randomString() }
val itemStack = ItemStack(Material.STONE)
assertNull(itemStack.loreOrNull)
itemStack.editItemMeta {
editLoreIfHas {
addAll(expected)
}.let(::assertFalse)
}
assertNull(itemStack.loreOrNull)
}

@Test
fun `lore can be set using editLore`() {
val expected = List(5) { randomString() }
val itemStack = ItemStack(Material.STONE)
assertNull(itemStack.loreOrNull)
itemStack.editItemMeta {
editLore {
addAll(expected)
}
}
assertEquals(expected, itemStack.loreOrNull)
}

@Test
fun `localizedName can be get`() {
val expected = randomString()
Expand Down

0 comments on commit b8eb569

Please sign in to comment.