From 779fa3bf65f6422b37a046d613712a0a0ffee136 Mon Sep 17 00:00:00 2001 From: leocth Date: Tue, 14 Jun 2022 18:01:38 +0800 Subject: [PATCH 1/6] compound tag DSL --- .../wrapper/minecraft/nbt/NbtCompoundDsl.kt | 443 ++++++++++++++++++ .../qkl/wrapper/minecraft/nbt/NbtListDsl.kt | 27 ++ 2 files changed, 470 insertions(+) create mode 100644 wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt create mode 100644 wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt diff --git a/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt b/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt new file mode 100644 index 0000000..0f5e2b7 --- /dev/null +++ b/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt @@ -0,0 +1,443 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qkl.wrapper.minecraft.nbt + +import net.minecraft.block.BlockState +import net.minecraft.nbt.NbtCompound +import net.minecraft.nbt.NbtElement +import net.minecraft.nbt.NbtHelper +import net.minecraft.util.Identifier +import net.minecraft.util.math.BlockPos +import java.util.UUID + +/** + * This class contains the functions for building a [NbtCompound] object. + * To use the DSL use [nbtCompound]. + * + * @author leocth + */ +@Suppress("MemberVisibilityCanBePrivate") +public class NbtCompoundDsl { + public val compound: NbtCompound = NbtCompound() + + //region Primitive put methods + + /** + * Inserts a new [NbtElement] with the given key. + * + * @author leocth + */ + public fun put(key: String, value: NbtElement) { + compound.put(key, value) + } + /** + * Inserts a new boolean tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value + * will be 1 if the value is true, and 0 if it is false. + * + * @author leocth + */ + public fun put(key: String, value: Boolean) { + compound.putBoolean(key, value) + } + /** + * Inserts a new 8-bit integer number tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Byte) { + compound.putByte(key, value) + } + /** + * Inserts a new 16-bit integer number tag with the given key and value. + * A new [NbtShort][net.minecraft.nbt.NbtShort] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Short) { + compound.putShort(key, value) + } + /** + * Inserts a new 32-bit integer number tag with the given key and value. + * A new [NbtInt][net.minecraft.nbt.NbtInt] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Int) { + compound.putInt(key, value) + } + /** + * Inserts a new 64-bit integer number tag with the given key and value. + * A new [NbtLong][net.minecraft.nbt.NbtLong] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Long) { + compound.putLong(key, value) + } + /** + * Inserts a new 32-bit floating-point number tag with the given key and value. + * A new [NbtFloat][net.minecraft.nbt.NbtFloat] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Float) { + compound.putFloat(key, value) + } + /** + * Inserts a new 64-bit floating-point number tag with the given key and value. + * A new [NbtDouble][net.minecraft.nbt.NbtDouble] will be created. + * + * @author leocth + */ + public fun put(key: String, value: Double) { + compound.putDouble(key, value) + } + /** + * Inserts a new string tag with the given key and value. + * A new [NbtString][net.minecraft.nbt.NbtString] will be created. + * + * @author leocth + */ + public fun put(key: String, value: String) { + compound.putString(key, value) + } + /** + * Inserts a new 8-bit integer array tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + * + * @author leocth + */ + public fun put(key: String, value: ByteArray) { + compound.putByteArray(key, value) + } + /** + * Inserts a new 8-bit integer array tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + * + * @author leocth + */ + @JvmName("putByteList") + public fun put(key: String, value: List) { + compound.putByteArray(key, value) + } + /** + * Inserts a new 32-bit integer array tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + * + * @author leocth + */ + public fun put(key: String, value: IntArray) { + compound.putIntArray(key, value) + } + /** + * Inserts a new 32-bit integer array tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + *List + * @author leocth + */ + @JvmName("putIntList") + public fun put(key: String, value: List) { + compound.putIntArray(key, value) + } + /** + * Inserts a new 64-bit integer array tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @author leocth + */ + public fun put(key: String, value: LongArray) { + compound.putLongArray(key, value) + } + /** + * Inserts a new 64-bit integer array tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @author leocth + */ + @JvmName("putLongList") + public fun put(key: String, value: List) { + compound.putLongArray(key, value) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @author leocth + */ + public inline fun putList(key: String, action: NbtListDsl.() -> Unit) { + put(key, nbtList(action)) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @author leocth + */ + public inline fun putCompound(key: String, action: NbtCompoundDsl.() -> Unit) { + put(key, nbtCompound(action)) + } + //endregion Primitive put methods + + //region Abbreviated syntax for primitive put methods + + /** + * Inserts a new [NbtElement] with the given key. + * + * @author leocth + */ + public operator fun String.invoke(value: NbtElement) { + put(this, value) + } + /** + * Inserts a new 8-bit integer number tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Boolean) { + put(this, value) + } + /** + * Inserts a new boolean tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value + * will be 1 if the value is true, and 0 if it is false. + * + * @author leocth + */ + public operator fun String.invoke(value: Byte) { + put(this, value) + } + /** + * Inserts a new 16-bit integer number tag with the given key and value. + * A new [NbtShort][net.minecraft.nbt.NbtShort] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Short) { + put(this, value) + } + /** + * Inserts a new 32-bit integer number tag with the given key and value. + * A new [NbtInt][net.minecraft.nbt.NbtInt] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Int) { + put(this, value) + } + /** + * Inserts a new 64-bit integer number tag with the given key and value. + * A new [NbtLong][net.minecraft.nbt.NbtLong] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Long) { + put(this, value) + } + /** + * Inserts a new 32-bit floating-point number tag with the given key and value. + * A new [NbtFloat][net.minecraft.nbt.NbtFloat] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Float) { + put(this, value) + } + /** + * Inserts a new 64-bit floating-point number tag with the given key and value. + * A new [NbtDouble][net.minecraft.nbt.NbtDouble] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: Double) { + put(this, value) + } + /** + * Inserts a new string tag with the given key and value. + * A new [NbtString][net.minecraft.nbt.NbtString] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: String) { + put(this, value) + } + /** + * Inserts a new 8-bit integer array tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: ByteArray) { + put(this, value) + } + /** + * Inserts a new 8-bit integer array tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + *List + * @author leocth + */ + @JvmName("putByteListShortcut") + public operator fun String.invoke(value: List) { + put(this, value) + } + /** + * Inserts a new 32-bit integer array tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: IntArray) { + put(this, value) + } + /** + * Inserts a new 32-bit integer array tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + *List + * @author leocth + */ + @JvmName("putIntListShortcut") + public operator fun String.invoke(value: List) { + put(this, value) + } + /** + * Inserts a new 64-bit integer array tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @author leocth + */ + public operator fun String.invoke(value: LongArray) { + put(this, value) + } + /** + * Inserts a new 64-bit integer array tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @author leocth + */ + @JvmName("putLongListShortcut") + public operator fun String.invoke(value: List) { + put(this, value) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @author leocth + */ + public inline fun String.list(action: NbtListDsl.() -> Unit) { + putList(this, action) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @author leocth + */ + public inline fun String.compound(action: NbtCompoundDsl.() -> Unit) { + putCompound(this, action) + } + //endregion Abbreviated syntax for primitive put methods + + //region Extended put methods + + /** + * Inserts a new tag encoding the UUID with the given key. + * + * Calls [NbtHelper.fromUuid] internally. + * + * @author leocth + */ + public fun put(key: String, value: UUID) { + compound.put(key, NbtHelper.fromUuid(value)) + } + /** + * Inserts a new tag encoding the [Identifier] with the given key. + * + * Currently, Minecraft represents [Identifier]s as [NbtString][net.minecraft.nbt.NbtString]s. + * + * @author leocth + */ + public fun put(key: String, value: Identifier) { + compound.putString(key, value.toString()) + } + /** + * Inserts a new tag encoding the [BlockPos] with the given key. + * + * Calls [NbtHelper.fromBlockPos] internally. + * + * @author leocth + */ + public fun put(key: String, value: BlockPos) { + compound.put(key, NbtHelper.fromBlockPos(value)) + } + /** + * Inserts a new tag encoding the [BlockState] with the given key. + * + * Calls [NbtHelper.fromBlockState] internally. + * + * @author leocth + */ + public fun put(key: String, value: BlockState) { + compound.put(key, NbtHelper.fromBlockState(value)) + } + //endregion Extended put methods + + //region Abbreviated syntax for extended put methods + + /** + * Inserts a new tag encoding the UUID with the given key. + * + * Calls [NbtHelper.fromUuid] internally. + * + * @author leocth + */ + public operator fun String.invoke(value: UUID) { + put(this, value) + } + /** + * Inserts a new tag encoding the [Identifier] with the given key. + * + * Currently, Minecraft represents [Identifier]s as [NbtString][net.minecraft.nbt.NbtString]s. + * + * @author leocth + */ + public operator fun String.invoke(value: Identifier) { + put(this, value) + } + /** + * Inserts a new tag encoding the [BlockPos] with the given key. + * + * Calls [NbtHelper.fromBlockPos] internally. + * + * @author leocth + */ + public operator fun String.invoke(value: BlockPos) { + put(this, value) + } + /** + * Inserts a new tag encoding the [BlockState] with the given key. + * + * Calls [NbtHelper.fromBlockState] internally. + * + * @author leocth + */ + public operator fun String.invoke(value: BlockState) { + put(this, value) + } + //endregion +} +public inline fun nbtCompound(action: NbtCompoundDsl.() -> Unit): NbtCompound { + return NbtCompoundDsl().apply(action).compound +} \ No newline at end of file diff --git a/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt b/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt new file mode 100644 index 0000000..e3f6215 --- /dev/null +++ b/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qkl.wrapper.minecraft.nbt + +import net.minecraft.nbt.NbtList + +public class NbtListDsl { + public val list: NbtList = NbtList() + +} +public inline fun nbtList(action: NbtListDsl.() -> Unit): NbtList { + return NbtListDsl().apply(action).list +} \ No newline at end of file From 161c9917daeb55ddfb3d7d18aa308cb0ca69e2a4 Mon Sep 17 00:00:00 2001 From: leocth Date: Tue, 14 Jun 2022 18:01:52 +0800 Subject: [PATCH 2/6] typo lol --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index eaf6a36..e3881de 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -# Gralde settings +# Gradle settings org.gradle.jvmargs = -Xmx1G org.gradle.parallel = true kotlin.incremental = true From d8e690ec7f1a6ae680f9fa99c1aa9f3dbc1bfe00 Mon Sep 17 00:00:00 2001 From: leocth Date: Wed, 15 Jun 2022 08:38:48 +0800 Subject: [PATCH 3/6] Structural access API --- .../minecraft/nbt/BuildCompoundDsl.kt} | 6 +- .../wrapper/minecraft/nbt/BuildListDsl.kt} | 0 .../qkl/wrapper/minecraft/nbt/NbtStruct.kt | 271 ++++++++++++++++++ 3 files changed, 274 insertions(+), 3 deletions(-) rename wrapper/{qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt => minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt} (98%) rename wrapper/{qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt => minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt} (100%) create mode 100644 wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt diff --git a/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt similarity index 98% rename from wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt rename to wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt index 0f5e2b7..316f494 100644 --- a/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtCompoundDsl.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt @@ -26,7 +26,7 @@ import java.util.UUID /** * This class contains the functions for building a [NbtCompound] object. - * To use the DSL use [nbtCompound]. + * To use the DSL use [buildNbtCompound]. * * @author leocth */ @@ -188,7 +188,7 @@ public class NbtCompoundDsl { * @author leocth */ public inline fun putCompound(key: String, action: NbtCompoundDsl.() -> Unit) { - put(key, nbtCompound(action)) + put(key, buildNbtCompound(action)) } //endregion Primitive put methods @@ -438,6 +438,6 @@ public class NbtCompoundDsl { } //endregion } -public inline fun nbtCompound(action: NbtCompoundDsl.() -> Unit): NbtCompound { +public inline fun buildNbtCompound(action: NbtCompoundDsl.() -> Unit): NbtCompound { return NbtCompoundDsl().apply(action).compound } \ No newline at end of file diff --git a/wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt similarity index 100% rename from wrapper/qsl/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtListDsl.kt rename to wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt new file mode 100644 index 0000000..6bba590 --- /dev/null +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt @@ -0,0 +1,271 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qkl.wrapper.minecraft.nbt + +import net.minecraft.nbt.NbtCompound +import net.minecraft.nbt.NbtList +import kotlin.reflect.KProperty + +/** + * The base class for structural NBT reading and writing. + * + * @author leocth + */ +public abstract class NbtStruct(protected val nbt: NbtCompound) { + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a boolean to the NBT compound with the given key. + * + * @author leocth + */ + public fun boolean(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Boolean { + return nbt.getBoolean(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Boolean) { + nbt.putBoolean(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write an 8-bit integer to the NBT compound with the given key. + * + * @author leocth + */ + public fun byte(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Byte { + return nbt.getByte(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Byte) { + nbt.putByte(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a 16-bit integer to the NBT compound with the given key. + * + * @author leocth + */ + public fun short(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Short { + return nbt.getShort(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Short) { + nbt.putShort(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a 32-bit integer to the NBT compound with the given key. + * + * @author leocth + */ + public fun int(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Int { + return nbt.getInt(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Int) { + nbt.putInt(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a 64-bit integer to the NBT compound with the given key. + * + * @author leocth + */ + public fun long(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Long { + return nbt.getLong(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Long) { + nbt.putLong(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a 32-bit floating-point number to the NBT compound with the given key. + * + * @author leocth + */ + public fun float(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Float { + return nbt.getFloat(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Float) { + nbt.putFloat(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a 64-bit floating-point number to the NBT compound with the given key. + * + * @author leocth + */ + public fun double(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): Double { + return nbt.getDouble(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: Double) { + nbt.putDouble(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a string to the NBT compound with the given key. + * + * @author leocth + */ + public fun string(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): String { + return nbt.getString(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: String) { + nbt.putString(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write an array of 8-bit integers to the NBT compound with the given key. + * + * @author leocth + */ + public fun byteArray(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): ByteArray { + return nbt.getByteArray(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: ByteArray) { + nbt.putByteArray(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write an array of 32-bit integers to the NBT compound with the given key. + * + * @author leocth + */ + public fun intArray(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): IntArray { + return nbt.getIntArray(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: IntArray) { + nbt.putIntArray(key ?: property.name, value) + } + } + } + + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write an array of 64-bit integers to the NBT compound with the given key. + * + * @author leocth + */ + public fun longArray(key: String? = null): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): LongArray { + return nbt.getLongArray(key ?: property.name) + } + override fun setValue(self: Any?, property: KProperty<*>, value: LongArray) { + nbt.putLongArray(key ?: property.name, value) + } + } + } + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write another [structural NBT data type][NbtStruct] to the NBT compound + * with the given key. + * + * @author leocth + */ + public fun struct( + mapper: (NbtCompound) -> T, + key: String? = null + ): NbtDelegate { + return object : NbtDelegate { + override fun getValue(self: Any?, property: KProperty<*>): T { + return mapper(nbt.getCompound(key ?: property.name)) + } + override fun setValue(self: Any?, property: KProperty<*>, value: T) { + nbt.put(key ?: property.name, value.nbt) + } + } + } + // TODO(leocth): listOfFloats, listOfDoubles, etc? + + /** + * Creates a [delegate](https://kotlinlang.org/docs/delegated-properties.html) that can + * read and write a list of objects of another [structural NBT data type][NbtStruct] + * to the NBT compound with the given key. + * + * @author leocth + */ + public fun listOfStructs( + mapper: (NbtCompound) -> T, + key: String? = null + ): NbtDelegate> { + return object : NbtDelegate> { + override fun getValue(self: Any?, property: KProperty<*>): List { + return nbt.getList(key ?: property.name, COMPOUND.toInt()).map { + if (it.type != COMPOUND) { + throw IllegalArgumentException("NBT element must be a compound element") + } + mapper(it as NbtCompound) + } + } + override fun setValue(self: Any?, property: KProperty<*>, value: List) { + val list = NbtList() + value.forEach { + list.add(it.nbt) + } + nbt.put(key, list) + } + } + } +} + +/** + * Interface containing the two operators for getting and setting via + * [delegation](https://kotlinlang.org/docs/delegated-properties.html). + * + * You shouldn't need to implement this interface—its sole purpose is to + * aid the implementation of [NbtStruct]. + * + * @author leocth + */ +public interface NbtDelegate { + public operator fun getValue(self: Any?, property: KProperty<*>): T + public operator fun setValue(self: Any?, property: KProperty<*>, value: T) +} \ No newline at end of file From 4bf203cb6f6d46983137e610701848c81984b5b3 Mon Sep 17 00:00:00 2001 From: leocth Date: Wed, 15 Jun 2022 09:24:06 +0800 Subject: [PATCH 4/6] nullable access stuff --- .../wrapper/minecraft/nbt/AccessExtensions.kt | 121 ++++++++++++++++++ .../qkl/wrapper/minecraft/nbt/NbtStruct.kt | 1 + 2 files changed, 122 insertions(+) create mode 100644 wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt new file mode 100644 index 0000000..51e8e3e --- /dev/null +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt @@ -0,0 +1,121 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qkl.wrapper.minecraft.nbt + +import net.minecraft.nbt.* + +//region Getting nullable values from compound tags + +// Implementor's note (leocth): +// +// For checking the types of the tags, the method we use below is theoretically a bit slower +// than the way Minecraft uses—instead of checking a single number, we use the `as?` operator +// which is a bit like an `instanceof` + a cast, which could be a *lot* slower. +// +// However, in absence of performance benchmarks, I think the convenience of using `as?` +// far outweighs any performance benefits comparing numeric NBT types might have. +// I might change our implementation in the future if a substantial performance increase +// can be identified. + +/** + * Gets a boolean from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getBooleanOrNull(key: String): Boolean? { + return get(key)?.let { + it as? NbtByte + }?.let { + it.byteValue() == 1.toByte() + } +} +/** + * Gets an 8-bit integer from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getByteOrNull(key: String): Byte? { + return get(key)?.let { it as? NbtByte }?.byteValue() +} +/** + * Gets a 16-bit integer from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getShortOrNull(key: String): Short? { + return get(key)?.let { it as? NbtShort }?.shortValue() +} +/** + * Gets a 32-bit integer from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getIntOrNull(key: String): Int? { + return get(key)?.let { it as? NbtInt }?.intValue() +} +/** + * Gets a 64-bit integer from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getLongOrNull(key: String): Long? { + return get(key)?.let { it as? NbtLong }?.longValue() +} +/** + * Gets a 32-bit floating-point number from the NBT compound with the given key, + * returning `null` if the key is not present in the compound, or if the found tag + * is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getFloatOrNull(key: String): Float? { + return get(key)?.let { it as? NbtFloat }?.floatValue() +} +/** + * Gets a 64-bit floating-point number from the NBT compound with the given key, + * returning `null` if the key is not present in the compound, or if the found tag + * is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getDoubleOrNull(key: String): Double? { + return get(key)?.let { it as? NbtDouble }?.doubleValue() +} +/** + * Gets a string from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getStringOrNull(key: String): String? { + return get(key)?.let { it as? NbtString }?.asString() +} +/** + * Gets an NBT compound from the NBT compound with the given key, returning `null` if + * the key is not present in the compound, or if the found tag is not of the expected type. + * + * @author leocth + */ +public fun NbtCompound.getCompoundOrNull(key: String): NbtCompound? { + return get(key)?.let { it as? NbtCompound } +} +//endregion Getting nullable values from compound tags \ No newline at end of file diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt index 6bba590..597d8e1 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt @@ -255,6 +255,7 @@ public abstract class NbtStruct(protected val nbt: NbtCompound) { } } } +private const val COMPOUND: Byte = 10 /** * Interface containing the two operators for getting and setting via From b411fe987811ec400d6088305a8b71ed5f937226 Mon Sep 17 00:00:00 2001 From: leocth Date: Wed, 15 Jun 2022 10:20:32 +0800 Subject: [PATCH 5/6] adjust documentation --- .../wrapper/minecraft/nbt/AccessExtensions.kt | 2 +- .../wrapper/minecraft/nbt/BuildCompoundDsl.kt | 86 +++---- .../qkl/wrapper/minecraft/nbt/BuildListDsl.kt | 209 +++++++++++++++++- .../qkl/wrapper/minecraft/nbt/NbtStruct.kt | 9 +- 4 files changed, 260 insertions(+), 46 deletions(-) diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt index 51e8e3e..45a8890 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt @@ -118,4 +118,4 @@ public fun NbtCompound.getStringOrNull(key: String): String? { public fun NbtCompound.getCompoundOrNull(key: String): NbtCompound? { return get(key)?.let { it as? NbtCompound } } -//endregion Getting nullable values from compound tags \ No newline at end of file +//endregion Getting nullable values from compound tags diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt index 316f494..3f8ba4a 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildCompoundDsl.kt @@ -45,9 +45,9 @@ public class NbtCompoundDsl { compound.put(key, value) } /** - * Inserts a new boolean tag with the given key and value. + * Inserts a new [Boolean] tag with the given key and value. * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value - * will be 1 if the value is true, and 0 if it is false. + * will be 1 if the boolean is true, and 0 if it is false. * * @author leocth */ @@ -55,7 +55,7 @@ public class NbtCompoundDsl { compound.putBoolean(key, value) } /** - * Inserts a new 8-bit integer number tag with the given key and value. + * Inserts a new [Byte] tag with the given key and value. * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. * * @author leocth @@ -64,7 +64,7 @@ public class NbtCompoundDsl { compound.putByte(key, value) } /** - * Inserts a new 16-bit integer number tag with the given key and value. + * Inserts a new [Short] tag with the given key and value. * A new [NbtShort][net.minecraft.nbt.NbtShort] will be created. * * @author leocth @@ -73,7 +73,7 @@ public class NbtCompoundDsl { compound.putShort(key, value) } /** - * Inserts a new 32-bit integer number tag with the given key and value. + * Inserts a new [Int] tag with the given key and value. * A new [NbtInt][net.minecraft.nbt.NbtInt] will be created. * * @author leocth @@ -82,7 +82,7 @@ public class NbtCompoundDsl { compound.putInt(key, value) } /** - * Inserts a new 64-bit integer number tag with the given key and value. + * Inserts a new [Long] tag with the given key and value. * A new [NbtLong][net.minecraft.nbt.NbtLong] will be created. * * @author leocth @@ -91,7 +91,7 @@ public class NbtCompoundDsl { compound.putLong(key, value) } /** - * Inserts a new 32-bit floating-point number tag with the given key and value. + * Inserts a new [Float] tag with the given key and value. * A new [NbtFloat][net.minecraft.nbt.NbtFloat] will be created. * * @author leocth @@ -100,7 +100,7 @@ public class NbtCompoundDsl { compound.putFloat(key, value) } /** - * Inserts a new 64-bit floating-point number tag with the given key and value. + * Inserts a new [Double] tag with the given key and value. * A new [NbtDouble][net.minecraft.nbt.NbtDouble] will be created. * * @author leocth @@ -109,7 +109,7 @@ public class NbtCompoundDsl { compound.putDouble(key, value) } /** - * Inserts a new string tag with the given key and value. + * Inserts a new [String] tag with the given key and value. * A new [NbtString][net.minecraft.nbt.NbtString] will be created. * * @author leocth @@ -118,7 +118,7 @@ public class NbtCompoundDsl { compound.putString(key, value) } /** - * Inserts a new 8-bit integer array tag with the given key and value. + * Inserts a new [ByteArray] tag with the given key and value. * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. * * @author leocth @@ -127,7 +127,7 @@ public class NbtCompoundDsl { compound.putByteArray(key, value) } /** - * Inserts a new 8-bit integer array tag with the given key and value. + * Inserts a new [ByteArray] tag with the given key and value. * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. * * @author leocth @@ -137,7 +137,7 @@ public class NbtCompoundDsl { compound.putByteArray(key, value) } /** - * Inserts a new 32-bit integer array tag with the given key and value. + * Inserts a new [IntArray] tag with the given key and value. * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. * * @author leocth @@ -146,9 +146,9 @@ public class NbtCompoundDsl { compound.putIntArray(key, value) } /** - * Inserts a new 32-bit integer array tag with the given key and value. + * Inserts a new [IntArray] tag with the given key and value. * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. - *List + * * @author leocth */ @JvmName("putIntList") @@ -156,7 +156,7 @@ public class NbtCompoundDsl { compound.putIntArray(key, value) } /** - * Inserts a new 64-bit integer array tag with the given key and value. + * Inserts a new [LongArray] tag with the given key and value. * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. * * @author leocth @@ -165,7 +165,7 @@ public class NbtCompoundDsl { compound.putLongArray(key, value) } /** - * Inserts a new 64-bit integer array tag with the given key and value. + * Inserts a new [LongArray] tag with the given key and value. * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. * * @author leocth @@ -179,11 +179,11 @@ public class NbtCompoundDsl { * * @author leocth */ - public inline fun putList(key: String, action: NbtListDsl.() -> Unit) { - put(key, nbtList(action)) + public inline fun putList(key: String, action: NbtListDsl.() -> Unit) { + put(key, buildNbtList(action)) } /** - * Builds and inserts a new list tag with the given key. + * Builds and inserts a new compound tag with the given key. * * @author leocth */ @@ -203,8 +203,9 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 8-bit integer number tag with the given key and value. - * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. + * Inserts a new [Boolean] tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value + * will be 1 if the boolean is true, and 0 if it is false. * * @author leocth */ @@ -212,9 +213,8 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new boolean tag with the given key and value. - * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value - * will be 1 if the value is true, and 0 if it is false. + * Inserts a new [Byte] tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. * * @author leocth */ @@ -222,7 +222,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 16-bit integer number tag with the given key and value. + * Inserts a new [Short] tag with the given key and value. * A new [NbtShort][net.minecraft.nbt.NbtShort] will be created. * * @author leocth @@ -231,7 +231,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 32-bit integer number tag with the given key and value. + * Inserts a new [Int] tag with the given key and value. * A new [NbtInt][net.minecraft.nbt.NbtInt] will be created. * * @author leocth @@ -240,7 +240,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 64-bit integer number tag with the given key and value. + * Inserts a new [Long] tag with the given key and value. * A new [NbtLong][net.minecraft.nbt.NbtLong] will be created. * * @author leocth @@ -249,7 +249,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 32-bit floating-point number tag with the given key and value. + * Inserts a new [Float] tag with the given key and value. * A new [NbtFloat][net.minecraft.nbt.NbtFloat] will be created. * * @author leocth @@ -258,7 +258,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 64-bit floating-point number tag with the given key and value. + * Inserts a new [Double] tag with the given key and value. * A new [NbtDouble][net.minecraft.nbt.NbtDouble] will be created. * * @author leocth @@ -267,7 +267,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new string tag with the given key and value. + * Inserts a new [String] tag with the given key and value. * A new [NbtString][net.minecraft.nbt.NbtString] will be created. * * @author leocth @@ -276,7 +276,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 8-bit integer array tag with the given key and value. + * Inserts a new [ByteArray] tag with the given key and value. * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. * * @author leocth @@ -285,7 +285,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 8-bit integer array tag with the given key and value. + * Inserts a new [ByteArray] tag with the given key and value. * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. *List * @author leocth @@ -295,7 +295,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 32-bit integer array tag with the given key and value. + * Inserts a new [IntArray] tag with the given key and value. * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. * * @author leocth @@ -304,7 +304,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 32-bit integer array tag with the given key and value. + * Inserts a new [IntArray] tag with the given key and value. * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. *List * @author leocth @@ -314,7 +314,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 64-bit integer array tag with the given key and value. + * Inserts a new [LongArray] tag with the given key and value. * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. * * @author leocth @@ -323,7 +323,7 @@ public class NbtCompoundDsl { put(this, value) } /** - * Inserts a new 64-bit integer array tag with the given key and value. + * Inserts a new [LongArray] tag with the given key and value. * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. * * @author leocth @@ -337,15 +337,15 @@ public class NbtCompoundDsl { * * @author leocth */ - public inline fun String.list(action: NbtListDsl.() -> Unit) { + public inline infix fun String.list(action: NbtListDsl.() -> Unit) { putList(this, action) } /** - * Builds and inserts a new list tag with the given key. + * Builds and inserts a new compound tag with the given key. * * @author leocth */ - public inline fun String.compound(action: NbtCompoundDsl.() -> Unit) { + public inline infix fun String.compound(action: NbtCompoundDsl.() -> Unit) { putCompound(this, action) } //endregion Abbreviated syntax for primitive put methods @@ -438,6 +438,12 @@ public class NbtCompoundDsl { } //endregion } +/** + * Builds a new [NbtCompound]. + * + * @see NbtCompoundDsl + * @author leocth + */ public inline fun buildNbtCompound(action: NbtCompoundDsl.() -> Unit): NbtCompound { return NbtCompoundDsl().apply(action).compound -} \ No newline at end of file +} diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt index e3f6215..37d0d90 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt @@ -16,12 +16,213 @@ package org.quiltmc.qkl.wrapper.minecraft.nbt +import net.minecraft.nbt.NbtByte +import net.minecraft.nbt.NbtByteArray +import net.minecraft.nbt.NbtDouble +import net.minecraft.nbt.NbtElement +import net.minecraft.nbt.NbtFloat +import net.minecraft.nbt.NbtInt +import net.minecraft.nbt.NbtIntArray import net.minecraft.nbt.NbtList +import net.minecraft.nbt.NbtLong +import net.minecraft.nbt.NbtLongArray +import net.minecraft.nbt.NbtShort +import net.minecraft.nbt.NbtString -public class NbtListDsl { +/** + * DSL for creating [NbtList]s. + * + * @author leocth + */ +public class NbtListDsl { + /** + * The underlying [NbtList]. + * + * @author leocth + */ public val list: NbtList = NbtList() + + /** + * Inserts a new [NbtElement] with the given key. + * + * @throws UnsupportedOperationException + * if the type of this list does not match the type of the value + * @author leocth + */ + public fun add(value: NbtElement) { + list.add(value) + } + /** + * Inserts a new [Boolean] tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created, and its value + * will be 1 if the boolean is true, and 0 if it is false. + * + * @throws UnsupportedOperationException if this list has anything other than [Byte]s + * @author leocth + */ + public fun add(value: Boolean) { + list.add(NbtByte.of(value)) + } + /** + * Inserts a new [Byte] tag with the given key and value. + * A new [NbtByte][net.minecraft.nbt.NbtByte] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Byte]s + * @author leocth + */ + public fun add(value: Byte) { + list.add(NbtByte.of(value)) + } + /** + * Inserts a new [Short] tag with the given key and value. + * A new [NbtShort][net.minecraft.nbt.NbtShort] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Short]s + * @author leocth + */ + public fun add(value: Short) { + list.add(NbtShort.of(value)) + } + /** + * Inserts a new [Int] tag with the given key and value. + * A new [NbtInt][net.minecraft.nbt.NbtInt] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Int]s + * @author leocth + */ + public fun add(value: Int) { + list.add(NbtInt.of(value)) + } + /** + * Inserts a new [Long] tag with the given key and value. + * A new [NbtLong][net.minecraft.nbt.NbtLong] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Long]s + * @author leocth + */ + public fun add(value: Long) { + list.add(NbtLong.of(value)) + } + /** + * Inserts a new [Float] tag with the given key and value. + * A new [NbtFloat][net.minecraft.nbt.NbtFloat] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Float]s + * @author leocth + */ + public fun add(value: Float) { + list.add(NbtFloat.of(value)) + } + /** + * Inserts a new [Double] tag with the given key and value. + * A new [NbtDouble][net.minecraft.nbt.NbtDouble] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [Double]s + * @author leocth + */ + public fun add(value: Double) { + list.add(NbtDouble.of(value)) + } + /** + * Inserts a new [String] tag with the given key and value. + * A new [NbtString][net.minecraft.nbt.NbtString] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [String]s + * @author leocth + */ + public fun add(value: String) { + list.add(NbtString.of(value)) + } + /** + * Inserts a new [ByteArray] tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [ByteArray]s + * @author leocth + */ + public fun add(value: ByteArray) { + list.add(NbtByteArray(value)) + } + /** + * Inserts a new [ByteArray] tag with the given key and value. + * A new [NbtByteArray][net.minecraft.nbt.NbtByteArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [ByteArray]s + * @author leocth + */ + @JvmName("addByteList") + public fun add(value: List) { + list.add(NbtByteArray(value)) + } + /** + * Inserts a new [IntArray] tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [IntArray]s + * @author leocth + */ + public fun add(value: IntArray) { + list.add(NbtIntArray(value)) + } + /** + * Inserts a new [IntArray] tag with the given key and value. + * A new [NbtIntArray][net.minecraft.nbt.NbtIntArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [IntArray]s + * @author leocth + */ + @JvmName("addIntList") + public fun add(value: List) { + list.add(NbtIntArray(value)) + } + /** + * Inserts a new [LongArray] tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [LongArray]s + * @author leocth + */ + public fun add(value: LongArray) { + list.add(NbtLongArray(value)) + } + /** + * Inserts a new [LongArray] tag with the given key and value. + * A new [NbtLongArray][net.minecraft.nbt.NbtLongArray] will be created. + * + * @throws UnsupportedOperationException if this list has anything other than [LongArray]s + * @author leocth + */ + @JvmName("addLongList") + public fun add(value: List) { + list.add(NbtLongArray(value)) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @throws UnsupportedOperationException if this list has anything other than lists + * @author leocth + */ + public inline fun putList(action: NbtListDsl.() -> Unit) { + list.add(buildNbtList(action)) + } + /** + * Builds and inserts a new list tag with the given key. + * + * @throws UnsupportedOperationException if this list has anything other than compounds + * @author leocth + */ + public inline fun putCompound(action: NbtCompoundDsl.() -> Unit) { + list.add(buildNbtCompound(action)) + } +} + +/** + * Builds a new [NbtList]. + * + * @see NbtListDsl + * @author leocth + */ +public inline fun buildNbtList(action: NbtListDsl.() -> Unit): NbtList { + return NbtListDsl().apply(action).list } -public inline fun nbtList(action: NbtListDsl.() -> Unit): NbtList { - return NbtListDsl().apply(action).list -} \ No newline at end of file diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt index 597d8e1..4b78e42 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/NbtStruct.kt @@ -267,6 +267,13 @@ private const val COMPOUND: Byte = 10 * @author leocth */ public interface NbtDelegate { + /** + * Gets the value of the property. + */ public operator fun getValue(self: Any?, property: KProperty<*>): T + + /** + * Sets the property to the given value. + */ public operator fun setValue(self: Any?, property: KProperty<*>, value: T) -} \ No newline at end of file +} From 3ced4c13ae8bb1276d80c65b08084e3e70803b6a Mon Sep 17 00:00:00 2001 From: leocth Date: Wed, 15 Jun 2022 10:21:34 +0800 Subject: [PATCH 6/6] adjust even more documentation --- .../wrapper/minecraft/nbt/AccessExtensions.kt | 18 +++++++++--------- .../qkl/wrapper/minecraft/nbt/BuildListDsl.kt | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt index 45a8890..0ae4245 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/AccessExtensions.kt @@ -32,7 +32,7 @@ import net.minecraft.nbt.* // can be identified. /** - * Gets a boolean from the NBT compound with the given key, returning `null` if + * Gets a [Boolean] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -45,7 +45,7 @@ public fun NbtCompound.getBooleanOrNull(key: String): Boolean? { } } /** - * Gets an 8-bit integer from the NBT compound with the given key, returning `null` if + * Gets a [Byte] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -54,7 +54,7 @@ public fun NbtCompound.getByteOrNull(key: String): Byte? { return get(key)?.let { it as? NbtByte }?.byteValue() } /** - * Gets a 16-bit integer from the NBT compound with the given key, returning `null` if + * Gets a [Short] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -63,7 +63,7 @@ public fun NbtCompound.getShortOrNull(key: String): Short? { return get(key)?.let { it as? NbtShort }?.shortValue() } /** - * Gets a 32-bit integer from the NBT compound with the given key, returning `null` if + * Gets a [Int] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -72,7 +72,7 @@ public fun NbtCompound.getIntOrNull(key: String): Int? { return get(key)?.let { it as? NbtInt }?.intValue() } /** - * Gets a 64-bit integer from the NBT compound with the given key, returning `null` if + * Gets a [Long] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -81,7 +81,7 @@ public fun NbtCompound.getLongOrNull(key: String): Long? { return get(key)?.let { it as? NbtLong }?.longValue() } /** - * Gets a 32-bit floating-point number from the NBT compound with the given key, + * Gets a [Float] from the NBT compound with the given key, * returning `null` if the key is not present in the compound, or if the found tag * is not of the expected type. * @@ -91,7 +91,7 @@ public fun NbtCompound.getFloatOrNull(key: String): Float? { return get(key)?.let { it as? NbtFloat }?.floatValue() } /** - * Gets a 64-bit floating-point number from the NBT compound with the given key, + * Gets a [Double] from the NBT compound with the given key, * returning `null` if the key is not present in the compound, or if the found tag * is not of the expected type. * @@ -101,7 +101,7 @@ public fun NbtCompound.getDoubleOrNull(key: String): Double? { return get(key)?.let { it as? NbtDouble }?.doubleValue() } /** - * Gets a string from the NBT compound with the given key, returning `null` if + * Gets a [String] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth @@ -110,7 +110,7 @@ public fun NbtCompound.getStringOrNull(key: String): String? { return get(key)?.let { it as? NbtString }?.asString() } /** - * Gets an NBT compound from the NBT compound with the given key, returning `null` if + * Gets an [NbtCompound] from the NBT compound with the given key, returning `null` if * the key is not present in the compound, or if the found tag is not of the expected type. * * @author leocth diff --git a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt index 37d0d90..002f3c6 100644 --- a/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt +++ b/wrapper/minecraft/src/main/kotlin/org/quiltmc/qkl/wrapper/minecraft/nbt/BuildListDsl.kt @@ -203,7 +203,7 @@ public class NbtListDsl { * @throws UnsupportedOperationException if this list has anything other than lists * @author leocth */ - public inline fun putList(action: NbtListDsl.() -> Unit) { + public inline fun addList(action: NbtListDsl.() -> Unit) { list.add(buildNbtList(action)) } /** @@ -212,7 +212,7 @@ public class NbtListDsl { * @throws UnsupportedOperationException if this list has anything other than compounds * @author leocth */ - public inline fun putCompound(action: NbtCompoundDsl.() -> Unit) { + public inline fun addCompound(action: NbtCompoundDsl.() -> Unit) { list.add(buildNbtCompound(action)) } }