Skip to content

Commit feb0927

Browse files
feat: add ServerMatcherConfiguration
1 parent 7540459 commit feb0927

File tree

8 files changed

+126
-0
lines changed

8 files changed

+126
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package app.simplecloud.plugin.api.shared.matcher
2+
3+
import app.simplecloud.plugin.api.shared.matcher.operation.*
4+
5+
enum class MatcherType(
6+
private val matcher: OperationMatcher
7+
) {
8+
9+
REGEX(RegexOperationMatcher),
10+
EQUALS(EqualsOperationMatcher),
11+
CONTAINS(ContainsOperationMatcher),
12+
STARTS_WITH(StartsWithOperationMatcher),
13+
ENDS_WITH(EndsWithOperationMatcher);
14+
15+
fun matches(name: String, value: String, negate: Boolean): Boolean {
16+
val matches = this.matcher.matches(name, value)
17+
if (negate) {
18+
return matches.not()
19+
}
20+
return matches
21+
}
22+
23+
fun anyMatches(names: List<String>, value: String, negate: Boolean): Boolean {
24+
return names.any { matches(it, value, negate) }
25+
}
26+
27+
fun allMatches(names: List<String>, value: String, negate: Boolean): Boolean {
28+
return names.all { matches(it, value, negate) }
29+
}
30+
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package app.simplecloud.plugin.api.shared.matcher
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
/**
6+
* @author Niklas Nieberler
7+
*/
8+
9+
@ConfigSerializable
10+
data class ServerMatcherConfiguration(
11+
val operation: MatcherType = MatcherType.STARTS_WITH,
12+
val value: String = "",
13+
val negate: Boolean = false,
14+
) {
15+
16+
fun matches(name: String): Boolean {
17+
return this.operation.matches(name, this.value, this.negate)
18+
}
19+
20+
fun anyMatches(names: List<String>): Boolean {
21+
return this.operation.anyMatches(names, this.value, this.negate)
22+
}
23+
24+
fun allMatches(names: List<String>): Boolean {
25+
return this.operation.allMatches(names, this.value, this.negate)
26+
}
27+
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
@ConfigSerializable
6+
object ContainsOperationMatcher : OperationMatcher {
7+
8+
override fun matches(name: String, value: String): Boolean {
9+
return name.contains(value, ignoreCase = true)
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
@ConfigSerializable
6+
object EndsWithOperationMatcher : OperationMatcher {
7+
8+
override fun matches(name: String, value: String): Boolean {
9+
return name.endsWith(value, ignoreCase = true)
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
@ConfigSerializable
6+
object EqualsOperationMatcher : OperationMatcher {
7+
8+
override fun matches(name: String, value: String): Boolean {
9+
return name.equals(value, ignoreCase = true)
10+
}
11+
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
interface OperationMatcher {
4+
5+
fun matches(name: String, value: String): Boolean
6+
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
@ConfigSerializable
6+
object RegexOperationMatcher : OperationMatcher {
7+
8+
override fun matches(name: String, value: String): Boolean {
9+
return Regex(value).matches(name)
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.simplecloud.plugin.api.shared.matcher.operation
2+
3+
import org.spongepowered.configurate.objectmapping.ConfigSerializable
4+
5+
@ConfigSerializable
6+
object StartsWithOperationMatcher : OperationMatcher {
7+
8+
override fun matches(name: String, value: String): Boolean {
9+
return name.startsWith(value, ignoreCase = true)
10+
}
11+
12+
}

0 commit comments

Comments
 (0)