-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow block tags in block conditions, add isSolid option
feat: Start working on a nicer conditions system feat: `geary test condition` command
- Loading branch information
Showing
11 changed files
with
162 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...tlin/com/mineinabyss/geary/papermc/features/common/conditions/location/BlockConditions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mineinabyss.geary.papermc.features.common.conditions.location | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.bukkit.Location | ||
|
||
@Serializable | ||
data class BlockConditions( | ||
val allow: MaterialOrTagMatcher? = null, | ||
val deny: MaterialOrTagMatcher? = null, | ||
val isSolid: Boolean? = null, | ||
) { | ||
fun check(location: Location?): CheckResult { | ||
val material = location?.block?.type ?: return CheckResult.Success | ||
return checks { | ||
checkOptional("Allowed materials", allow) { it.matches(material) } | ||
checkOptional("Denied materials", deny) { !it.matches(material) } | ||
checkOptional("Solid", isSolid) { it == material.isSolid } | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...n/kotlin/com/mineinabyss/geary/papermc/features/common/conditions/location/CheckResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mineinabyss.geary.papermc.features.common.conditions.location | ||
|
||
sealed interface CheckResult { | ||
data object Success : CheckResult | ||
data class Failure(val message: String) : CheckResult | ||
|
||
fun successOrThrow() = when (this) { | ||
is Success-> true | ||
is Failure -> error(message) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...c/main/kotlin/com/mineinabyss/geary/papermc/features/common/conditions/location/Checks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.mineinabyss.geary.papermc.features.common.conditions.location | ||
|
||
class Checks() { | ||
var failed = StringBuilder() | ||
|
||
inline fun check(name: String, run: () -> Boolean) { | ||
val result = runCatching { run() } | ||
.onFailure { exception -> | ||
with(failed) { | ||
appendLine("$name: ${exception.message}") | ||
} | ||
|
||
} | ||
.getOrNull() ?: return | ||
if (!result) failed.appendLine("$name: Condition failed") | ||
} | ||
|
||
inline fun <T> checkOptional(name: String, argument: T?, run: (T) -> Boolean) { | ||
if (argument == null) return | ||
check(name) { run(argument) } | ||
} | ||
|
||
val result get() = if(failed.isEmpty()) CheckResult.Success else CheckResult.Failure(failed.toString()) | ||
} | ||
|
||
inline fun checks(check: Checks.() -> Unit): CheckResult { | ||
return Checks().apply { check() }.result | ||
} |
48 changes: 48 additions & 0 deletions
48
...com/mineinabyss/geary/papermc/features/common/conditions/location/MaterialOrTagMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.mineinabyss.geary.papermc.features.common.conditions.location | ||
|
||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.builtins.serializer | ||
import org.bukkit.Bukkit | ||
import org.bukkit.Material | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.Tag | ||
|
||
//TODO probably useful in idofront? | ||
/** | ||
* Serializable class for matching materials and tags, uses a string list where tags start with # | ||
*/ | ||
@Serializable(with = MaterialOrTagMatcher.Serializer::class) | ||
data class MaterialOrTagMatcher( | ||
val materials: List<Material>, | ||
val tags: List<Tag<Material>>, | ||
) { | ||
fun matches(material: Material) = | ||
(materials.isEmpty() || material in materials) && (tags.isEmpty() || tags.any { it.isTagged(material) }) | ||
|
||
object Serializer : InnerSerializer<List<String>, MaterialOrTagMatcher>( | ||
"MaterialOrTagMatcher", | ||
ListSerializer(String.Companion.serializer()), | ||
{ Serializer.decodeList(it) }, | ||
{ Serializer.encodeList(it) } | ||
) { | ||
fun encodeList(matcher: MaterialOrTagMatcher): List<String> { | ||
return matcher.materials.map { it.key.toString() } + matcher.tags.map { "#" + it.key.asMinimalString() } | ||
} | ||
|
||
fun decodeList(list: List<String>): MaterialOrTagMatcher { | ||
val (tags, materials) = list.partition { it.startsWith("#") } | ||
return MaterialOrTagMatcher( | ||
materials.mapNotNull { Material.getMaterial(it) }, | ||
tags.mapNotNull { | ||
Bukkit.getTag<Material>( | ||
Tag.REGISTRY_BLOCKS, | ||
NamespacedKey.fromString(it.removePrefix("#")) ?: return@mapNotNull null, | ||
Material::class.java | ||
) | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/commands/TestCommands.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.mineinabyss.geary.papermc.plugin.commands | ||
|
||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.actions.Condition | ||
import com.mineinabyss.geary.papermc.gearyPaper | ||
import com.mineinabyss.geary.papermc.tracking.entities.toGeary | ||
import com.mineinabyss.geary.serialization.SerializableComponents | ||
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer | ||
import com.mineinabyss.idofront.commands.brigadier.Args | ||
import com.mineinabyss.idofront.commands.brigadier.IdoCommand | ||
import com.mineinabyss.idofront.commands.brigadier.context.IdoPlayerCommandContext | ||
import com.mineinabyss.idofront.commands.brigadier.playerExecutes | ||
import com.mineinabyss.idofront.messaging.error | ||
import com.mineinabyss.idofront.messaging.success | ||
|
||
fun IdoCommand.testCommands() = "test" { | ||
requiresPermission("geary.admin.test") | ||
"condition" { | ||
playerExecutes(Args.greedyString()) { yaml -> | ||
test(yaml) | ||
} | ||
} | ||
} | ||
|
||
fun IdoPlayerCommandContext.test(yaml: String) { | ||
val decoded = gearyPaper.worldManager.global.getAddon(SerializableComponents) | ||
.formats["yml"] | ||
?.decodeFromString(PolymorphicListAsMapSerializer.ofComponents(), yaml) | ||
?: fail("Could not decode yaml") | ||
decoded.forEach { condition -> | ||
if (condition !is Condition) return@forEach | ||
val conditionName = condition::class.simpleName ?: return@forEach | ||
with(condition) { | ||
runCatching { ActionGroupContext(player.toGeary()).execute() } | ||
.onSuccess { sender.success("Condition $conditionName passed") } | ||
.onFailure { exception -> sender.error("Condition $conditionName failed:\n${exception.message}") } | ||
} | ||
} | ||
} |