Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {

// Networking
api("io.ktor:ktor-server-netty:3.1.2")
api("io.github.dockyardmc:tide:3.1")
api("io.github.dockyardmc:tide:3.7")
api("io.github.dockyardmc:bytesocks-client-java:1.0-SNAPSHOT") {
exclude(module = "slf4j-api")
}
Expand Down
6 changes: 4 additions & 2 deletions custom_codec_definitions.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"codec": {
"Component": "ComponentCodecs.STRING"
"Component": "ComponentCodecs.STRING",
"PotionEffect": "RegistryCodec.codec(PotionEffectRegistry)"
},
"stream_codec": {
"Component": "ComponentCodecs.STREAM"
"Component": "ComponentCodecs.STREAM",
"PotionEffect": "RegistryCodec.stream(PotionEffectRegistry)"
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/io/github/dockyardmc/codec/DurationCodec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.dockyardmc.codec

import io.github.dockyardmc.extentions.readVarInt
import io.github.dockyardmc.extentions.writeVarInt
import io.github.dockyardmc.scheduler.runnables.inWholeMinecraftTicks
import io.github.dockyardmc.scheduler.runnables.ticks
import io.github.dockyardmc.tide.codec.Codec
import io.github.dockyardmc.tide.stream.StreamCodec
import io.github.dockyardmc.tide.transcoder.Transcoder
import io.netty.buffer.ByteBuf
import kotlin.time.Duration

object DurationCodec {

val STREAM_CODEC_INT_TICKS = object : StreamCodec<Duration> {

override fun write(buffer: ByteBuf, value: Duration) {
buffer.writeVarInt(value.inWholeMinecraftTicks)
}

override fun read(buffer: ByteBuf): Duration {
return buffer.readVarInt().ticks
}
}

val CODEC_INT_TICKS = object : Codec<Duration> {

override fun <D> encode(transcoder: Transcoder<D>, value: Duration): D {
return transcoder.encodeInt(value.inWholeMinecraftTicks)
}

override fun <D> decode(transcoder: Transcoder<D>, value: D): Duration {
return transcoder.decodeInt(value).ticks

}
}

}
22 changes: 22 additions & 0 deletions src/main/kotlin/io/github/dockyardmc/codec/LocationCodecs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.dockyardmc.codec

import io.github.dockyardmc.maths.vectors.Vector3
import io.github.dockyardmc.tide.stream.StreamCodec

object LocationCodecs {

val BLOCK_POSITION = StreamCodec.LONG.transform<Vector3>(
{ from ->
val blockX = from.x.toLong()
val blockY = from.y.toLong()
val blockZ = from.z.toLong()
(blockX and 0x3FFFFFF shl 38) or ((blockZ and 0x3FFFFFF) shl 12) or (blockY and 0xFFF)
},
{ to ->
val x = (to shr 38).toInt()
val y = (to shl 52 shr 52).toInt()
val z = (to shl 26 shr 38).toInt()
Vector3(x, y, z)
})

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ object BinaryTagTranscoder : Transcoder<BinaryTag> {
}

override fun put(key: String, value: BinaryTag): Transcoder.VirtualMapBuilder<BinaryTag> {
compound.put(key, value)
if (value !is EndBinaryTag) compound.put(key, value)
return this
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package io.github.dockyardmc.codec.transcoder

import io.github.dockyardmc.data.CRC32CHasher
import io.github.dockyardmc.data.CRC32CHasher.EMPTY
import io.github.dockyardmc.data.Hasher
import io.github.dockyardmc.tide.transcoder.Transcoder

object CRC32CTranscoder : Transcoder<Int> {

override fun encodeBoolean(value: Boolean): Int {
return if (value) CRC32CHasher.TRUE else CRC32CHasher.FALSE
}

override fun encodeByte(value: Byte): Int {
return CRC32CHasher.ofByte(value)
}

override fun encodeDouble(value: Double): Int {
return CRC32CHasher.ofDouble(value)
}

override fun encodeFloat(value: Float): Int {
return CRC32CHasher.ofFloat(value)
}

override fun encodeInt(value: Int): Int {
return CRC32CHasher.ofInt(value)
}

override fun encodeList(size: Int): Transcoder.ListBuilder<Int> {
val hasher = Hasher().putByte(CRC32CHasher.TAG_LIST_START)
return object : Transcoder.ListBuilder<Int> {

override fun add(value: Int): Transcoder.ListBuilder<Int> {
hasher.putIntBytes(value)
return this
}

override fun build(): Int {
return hasher.putByte(CRC32CHasher.TAG_LIST_END).hash()
}

}
}

override fun encodeLong(value: Long): Int {
return CRC32CHasher.ofLong(value)
}

override fun encodeMap(): Transcoder.VirtualMapBuilder<Int> {
val map = mutableMapOf<Int, Int>()
return object : Transcoder.VirtualMapBuilder<Int> {

override fun put(key: Int, value: Int): Transcoder.VirtualMapBuilder<Int> {
if (value != EMPTY) {
map[key] = value
}
return this
}

override fun build(): Int {
if (map.isEmpty()) return CRC32CHasher.EMPTY_MAP
val hasher = Hasher().putByte(CRC32CHasher.TAG_MAP_START)

map.entries.stream().sorted(CRC32CHasher.MAP_COMPARATOR).forEach { entry ->
hasher.putIntBytes(entry.key)
hasher.putIntBytes(entry.value)
}

return hasher.putByte(CRC32CHasher.TAG_MAP_END).hash()

}

override fun put(key: String, value: Int): Transcoder.VirtualMapBuilder<Int> {
return put(encodeString(key), value)
}

}
}

override fun encodeNull(): Int {
return EMPTY
}

override fun encodeShort(value: Short): Int {
return CRC32CHasher.ofShort(value)
}

override fun encodeString(value: String): Int {
return CRC32CHasher.ofString(value)
}

override fun encodeByteArray(value: ByteArray): Int {
return CRC32CHasher.ofByteArray(value)
}

override fun encodeIntArray(value: IntArray): Int {
return CRC32CHasher.ofIntArray(value)
}

override fun encodeLongArray(value: LongArray): Int {
return CRC32CHasher.ofLongArray(value)
}

// only encode, fuck the decoding

override fun decodeBoolean(value: Int): Boolean {
throw UnsupportedOperationException()
}

override fun decodeByte(value: Int): Byte {
throw UnsupportedOperationException()
}

override fun decodeDouble(value: Int): Double {
throw UnsupportedOperationException()
}

override fun decodeFloat(value: Int): Float {
throw UnsupportedOperationException()
}

override fun decodeInt(value: Int): Int {
throw UnsupportedOperationException()
}

override fun decodeList(value: Int): List<Int> {
throw UnsupportedOperationException()
}

override fun decodeLong(value: Int): Long {
throw UnsupportedOperationException()
}

override fun decodeMap(value: Int): Transcoder.VirtualMap<Int> {
throw UnsupportedOperationException()
}

override fun decodeShort(value: Int): Short {
throw UnsupportedOperationException()
}

override fun decodeString(value: Int): String {
throw UnsupportedOperationException()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/io/github/dockyardmc/data/CRC32CHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ object CRC32CHasher {
// should have 0 probability to appear as CRC32C hash due to odd parity, we can use it as marker for inline values in maps
const val INLINE: Int = 0x00000001

private val KEY_COMPARATOR: Comparator<Map.Entry<Int, Int>> = java.util.Map.Entry.comparingByKey(Comparator.comparingLong { x: Int -> Integer.toUnsignedLong(x) })
private val VALUE_COMPARATOR: Comparator<Map.Entry<Int, Int>> = java.util.Map.Entry.comparingByValue(Comparator.comparingLong { x: Int -> Integer.toUnsignedLong(x) })
private val MAP_COMPARATOR: Comparator<Map.Entry<Int, Int>> = KEY_COMPARATOR.thenComparing(VALUE_COMPARATOR)
val KEY_COMPARATOR: Comparator<Map.Entry<Int, Int>> = java.util.Map.Entry.comparingByKey(Comparator.comparingLong { x: Int -> Integer.toUnsignedLong(x) })
val VALUE_COMPARATOR: Comparator<Map.Entry<Int, Int>> = java.util.Map.Entry.comparingByValue(Comparator.comparingLong { x: Int -> Integer.toUnsignedLong(x) })
val MAP_COMPARATOR: Comparator<Map.Entry<Int, Int>> = KEY_COMPARATOR.thenComparing(VALUE_COMPARATOR)

const val TAG_EMPTY: Byte = 1
const val TAG_MAP_START: Byte = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
package io.github.dockyardmc.data.components

import io.github.dockyardmc.data.CRC32CHasher
import io.github.dockyardmc.codec.RegistryCodec
import io.github.dockyardmc.codec.transcoder.CRC32CTranscoder
import io.github.dockyardmc.data.DataComponent
import io.github.dockyardmc.data.HashHolder
import io.github.dockyardmc.extentions.readRegistryEntry
import io.github.dockyardmc.extentions.writeRegistryEntry
import io.github.dockyardmc.data.StaticHash
import io.github.dockyardmc.protocol.NetworkReadable
import io.github.dockyardmc.registry.registries.TrimMaterial
import io.github.dockyardmc.registry.registries.TrimMaterialRegistry
import io.github.dockyardmc.registry.registries.TrimPattern
import io.github.dockyardmc.registry.registries.TrimPatternRegistry
import io.github.dockyardmc.tide.codec.StructCodec
import io.github.dockyardmc.tide.stream.StreamCodec
import io.netty.buffer.ByteBuf

class ArmorTrimComponent(val material: TrimMaterial, val pattern: TrimPattern) : DataComponent() {
data class ArmorTrimComponent(val material: TrimMaterial, val pattern: TrimPattern) : DataComponent() {

override fun write(buffer: ByteBuf) {
buffer.writeRegistryEntry(material)
buffer.writeRegistryEntry(pattern)
STREAM_CODEC.write(buffer, this)
}

override fun hashStruct(): HashHolder {
return CRC32CHasher.of {
static("material", CRC32CHasher.ofRegistryEntry(material))
static("pattern", CRC32CHasher.ofRegistryEntry(pattern))
}
return StaticHash(CODEC.encode(CRC32CTranscoder, this))
}

companion object : NetworkReadable<ArmorTrimComponent> {
val CODEC = StructCodec.of(
"material", RegistryCodec.codec(TrimMaterialRegistry), ArmorTrimComponent::material,
"pattern", RegistryCodec.codec(TrimPatternRegistry), ArmorTrimComponent::pattern,
::ArmorTrimComponent
)

val STREAM_CODEC = StreamCodec.of(
RegistryCodec.stream(TrimMaterialRegistry), ArmorTrimComponent::material,
RegistryCodec.stream(TrimPatternRegistry), ArmorTrimComponent::pattern,
::ArmorTrimComponent
)

override fun read(buffer: ByteBuf): ArmorTrimComponent {
return ArmorTrimComponent(
buffer.readRegistryEntry(TrimMaterialRegistry),
buffer.readRegistryEntry(TrimPatternRegistry),
)
return STREAM_CODEC.read(buffer)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.dockyardmc.data.components

import io.github.dockyardmc.data.DataComponent
import io.github.dockyardmc.data.HashHolder
import io.github.dockyardmc.extentions.readNBT
import io.github.dockyardmc.extentions.readNBTCompound
import io.github.dockyardmc.extentions.writeNBT
import io.github.dockyardmc.protocol.NetworkReadable
Expand All @@ -16,7 +15,7 @@ class BlockEntityDataComponent(val nbt: CompoundBinaryTag) : DataComponent() {
}

override fun hashStruct(): HashHolder {
return unsupported(this::class)
return unsupported(this)
}

companion object : NetworkReadable<BlockEntityDataComponent> {
Expand Down
Loading
Loading