From b8eb569cc184adee4d5e69fe1aec7449d57cb243 Mon Sep 17 00:00:00 2001 From: sya-ri Date: Sat, 9 Apr 2022 19:26:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20editLore=20=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/s7a/ktspigot/item/extension.kt | 24 +++++++ src/test/kotlin/util/ItemTest.kt | 62 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/api/v1_8/src/main/kotlin/dev/s7a/ktspigot/item/extension.kt b/api/v1_8/src/main/kotlin/dev/s7a/ktspigot/item/extension.kt index 3f54735f..cc787b2f 100644 --- a/api/v1_8/src/main/kotlin/dev/s7a/ktspigot/item/extension.kt +++ b/api/v1_8/src/main/kotlin/dev/s7a/ktspigot/item/extension.kt @@ -76,6 +76,30 @@ inline fun ItemStack.editItemMeta(action: T.() -> Unit): } } +/** + * アイテムの説明文を変更する + * + * @param altColorChar [color] に使う文字 / '&' + * @param action 説明文の変更処理 + * @since 1.0.0 + */ +inline fun ItemMeta.editLore(altColorChar: Char? = '&', action: MutableList.() -> 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.() -> Unit): Boolean { + lore = loreOrNull?.toMutableList()?.apply(action)?.color(altColorChar) ?: return false + return true +} + /** * [ItemStack] を生成する * diff --git a/src/test/kotlin/util/ItemTest.kt b/src/test/kotlin/util/ItemTest.kt index 49854202..8837c96a 100644 --- a/src/test/kotlin/util/ItemTest.kt +++ b/src/test/kotlin/util/ItemTest.kt @@ -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 @@ -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()