|
| 1 | +package dev.slne.surf.surfapi.bukkit.test.command.subcommands |
| 2 | + |
| 3 | +import com.github.shynixn.mccoroutine.folia.regionDispatcher |
| 4 | +import dev.jorel.commandapi.CommandAPICommand |
| 5 | +import dev.jorel.commandapi.arguments.LocationType |
| 6 | +import dev.jorel.commandapi.kotlindsl.* |
| 7 | +import dev.slne.surf.api.paper.command.executors.playerExecutorSuspend |
| 8 | +import dev.slne.surf.api.paper.pdc.block.pdc |
| 9 | +import dev.slne.surf.api.paper.util.chunkX |
| 10 | +import dev.slne.surf.api.paper.util.chunkZ |
| 11 | +import dev.slne.surf.api.paper.util.doInChunkAsync |
| 12 | +import dev.slne.surf.surfapi.bukkit.test.plugin |
| 13 | +import kotlinx.coroutines.withContext |
| 14 | +import org.bukkit.Location |
| 15 | +import org.bukkit.NamespacedKey |
| 16 | +import org.bukkit.entity.Player |
| 17 | +import org.bukkit.persistence.PersistentDataType |
| 18 | +import kotlin.math.abs |
| 19 | +import kotlin.math.max |
| 20 | + |
| 21 | +class BlockPdcContainerTest(name: String) : CommandAPICommand(name) { |
| 22 | + init { |
| 23 | + setCommand() |
| 24 | + getCommand() |
| 25 | + listCommand() |
| 26 | + clearCommand() |
| 27 | + copyNearCommand() |
| 28 | + copyFarCommand() |
| 29 | + } |
| 30 | + |
| 31 | + private fun setCommand() = subcommand("set") { |
| 32 | + locationArgument("location", LocationType.BLOCK_POSITION) |
| 33 | + textArgument("key") |
| 34 | + greedyStringArgument("value") |
| 35 | + |
| 36 | + playerExecutorSuspend { sender, args -> |
| 37 | + val location: Location by args |
| 38 | + val key: String by args |
| 39 | + val value: String by args |
| 40 | + |
| 41 | + val world = location.world ?: run { |
| 42 | + sender.sendMessage("Location has no world.") |
| 43 | + return@playerExecutorSuspend |
| 44 | + } |
| 45 | + |
| 46 | + val nsKey = NamespacedKey(plugin, key) |
| 47 | + |
| 48 | + world.doInChunkAsync(location.chunkX, location.chunkZ) { chunk -> |
| 49 | + val block = chunk.getBlock(location.blockX and 15, location.blockY, location.blockZ and 15) |
| 50 | + block.pdc().set(nsKey, PersistentDataType.STRING, value) |
| 51 | + } |
| 52 | + |
| 53 | + sender.sendMessage("Set '$key' = '$value' on block at (${location.blockX}, ${location.blockY}, ${location.blockZ}).") |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private fun getCommand() = subcommand("get") { |
| 58 | + locationArgument("location", LocationType.BLOCK_POSITION) |
| 59 | + textArgument("key") |
| 60 | + |
| 61 | + playerExecutorSuspend { sender, args -> |
| 62 | + val location: Location by args |
| 63 | + val key: String by args |
| 64 | + |
| 65 | + val world = location.world ?: run { |
| 66 | + sender.sendMessage("Location has no world.") |
| 67 | + return@playerExecutorSuspend |
| 68 | + } |
| 69 | + |
| 70 | + val nsKey = NamespacedKey(plugin, key) |
| 71 | + |
| 72 | + val result = world.doInChunkAsync(location.chunkX, location.chunkZ) { chunk -> |
| 73 | + val block = chunk.getBlock(location.blockX and 15, location.blockY, location.blockZ and 15) |
| 74 | + block.pdc().get(nsKey, PersistentDataType.STRING) |
| 75 | + } |
| 76 | + |
| 77 | + if (result != null) { |
| 78 | + sender.sendMessage("Block PDC [$key] = '$result' at (${location.blockX}, ${location.blockY}, ${location.blockZ}).") |
| 79 | + } else { |
| 80 | + sender.sendMessage("No value for key '$key' on block at (${location.blockX}, ${location.blockY}, ${location.blockZ}).") |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun listCommand() = subcommand("list") { |
| 86 | + locationArgument("location", LocationType.BLOCK_POSITION) |
| 87 | + |
| 88 | + playerExecutorSuspend { sender, args -> |
| 89 | + val location: Location by args |
| 90 | + |
| 91 | + val world = location.world ?: run { |
| 92 | + sender.sendMessage("Location has no world.") |
| 93 | + return@playerExecutorSuspend |
| 94 | + } |
| 95 | + |
| 96 | + val keys = world.doInChunkAsync(location.chunkX, location.chunkZ) { chunk -> |
| 97 | + val block = chunk.getBlock(location.blockX and 15, location.blockY, location.blockZ and 15) |
| 98 | + block.pdc().keys.map { it.toString() } |
| 99 | + } |
| 100 | + |
| 101 | + if (keys.isEmpty()) { |
| 102 | + sender.sendMessage("Block PDC at (${location.blockX}, ${location.blockY}, ${location.blockZ}) is empty.") |
| 103 | + } else { |
| 104 | + sender.sendMessage("Block PDC keys at (${location.blockX}, ${location.blockY}, ${location.blockZ}) [${keys.size}]:") |
| 105 | + keys.forEach { sender.sendMessage(" - $it") } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private fun clearCommand() = subcommand("clear") { |
| 111 | + locationArgument("location", LocationType.BLOCK_POSITION) |
| 112 | + |
| 113 | + playerExecutorSuspend { sender, args -> |
| 114 | + val location: Location by args |
| 115 | + |
| 116 | + val world = location.world ?: run { |
| 117 | + sender.sendMessage("Location has no world.") |
| 118 | + return@playerExecutorSuspend |
| 119 | + } |
| 120 | + |
| 121 | + world.doInChunkAsync(location.chunkX, location.chunkZ) { chunk -> |
| 122 | + val block = chunk.getBlock(location.blockX and 15, location.blockY, location.blockZ and 15) |
| 123 | + block.pdc().clear() |
| 124 | + } |
| 125 | + |
| 126 | + sender.sendMessage("Cleared block PDC at (${location.blockX}, ${location.blockY}, ${location.blockZ}).") |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private fun copyNearCommand() = subcommand("copy-near") { |
| 131 | + locationArgument("source", LocationType.BLOCK_POSITION) |
| 132 | + locationArgument("target", LocationType.BLOCK_POSITION) |
| 133 | + |
| 134 | + playerExecutorSuspend { sender, args -> |
| 135 | + val source: Location by args |
| 136 | + val target: Location by args |
| 137 | + |
| 138 | + if (!isSameWorld(sender, source, target)) { |
| 139 | + return@playerExecutorSuspend |
| 140 | + } |
| 141 | + |
| 142 | + val chunkDistance = chunkDistance(source, target) |
| 143 | + if (chunkDistance > 1) { |
| 144 | + sender.sendMessage("copy-near expects source and target in the same region (chunk distance <= 1), got $chunkDistance.") |
| 145 | + return@playerExecutorSuspend |
| 146 | + } |
| 147 | + |
| 148 | + runCopyTest(sender, source, target, "near") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private fun copyFarCommand() = subcommand("copy-far") { |
| 153 | + locationArgument("source", LocationType.BLOCK_POSITION) |
| 154 | + locationArgument("target", LocationType.BLOCK_POSITION) |
| 155 | + |
| 156 | + playerExecutorSuspend { sender, args -> |
| 157 | + val source: Location by args |
| 158 | + val target: Location by args |
| 159 | + |
| 160 | + if (!isSameWorld(sender, source, target)) { |
| 161 | + return@playerExecutorSuspend |
| 162 | + } |
| 163 | + |
| 164 | + val chunkDistance = chunkDistance(source, target) |
| 165 | + if (chunkDistance < FAR_MIN_CHUNK_DISTANCE) { |
| 166 | + sender.sendMessage("copy-far expects blocks to be far apart (chunk distance >= $FAR_MIN_CHUNK_DISTANCE), got $chunkDistance.") |
| 167 | + return@playerExecutorSuspend |
| 168 | + } |
| 169 | + |
| 170 | + runCopyTest(sender, source, target, "far") |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private fun isSameWorld(sender: Player, source: Location, target: Location): Boolean { |
| 175 | + if (source.world == null || target.world == null) { |
| 176 | + sender.sendMessage("Both source and target must include a world.") |
| 177 | + return false |
| 178 | + } |
| 179 | + |
| 180 | + if (source.world != target.world) { |
| 181 | + sender.sendMessage("Source and target must be in the same world.") |
| 182 | + return false |
| 183 | + } |
| 184 | + |
| 185 | + return true |
| 186 | + } |
| 187 | + |
| 188 | + private suspend fun runCopyTest(sender: Player, source: Location, target: Location, label: String) { |
| 189 | + val value = "$label-copy-${System.currentTimeMillis()}" |
| 190 | + |
| 191 | + val result = runCatching { |
| 192 | + val sourcePdc = withContext(plugin.regionDispatcher(source)) { |
| 193 | + val sourceBlock = source.block |
| 194 | + val sourcePdc = sourceBlock.pdc() |
| 195 | + sourcePdc.set(TEST_KEY, PersistentDataType.STRING, value) |
| 196 | + sourcePdc |
| 197 | + } |
| 198 | + |
| 199 | + withContext(plugin.regionDispatcher(target)) { |
| 200 | + val targetBlock = target.block |
| 201 | + sourcePdc.copyTo(targetBlock) |
| 202 | + targetBlock.pdc().get(TEST_KEY, PersistentDataType.STRING) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + result.onSuccess { copiedValue -> |
| 207 | + if (copiedValue == value) { |
| 208 | + sender.sendMessage("Block PDC copy test [$label] passed. Value '$copiedValue' was copied successfully.") |
| 209 | + } else { |
| 210 | + sender.sendMessage("Block PDC copy test [$label] failed. Expected '$value', got '${copiedValue ?: "null"}'.") |
| 211 | + } |
| 212 | + }.onFailure { error -> |
| 213 | + sender.sendMessage("Block PDC copy test [$label] failed with exception: ${error::class.simpleName}: ${error.message}") |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private fun chunkDistance(source: Location, target: Location): Int { |
| 218 | + val dx = abs(source.chunkX - target.chunkX) |
| 219 | + val dz = abs(source.chunkZ - target.chunkZ) |
| 220 | + return max(dx, dz) |
| 221 | + } |
| 222 | + |
| 223 | + companion object { |
| 224 | + private const val FAR_MIN_CHUNK_DISTANCE = 32 |
| 225 | + private val TEST_KEY = NamespacedKey(plugin, "block-pdc-copy-test") |
| 226 | + } |
| 227 | +} |
| 228 | + |
0 commit comments