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

net.chestmc.common.extensions.strings.Converters.kt Maven / Gradle / Ivy

The newest version!
package net.chestmc.common.extensions.strings

import net.md_5.bungee.api.chat.BaseComponent
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.OfflinePlayer
import org.bukkit.World
import org.bukkit.entity.Player

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

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

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

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

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

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

/**
 * 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 if (contains(oldValue)) replace(oldValue, "$newValue", true) else this
}

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy