Skip to content

Commit 495c4a2

Browse files
committed
Add wildcard username matching
1 parent 6299ba0 commit 495c4a2

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "me.zavdav"
8-
version = "0.23.4"
8+
version = "0.23.5"
99

1010
repositories {
1111
mavenCentral()

src/main/kotlin/me/zavdav/zcore/ZCore.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import me.zavdav.zcore.player.OfflinePlayer
2525
import me.zavdav.zcore.statistic.Statistic
2626
import me.zavdav.zcore.util.Materials
2727
import me.zavdav.zcore.util.getField
28+
import me.zavdav.zcore.util.wildcardMatchesIgnoreCase
2829
import me.zavdav.zcore.version.ZCoreVersion
2930
import org.bukkit.Bukkit
3031
import org.bukkit.Location
@@ -161,9 +162,7 @@ class ZCore : JavaPlugin() {
161162
matches.clear()
162163
matches.add(player)
163164
break
164-
}
165-
166-
if (player.name.startsWith(partialName, true)) {
165+
} else if (player.name.wildcardMatchesIgnoreCase(partialName)) {
167166
matches.add(player)
168167
}
169168
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.zavdav.zcore.util
2+
3+
import java.util.regex.Pattern
4+
5+
internal fun String.wildcardMatchesIgnoreCase(regex: String): Boolean {
6+
if (regex.isEmpty()) {
7+
return this.isEmpty()
8+
}
9+
10+
val sanitizedRegex = "^" + regex
11+
.replace("[\\\\\\[(){^$|?+.]".toRegex(), "\\\\$0")
12+
.replace(Pattern.quote("*").toRegex(), ".+") + "$"
13+
14+
15+
return Pattern.compile(sanitizedRegex, Pattern.CASE_INSENSITIVE)
16+
.matcher(this)
17+
.find()
18+
}

0 commit comments

Comments
 (0)