Skip to content

Commit

Permalink
Add BedrockNetwork protocolVersion option
Browse files Browse the repository at this point in the history
Does not have any affect currently, but safeguards against format
changes being introduced in the future.
  • Loading branch information
BenWoodworth committed Sep 13, 2024
1 parent 660abd0 commit e50b560
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions api/knbt.api
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public final class net/benwoodworth/knbt/BedrockNetworkNbt : net/benwoodworth/kn

public final class net/benwoodworth/knbt/BedrockNetworkNbtBuilder : net/benwoodworth/knbt/BinaryNbtFormatBuilder {
public synthetic fun build$knbt ()Lnet/benwoodworth/knbt/NbtFormat;
public final fun getProtocolVersion ()Ljava/lang/Integer;
public final fun setProtocolVersion (Ljava/lang/Integer;)V
}

public final class net/benwoodworth/knbt/BedrockNetworkNbtConfiguration : net/benwoodworth/knbt/BinaryNbtFormatConfiguration {
public fun getCompression ()Lnet/benwoodworth/knbt/NbtCompression;
public fun getCompressionLevel ()Ljava/lang/Integer;
public fun getEncodeDefaults ()Z
public fun getIgnoreUnknownKeys ()Z
public final fun getProtocolVersion ()I
public fun toString ()Ljava/lang/String;
}

Expand Down
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/BedrockNetworkNbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BedrockNetworkNbt internal constructor(
override val configuration: BedrockNetworkNbtConfiguration,
override val serializersModule: SerializersModule,
) : BinaryNbtFormat() {
override val name: String get() = "BedrockNetwork"
override val name: String get() = "BedrockNetwork(v${configuration.protocolVersion})"
override val capabilities: NbtCapabilities get() = bedrockNetworkNbtCapabilities

override fun getNbtReader(context: NbtContext, source: BufferedSource): BinaryNbtReader =
Expand Down Expand Up @@ -54,7 +54,8 @@ public class BedrockNetworkNbt internal constructor(
* Creates an instance of [BedrockNetworkNbt] configured from the optionally given [BedrockNetworkNbt instance][from]
* and adjusted with [builderAction].
*
* [compression][BedrockNbtBuilder.compression] is required.
* [protocolVersion][JavaNetworkNbtBuilder.protocolVersion] and [compression][BedrockNetworkNbtBuilder.compression] are
* required.
*/
public fun BedrockNetworkNbt(
from: BedrockNetworkNbt? = null,
Expand Down
24 changes: 24 additions & 0 deletions src/commonMain/kotlin/BedrockNetworkNbtConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ public class BedrockNetworkNbtConfiguration internal constructor(
override val ignoreUnknownKeys: Boolean,
override val compression: NbtCompression,
override val compressionLevel: Int?,
public val protocolVersion: Int,
) : BinaryNbtFormatConfiguration() {
override fun toString(): String =
"BedrockNetworkNbtConfiguration(" +
"encodeDefaults=$encodeDefaults" +
", ignoreUnknownKeys=$ignoreUnknownKeys" +
", compression=$compression" +
", compressionLevel=$compressionLevel" +
", protocolVersion=$protocolVersion" +
")"
}

Expand All @@ -20,15 +22,37 @@ public class BedrockNetworkNbtConfiguration internal constructor(
*/
@NbtDslMarker
public class BedrockNetworkNbtBuilder internal constructor(nbt: BedrockNetworkNbt?) : BinaryNbtFormatBuilder(nbt) {
/**
* The protocol version of the Minecraft client and server. Required.
*
* **Note:** There are currently no NBT differences between Bedrock protocol versions, but there may be changes
* introduced later similar to [JavaNetworkNbt]'s [protocolVersion][JavaNetworkNbtBuilder.protocolVersion].
*/
public var protocolVersion: Int? = nbt?.configuration?.protocolVersion
set(value) {
if (value != null) {
require(value >= 0) { "Protocol version must be non-negative, but is $value" }
}
field = value
}

private fun getConfiguredProtocolVersion(): Int =
requireNotNull(protocolVersion) { "Protocol version is required, but has not been configured." }

override fun build(): BedrockNetworkNbt {
return BedrockNetworkNbt(
configuration = BedrockNetworkNbtConfiguration(
encodeDefaults = encodeDefaults,
ignoreUnknownKeys = ignoreUnknownKeys,
compression = getConfiguredCompression(),
compressionLevel = compressionLevel,
protocolVersion = getConfiguredProtocolVersion(),
),
serializersModule = serializersModule,
)
}

private fun bleh(a: String?) {
a!!
}
}
20 changes: 20 additions & 0 deletions src/commonTest/kotlin/BedrockNetworkNbtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.benwoodworth.knbt

import com.benwoodworth.parameterize.parameterOf
import net.benwoodworth.knbt.test.parameterizeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class BedrockNetworkNbtTest {
@Test
fun negative_protocol_version_should_throw() = parameterizeTest {
val protocolVersion by parameterOf(-1, Int.MIN_VALUE)

val failure = assertFailsWith<IllegalArgumentException> {
BedrockNetworkNbt { this.protocolVersion = protocolVersion }
}

assertEquals("Protocol version must be non-negative, but is $protocolVersion", failure.message)
}
}

0 comments on commit e50b560

Please sign in to comment.