more Components instead of Strings (#44)

Closes #36
This commit is contained in:
Jannis Kramer
2022-03-29 16:31:19 +02:00
committed by GitHub
parent 4a561950ee
commit 8b094e3344
9 changed files with 89 additions and 20 deletions

View File

@@ -3,6 +3,7 @@
package net.axay.kspigot.extensions
import net.axay.kspigot.main.PluginInstance
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.text
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
@@ -41,6 +42,13 @@ val pluginManager get() = Bukkit.getPluginManager()
*/
fun broadcast(msg: String) = Bukkit.getServer().broadcast(text(msg))
/**
* Broadcasts a message ([msg]) on the server.
* @return the number of recipients
* @see Bukkit.broadcastMessage
*/
fun broadcast(msg: Component) = Bukkit.getServer().broadcast(msg)
/**
* Shortcut to get the ConsoleSender.
* @see Bukkit.getConsoleSender

View File

@@ -0,0 +1,43 @@
@file:Suppress("unused")
package net.axay.kspigot.extensions.bukkit
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import java.util.Locale
/**
* Returns a [Component] from a [String]
*/
fun String.toComponent(): Component = Component.text(this)
/**
* Returns a [TranslatableComponent] with the given [String] as key and [args]
*/
fun String.asTranslatable(vararg args: Component): TranslatableComponent = Component.translatable(this, *args)
/**
* Returns a [Component] from a [String] with legacy formatting
*/
fun String.legacyToComponent(): Component = LegacyComponentSerializer.legacyAmpersand().deserialize(this)
/**
* Returns a [String] with legacy formatting from a [Component]
*
* Note: Render [TranslatableComponent]s before using this
*/
fun Component.toLegacyString(): String = LegacyComponentSerializer.legacyAmpersand().serialize(this)
/**
* Returns a [String] from a [Component]
*
* Note: Render [TranslatableComponent]s before using this
*/
fun Component.plainText(): String = PlainTextComponentSerializer.plainText().serialize(this)
/**
* Renders a [TranslatableComponent] with the given [locale]
*/
fun TranslatableComponent.render(locale: Locale): Component = GlobalTranslator.render(this, locale)

View File

@@ -7,12 +7,20 @@ import net.axay.kspigot.chat.literalText
import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.PluginInstance
import net.axay.kspigot.pluginmessages.PluginMessageConnect
import net.kyori.adventure.text.Component
import net.kyori.adventure.title.Title
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.attribute.Attribute
import org.bukkit.entity.*
import org.bukkit.entity.ArmorStand
import org.bukkit.entity.Damageable
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import java.time.Duration
/**
* Checks if the entities' head is in water.
@@ -123,14 +131,13 @@ fun Location.spawnCleanEntity(entityType: EntityType): Entity? {
* @param fadeOut time in ticks for titles to fade out
*/
fun Player.title(
mainText: String? = null,
subText: String? = null,
fadeIn: Int = 10,
stay: Int = 70,
fadeOut: Int = 20,
mainText: Component = Component.empty(),
subText: Component = Component.empty(),
fadeIn: Duration = Duration.ofMillis(500),
stay: Duration = Duration.ofMillis(3500),
fadeOut: Duration = Duration.ofMillis(1000),
) {
@Suppress("DEPRECATION")
sendTitle(mainText, subText, fadeIn, stay, fadeOut)
showTitle(Title.title(mainText, subText, Title.Times.times(fadeIn, stay, fadeOut)))
}
/**