Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 2e5a287

Browse files
committed
fix: update AFK check to use isAfk() method for player sessions
1 parent ebe697a commit 2e5a287

File tree

81 files changed

+1526
-1167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1526
-1167
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import java.nio.file.Files
2+
import java.nio.file.StandardOpenOption
3+
4+
val streamCodecPackage = "dev.slne.surf.cloud.api.common.netty.network.codec"
5+
val compositeStartFrom = 2
6+
val compositeMaxArity = 20
7+
8+
val generatedDir = layout.buildDirectory.dir("generated/sources/streamcodec")
9+
10+
plugins {
11+
java
12+
}
13+
14+
tasks.register("generateStreamCodecComposites") {
15+
group = "generate"
16+
17+
outputs.dir(generatedDir)
18+
inputs.property("startFrom", compositeStartFrom)
19+
inputs.property("maxArity", compositeMaxArity)
20+
21+
doLast {
22+
require(compositeMaxArity >= compositeStartFrom) { "maxArity ($compositeMaxArity) must be >= startFrom ($compositeStartFrom)" }
23+
val file = generatedDir.get().file("StreamCodecComposites.kt").asFile
24+
file.parentFile.mkdirs()
25+
val content = buildString {
26+
appendLine("// GENERATED FILE — do not edit manually")
27+
appendLine("package $streamCodecPackage")
28+
appendLine()
29+
appendLine("import java.util.function.Function")
30+
appendLine()
31+
appendLine("@Suppress(\"UNCHECKED_CAST\", \"RedundantVisibilityModifier\")")
32+
appendLine("private object _CompositeGenSupport {")
33+
appendLine(" @JvmStatic inline fun <B, T> dec(c: StreamCodec<in B, T>, buf: B): T = (c as StreamCodec<B, T>).decode(buf)")
34+
appendLine(" @JvmStatic inline fun <B, T> enc(c: StreamCodec<in B, T>, buf: B, v: T) = (c as StreamCodec<B, T>).encode(buf, v)")
35+
appendLine("}")
36+
appendLine()
37+
38+
39+
fun genArity(n: Int) {
40+
val typeParams = (1..n).joinToString(", ") { "T$it" }
41+
val params = buildString {
42+
for (i in 1..n) {
43+
appendLine(" codec$i: StreamCodec<in B, T$i>,")
44+
appendLine(" from$i: Function<C, T$i>,")
45+
}
46+
append(" to: (")
47+
append((1..n).joinToString(", ") { "T$it" })
48+
appendLine(") -> C")
49+
}
50+
val decodeVars = (1..n).joinToString("\n") { i ->
51+
" val o$i: T$i = _CompositeGenSupport.dec(codec$i, buf)"
52+
}
53+
val encodeLines = (1..n).joinToString("\n") { i ->
54+
" _CompositeGenSupport.enc(codec$i, buf, from$i.apply(value))"
55+
}
56+
val toArgs = (1..n).joinToString(", ") { "o$it" }
57+
58+
appendLine("public fun <B, C, $typeParams> StreamCodec.Companion.composite(")
59+
appendLine(params)
60+
appendLine("): StreamCodec<B, C> {")
61+
appendLine(" return object : StreamCodec<B, C> {")
62+
appendLine(" override fun decode(buf: B): C {")
63+
appendLine(decodeVars)
64+
appendLine(" return to($toArgs)")
65+
appendLine(" }")
66+
appendLine()
67+
appendLine(" override fun encode(buf: B, value: C) {")
68+
appendLine(encodeLines)
69+
appendLine(" }")
70+
appendLine(" }")
71+
appendLine("}")
72+
appendLine()
73+
}
74+
75+
for (n in compositeStartFrom..compositeMaxArity) genArity(n)
76+
77+
}
78+
Files.write(
79+
file.toPath(),
80+
content.toByteArray(),
81+
StandardOpenOption.CREATE,
82+
StandardOpenOption.TRUNCATE_EXISTING,
83+
StandardOpenOption.WRITE
84+
)
85+
86+
println("Generated ${file.relativeTo(project.projectDir)} with composite${compositeStartFrom}..$compositeMaxArity")
87+
}
88+
}
89+
sourceSets {
90+
named("main") {
91+
java.srcDir(generatedDir)
92+
}
93+
}
94+
95+
tasks.named("compileKotlin") {
96+
dependsOn("generateStreamCodecComposites")
97+
}

surf-cloud-api/surf-cloud-api-common/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import dev.slne.surf.surfapi.gradle.util.slneReleases
2+
import gradle.kotlin.dsl.accessors._2c95f20277cbe6143532f6e8d67e36cc.processResources
3+
import org.gradle.internal.impldep.org.junit.experimental.categories.Categories.CategoryFilter.exclude
4+
import org.jetbrains.kotlin.gradle.idea.proto.com.google.protobuf.api
5+
import org.jetbrains.kotlin.gradle.internal.config.AnalysisFlags.optIn
26

37
plugins {
48
`exclude-kotlin`
59
id("dev.slne.surf.surfapi.gradle.core")
10+
`generate-stream-codec`
611
}
712

813
dependencies {
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package dev.slne.surf.cloud.api.common.netty.network.codec
2+
3+
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.*
4+
import dev.slne.surf.cloud.api.common.netty.protocol.buffer.types.Utf8String
5+
import dev.slne.surf.cloud.api.common.util.createUnresolvedInetSocketAddress
6+
import dev.slne.surf.surfapi.core.api.messages.adventure.key
7+
import io.netty.buffer.ByteBuf
8+
import net.kyori.adventure.key.Key
9+
import net.kyori.adventure.nbt.BinaryTagIO
10+
import net.kyori.adventure.sound.Sound
11+
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
12+
import java.io.ByteArrayInputStream
13+
import java.io.ByteArrayOutputStream
14+
import java.math.BigDecimal
15+
import java.math.BigInteger
16+
import java.math.MathContext
17+
import java.net.Inet4Address
18+
import java.net.InetSocketAddress
19+
import java.net.URI
20+
import java.time.Instant
21+
import java.time.ZoneId
22+
import java.time.ZonedDateTime
23+
import java.util.*
24+
import kotlin.time.Duration
25+
import kotlin.time.Duration.Companion.milliseconds
26+
27+
object ByteBufCodecs {
28+
val BOOLEAN_CODEC = streamCodec(ByteBuf::writeBoolean, ByteBuf::readBoolean)
29+
val BYTE_CODEC = streamCodec({ buf, byte -> buf.writeByte(byte.toInt()) }, ByteBuf::readByte)
30+
val SHORT_CODEC =
31+
streamCodec({ buf, short -> buf.writeShort(short.toInt()) }, ByteBuf::readShort)
32+
33+
val INT_CODEC = streamCodec(ByteBuf::writeInt, ByteBuf::readInt)
34+
val VAR_INT_CODEC = streamCodec(ByteBuf::writeVarInt, ByteBuf::readVarInt)
35+
val FLOAT_CODEC = streamCodec(ByteBuf::writeFloat, ByteBuf::readFloat)
36+
val DOUBLE_CODEC = streamCodec(ByteBuf::writeDouble, ByteBuf::readDouble)
37+
val LONG_CODEC = streamCodec(ByteBuf::writeLong, ByteBuf::readLong)
38+
val VAR_LONG_CODEC = streamCodec(ByteBuf::writeVarLong, ByteBuf::readVarLong)
39+
val STRING_CODEC = Utf8String.STREAM_CODEC
40+
41+
42+
val UUID_CODEC = streamCodec(ByteBuf::writeUuid, ByteBuf::readUuid)
43+
val BYTE_ARRAY_CODEC = streamCodec(ByteBuf::writeByteArray, ByteBuf::readByteArray)
44+
45+
val OPTIONAL_LONG_CODEC = streamCodec<ByteBuf, OptionalLong>({ buf, optionalLong ->
46+
BOOLEAN_CODEC.encode(buf, optionalLong.isPresent)
47+
optionalLong.ifPresent { LONG_CODEC.encode(buf, it) }
48+
}) { buf ->
49+
if (BOOLEAN_CODEC.decode(buf)) {
50+
OptionalLong.of(LONG_CODEC.decode(buf))
51+
} else {
52+
OptionalLong.empty()
53+
}
54+
}
55+
56+
val KEY_CODEC = streamCodecComposite(STRING_CODEC, Key::asString, Key::key)
57+
val SOUND_CODEC = streamCodecComposite(
58+
KEY_CODEC,
59+
Sound::name,
60+
enumStreamCodec<Sound.Source>(),
61+
Sound::source,
62+
FLOAT_CODEC,
63+
Sound::volume,
64+
FLOAT_CODEC,
65+
Sound::pitch,
66+
OPTIONAL_LONG_CODEC,
67+
Sound::seed
68+
) { type, source, volume, pitch, seed ->
69+
Sound.sound()
70+
.type(type)
71+
.source(source)
72+
.volume(volume)
73+
.pitch(pitch)
74+
.seed(seed)
75+
.build()
76+
}
77+
78+
val COMPONENT_CODEC = streamCodecComposite(
79+
STRING_CODEC,
80+
{ GsonComponentSerializer.gson().serialize(it.compact()) },
81+
{ GsonComponentSerializer.gson().deserialize(it) }
82+
)
83+
84+
val COMPOUND_TAG_CODEC = streamCodecComposite(BYTE_ARRAY_CODEC, { tag ->
85+
ByteArrayOutputStream().use { out ->
86+
BinaryTagIO.writer().write(tag, out, BinaryTagIO.Compression.GZIP)
87+
out.toByteArray()
88+
}
89+
}, { bytes ->
90+
ByteArrayInputStream(bytes).use { input ->
91+
BinaryTagIO.unlimitedReader().read(input, BinaryTagIO.Compression.GZIP)
92+
}
93+
})
94+
95+
val URI_CODEC = streamCodecComposite(STRING_CODEC, URI::toString, URI::create)
96+
val INET_SOCKET_ADDRESS_CODEC = streamCodecComposite(
97+
STRING_CODEC,
98+
InetSocketAddress::getHostString,
99+
INT_CODEC,
100+
InetSocketAddress::getPort,
101+
::createUnresolvedInetSocketAddress
102+
)
103+
val INET_4_ADDRESS_CODEC = streamCodecComposite(
104+
BYTE_ARRAY_CODEC,
105+
Inet4Address::getAddress
106+
) { Inet4Address.getByAddress(it) as Inet4Address }
107+
108+
val ZONED_DATE_TIME_CODEC = streamCodecComposite(
109+
LONG_CODEC,
110+
{ it.toInstant().toEpochMilli() },
111+
STRING_CODEC,
112+
{ it.zone.id },
113+
{ epoch, zoneId -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.of(zoneId)) }
114+
)
115+
116+
val DURATION_CODEC = streamCodecComposite(
117+
LONG_CODEC,
118+
Duration::inWholeMilliseconds
119+
) { it.milliseconds }
120+
121+
val BIG_INTEGER_CODEC =
122+
streamCodecComposite(BYTE_ARRAY_CODEC, BigInteger::toByteArray, ::BigInteger)
123+
val BIG_DECIMAL_CODEC = streamCodecComposite(
124+
BIG_INTEGER_CODEC,
125+
BigDecimal::unscaledValue,
126+
VAR_INT_CODEC,
127+
BigDecimal::scale,
128+
VAR_INT_CODEC,
129+
BigDecimal::precision
130+
) { unscaledValue, scale, precision ->
131+
BigDecimal(unscaledValue, scale, MathContext(precision))
132+
}
133+
134+
135+
fun <E : Enum<E>> enumStreamCodec(clazz: Class<E>) =
136+
streamCodec(ByteBuf::writeEnum) { it.readEnum(clazz) }
137+
138+
inline fun <reified E : Enum<E>> enumStreamCodec() =
139+
streamCodec(ByteBuf::writeEnum) { it.readEnum<E>() }
140+
141+
private fun test() {
142+
Sound.sound(key(""), Sound.Source.MASTER, 1f, 1f)
143+
}
144+
}

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/network/codec/StreamCodec.kt

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package dev.slne.surf.cloud.api.common.netty.network.codec
22

3-
import com.mojang.datafixers.util.Function3
4-
import com.mojang.datafixers.util.Function4
5-
import com.mojang.datafixers.util.Function5
6-
import com.mojang.datafixers.util.Function6
73
import io.netty.buffer.ByteBuf
84
import java.util.function.BiFunction
95
import java.util.function.Function
@@ -94,126 +90,6 @@ interface StreamCodec<B, V> : StreamDecoder<B, V>, StreamEncoder<B, V> {
9490
}
9591
}
9692

97-
98-
fun <B, C, T1, T2, T3> composite(
99-
codec1: StreamCodec<in B, T1>,
100-
from1: Function<C, T1>,
101-
codec2: StreamCodec<in B, T2>,
102-
from2: Function<C, T2>,
103-
codec3: StreamCodec<in B, T3>,
104-
from3: Function<C, T3>,
105-
to: Function3<T1, T2, T3, C>
106-
): StreamCodec<B, C> = object : StreamCodec<B, C> {
107-
override fun decode(buf: B): C {
108-
return to.apply(codec1.decode(buf), codec2.decode(buf), codec3.decode(buf))
109-
}
110-
111-
override fun encode(buf: B, value: C) {
112-
codec1.encode(buf, from1.apply(value))
113-
codec2.encode(buf, from2.apply(value))
114-
codec3.encode(buf, from3.apply(value))
115-
}
116-
}
117-
118-
fun <B, C, T1, T2, T3, T4> composite(
119-
codec1: StreamCodec<in B, T1>,
120-
from1: Function<C, T1>,
121-
codec2: StreamCodec<in B, T2>,
122-
from2: Function<C, T2>,
123-
codec3: StreamCodec<in B, T3>,
124-
from3: Function<C, T3>,
125-
codec4: StreamCodec<in B, T4>,
126-
from4: Function<C, T4>,
127-
to: Function4<T1, T2, T3, T4, C>
128-
): StreamCodec<B, C> {
129-
return object : StreamCodec<B, C> {
130-
override fun decode(buf: B): C {
131-
val `object`: T1 = codec1.decode(buf)
132-
val object2: T2 = codec2.decode(buf)
133-
val object3: T3 = codec3.decode(buf)
134-
val object4: T4 = codec4.decode(buf)
135-
return to.apply(`object`, object2, object3, object4)
136-
}
137-
138-
override fun encode(buf: B, value: C) {
139-
codec1.encode(buf, from1.apply(value))
140-
codec2.encode(buf, from2.apply(value))
141-
codec3.encode(buf, from3.apply(value))
142-
codec4.encode(buf, from4.apply(value))
143-
}
144-
}
145-
}
146-
147-
fun <B, C, T1, T2, T3, T4, T5> composite(
148-
codec1: StreamCodec<in B, T1>,
149-
from1: Function<C, T1>,
150-
codec2: StreamCodec<in B, T2>,
151-
from2: Function<C, T2>,
152-
codec3: StreamCodec<in B, T3>,
153-
from3: Function<C, T3>,
154-
codec4: StreamCodec<in B, T4>,
155-
from4: Function<C, T4>,
156-
codec5: StreamCodec<in B, T5>,
157-
from5: Function<C, T5>,
158-
to: Function5<T1, T2, T3, T4, T5, C>
159-
): StreamCodec<B, C> {
160-
return object : StreamCodec<B, C> {
161-
override fun decode(buf: B): C {
162-
val `object`: T1 = codec1.decode(buf)
163-
val object2: T2 = codec2.decode(buf)
164-
val object3: T3 = codec3.decode(buf)
165-
val object4: T4 = codec4.decode(buf)
166-
val object5: T5 = codec5.decode(buf)
167-
return to.apply(`object`, object2, object3, object4, object5)
168-
}
169-
170-
override fun encode(buf: B, value: C) {
171-
codec1.encode(buf, from1.apply(value))
172-
codec2.encode(buf, from2.apply(value))
173-
codec3.encode(buf, from3.apply(value))
174-
codec4.encode(buf, from4.apply(value))
175-
codec5.encode(buf, from5.apply(value))
176-
}
177-
}
178-
}
179-
180-
fun <B, C, T1, T2, T3, T4, T5, T6> composite(
181-
codec1: StreamCodec<in B, T1>,
182-
from1: Function<C, T1>,
183-
codec2: StreamCodec<in B, T2>,
184-
from2: Function<C, T2>,
185-
codec3: StreamCodec<in B, T3>,
186-
from3: Function<C, T3>,
187-
codec4: StreamCodec<in B, T4>,
188-
from4: Function<C, T4>,
189-
codec5: StreamCodec<in B, T5>,
190-
from5: Function<C, T5>,
191-
codec6: StreamCodec<in B, T6>,
192-
from6: Function<C, T6>,
193-
to: Function6<T1, T2, T3, T4, T5, T6, C>
194-
): StreamCodec<B, C> {
195-
return object : StreamCodec<B, C> {
196-
override fun decode(buf: B): C {
197-
val `object`: T1 = codec1.decode(buf)
198-
val object2: T2 = codec2.decode(buf)
199-
val object3: T3 = codec3.decode(buf)
200-
val object4: T4 = codec4.decode(buf)
201-
val object5: T5 = codec5.decode(buf)
202-
val object6: T6 = codec6.decode(buf)
203-
return to.apply(`object`, object2, object3, object4, object5, object6)
204-
}
205-
206-
override fun encode(buf: B, value: C) {
207-
codec1.encode(buf, from1.apply(value))
208-
codec2.encode(buf, from2.apply(value))
209-
codec3.encode(buf, from3.apply(value))
210-
codec4.encode(buf, from4.apply(value))
211-
codec5.encode(buf, from5.apply(value))
212-
codec6.encode(buf, from6.apply(value))
213-
}
214-
}
215-
}
216-
21793
fun <B, T> recursive(codecGetter: UnaryOperator<StreamCodec<B, T>>) =
21894
object : StreamCodec<B, T> {
21995
private val inner by lazy { codecGetter.apply(this) }

0 commit comments

Comments
 (0)