-
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.
- Loading branch information
Showing
10 changed files
with
162 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mcstarrysky.land.data | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.bukkit.Chunk | ||
|
||
/** | ||
* Land | ||
* com.mcstarrysky.land.data.LandChunk | ||
* | ||
* @author mical | ||
* @since 2024/8/16 14:02 | ||
*/ | ||
@Serializable | ||
data class LandChunk( | ||
val world: String, | ||
val x: Int, | ||
val z: Int | ||
) { | ||
|
||
constructor(bkChunk: Chunk) : this(bkChunk.world.name, bkChunk.x, bkChunk.z) | ||
|
||
fun isEqualBkChunk(bkChunk: Chunk): Boolean { | ||
return world == bkChunk.world.name && x == bkChunk.x && z == bkChunk.z | ||
} | ||
} | ||
|
||
fun Chunk.toLandChunk(): LandChunk = LandChunk(this) |
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
61 changes: 45 additions & 16 deletions
61
src/main/kotlin/com/mcstarrysky/land/serializers/ChunkSerializer.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 |
---|---|---|
@@ -1,31 +1,60 @@ | ||
package com.mcstarrysky.land.serializers | ||
|
||
import com.mcstarrysky.land.util.deserializeToChunk | ||
import com.mcstarrysky.land.util.serializeToString | ||
import com.mcstarrysky.land.data.LandChunk | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import org.bukkit.Chunk | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
|
||
/** | ||
* Land | ||
* com.mcstarrysky.land.serializers.ChunkSerializer | ||
* 保留旧版本区块序列化兼容性 | ||
* | ||
* @author mical | ||
* @since 2024/8/2 22:45 | ||
*/ | ||
object ChunkSerializer : KSerializer<Chunk> { | ||
object ChunkSerializer : KSerializer<LandChunk> { | ||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("org.bukkit.Chunk", PrimitiveKind.STRING) | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("LandChunk") { | ||
element<String>("world") | ||
element<Int>("x") | ||
element<Int>("z") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Chunk) { | ||
encoder.encodeString(value.serializeToString()) | ||
override fun serialize(encoder: Encoder, value: LandChunk) { | ||
// 全部正常序列化成 Json | ||
encoder.encodeStructure(descriptor) { | ||
encodeStringElement(descriptor, 0, value.world) | ||
encodeIntElement(descriptor, 1, value.x) | ||
encodeIntElement(descriptor, 2, value.z) | ||
} | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Chunk { | ||
return decoder.decodeString().deserializeToChunk() | ||
override fun deserialize(decoder: Decoder): LandChunk { | ||
val input = decoder.decodeString() | ||
|
||
return if (input.contains("~") && input.contains(",")) { | ||
// 旧版本格式解析 | ||
val data = input.split("~") | ||
val world = data[0] | ||
val (x, z) = data[1].split(",").map { it.toInt() } | ||
LandChunk(world, x, z) | ||
} else { | ||
// 新版本格式解析 | ||
decoder.decodeStructure(descriptor) { | ||
var world = "" | ||
var x = 0 | ||
var z = 0 | ||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> world = decodeStringElement(descriptor, 0) | ||
1 -> x = decodeIntElement(descriptor, 1) | ||
2 -> z = decodeIntElement(descriptor, 2) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> throw SerializationException("Unknown index $index") | ||
} | ||
} | ||
LandChunk(world, x, z) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.