Skip to content

Commit

Permalink
testmod: Test all persistence implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkmar committed May 13, 2024
1 parent 802486f commit 1f96e5f
Showing 1 changed file with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package net.silkmc.silk.test.commands

import kotlinx.serialization.Serializable
import net.minecraft.commands.CommandSource
import net.minecraft.commands.CommandSourceStack
import net.silkmc.silk.commands.LiteralCommandBuilder
import net.silkmc.silk.core.entity.blockPos
import net.silkmc.silk.core.text.sendText
import net.silkmc.silk.persistence.PersistentCompound
import net.silkmc.silk.persistence.compoundKey
import net.silkmc.silk.persistence.persistentCompound
import net.silkmc.silk.test.testmodId
Expand All @@ -14,42 +19,56 @@ private val personKey = compoundKey<Person>("person".testmodId)

val persistenceTestCommand = testCommand("persistence") {
literal("player") {
literal("set") runs {
source.playerOrException.persistentCompound.let {
it[simpleIntKey] = 1234

it[personKey] = Person(
"John",
32,
listOf("Peter", "James", "Sofia")
)
}
}
applyTestSubCommands { it.playerOrException.persistentCompound }
}
literal("world") {
applyTestSubCommands { it.level.persistentCompound }
}
literal("chunk") {
applyTestSubCommands { it.level.getChunk(it.playerOrException.blockPos).persistentCompound }
}
}

literal("get") runs {
source.playerOrException.persistentCompound.let {
val simpleInt = it[simpleIntKey]
val complexClass = it[personKey]

source.playerOrException.sendText {
text("simple_int = ")
text("$simpleInt") {
bold = true
}
newLine()
text("complex_class = ")
text("$complexClass") {
bold = true
}
}
}
}
fun LiteralCommandBuilder<CommandSourceStack>.applyTestSubCommands(
compoundGetter: (CommandSourceStack) -> PersistentCompound) {
literal("set") runs {
compoundGetter(source).testSet()
}
literal("get") runs {
compoundGetter(source).testGet(source.playerOrException)
}
literal("remove") runs {
compoundGetter(source).testRemove()
}
}

private fun PersistentCompound.testSet() {
this[simpleIntKey] = 1234
this[personKey] = Person(
"John",
32,
listOf("Peter", "James", "Sofia")
)
}

literal("remove") runs {
source.playerOrException.persistentCompound.let {
it -= simpleIntKey
it -= personKey
}
private fun PersistentCompound.testGet(source: CommandSource) {
val simpleInt = this[simpleIntKey]
val complexClass = this[personKey]

source.sendText {
text("simple_int = ")
text("$simpleInt") {
bold = true
}
newLine()
text("complex_class = ")
text("$complexClass") {
bold = true
}
}
}

private fun PersistentCompound.testRemove() {
this -= simpleIntKey
this -= personKey
}

0 comments on commit 1f96e5f

Please sign in to comment.