11package com.r4g3baby.simplescore.bukkit.util
22
33import com.r4g3baby.simplescore.api.scoreboard.data.Provider
4+ import com.r4g3baby.simplescore.bukkit.protocol.util.Utils
5+ import com.r4g3baby.simplescore.core.util.Reflection
6+ import org.bukkit.entity.Player
47import org.bukkit.plugin.Plugin
8+ import java.util.function.Function
59
610fun String.lazyReplace (oldValue : String , newValueFunc : () -> String ): String {
7- run {
8- var occurrenceIndex: Int = indexOf(oldValue, 0 , true )
9- if (occurrenceIndex < 0 ) return this
10-
11- val newValue = newValueFunc()
12-
13- val oldValueLength = oldValue.length
14- val searchStep = oldValueLength.coerceAtLeast(1 )
15- val newLengthHint = length - oldValueLength + newValue.length
16- if (newLengthHint < 0 ) throw OutOfMemoryError ()
17- val stringBuilder = StringBuilder (newLengthHint)
18-
19- var i = 0
20- do {
21- stringBuilder.append(this , i, occurrenceIndex).append(newValue)
22- i = occurrenceIndex + oldValueLength
23- if (occurrenceIndex >= length) break
24- occurrenceIndex = indexOf(oldValue, occurrenceIndex + searchStep, true )
25- } while (occurrenceIndex > 0 )
26- return stringBuilder.append(this , i, length).toString()
11+ var occurrenceIndex = this .indexOf(oldValue, ignoreCase = true )
12+ if (occurrenceIndex < 0 ) return this
13+
14+ val newValue = newValueFunc()
15+ val oldValueLength = oldValue.length
16+
17+ val newLengthHint = this .length - oldValueLength + newValue.length
18+ if (newLengthHint < 0 ) throw OutOfMemoryError ()
19+
20+ var cursor = 0
21+ val stringBuilder = StringBuilder (newLengthHint)
22+ while (occurrenceIndex >= 0 ) {
23+ stringBuilder.append(this , cursor, occurrenceIndex).append(newValue)
24+
25+ cursor = occurrenceIndex + oldValueLength
26+ occurrenceIndex = this .indexOf(oldValue, cursor, ignoreCase = true )
2727 }
28+
29+ if (cursor < this .length) {
30+ stringBuilder.append(this , cursor, this .length)
31+ }
32+
33+ return stringBuilder.toString()
2834}
2935
3036fun bukkitProvider (plugin : Plugin ): Provider {
3137 return Provider (plugin.name)
32- }
38+ }
39+
40+ @JvmField
41+ val getPlayerPing = object : Function <Player , Int > {
42+ private val getPingMethod: Reflection .MethodInvoker ? = try {
43+ Reflection .getMethodByName(Player ::class .java, " getPing" )
44+ } catch (_: Exception ) { null }
45+
46+ private val getPlayerHandle: Reflection .MethodInvoker ?
47+ private val pingField: Reflection .FieldAccessor ?
48+
49+ init {
50+ if (getPingMethod == null ) {
51+ getPlayerHandle = try {
52+ val craftPlayer = Reflection .getClass(" ${Utils .OBC } .entity.CraftPlayer" )
53+ Reflection .getMethodByName(craftPlayer, " getHandle" )
54+ } catch (_: Exception ) { null }
55+
56+ pingField = try {
57+ val entityPlayer = Reflection .findClass(
58+ " net.minecraft.server.level.ServerPlayer" ,
59+ " net.minecraft.server.level.EntityPlayer" , " ${Utils .NMS } .EntityPlayer"
60+ )
61+ Reflection .getField(entityPlayer, Int ::class .java, filter = { field -> field.name == " ping" })
62+ } catch (_: Exception ) { null }
63+ } else {
64+ getPlayerHandle = null
65+ pingField = null
66+ }
67+ }
68+
69+ override fun apply (player : Player ): Int {
70+ return when {
71+ getPingMethod != null -> getPingMethod.invoke(player) as ? Int ? : - 1
72+ getPlayerHandle != null && pingField != null -> pingField.get(getPlayerHandle.invoke(player)) as ? Int ? : - 1
73+ else -> - 1
74+ }
75+ }
76+ }
0 commit comments