Added simple way to send complex colored messages

This commit is contained in:
bluefireoly
2020-07-06 02:06:47 +02:00
parent 9b62f98d3e
commit 778cacc88d

View File

@@ -16,6 +16,43 @@ object KSpigotChat {
this.spigot().sendMessage(*components)
}
/**
* Takes objects of the type [String] or [ChatColor],
* combines them to a message and sends it to the player.
*/
fun Player.sendMessage(vararg messageParts: Any) {
sendMessage(buildComponent {
var currentColor = ChatColor.WHITE
val currentMessage = StringBuilder()
for (messagePart in messageParts.withIndex()) {
val index = messagePart.index
val value = messagePart.value
fun buildText() {
text {
text = currentMessage.toString()
color = currentColor
}
currentMessage.clear()
}
if (value is String) {
currentMessage.append(value)
if (index == messageParts.lastIndex)
buildText()
} else if (value is ChatColor) {
buildText()
currentColor = value
}
}
})
}
}
class KSpigotComponentBuilder {