aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/apis
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-08-12 22:15:07 +0200
committerLinnea Gräf <nea@nea.moe>2024-08-12 22:15:07 +0200
commitb8a45b9a0438a12ba3c609f6e416d519829471be (patch)
treeaff190bb953d94f1187896b2c245644a29e6badf /src/main/kotlin/moe/nea/firmament/apis
parent3c7e6b6177de6ef3cff8a46bb1726466a299cdde (diff)
downloadFirmament-b8a45b9a0438a12ba3c609f6e416d519829471be.tar.gz
Firmament-b8a45b9a0438a12ba3c609f6e416d519829471be.tar.bz2
Firmament-b8a45b9a0438a12ba3c609f6e416d519829471be.zip
Improve location and profile detection
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/apis')
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/ingame/FirmamentCustomPayload.kt31
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt51
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt48
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/ingame/JoinedCustomPayload.kt20
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/ingame/packets/PartyInfoRequest.kt134
5 files changed, 0 insertions, 284 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/FirmamentCustomPayload.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/FirmamentCustomPayload.kt
deleted file mode 100644
index 10af7d0..0000000
--- a/src/main/kotlin/moe/nea/firmament/apis/ingame/FirmamentCustomPayload.kt
+++ /dev/null
@@ -1,31 +0,0 @@
-
-package moe.nea.firmament.apis.ingame
-
-import io.netty.buffer.ByteBuf
-import net.minecraft.network.codec.PacketCodec
-import net.minecraft.network.packet.CustomPayload
-import net.minecraft.util.Identifier
-
-interface FirmamentCustomPayload : CustomPayload {
-
- class Unhandled private constructor(val identifier: Identifier) : FirmamentCustomPayload {
- override fun getId(): CustomPayload.Id<out CustomPayload> {
- return CustomPayload.id(identifier.toString())
- }
-
- companion object {
- fun <B : ByteBuf> createCodec(identifier: Identifier): PacketCodec<B, Unhandled> {
- return object : PacketCodec<B, Unhandled> {
- override fun decode(buf: B): Unhandled {
- return Unhandled(identifier)
- }
-
- override fun encode(buf: B, value: Unhandled) {
- // we will never send an unhandled packet stealthy
- }
- }
- }
- }
-
- }
-}
diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt
deleted file mode 100644
index fb01a2f..0000000
--- a/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt
+++ /dev/null
@@ -1,51 +0,0 @@
-
-package moe.nea.firmament.apis.ingame
-
-import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket
-import net.minecraft.text.Text
-import moe.nea.firmament.annotations.Subscribe
-import moe.nea.firmament.apis.ingame.packets.PartyInfoRequest
-import moe.nea.firmament.apis.ingame.packets.PartyInfoResponse
-import moe.nea.firmament.commands.thenExecute
-import moe.nea.firmament.events.CommandEvent
-import moe.nea.firmament.events.FirmamentCustomPayloadEvent
-import moe.nea.firmament.events.subscription.SubscriptionOwner
-import moe.nea.firmament.features.FirmamentFeature
-import moe.nea.firmament.features.debug.DeveloperFeatures
-import moe.nea.firmament.util.MC
-
-
-object HypixelModAPI : SubscriptionOwner {
- init {
- InGameCodecWrapper.Direction.C2S.customCodec =
- InGameCodecWrapper.createStealthyCodec(
- PartyInfoRequest.intoType()
- )
- InGameCodecWrapper.Direction.S2C.customCodec =
- InGameCodecWrapper.createStealthyCodec(
- PartyInfoResponse.intoType()
- )
- }
-
- @JvmStatic
- fun sendRequest(packet: FirmamentCustomPayload) {
- MC.networkHandler?.sendPacket(CustomPayloadC2SPacket(packet))
- }
-
- @Subscribe
- fun testCommand(event: CommandEvent.SubCommand) {
- event.subcommand("sendpartyrequest") {
- thenExecute {
- sendRequest(PartyInfoRequest(1))
- }
- }
- }
-
- @Subscribe
- fun logEvents(event: FirmamentCustomPayloadEvent) {
- MC.sendChat(Text.stringifiedTranslatable("firmament.modapi.event", event.toString()))
- }
-
- override val delegateFeature: FirmamentFeature
- get() = DeveloperFeatures
-}
diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt
deleted file mode 100644
index 1a4710f..0000000
--- a/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt
+++ /dev/null
@@ -1,48 +0,0 @@
-
-package moe.nea.firmament.apis.ingame
-
-import net.minecraft.network.PacketByteBuf
-import net.minecraft.network.codec.PacketCodec
-import net.minecraft.network.packet.CustomPayload
-
-class InGameCodecWrapper(
- val wrapped: PacketCodec<PacketByteBuf, CustomPayload>,
- val direction: Direction,
-) : PacketCodec<PacketByteBuf, CustomPayload> {
- enum class Direction {
- S2C,
- C2S,
- ;
-
- var customCodec: PacketCodec<PacketByteBuf, FirmamentCustomPayload> = createStealthyCodec()
- }
-
- companion object {
- fun createStealthyCodec(vararg codecs: CustomPayload.Type<PacketByteBuf, out FirmamentCustomPayload>): PacketCodec<PacketByteBuf, FirmamentCustomPayload> {
- return CustomPayload.createCodec(
- { FirmamentCustomPayload.Unhandled.createCodec(it) },
- codecs.toList()
- ) as PacketCodec<PacketByteBuf, FirmamentCustomPayload>
- }
-
- }
-
- override fun decode(buf: PacketByteBuf): CustomPayload {
- val duplicateBuffer = PacketByteBuf(buf.slice())
- val original = wrapped.decode(buf)
- buf.skipBytes(buf.readableBytes())
- val duplicate = runCatching { direction.customCodec.decode(duplicateBuffer) }
- .getOrNull()
- if (duplicate is FirmamentCustomPayload.Unhandled || duplicate == null)
- return original
- return JoinedCustomPayload(original, duplicate)
- }
-
- override fun encode(buf: PacketByteBuf, value: CustomPayload) {
- if (value is FirmamentCustomPayload) {
- direction.customCodec.encode(buf, value)
- } else {
- wrapped.encode(buf, value)
- }
- }
-}
diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/JoinedCustomPayload.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/JoinedCustomPayload.kt
deleted file mode 100644
index 4b6a6bc..0000000
--- a/src/main/kotlin/moe/nea/firmament/apis/ingame/JoinedCustomPayload.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-
-package moe.nea.firmament.apis.ingame
-
-import net.minecraft.network.packet.CustomPayload
-
-/**
- * A class to smuggle two parsed instances of the same custom payload packet.
- */
-class JoinedCustomPayload(
- val original: CustomPayload,
- val smuggled: FirmamentCustomPayload
-) : CustomPayload {
- companion object {
- val joinedId = CustomPayload.id<JoinedCustomPayload>("firmament:joined")
- }
-
- override fun getId(): CustomPayload.Id<out JoinedCustomPayload> {
- return joinedId
- }
-}
diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/packets/PartyInfoRequest.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/packets/PartyInfoRequest.kt
deleted file mode 100644
index a1b4a71..0000000
--- a/src/main/kotlin/moe/nea/firmament/apis/ingame/packets/PartyInfoRequest.kt
+++ /dev/null
@@ -1,134 +0,0 @@
-
-package moe.nea.firmament.apis.ingame.packets
-
-import io.netty.buffer.ByteBuf
-import java.util.UUID
-import net.minecraft.network.PacketByteBuf
-import net.minecraft.network.codec.PacketCodec
-import net.minecraft.network.codec.PacketCodecs
-import net.minecraft.network.packet.CustomPayload
-import net.minecraft.util.Identifier
-import net.minecraft.util.Uuids
-import moe.nea.firmament.apis.ingame.FirmamentCustomPayload
-
-interface FirmamentCustomPayloadMeta<T : FirmamentCustomPayload> {
- val ID: CustomPayload.Id<T>
- val CODEC: PacketCodec<PacketByteBuf, T>
-
- fun id(name: String): CustomPayload.Id<T> {
- return CustomPayload.Id<T>(Identifier.of(name))
- }
-
- fun intoType(): CustomPayload.Type<PacketByteBuf, T> {
- return CustomPayload.Type(ID, CODEC)
- }
-}
-
-data class PartyInfoRequest(val version: Int) : FirmamentCustomPayload {
- companion object : FirmamentCustomPayloadMeta<PartyInfoRequest> {
- override val ID = id("hypixel:party_info")
- override val CODEC =
- PacketCodecs.VAR_INT.cast<PacketByteBuf>()
- .xmap(::PartyInfoRequest, PartyInfoRequest::version)
- }
-
- override fun getId(): CustomPayload.Id<out CustomPayload> {
- return ID
- }
-}
-
-sealed interface PartyInfoResponseV
-sealed interface HypixelVersionedPacketData<out T>
-data class HypixelSuccessfulResponse<T>(val data: T) : HypixelVersionedPacketData<T>
-data class HypixelUnknownVersion(val version: Int) : HypixelVersionedPacketData<Nothing>
-data class HypixelApiError(val label: String, val errorId: Int) : HypixelVersionedPacketData<Nothing> {
- companion object {
- fun <B : ByteBuf> createCodec(label: String): PacketCodec<B, HypixelApiError> {
- return PacketCodecs.VAR_INT
- .cast<B>()
- .xmap({ HypixelApiError(label, it) }, HypixelApiError::errorId)
- }
- }
-}
-
-object CodecUtils {
- fun <B : PacketByteBuf, T> dispatchVersioned(
- versions: Map<Int, PacketCodec<B, out T>>,
- errorCodec: PacketCodec<B, HypixelApiError>
- ): PacketCodec<B, HypixelVersionedPacketData<T>> {
- return object : PacketCodec<B, HypixelVersionedPacketData<T>> {
- override fun decode(buf: B): HypixelVersionedPacketData<T> {
- if (!buf.readBoolean()) {
- return errorCodec.decode(buf)
- }
- val version = buf.readVarInt()
- val versionCodec = versions[version]
- ?: return HypixelUnknownVersion(version)
- return HypixelSuccessfulResponse(versionCodec.decode(buf))
- }
-
- override fun encode(buf: B, value: HypixelVersionedPacketData<T>?) {
- error("Cannot encode a hypixel packet")
- }
- }
- }
-
- fun <B : PacketByteBuf, T> dispatchS2CBoolean(
- ifTrue: PacketCodec<B, out T>,
- ifFalse: PacketCodec<B, out T>
- ): PacketCodec<B, T> {
- return object : PacketCodec<B, T> {
- override fun decode(buf: B): T {
- return if (buf.readBoolean()) {
- ifTrue.decode(buf)
- } else {
- ifFalse.decode(buf)
- }
- }
-
- override fun encode(buf: B, value: T) {
- error("Cannot reverse dispatch boolean")
- }
- }
- }
-
-}
-
-
-data object PartyInfoResponseVUnknown : PartyInfoResponseV
-data class PartyInfoResponseV1(
- val leader: UUID?,
- val members: Set<UUID>,
-) : PartyInfoResponseV {
- data object PartyMember
- companion object {
- val CODEC: PacketCodec<PacketByteBuf, PartyInfoResponseV1> =
- CodecUtils.dispatchS2CBoolean(
- PacketCodec.tuple(
- Uuids.PACKET_CODEC, PartyInfoResponseV1::leader,
- Uuids.PACKET_CODEC.collect(PacketCodecs.toCollection(::HashSet)), PartyInfoResponseV1::members,
- ::PartyInfoResponseV1
- ),
- PacketCodec.unit(PartyInfoResponseV1(null, setOf())))
- }
-}
-
-
-data class PartyInfoResponse(val data: HypixelVersionedPacketData<PartyInfoResponseV>) : FirmamentCustomPayload {
- companion object : FirmamentCustomPayloadMeta<PartyInfoResponse> {
- override val ID = id("hypixel:party_info")
- override val CODEC =
- CodecUtils
- .dispatchVersioned<PacketByteBuf, PartyInfoResponseV>(
- mapOf(
- 1 to PartyInfoResponseV1.CODEC,
- ),
- HypixelApiError.createCodec("PartyInfoResponse"))
- .xmap(::PartyInfoResponse, PartyInfoResponse::data)
-
- }
-
- override fun getId(): CustomPayload.Id<out CustomPayload> {
- return ID
- }
-}