From 765b523a6c7966ee8688fb841b502133f7dabf06 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 17:53:36 +0200 Subject: [PATCH 01/19] Add code style files --- .gitignore | 2 +- .idea/codeStyles/Project.xml | 26 ++++++++++++++++++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.gitignore b/.gitignore index a09754c..8d4941c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,5 @@ gradle-app.setting .gradletasknamecache # IntelliJ IDEA -/.idea/ +/.idea/* !/.idea/codeStyles \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..03529d1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file From 687a01facc482f16b3e8239e0a4aed7f55a19699 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 19:54:37 +0200 Subject: [PATCH 02/19] Add extension for ConfigurationSection, add book serialization, add comments --- .../configurationExtension.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt new file mode 100644 index 0000000..db5f4b2 --- /dev/null +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt @@ -0,0 +1,20 @@ +package me.patrykanuszczyk.textcomponentserialization + +import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.configuration.ConfigurationSection + +fun ConfigurationSection.getTextComponent(path: String): TextComponent? { + return deserializeTextComponent(this.get(path)) +} + +fun ConfigurationSection.setTextComponent(path: String, value: TextComponent? = null) { + this.set(path, value) +} + +fun ConfigurationSection.getTextComponentList(path: String): List { + return this.getList(path).map { deserializeTextComponent(it) } +} + +fun ConfigurationSection.setTextComponentList(path: String, value: List) { + this.set(path, value.map { it.serialize() }) +} \ No newline at end of file From 296b8b5bb677bd6c7e9796886712b2e330b93c56 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 19:58:57 +0200 Subject: [PATCH 03/19] Add extension for ConfigurationSection, add book serialization, add comments --- .../textcomponentserialization/deserialize.kt | 61 ++++++++++++ .../textcomponentserialization/serialize.kt | 96 +++++++++++++++---- 2 files changed, 139 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt index f9b1ca0..473001a 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt @@ -4,8 +4,15 @@ import net.md_5.bungee.api.ChatColor import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.Material import org.bukkit.configuration.ConfigurationSection +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.BookMeta +/** + * Deserializes a text component. + * @since 1.0 + */ fun deserializeTextComponent(obj: Any?): TextComponent? { val map = when (obj) { null -> return null @@ -46,6 +53,10 @@ fun deserializeTextComponent(obj: Any?): TextComponent? { return component } +/** + * Deserializes a chat color. + * @since 1.0 + */ fun deserializeChatColor(obj: Any?): ChatColor? { if(obj == null) return null require(obj is String) { "Color has to be a string!" } @@ -57,6 +68,10 @@ fun deserializeChatColor(obj: Any?): ChatColor? { return ChatColor.valueOf(canonicalName) } +/** + * Deserializes a click event. + * @since 1.0 + */ fun deserializeClickEvent(obj: Any?): ClickEvent? { if (obj == null) return null require(obj is ConfigurationSection) { "Click event has to be a configuration section or null!" } @@ -72,6 +87,10 @@ fun deserializeClickEvent(obj: Any?): ClickEvent? { return ClickEvent(action, value) } +/** + * Deserializes a hover event. + * @since 1.0 + */ fun deserializeHoverEvent(obj: Any?): HoverEvent? { if(obj == null) return null require(obj is ConfigurationSection) { "Hover event has to be a configuration section or null!" } @@ -86,4 +105,46 @@ fun deserializeHoverEvent(obj: Any?): HoverEvent? { deserializeTextComponent(obj.get("value")) return HoverEvent(action, arrayOf(value)) +} + +/** + * Deserializes a book. + * @since 1.0 + */ +fun deserializeBook(obj: Any?): ItemStack? { + val map = when(obj) { + null -> return null + is ConfigurationSection -> obj.getValues(true) + is Map<*, *> -> obj.toMapOf() + else -> throw IllegalArgumentException("Couldn't deserialize text component from ${obj::class.qualifiedName}!") + } + + val stack = ItemStack(Material.WRITTEN_BOOK) + + val meta = stack.itemMeta as BookMeta + + meta.title = map["title"] as String? + meta.author = map["author"] as String? + meta.generation = deserializeBookGeneration(map["generation"]) + + meta.spigot().pages = (map["pages"] as List<*>).map { + arrayOf(deserializeTextComponent(it)) + } + + return stack +} + +/** + * Deserializes book generation. + * @since 1.0 + */ +fun deserializeBookGeneration(obj: Any?): BookMeta.Generation? { + if(obj == null) return null + require(obj is String) { "Book generation has to be a string!" } + + val canonicalName = obj + .replace(' ', '_') + .toUpperCase() + + return BookMeta.Generation.valueOf(canonicalName) } \ No newline at end of file diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/serialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/serialize.kt index a804f24..a00b257 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/serialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/serialize.kt @@ -4,42 +4,49 @@ import net.md_5.bungee.api.ChatColor import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.TextComponent - +import org.bukkit.Material +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.BookMeta + +/** + * Serializes a text component. + * @since 1.0 + */ @JvmName("serializeTextComponent") fun TextComponent?.serialize(): Any? { - if(this == null) return null + if (this == null) return null val map = mutableMapOf() if (!text.isNullOrEmpty()) - map += "text" to text + map["text"] = text if (!extra.isNullOrEmpty()) - map += "extra" to extra.map { (it as TextComponent).serialize() } + map["extra"] = extra.map { (it as TextComponent).serialize() } if (colorRaw != null) - map += "color" to serializeChatColor(colorRaw)!! + map["color"] = serializeChatColor(colorRaw)!! if (isBoldRaw != null) - map += "bold" to isBoldRaw + map["bold"] = isBoldRaw if (isItalicRaw != null) - map += "italic" to isItalicRaw + map["italic"] = isItalicRaw if (isUnderlinedRaw != null) - map += "underlined" to isUnderlinedRaw + map["underlined"] = isUnderlinedRaw if (isStrikethroughRaw != null) - map += "strikethrough" to isStrikethroughRaw + map["strikethrough"] = isStrikethroughRaw if (isObfuscatedRaw != null) - map += "obfuscated" to isObfuscatedRaw + map["obfuscated"] = isObfuscatedRaw if (insertion != null) - map += "insertion" to insertion + map["insertion"] = insertion if (clickEvent != null) { - map += "clickEvent" to serializeClickEvent( + map["clickEvent"] = serializeClickEvent( clickEvent )!! } if (hoverEvent != null) { - map += "hoverEvent" to serializeHoverEvent( + map["hoverEvent"] = serializeHoverEvent( hoverEvent )!! } @@ -49,23 +56,31 @@ fun TextComponent?.serialize(): Any? { "text" -> return map.getValue("text") "extra" -> return map.getValue("extra") } - } else if(map.keys.isEmpty()) { + } else if (map.keys.isEmpty()) { return "" } return map } +/** + * Serializes a chat color. + * @since 1.0 + */ fun serializeChatColor(color: ChatColor?): Any? { - if(color == null) return null + if (color == null) return null return color.name .replace('_', ' ') .toLowerCase() } +/** + * Serializes a click event. + * @since 1.0 + */ fun serializeClickEvent(event: ClickEvent?): Any? { - if(event == null) return null + if (event == null) return null val action = event.action.name .replace('_', ' ') @@ -79,8 +94,12 @@ fun serializeClickEvent(event: ClickEvent?): Any? { ) } +/** + * Serializes a hover event. + * @since 1.0 + */ fun serializeHoverEvent(event: HoverEvent?): Any? { - if(event == null) return null + if (event == null) return null val action = event.action.name .replace('_', ' ') @@ -88,7 +107,7 @@ fun serializeHoverEvent(event: HoverEvent?): Any? { val values = event.value - val component = (if(values.size == 1) + val component = (if (values.size == 1) values[0] else TextComponent(*values)) as TextComponent @@ -99,4 +118,45 @@ fun serializeHoverEvent(event: HoverEvent?): Any? { "action" to action, "value" to value ) +} + +/** + * Serializes a book item stack. + * @since 1.0 + */ +fun serializeBook(stack: ItemStack?): Any? { + if (stack == null) return null + require(stack.type == Material.WRITTEN_BOOK) { "Book has to be of type WRITTEN_BOOK." } + + val bookMeta = stack.itemMeta as BookMeta + val map = mutableMapOf() + + map["title"] = bookMeta.title + map["author"] = bookMeta.author + map["generation"] = serializeBookGeneration(bookMeta.generation)!! + + map["pages"] = bookMeta.spigot().pages + .map { + if (it.size == 1) + it[0] as TextComponent + else + TextComponent(*it) + } + .map { + it.serialize() + } + + return map +} + +/** + * Serializes book generation. + * @since 1.0 + */ +fun serializeBookGeneration(generation: BookMeta.Generation?): Any? { + if (generation == null) return null + + return generation.name + .replace('_', ' ') + .toLowerCase() } \ No newline at end of file From 51a555fbead7ad7df7c82b99e6d17fd91165c8fd Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 20:09:24 +0200 Subject: [PATCH 04/19] Add meta to item stack when deserializing --- .../patrykanuszczyk/textcomponentserialization/deserialize.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt index 473001a..8aa6a6d 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt @@ -131,6 +131,8 @@ fun deserializeBook(obj: Any?): ItemStack? { arrayOf(deserializeTextComponent(it)) } + stack.itemMeta = meta + return stack } From ba9f5686a5882f963287b60d4199679f9863cd02 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 20:13:57 +0200 Subject: [PATCH 05/19] Add getBook and setBook methods --- .../configurationExtension.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt index db5f4b2..b5f0cf9 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/configurationExtension.kt @@ -2,13 +2,14 @@ package me.patrykanuszczyk.textcomponentserialization import net.md_5.bungee.api.chat.TextComponent import org.bukkit.configuration.ConfigurationSection +import org.bukkit.inventory.ItemStack fun ConfigurationSection.getTextComponent(path: String): TextComponent? { return deserializeTextComponent(this.get(path)) } -fun ConfigurationSection.setTextComponent(path: String, value: TextComponent? = null) { - this.set(path, value) +fun ConfigurationSection.setTextComponent(path: String, value: TextComponent?) { + this.set(path, value.serialize()) } fun ConfigurationSection.getTextComponentList(path: String): List { @@ -17,4 +18,12 @@ fun ConfigurationSection.getTextComponentList(path: String): List) { this.set(path, value.map { it.serialize() }) +} + +fun ConfigurationSection.getBook(path: String): ItemStack? { + return deserializeBook(this.get(path)) +} + +fun ConfigurationSection.setBook(path: String, value: ItemStack?) { + this.set(path, serializeBook(value)) } \ No newline at end of file From be294c7974cb8b7dba92203b60bff29ef0348751 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 21:50:20 +0200 Subject: [PATCH 06/19] Add formatting methods --- .../textcomponentserialization/format.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt new file mode 100644 index 0000000..8838c82 --- /dev/null +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -0,0 +1,49 @@ +package me.patrykanuszczyk.textcomponentserialization + +import net.md_5.bungee.api.chat.ClickEvent +import net.md_5.bungee.api.chat.HoverEvent +import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.ChatColor +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.BookMeta + +@JvmSynthetic +internal fun formatString(string: String, placeholders: Map? = null): String { + var stringFormatted = string + placeholders?.forEach { (key, value) -> + stringFormatted = stringFormatted.replace("{$key}", value, true) + } + return ChatColor.translateAlternateColorCodes('&', stringFormatted) +} + +fun TextComponent.format(placeholders: Map? = null): TextComponent { + text = formatString(text, placeholders) + extra.forEach { (it as TextComponent).format(placeholders) } + insertion = formatString(insertion, placeholders) + clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) + hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { + (it as TextComponent).format(placeholders) + }.toTypedArray()) + return this +} + +fun formatBook(book: ItemStack?, placeholders: Map? = null): ItemStack? { + if(book == null) return null + + val meta = book.itemMeta as BookMeta + + meta.apply { + title = formatString(title, placeholders) + author = formatString(author, placeholders) + + spigot().pages = spigot().pages.map { page -> + page.map { component -> + (component as TextComponent).format(placeholders) + }.toTypedArray() + } + } + + book.itemMeta = meta + + return book +} \ No newline at end of file From a6bfd1df3f68cd6e3676b24fbc475186499db5e5 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 21:58:23 +0200 Subject: [PATCH 07/19] Fix IllegalStateException when formatting TextComponent with null extra --- .../me/patrykanuszczyk/textcomponentserialization/format.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index 8838c82..1757c8e 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -18,7 +18,7 @@ internal fun formatString(string: String, placeholders: Map? = n fun TextComponent.format(placeholders: Map? = null): TextComponent { text = formatString(text, placeholders) - extra.forEach { (it as TextComponent).format(placeholders) } + extra?.forEach { (it as TextComponent).format(placeholders) } insertion = formatString(insertion, placeholders) clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { From 9cecb0ae496ac3f66ee613507b408f53e9c43f84 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 22:05:38 +0200 Subject: [PATCH 08/19] Fix IllegalStateException when formatting components or books with null properties --- .../textcomponentserialization/format.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index 1757c8e..555f91d 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -17,24 +17,26 @@ internal fun formatString(string: String, placeholders: Map? = n } fun TextComponent.format(placeholders: Map? = null): TextComponent { - text = formatString(text, placeholders) + if (text != null) text = formatString(text, placeholders) extra?.forEach { (it as TextComponent).format(placeholders) } - insertion = formatString(insertion, placeholders) - clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) - hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { - (it as TextComponent).format(placeholders) - }.toTypedArray()) + if (insertion != null) insertion = formatString(insertion, placeholders) + if (clickEvent != null) + clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) + if (hoverEvent != null) + hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { + (it as TextComponent).format(placeholders) + }.toTypedArray()) return this } fun formatBook(book: ItemStack?, placeholders: Map? = null): ItemStack? { - if(book == null) return null + if (book == null) return null val meta = book.itemMeta as BookMeta meta.apply { - title = formatString(title, placeholders) - author = formatString(author, placeholders) + if(title!=null)title = formatString(title, placeholders) + if(author!=null)author = formatString(author, placeholders) spigot().pages = spigot().pages.map { page -> page.map { component -> From e3fc6c3546ebbcf4f2883f33069bf7359eab679f Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 22:53:28 +0200 Subject: [PATCH 09/19] Fix component deserialization when extra is null --- .../textcomponentserialization/deserialize.kt | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt index 8aa6a6d..b5507c0 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt @@ -27,12 +27,13 @@ fun deserializeTextComponent(obj: Any?): TextComponent? { component.text = map["text"] as String? ?: "" - component.extra = (map["extra"] as List<*>) - .map { - deserializeTextComponent( - it ?: throw NullPointerException("Extra object is null!") - ) - } + if (map["extra"] != null) + component.extra = (map["extra"] as List<*>) + .map { + deserializeTextComponent( + it ?: throw NullPointerException("Extra object is null!") + ) + } val color = deserializeChatColor(map["color"]) @@ -58,7 +59,7 @@ fun deserializeTextComponent(obj: Any?): TextComponent? { * @since 1.0 */ fun deserializeChatColor(obj: Any?): ChatColor? { - if(obj == null) return null + if (obj == null) return null require(obj is String) { "Color has to be a string!" } val canonicalName = obj @@ -92,7 +93,7 @@ fun deserializeClickEvent(obj: Any?): ClickEvent? { * @since 1.0 */ fun deserializeHoverEvent(obj: Any?): HoverEvent? { - if(obj == null) return null + if (obj == null) return null require(obj is ConfigurationSection) { "Hover event has to be a configuration section or null!" } val actionString = obj.getString("action") @@ -112,7 +113,7 @@ fun deserializeHoverEvent(obj: Any?): HoverEvent? { * @since 1.0 */ fun deserializeBook(obj: Any?): ItemStack? { - val map = when(obj) { + val map = when (obj) { null -> return null is ConfigurationSection -> obj.getValues(true) is Map<*, *> -> obj.toMapOf() @@ -141,7 +142,7 @@ fun deserializeBook(obj: Any?): ItemStack? { * @since 1.0 */ fun deserializeBookGeneration(obj: Any?): BookMeta.Generation? { - if(obj == null) return null + if (obj == null) return null require(obj is String) { "Book generation has to be a string!" } val canonicalName = obj From d4e3570e5d9318787ba9faadef430b05c1811660 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 23:26:34 +0200 Subject: [PATCH 10/19] Return new ItemStack instance when formatting book --- .../patrykanuszczyk/textcomponentserialization/format.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index 555f91d..bb4e5ad 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -4,6 +4,7 @@ import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.TextComponent import org.bukkit.ChatColor +import org.bukkit.Material import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.BookMeta @@ -45,7 +46,9 @@ fun formatBook(book: ItemStack?, placeholders: Map? = null): Ite } } - book.itemMeta = meta + val newBook = ItemStack(Material.WRITTEN_BOOK) - return book + newBook.itemMeta = meta + + return newBook } \ No newline at end of file From dfda77bca9e0be725729ea4037ba0df104a1662d Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 23:37:51 +0200 Subject: [PATCH 11/19] Modify formatting code to return new instances --- .../textcomponentserialization/format.kt | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index bb4e5ad..b752d1c 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -18,35 +18,36 @@ internal fun formatString(string: String, placeholders: Map? = n } fun TextComponent.format(placeholders: Map? = null): TextComponent { - if (text != null) text = formatString(text, placeholders) - extra?.forEach { (it as TextComponent).format(placeholders) } - if (insertion != null) insertion = formatString(insertion, placeholders) + val newComponent = TextComponent() + if (text != null) newComponent.text = formatString(text, placeholders) + extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } + if (insertion != null) newComponent.insertion = formatString(insertion, placeholders) if (clickEvent != null) - clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) + newComponent.clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) if (hoverEvent != null) - hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { + newComponent.hoverEvent = HoverEvent(hoverEvent.action, hoverEvent.value.map { (it as TextComponent).format(placeholders) }.toTypedArray()) - return this + return newComponent } fun formatBook(book: ItemStack?, placeholders: Map? = null): ItemStack? { if (book == null) return null - val meta = book.itemMeta as BookMeta - - meta.apply { - if(title!=null)title = formatString(title, placeholders) - if(author!=null)author = formatString(author, placeholders) - - spigot().pages = spigot().pages.map { page -> - page.map { component -> - (component as TextComponent).format(placeholders) - }.toTypedArray() - } - } + val oldMeta = book.itemMeta as BookMeta val newBook = ItemStack(Material.WRITTEN_BOOK) + + val meta = newBook.itemMeta as BookMeta + meta.title = formatString(oldMeta.title, placeholders) + meta.author = formatString(oldMeta.author, placeholders) + meta.generation = oldMeta.generation + + meta.spigot().addPage(*oldMeta.spigot().pages.map { page -> + page.map { + (it as TextComponent).format(placeholders) + }.toTypedArray() + }.toTypedArray()) newBook.itemMeta = meta From 1f283e4feb12854a011e5e76f645e6aa650f65fa Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Mon, 20 Jul 2020 23:55:41 +0200 Subject: [PATCH 12/19] Improve hover and click event deserializing --- .../textcomponentserialization/deserialize.kt | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt index b5507c0..6dca936 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt @@ -74,16 +74,20 @@ fun deserializeChatColor(obj: Any?): ChatColor? { * @since 1.0 */ fun deserializeClickEvent(obj: Any?): ClickEvent? { - if (obj == null) return null - require(obj is ConfigurationSection) { "Click event has to be a configuration section or null!" } + val map = when(obj) { + null -> return null + is ConfigurationSection -> obj.getValues(false) + is Map<*,*> -> obj + else -> throw IllegalArgumentException("Couldn't deserialize click event from ${obj::class.qualifiedName}!") + }.toMapOf() - val actionString = obj.getString("action") + val actionString = map["action"] ?: throw NullPointerException("No action specified for click event!") val actionCanonicalName = actionString .replace(' ', '_') .toUpperCase() val action = ClickEvent.Action.valueOf(actionCanonicalName) - val value = obj.getString("value") + val value = map["value"] return ClickEvent(action, value) } @@ -93,17 +97,21 @@ fun deserializeClickEvent(obj: Any?): ClickEvent? { * @since 1.0 */ fun deserializeHoverEvent(obj: Any?): HoverEvent? { - if (obj == null) return null - require(obj is ConfigurationSection) { "Hover event has to be a configuration section or null!" } + val map = when(obj) { + null -> return null + is ConfigurationSection -> obj.getValues(true) + is Map<*,*> -> obj + else -> throw IllegalArgumentException("Couldn't deserialize click event from ${obj::class.qualifiedName}!") + }.toMapOf() - val actionString = obj.getString("action") + val actionString = map["action"] as String val actionCanonicalName = actionString .replace(' ', '_') .toUpperCase() val action = HoverEvent.Action.valueOf(actionCanonicalName) val value = - deserializeTextComponent(obj.get("value")) + deserializeTextComponent(map["value"]) return HoverEvent(action, arrayOf(value)) } From 3dbe59a25f92e22591c9212879909b9a045c611f Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Tue, 21 Jul 2020 00:39:54 +0200 Subject: [PATCH 13/19] Fix text component formatting not copying color and format --- .../textcomponentserialization/deserialize.kt | 2 +- .../patrykanuszczyk/textcomponentserialization/format.kt | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt index 6dca936..a8d7c45 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/deserialize.kt @@ -36,8 +36,8 @@ fun deserializeTextComponent(obj: Any?): TextComponent? { } val color = deserializeChatColor(map["color"]) - component.color = color + component.setBold(map["bold"] as Boolean?) component.setItalic(map["italic"] as Boolean?) component.setUnderlined(map["underlined"] as Boolean?) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index b752d1c..5999914 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -4,7 +4,6 @@ import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.HoverEvent import net.md_5.bungee.api.chat.TextComponent import org.bukkit.ChatColor -import org.bukkit.Material import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.BookMeta @@ -18,9 +17,10 @@ internal fun formatString(string: String, placeholders: Map? = n } fun TextComponent.format(placeholders: Map? = null): TextComponent { - val newComponent = TextComponent() + val newComponent = TextComponent(this) if (text != null) newComponent.text = formatString(text, placeholders) - extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } + //extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } + extra = extra.map { (it as TextComponent).format(placeholders) } if (insertion != null) newComponent.insertion = formatString(insertion, placeholders) if (clickEvent != null) newComponent.clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) @@ -36,7 +36,7 @@ fun formatBook(book: ItemStack?, placeholders: Map? = null): Ite val oldMeta = book.itemMeta as BookMeta - val newBook = ItemStack(Material.WRITTEN_BOOK) + val newBook = ItemStack(book) val meta = newBook.itemMeta as BookMeta meta.title = formatString(oldMeta.title, placeholders) From 2c01d7df94f701cda65cd8cb02394804ed560715 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Tue, 21 Jul 2020 00:45:26 +0200 Subject: [PATCH 14/19] Fix issue when extra is null when formatting TextComponent. Again. --- .../me/patrykanuszczyk/textcomponentserialization/format.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index 5999914..2b94cf4 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -20,7 +20,7 @@ fun TextComponent.format(placeholders: Map? = null): TextCompone val newComponent = TextComponent(this) if (text != null) newComponent.text = formatString(text, placeholders) //extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } - extra = extra.map { (it as TextComponent).format(placeholders) } + extra = extra?.map { (it as TextComponent).format(placeholders) } if (insertion != null) newComponent.insertion = formatString(insertion, placeholders) if (clickEvent != null) newComponent.clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) From 304c7d507f990488de4c7dc80f75efcd2478c288 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Tue, 21 Jul 2020 00:50:14 +0200 Subject: [PATCH 15/19] Fix issue when extra is null when formatting TextComponent. Maybe this time? --- .../me/patrykanuszczyk/textcomponentserialization/format.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index 2b94cf4..f11f6d6 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -20,7 +20,8 @@ fun TextComponent.format(placeholders: Map? = null): TextCompone val newComponent = TextComponent(this) if (text != null) newComponent.text = formatString(text, placeholders) //extra?.forEach { newComponent.addExtra((it as TextComponent).format(placeholders)) } - extra = extra?.map { (it as TextComponent).format(placeholders) } + if(extra != null) + newComponent.extra = extra.map { (it as TextComponent).format(placeholders) } if (insertion != null) newComponent.insertion = formatString(insertion, placeholders) if (clickEvent != null) newComponent.clickEvent = ClickEvent(clickEvent.action, formatString(clickEvent.value, placeholders)) From a9a3c7b80ff85ce3565fec8a4e50e19c3c55d438 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Sun, 26 Jul 2020 00:42:28 +0200 Subject: [PATCH 16/19] Make formatString function public --- .../me/patrykanuszczyk/textcomponentserialization/format.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt index f11f6d6..0a50b4b 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/format.kt @@ -7,8 +7,7 @@ import org.bukkit.ChatColor import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.BookMeta -@JvmSynthetic -internal fun formatString(string: String, placeholders: Map? = null): String { +fun formatString(string: String, placeholders: Map? = null): String { var stringFormatted = string placeholders?.forEach { (key, value) -> stringFormatted = stringFormatted.replace("{$key}", value, true) From 7dc3777eef93e490b95522da019bbd951c5fce85 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Sun, 26 Jul 2020 03:12:03 +0200 Subject: [PATCH 17/19] Add isEmpty extension to TextComponent --- .../textcomponentserialization/isEmpty.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt new file mode 100644 index 0000000..d6cbb68 --- /dev/null +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt @@ -0,0 +1,12 @@ +package me.patrykanuszczyk.textcomponentserialization + +import net.md_5.bungee.api.chat.TextComponent + +val TextComponent.isEmpty: Boolean + get() { + if(!text.isNullOrEmpty()) return true + + return extra.all { it is TextComponent && it.isEmpty } + } + +val TextComponent.isNotEmpty: Boolean get() = !isEmpty \ No newline at end of file From 3f59367e484f9123f6af22bc50605020650cc3b4 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Sun, 26 Jul 2020 03:12:51 +0200 Subject: [PATCH 18/19] Fix isEmpty --- .../me/patrykanuszczyk/textcomponentserialization/isEmpty.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt index d6cbb68..d664614 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/isEmpty.kt @@ -4,7 +4,7 @@ import net.md_5.bungee.api.chat.TextComponent val TextComponent.isEmpty: Boolean get() { - if(!text.isNullOrEmpty()) return true + if(!text.isNullOrEmpty()) return false return extra.all { it is TextComponent && it.isEmpty } } From 49e4bdfd74698b6dd0eef984b1642316ccec0819 Mon Sep 17 00:00:00 2001 From: Patryk Anuszczyk Date: Sun, 26 Jul 2020 14:40:16 +0200 Subject: [PATCH 19/19] Make map converting function public --- .../patrykanuszczyk/textcomponentserialization/mapConvert.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/mapConvert.kt b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/mapConvert.kt index 20a716b..c9f8019 100644 --- a/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/mapConvert.kt +++ b/src/main/kotlin/me/patrykanuszczyk/textcomponentserialization/mapConvert.kt @@ -1,7 +1,6 @@ package me.patrykanuszczyk.textcomponentserialization -@JvmSynthetic -internal inline fun Map<*, *>.toMapOf(): MutableMap { +inline fun Map<*, *>.toMapOf(): MutableMap { val map = mutableMapOf() for ((key, value) in this) {