All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.extensions.strings.Converters.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
@file:Suppress("NOTHING_TO_INLINE")

package walkmc.extensions.strings

import net.md_5.bungee.api.chat.*
import org.bukkit.*
import org.bukkit.entity.*

/**
 * Parses this string as [Player]
 */
inline fun String.toPlayer(): Player = Bukkit.getPlayer(this)

/**
 * Parses this string as [OfflinePlayer]
 */
inline fun String.toOfflinePlayer(): OfflinePlayer = Bukkit.getOfflinePlayer(this)

/**
 * Parses this string as [World]
 */
inline fun String.toWorld(): World = Bukkit.getWorld(this)

/**
 * Colorizes this string, replacing '§' to '&'.
 */
inline fun String.colorize(): String = replace('§', '&')

/**
 * Reverse colorizes this string, replacing '&' to '§'.
 */
inline fun String.reverseColorize(): String = replace('&', '§')

/**
 * Colorizes this string, replacing '§' to '&'.
 */
inline operator fun String.unaryMinus(): String = replace('§', '&')

/**
 * Reverse colorizes this string, replacing '&' to '§'.
 */
inline operator fun String.unaryPlus(): String = replace('&', '§')

/**
 * Uncolorize this string, this is, making this string without colors.
 */
inline fun String.uncolorize(): String = ChatColor.stripColor(this)

/**
 * Capitalizes this string, making their first char uppercase.
 */
fun String.cap(): String = replaceFirstChar { it.uppercase() }

/**
 * Decapitalizes this string, making their first char lowercase.
 */
fun String.decap(): String = replaceFirstChar { it.lowercase() }

/**
 * Repeat this string by the specified [n] times.
 */
inline operator fun String.times(n: Int): String = repeat(n)

/**
 * Process a string by the specified [oldValue] to [newValue].
 * This is equals to
 * ```
 * replace(oldValue, "$newValue", true)
 * ```
 */
fun String.process(oldValue: String, newValue: Any): String {
   return replace(oldValue, "$newValue", true)
}

/**
 * Process a string by the specified [oldValue] to [newValue] by lazy.
 */
fun String.process(oldValue: String, newValue: () -> Any): String =
  if (contains(oldValue)) replace(oldValue, "${newValue()}", true) else this

/**
 * Converts this string in a simple text component.
 */
inline fun String.toSimpleText(): TextComponent = TextComponent(this)

/**
 * Converts this string in a text component.
 */
inline fun String.toText(): Array = TextComponent.fromLegacyText(this)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy