Skip to content

Commit

Permalink
Lazily replace variables
Browse files Browse the repository at this point in the history
  • Loading branch information
r4g3baby committed Feb 13, 2022
1 parent aeecf90 commit 8f660c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.r4g3baby.simplescore.scoreboard.placeholders

import com.r4g3baby.simplescore.utils.lazyReplace
import org.bukkit.Bukkit
import org.bukkit.ChatColor.DARK_RED
import org.bukkit.ChatColor.GRAY
Expand All @@ -10,19 +11,30 @@ import kotlin.math.roundToInt

object VariablesReplacer {
fun replace(input: String, player: Player): String {
val hearts = min(10, max(0, ((player.health / player.maxHealth) * 10).roundToInt()))
return input
.replace("%online%", Bukkit.getOnlinePlayers().count().toString())
.replace("%onworld%", player.world.players.count().toString())
.replace("%world%", player.world.name)
.replace("%maxplayers%", Bukkit.getMaxPlayers().toString())
.replace("%player%", player.name)
.replace("%displayname%", player.displayName)
.replace("%health%", player.health.roundToInt().toString())
.replace("%maxhealth%", player.maxHealth.roundToInt().toString())
.replace("%hearts%", "$DARK_RED${"".repeat(hearts)}$GRAY${"".repeat(10 - hearts)}")
.replace("%level%", player.level.toString())
.replace("%gamemode%", player.gameMode.name.lowercase().replaceFirstChar { it.uppercase() })
return input.lazyReplace("%online%") {
Bukkit.getOnlinePlayers().count().toString()
}.lazyReplace("%onworld%") {
player.world.players.count().toString()
}.lazyReplace("%world%") {
player.world.name
}.lazyReplace("%maxplayers%") {
Bukkit.getMaxPlayers().toString()
}.lazyReplace("%player%") {
player.name
}.lazyReplace("%displayname%") {
player.displayName
}.lazyReplace("%health%") {
player.health.roundToInt().toString()
}.lazyReplace("%maxhealth%") {
player.maxHealth.roundToInt().toString()
}.lazyReplace("%hearts%") {
val hearts = min(10, max(0, ((player.health / player.maxHealth) * 10).roundToInt()))
"$DARK_RED${"".repeat(hearts)}$GRAY${"".repeat(10 - hearts)}"
}.lazyReplace("%level%") {
player.level.toString()
}.lazyReplace("%gamemode%") {
player.gameMode.name.lowercase().replaceFirstChar { it.uppercase() }
}
}

fun String.replaceVariables(player: Player): String {
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/com/r4g3baby/simplescore/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,27 @@ fun List<Any>.isEqual(other: List<Any>): Boolean {

return this.toTypedArray() contentEquals other.toTypedArray()
}

fun String.lazyReplace(oldValue: String, newValueFunc: () -> String): String {
run {
var occurrenceIndex: Int = indexOf(oldValue, 0, true)
if (occurrenceIndex < 0) return this

val newValue = newValueFunc()

val oldValueLength = oldValue.length
val searchStep = oldValueLength.coerceAtLeast(1)
val newLengthHint = length - oldValueLength + newValue.length
if (newLengthHint < 0) throw OutOfMemoryError()
val stringBuilder = StringBuilder(newLengthHint)

var i = 0
do {
stringBuilder.append(this, i, occurrenceIndex).append(newValue)
i = occurrenceIndex + oldValueLength
if (occurrenceIndex >= length) break
occurrenceIndex = indexOf(oldValue, occurrenceIndex + searchStep, true)
} while (occurrenceIndex > 0)
return stringBuilder.append(this, i, length).toString()
}
}

0 comments on commit 8f660c4

Please sign in to comment.