Skip to content

Commit

Permalink
feat: gapBelow, gapAbove conditions
Browse files Browse the repository at this point in the history
chore: Show yaml decoding error when running test command
  • Loading branch information
0ffz committed Jan 18, 2025
1 parent a9fa49a commit e20374b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mineinabyss.geary.papermc.features.common.conditions.location

import com.mineinabyss.geary.actions.ActionGroupContext
import com.mineinabyss.geary.actions.Condition
import com.mineinabyss.geary.papermc.features.common.conditions.location.GapCondition.Companion.checkGap
import com.mineinabyss.geary.papermc.location
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.idofront.serialization.IntRangeSerializer
import kotlinx.serialization.Serializable

@Serializable(with = GapAboveCondition.Serializer::class)
class GapAboveCondition(
val gap: @Serializable(with = IntRangeSerializer::class) IntRange,
) : Condition {
override fun ActionGroupContext.execute(): Boolean {
val location = location?.clone() ?: return true
return checkGap(location, gap, checkBelow = false) { it.block.isEmpty }
}

object Serializer : InnerSerializer<IntRange, GapAboveCondition>(
"geary:gap_above",
IntRangeSerializer,
{ GapAboveCondition(it) },
{ it.gap },
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mineinabyss.geary.papermc.features.common.conditions.location

import com.mineinabyss.geary.actions.ActionGroupContext
import com.mineinabyss.geary.actions.Condition
import com.mineinabyss.geary.papermc.features.common.conditions.location.GapCondition.Companion.checkGap
import com.mineinabyss.geary.papermc.location
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.idofront.serialization.IntRangeSerializer
import kotlinx.serialization.Serializable

@Serializable(with = GapBelowCondition.Serializer::class)
class GapBelowCondition(
val gap: @Serializable(with = IntRangeSerializer::class) IntRange,
) : Condition {
override fun ActionGroupContext.execute(): Boolean {
val location = location?.clone() ?: return true
return checkGap(location, gap, checkAbove = false) { it.block.isEmpty }
}

object Serializer : InnerSerializer<IntRange, GapBelowCondition>(
"geary:gap_below",
IntRangeSerializer,
{ GapBelowCondition(it) },
{ it.gap },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ class GapCondition(
}

companion object {
fun checkGap(location: Location, gap: IntRange, isPartOfGap: (Location) -> Boolean): Boolean {
fun checkGap(
location: Location,
gap: IntRange,
checkBelow: Boolean = true,
checkAbove: Boolean = true,
isPartOfGap: (Location) -> Boolean,
): Boolean {
val y = location.y.roundToInt().coerceIn(location.world.minHeight..location.world.maxHeight)
val topRange = (y + gap.max()).coerceAtMost(location.world.maxHeight)
val bottomRange = (y - gap.max()).coerceAtLeast(location.world.minHeight)
val topGap = (y..topRange + 1).firstOrNull {
val topGap = if (!checkAbove) y + 1 else (y..topRange + 1).firstOrNull {
if (it > gap.min() && gap.max() == Int.MAX_VALUE) return true
!isPartOfGap(location.apply { this.y = it.toDouble() })
} ?: (topRange + 1)

val bottomGap = (y downTo bottomRange - 1).firstOrNull {
val bottomGap = if (!checkBelow) y - 1 else (y downTo bottomRange - 1).firstOrNull {
if (topGap + it > gap.min() && gap.max() == Int.MAX_VALUE) return true
!isPartOfGap(location.apply { this.y = it.toDouble() })
} ?: (bottomRange - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ object TestCommands {
}

private fun IdoPlayerCommandContext.executeYaml(yaml: String) {
val decoded = gearyPaper.worldManager.global.getAddon(SerializableComponents)
.formats["yml"]
?.decodeFromString(PolymorphicListAsMapSerializer.ofComponents(), yaml)
?: fail("Could not decode yaml")
val decoded = runCatching {
gearyPaper.worldManager.global.getAddon(SerializableComponents)
.formats["yml"]
?.decodeFromString(PolymorphicListAsMapSerializer.ofComponents(), yaml)
?: fail("Could not decode yaml")
}.getOrElse { fail("Could not decode yaml:\n${it.message}") }
decoded.forEach { comp ->
val className = comp::class.simpleName ?: return@forEach
when (comp) {
Expand Down

0 comments on commit e20374b

Please sign in to comment.