Skip to content

Commit

Permalink
merge: pull request from wanderia/dev/1.2.0 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustPyrrha authored Aug 28, 2024
2 parents a554fe2 + 3795af4 commit 8b3877e
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 58 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Debug log lines to print payload info when initializing.

### Changed

- Updated `minecraft` from `1.21` to `1.21.1`
- Updated `kotlin` from `2.0.0` to `2.0.20`
- Updated `fabric-api` from `0.100.7+1.21` to `0.103.0+1.21.1`
- Updated `fabric-kotlin` from `1.11.0+kotlin.2.0.0` to `1.12.1+kotlin.2.0.20`
- Updated `fabric-loader` from `0.16.0` to `0.16.3`
- Updated `spotless` from `7.0.0.BETA1` to `7.0.0.BETA2`
- Moved use of client networking and events to a `client` entrypoint. (TestMod)

### Deprecated

### Removed

### Fixed

- Broken custom entrypoint.

### Security

## [1.1.0] - 2024-07-20
Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ buildscript {

group = "dev.wanderia"

version = "1.1.0"
version = "1.2.0"

repositories { mavenCentral() }

Expand Down Expand Up @@ -108,6 +108,7 @@ loom {
if (enableDCEVM) {
vmArgs("-XX:+AllowEnhancedClassRedefinition")
}
property("dev.wanderia.netlib.debug", "true")
ideConfigGenerated(name.contains("testmod"))
runDir("runs/$name")
}
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
14 changes: 7 additions & 7 deletions libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[versions]
dokka = "1.9.20" # https://github.com/Kotlin/dokka/releases
changelog = "2.2.1" # https://github.com/JetBrains/gradle-changelog-plugin/releases
fabricApi = "0.100.7+1.21" # https://github.com/FabricMC/fabric/releases
fabricKotlin = "1.11.0+kotlin.2.0.0" # https://github.com/FabricMC/fabric-language-kotlin/releases
fabricLoader = "0.16.0" # https://github.com/FabricMC/fabric-loader/releases
fabricApi = "0.103.0+1.21.1" # https://github.com/FabricMC/fabric/releases
fabricKotlin = "1.12.1+kotlin.2.0.20" # https://github.com/FabricMC/fabric-language-kotlin/releases
fabricLoader = "0.16.3" # https://github.com/FabricMC/fabric-loader/releases
fabricLoom = "1.7-SNAPSHOT" # https://github.com/FabricMC/fabric-loom/releases
junit-params = "5.10.1" # check which version of junit-jupiter-api/engine kotlin-test adds
kotlin = "2.0.0"
kotlinx-kover = "0.8.3" #https://github.com/Kotlin/kotlinx-kover/releases
kotlin = "2.0.20"
kotlinx-kover = "0.8.3" # https://github.com/Kotlin/kotlinx-kover/releases
kotlinxSerialization = "1.7.1" # https://github.com/Kotlin/kotlinx.serialization/releases
minecraft = "1.21"
minecraft = "1.21.1"
minotaur = "2.8.7" # https://github.com/modrinth/minotaur/releases
spotless = "7.0.0.BETA1" # https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
spotless = "7.0.0.BETA2" # https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md

[libraries]
dokka-pluginBase = { group = "org.jetbrains.dokka", name = "dokka-base", version.ref = "dokka" }
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/dev/wanderia/netlib/NetLibEntrypoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package dev.wanderia.netlib
import dev.wanderia.netlib.payload.api.SerializedPayloadConfiguration
import kotlinx.serialization.ExperimentalSerializationApi

public const val ENTRYPOINT_NAME: String = "wanderia-netlib"

@ExperimentalSerializationApi
public fun interface NetLibEntrypoint {
public fun register(register: (List<SerializedPayloadConfiguration<*>>) -> Unit)
Expand Down
34 changes: 25 additions & 9 deletions src/main/kotlin/dev/wanderia/netlib/WanderiaNetLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import net.fabricmc.loader.api.FabricLoader
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.network.codec.StreamCodec
import org.jetbrains.annotations.ApiStatus
import org.slf4j.LoggerFactory

@ExperimentalSerializationApi
@ApiStatus.Internal
public object WanderiaNetLib : ModInitializer {

private val logger = LoggerFactory.getLogger("netlib")

@Suppress("UNCHECKED_CAST")
private fun <T : SerializedPayload<T>> register(
configuration: SerializedPayloadConfiguration<T>
Expand All @@ -32,37 +35,50 @@ public object WanderiaNetLib : ModInitializer {
PayloadTypeRegistry.configurationC2S()
.register(
configuration.payloadId,
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>,
)
PayloadChannel.ServerboundPlay ->
PayloadTypeRegistry.playC2S()
.register(
configuration.payloadId,
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>,
)
PayloadChannel.ClientboundConfiguration ->
PayloadTypeRegistry.configurationS2C()
.register(
configuration.payloadId,
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>,
)
PayloadChannel.ClientboundPlay ->
PayloadTypeRegistry.playS2C()
.register(
configuration.payloadId,
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>
configuration.payloadCodec as StreamCodec<in FriendlyByteBuf, T>,
)
}
}
}

override fun onInitialize() {
FabricLoader.getInstance()
.getEntrypoints("waderia-netlib", NetLibEntrypoint::class.java)
.forEach { entry ->
entry.register { payloads: List<SerializedPayloadConfiguration<*>> ->
payloads.forEach { payload -> register(payload) }
logger.info("[netlib] trans rights are human rights!")
val debug = System.getProperty("dev.wanderia.netlib.debug", "false") == "true"
val entrypoints =
FabricLoader.getInstance().getEntrypoints(ENTRYPOINT_NAME, NetLibEntrypoint::class.java)
if (debug) {
logger.info("[netlib] Found ${entrypoints.size} entrypoints.")
}

entrypoints.forEach { entry ->
entry.register { payloads: List<SerializedPayloadConfiguration<*>> ->
payloads.forEach { payload ->
if (debug) {
logger.info(
"[netlib] Registering ${payload::class.qualifiedName} on ${payload.channels}."
)
}
register(payload)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import net.minecraft.network.FriendlyByteBuf
@ExperimentalSerializationApi
public class PacketByteBufDecoder(
private val buf: FriendlyByteBuf,
private var elementsCount: Int = 0
private var elementsCount: Int = 0,
) : AbstractDecoder() {
private var elementIndex: Int = 0
override val serializersModule: SerializersModule = WanderiaSerializersModule()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class PacketByteBufEncoder(private val buf: FriendlyByteBuf) : AbstractEn

override fun beginCollection(
descriptor: SerialDescriptor,
collectionSize: Int
collectionSize: Int,
): CompositeEncoder {
encodeInt(collectionSize)
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public enum class PayloadChannel {
ClientboundConfiguration,

/** S2C during the play phase. */
ClientboundPlay
ClientboundPlay,
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class SerializedPayload<T : SerializedPayload<T>> : CustomPacket
StreamCodec<RegistryFriendlyByteBuf, T> =
CustomPacketPayload.codec(
{ value, buffer -> encodeTo<T>(buffer, value) },
{ buffer -> decodeFrom<T>(buffer) }
{ buffer -> decodeFrom<T>(buffer) },
)
}
}
12 changes: 10 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
},
"license": "MPL-2.0",
"environment": "*",
"entrypoints": {
"main": [
{
"adapter": "kotlin",
"value": "dev.wanderia.netlib.WanderiaNetLib"
}
]
},
"depends": {
"minecraft": ">=1.21",
"fabric-language-kotlin": ">=1.11.0+kotlin.2.0.0",
"minecraft": ">=1.21.1",
"fabric-language-kotlin": ">=1.12.1+kotlin.2.0.20",
"fabric-networking-api-v1": "*"
},
"custom": {
Expand Down
16 changes: 8 additions & 8 deletions src/test/kotlin/EncodeDecodeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EncodeDecodeTests {
val long: Long?,
val short: Short?,
val string: String?,
val enum: PayloadChannel?
val enum: PayloadChannel?,
)

@Serializable
Expand All @@ -48,7 +48,7 @@ class EncodeDecodeTests {
val longCollection: List<Long>,
val shortCollection: List<Short>,
val stringCollection: List<String>,
val enumCollection: List<PayloadChannel>
val enumCollection: List<PayloadChannel>,
)

@Serializable
Expand Down Expand Up @@ -149,7 +149,7 @@ class EncodeDecodeTests {
0.toShort(),
(-1).toShort(),
1.toShort(),
(Short.MIN_VALUE..Short.MAX_VALUE).random().toShort()
(Short.MIN_VALUE..Short.MAX_VALUE).random().toShort(),
)[index.coerceIn(0, 4)],
string = listOf(null, "", UUID.randomUUID().toString())[index.coerceIn(0, 2)],
enum =
Expand All @@ -158,7 +158,7 @@ class EncodeDecodeTests {
PayloadChannel.ClientboundPlay,
PayloadChannel.ServerboundPlay,
PayloadChannel.ClientboundConfiguration,
PayloadChannel.ServerboundConfiguration
PayloadChannel.ServerboundConfiguration,
)[index.coerceIn(0, 4)],
)

Expand All @@ -179,15 +179,15 @@ class EncodeDecodeTests {
0.toShort(),
(-1).toShort(),
1.toShort(),
(Short.MIN_VALUE..Short.MAX_VALUE).random().toShort()
(Short.MIN_VALUE..Short.MAX_VALUE).random().toShort(),
),
stringCollection = listOf("", UUID.randomUUID().toString()),
enumCollection =
listOf(
PayloadChannel.ClientboundPlay,
PayloadChannel.ServerboundPlay,
PayloadChannel.ClientboundConfiguration,
PayloadChannel.ServerboundConfiguration
PayloadChannel.ServerboundConfiguration,
),
)

Expand All @@ -202,15 +202,15 @@ class EncodeDecodeTests {
randomDataA(Random.nextInt(5)),
randomDataA(Random.nextInt(5)),
randomDataA(Random.nextInt(5)),
randomDataA(Random.nextInt(5))
randomDataA(Random.nextInt(5)),
),
collectionB =
listOf(
randomDataB(),
randomDataB(),
randomDataB(),
randomDataB(),
randomDataB()
randomDataB(),
),
)
}
Expand Down
28 changes: 2 additions & 26 deletions src/testmod/kotlin/TestMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import dev.wanderia.netlib.payload.api.PayloadChannel
import dev.wanderia.testmod.payloads.TestModPayload
import java.util.*
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
Expand Down Expand Up @@ -43,29 +39,9 @@ object TestMod : ModInitializer {
ServerConfigurationNetworking.send(handler, dummyPayload)
}
}

ClientPlayConnectionEvents.JOIN.register { _, _, _ ->
ClientPlayNetworking.registerReceiver(TestModPayload.payloadId) { payload, _ ->
println(payload.toString())
}

if (ClientPlayNetworking.canSend(TestModPayload.payloadId)) {
ClientPlayNetworking.send(dummyPayload)
}
}

ClientConfigurationConnectionEvents.START.register { _, _ ->
ClientConfigurationNetworking.registerReceiver(TestModPayload.payloadId) { payload, _ ->
println(payload.toString())
}

if (ClientConfigurationNetworking.canSend(TestModPayload.payloadId)) {
ClientConfigurationNetworking.send(dummyPayload)
}
}
}

private val dummyPayload: TestModPayload =
public val dummyPayload: TestModPayload =
TestModPayload(
testBoolean = true,
testByte = 0b0,
Expand All @@ -79,6 +55,6 @@ object TestMod : ModInitializer {
testEnum = PayloadChannel.ClientboundPlay,
testCollection = PayloadChannel.entries,
testId = TestModPayload.payloadId.id,
testUUID = UUID.randomUUID()
testUUID = UUID.randomUUID(),
)
}
40 changes: 40 additions & 0 deletions src/testmod/kotlin/TestModClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Wanderia - All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package dev.wanderia.testmod

import dev.wanderia.testmod.TestMod.dummyPayload
import dev.wanderia.testmod.payloads.TestModPayload
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking

object TestModClient : ClientModInitializer {
override fun onInitializeClient() {
ClientConfigurationConnectionEvents.START.register { _, _ ->
ClientConfigurationNetworking.registerReceiver(TestModPayload.payloadId) { payload, _ ->
println(payload.toString())
}

if (ClientConfigurationNetworking.canSend(TestModPayload.payloadId)) {
ClientConfigurationNetworking.send(dummyPayload)
}
}

ClientPlayConnectionEvents.JOIN.register { _, _, _ ->
ClientPlayNetworking.registerReceiver(TestModPayload.payloadId) { payload, _ ->
println(payload.toString())
}

if (ClientPlayNetworking.canSend(TestModPayload.payloadId)) {
ClientPlayNetworking.send(dummyPayload)
}
}
}
}
2 changes: 1 addition & 1 deletion src/testmod/kotlin/payloads/TestModPayload.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class TestModPayload(
val testEnum: PayloadChannel,
val testCollection: Collection<PayloadChannel>,
@Contextual val testId: ResourceLocation,
@Contextual val testUUID: UUID
@Contextual val testUUID: UUID,
) : SerializedPayload<TestModPayload>() {
override fun codec(): StreamCodec<RegistryFriendlyByteBuf, TestModPayload> = payloadCodec

Expand Down
6 changes: 6 additions & 0 deletions src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"value": "dev.wanderia.testmod.TestMod"
}
],
"client": [
{
"adapter": "kotlin",
"value": "dev.wanderia.testmod.TestModClient"
}
],
"wanderia-netlib": [
{
"adapter": "kotlin",
Expand Down

0 comments on commit 8b3877e

Please sign in to comment.