Removed unnecessary method and restructured code
This commit is contained in:
@@ -7,7 +7,6 @@ import net.md_5.bungee.api.chat.*
|
||||
import net.md_5.bungee.api.chat.hover.content.Entity
|
||||
import net.md_5.bungee.api.chat.hover.content.Item
|
||||
import net.md_5.bungee.api.chat.hover.content.Text
|
||||
import org.bukkit.command.CommandSender
|
||||
|
||||
object KSpigotChat {
|
||||
|
||||
@@ -15,47 +14,6 @@ object KSpigotChat {
|
||||
return KSpigotComponentBuilder().apply(builder).create()
|
||||
}
|
||||
|
||||
fun CommandSender.sendMessage(vararg components: BaseComponent) {
|
||||
this.spigot().sendMessage(*components)
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes objects of the type [String] or [ChatColor],
|
||||
* combines them to a message and sends it to the player.
|
||||
*/
|
||||
fun CommandSender.sendMessage(vararg messageParts: Any) {
|
||||
sendMessage(buildComponent {
|
||||
|
||||
var currentColor = ChatColor.WHITE
|
||||
val currentMessage = StringBuilder()
|
||||
|
||||
fun buildText() {
|
||||
text {
|
||||
text = currentMessage.toString()
|
||||
color = currentColor
|
||||
}
|
||||
currentMessage.clear()
|
||||
}
|
||||
|
||||
for (messagePart in messageParts.withIndex()) {
|
||||
|
||||
val index = messagePart.index
|
||||
val value = messagePart.value
|
||||
|
||||
if (value is String) {
|
||||
currentMessage.append(value)
|
||||
if (index == messageParts.lastIndex)
|
||||
buildText()
|
||||
} else if (value is ChatColor) {
|
||||
buildText()
|
||||
currentColor = value
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class KSpigotComponentBuilder {
|
||||
|
14
src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt
Normal file
14
src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||
|
||||
package net.axay.kspigot.chat
|
||||
|
||||
import net.md_5.bungee.api.chat.BaseComponent
|
||||
import org.bukkit.command.CommandSender
|
||||
|
||||
fun CommandSender.sendMessage(vararg components: BaseComponent) {
|
||||
this.spigot().sendMessage(*components)
|
||||
}
|
||||
|
||||
fun CommandSender.sendMessage(builder: KSpigotComponentBuilder.() -> Unit) {
|
||||
this.spigot().sendMessage(*KSpigotComponentBuilder().apply(builder).create())
|
||||
}
|
Reference in New Issue
Block a user