Skip to content

Commit

Permalink
more Components instead of Strings (#44)
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
l4zs authored Mar 29, 2022
1 parent 4a56195 commit 8b094e3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/main/kotlin/net/axay/kspigot/chat/LiteralTextBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class LiteralTextBuilder(val internalText: Component) {
* This can be set in the following way:
*
* e.g. Medium turquoise:
* - `color = col(0x4BD6CB)`
* - `color = col(4970187)`
* - `color = col("#4BD6CB")`
* - `color = TextColor.color(72, 209, 204)`
* - `color = KColors.MEDIUMTURQUOISE`
*/
var color: TextColor? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal class PlayerInputChat(
override val inputListeners = listOf(
listen<AsyncChatEvent>(EventPriority.LOWEST) {
if (it.player == player) {
onReceive(it.message())
it.isCancelled = true
onReceive(it.message())
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)))
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/net/axay/kspigot/gui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package net.axay.kspigot.gui
import net.axay.kspigot.event.listen
import net.axay.kspigot.extensions.bukkit.closeForViewers
import net.axay.kspigot.main.PluginInstance
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.player.PlayerQuitEvent
Expand All @@ -13,7 +14,7 @@ import org.bukkit.inventory.ItemStack

class GUIData<T : ForInventory>(
val guiType: GUIType<T>,
val title: String?,
val title: Component,
internal val pages: Map<Int, GUIPage<T>>,
val defaultPage: Int,
val transitionTo: InventoryChangeEffect?,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package net.axay.kspigot.gui

import net.axay.kspigot.gui.elements.*
import org.bukkit.event.inventory.InventoryCloseEvent
import net.kyori.adventure.text.Component
import org.bukkit.inventory.ItemStack
import kotlin.math.absoluteValue

Expand All @@ -24,7 +24,7 @@ class GUIBuilder<T : ForInventory>(
* This title will be visible for every page of
* this GUI.
*/
var title: String = ""
var title: Component = Component.empty()

/**
* The transition applied, if another GUI redirects to
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/net/axay/kspigot/gui/GUIType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

package net.axay.kspigot.gui

import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.empty
import org.bukkit.Bukkit
import org.bukkit.event.inventory.InventoryType
import org.bukkit.inventory.Inventory
Expand All @@ -25,11 +26,10 @@ class GUIType<in T : ForInventory>(
GUIType<ForInventoryThreeByThree>(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER)
}

fun createBukkitInv(holder: InventoryHolder? = null, title: String? = null): Inventory {
val realTitle = title ?: ""
fun createBukkitInv(holder: InventoryHolder? = null, title: Component = empty()): Inventory {
return when {
bukkitType != null -> Bukkit.createInventory(holder, bukkitType, text(realTitle))
else -> Bukkit.createInventory(holder, dimensions.slotAmount, text(realTitle))
bukkitType != null -> Bukkit.createInventory(holder, bukkitType, title)
else -> Bukkit.createInventory(holder, dimensions.slotAmount, title)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/net/axay/kspigot/items/KSpigotItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,19 @@ inline fun ItemMeta.addLore(builder: ItemMetaLoreBuilder.() -> Unit) {
*/
class ItemMetaLoreBuilder {
val lorelist = ArrayList<Component>()

/**
* Adds a new line to the lore.
*
* Note: Render [TranslatableComponent]s before adding them to the lore.
*/
operator fun Component.unaryPlus() {
lorelist += this
}

/**
* Adds a new line to the lore.
*/
operator fun String.unaryPlus() {
lorelist += text(this)
}
Expand Down Expand Up @@ -112,6 +122,8 @@ fun ItemMeta.removeFlags(vararg itemFlag: ItemFlag) = removeItemFlags(*itemFlag)

/**
* Provides safe access to the items' displayName.
*
* Note: Render [TranslatableComponent]s before setting them as the displayName.
*/
var ItemMeta.name: Component?
get() = if (hasDisplayName()) displayName() else null
Expand Down

0 comments on commit 8b094e3

Please sign in to comment.