From b11de5422bca879f5aff88e4f913bf53d37f7342 Mon Sep 17 00:00:00 2001 From: jimchen5209 Date: Fri, 26 Apr 2024 16:06:13 +0800 Subject: [PATCH] fix: update networking to 1.20.5 --- src/main/kotlin/one/oktw/galaxy/Main.kt | 11 ++--- .../kotlin/one/oktw/galaxy/chat/Exchange.kt | 27 ++++++------ .../one/oktw/galaxy/command/commands/Join.kt | 15 ++----- .../oktw/galaxy/network/ProxyAPIPayload.kt | 43 +++++++++++++++++++ .../oktw/galaxy/network/ProxyChatPayload.kt | 43 +++++++++++++++++++ 5 files changed, 108 insertions(+), 31 deletions(-) create mode 100644 src/main/kotlin/one/oktw/galaxy/network/ProxyAPIPayload.kt create mode 100644 src/main/kotlin/one/oktw/galaxy/network/ProxyChatPayload.kt diff --git a/src/main/kotlin/one/oktw/galaxy/Main.kt b/src/main/kotlin/one/oktw/galaxy/Main.kt index ab61ee396..82e2b46d7 100644 --- a/src/main/kotlin/one/oktw/galaxy/Main.kt +++ b/src/main/kotlin/one/oktw/galaxy/Main.kt @@ -1,6 +1,6 @@ /* * OKTW Galaxy Project - * Copyright (C) 2018-2023 + * Copyright (C) 2018-2024 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -24,9 +24,9 @@ import kotlinx.coroutines.asCoroutineDispatcher import net.fabricmc.api.DedicatedServerModInitializer import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking import net.minecraft.server.dedicated.MinecraftDedicatedServer -import net.minecraft.util.Identifier import one.oktw.galaxy.block.CustomBlock import one.oktw.galaxy.block.event.AngelBlock import one.oktw.galaxy.block.event.BlockEvents @@ -40,6 +40,7 @@ import one.oktw.galaxy.event.EventManager import one.oktw.galaxy.event.type.ProxyResponseEvent import one.oktw.galaxy.item.event.CustomItemEventHandler import one.oktw.galaxy.item.event.Wrench +import one.oktw.galaxy.network.ProxyAPIPayload import one.oktw.galaxy.player.Harvest import one.oktw.galaxy.proxy.api.ProxyAPI import one.oktw.galaxy.recipe.RecipeRegistry @@ -57,7 +58,6 @@ class Main : DedicatedServerModInitializer, CoroutineScope { get() = job + server.asCoroutineDispatcher() companion object { - val PROXY_IDENTIFIER = Identifier("galaxy", "proxy") var main: Main? = null private set val selfUUID by lazy { @@ -87,8 +87,9 @@ class Main : DedicatedServerModInitializer, CoroutineScope { eventManager = EventManager(server) // Register Proxy packet receiver - ServerPlayNetworking.registerGlobalReceiver(PROXY_IDENTIFIER) { _, player, _, buf, _ -> - eventManager.emit(ProxyResponseEvent(player, ProxyAPI.decode(buf.nioBuffer()))) + PayloadTypeRegistry.playS2C().register(ProxyAPIPayload.ID, ProxyAPIPayload.CODEC) + ServerPlayNetworking.registerGlobalReceiver(ProxyAPIPayload.ID) { payload, context -> + eventManager.emit(ProxyResponseEvent(context.player(), payload.packet)) } //Events diff --git a/src/main/kotlin/one/oktw/galaxy/chat/Exchange.kt b/src/main/kotlin/one/oktw/galaxy/chat/Exchange.kt index 6545df625..cace6ef56 100644 --- a/src/main/kotlin/one/oktw/galaxy/chat/Exchange.kt +++ b/src/main/kotlin/one/oktw/galaxy/chat/Exchange.kt @@ -1,6 +1,6 @@ /* * OKTW Galaxy Project - * Copyright (C) 2018-2023 + * Copyright (C) 2018-2024 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -18,21 +18,22 @@ package one.oktw.galaxy.chat -import io.netty.buffer.Unpooled +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking.createS2CPacket -import net.minecraft.network.PacketByteBuf +import net.minecraft.registry.DynamicRegistryManager import net.minecraft.text.Text -import net.minecraft.util.Identifier import one.oktw.galaxy.Main import one.oktw.galaxy.event.annotation.EventListener import one.oktw.galaxy.event.type.PlayerChatEvent +import one.oktw.galaxy.network.ProxyChatPayload import one.oktw.galaxy.proxy.api.ProxyAPI -import one.oktw.galaxy.proxy.api.ProxyAPI.encode import one.oktw.galaxy.proxy.api.packet.MessageSend class Exchange { companion object { - val PROXY_CHAT_IDENTIFIER = Identifier("galaxy", "proxy-chat") + init { + PayloadTypeRegistry.playS2C().register(ProxyChatPayload.ID, ProxyChatPayload.CODEC) + } } @EventListener(true) @@ -43,15 +44,11 @@ class Exchange { event.player.networkHandler.sendPacket( createS2CPacket( - PROXY_CHAT_IDENTIFIER, PacketByteBuf( - Unpooled.wrappedBuffer( - encode( - MessageSend( - sender = event.player.uuid, - message = Text.Serialization.toJsonString(event.message), - targets = listOf(ProxyAPI.globalChatChannel) - ) - ) + ProxyChatPayload( + MessageSend( + sender = event.player.uuid, + message = Text.Serialization.toJsonString(event.message, DynamicRegistryManager.EMPTY), + targets = listOf(ProxyAPI.globalChatChannel) ) ) ) diff --git a/src/main/kotlin/one/oktw/galaxy/command/commands/Join.kt b/src/main/kotlin/one/oktw/galaxy/command/commands/Join.kt index ab1506481..3348b5fe8 100644 --- a/src/main/kotlin/one/oktw/galaxy/command/commands/Join.kt +++ b/src/main/kotlin/one/oktw/galaxy/command/commands/Join.kt @@ -1,6 +1,6 @@ /* * OKTW Galaxy Project - * Copyright (C) 2018-2023 + * Copyright (C) 2018-2024 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -21,21 +21,18 @@ package one.oktw.galaxy.command.commands import com.mojang.authlib.GameProfile import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.suggestion.Suggestions -import io.netty.buffer.Unpooled.wrappedBuffer import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking import net.minecraft.command.argument.GameProfileArgumentType -import net.minecraft.network.PacketByteBuf import net.minecraft.server.command.CommandManager import net.minecraft.server.command.ServerCommandSource import net.minecraft.server.network.ServerPlayerEntity import net.minecraft.text.Text -import one.oktw.galaxy.Main.Companion.PROXY_IDENTIFIER import one.oktw.galaxy.Main.Companion.main import one.oktw.galaxy.command.Command import one.oktw.galaxy.event.type.ProxyResponseEvent -import one.oktw.galaxy.proxy.api.ProxyAPI.encode +import one.oktw.galaxy.network.ProxyAPIPayload import one.oktw.galaxy.proxy.api.packet.CreateGalaxy import one.oktw.galaxy.proxy.api.packet.ProgressStage.* import one.oktw.galaxy.proxy.api.packet.SearchPlayer @@ -56,11 +53,7 @@ class Join : Command, CoroutineScope by CoroutineScope(Dispatchers.Default + Sup val future = CompletableFuture() val player = commandContext.source.playerOrThrow - ServerPlayNetworking.send( - player, - PROXY_IDENTIFIER, - PacketByteBuf(wrappedBuffer(encode(SearchPlayer(suggestionsBuilder.remaining, 10)))) - ) + ServerPlayNetworking.send(player, ProxyAPIPayload(SearchPlayer(suggestionsBuilder.remaining, 10))) val listeners = fun(event: ProxyResponseEvent) { val result = event.packet as? SearchPlayer.Result ?: return @@ -93,7 +86,7 @@ class Join : Command, CoroutineScope by CoroutineScope(Dispatchers.Default + Sup val targetPlayer = collection.first() - ServerPlayNetworking.send(sourcePlayer, PROXY_IDENTIFIER, PacketByteBuf(wrappedBuffer(encode(CreateGalaxy(targetPlayer.id))))) + ServerPlayNetworking.send(sourcePlayer, ProxyAPIPayload(CreateGalaxy(targetPlayer.id))) source.sendFeedback({ Text.of(if (sourcePlayer.gameProfile == targetPlayer) "正在加入您的星系" else "正在加入 ${targetPlayer.name} 的星系") }, false) launch { diff --git a/src/main/kotlin/one/oktw/galaxy/network/ProxyAPIPayload.kt b/src/main/kotlin/one/oktw/galaxy/network/ProxyAPIPayload.kt new file mode 100644 index 000000000..a60d1c936 --- /dev/null +++ b/src/main/kotlin/one/oktw/galaxy/network/ProxyAPIPayload.kt @@ -0,0 +1,43 @@ +/* + * OKTW Galaxy Project + * Copyright (C) 2018-2024 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package one.oktw.galaxy.network + +import io.netty.buffer.Unpooled.wrappedBuffer +import net.minecraft.network.PacketByteBuf +import net.minecraft.network.codec.PacketCodec +import net.minecraft.network.packet.CustomPayload +import net.minecraft.util.Identifier +import one.oktw.galaxy.proxy.api.ProxyAPI +import one.oktw.galaxy.proxy.api.ProxyAPI.encode +import one.oktw.galaxy.proxy.api.packet.Packet + +@JvmRecord +data class ProxyAPIPayload(val packet: Packet) : CustomPayload { + companion object { + private val PROXY_IDENTIFIER = Identifier("galaxy", "proxy") + val ID = CustomPayload.Id(PROXY_IDENTIFIER) + val CODEC: PacketCodec = PacketCodec.of( + { value, buf -> buf.writeBytes(wrappedBuffer(encode(value.packet))) }, + { buf -> ProxyAPI.decode(buf.nioBuffer()) }) + } + + override fun getId(): CustomPayload.Id { + return ID + } +} diff --git a/src/main/kotlin/one/oktw/galaxy/network/ProxyChatPayload.kt b/src/main/kotlin/one/oktw/galaxy/network/ProxyChatPayload.kt new file mode 100644 index 000000000..ec8f55ebd --- /dev/null +++ b/src/main/kotlin/one/oktw/galaxy/network/ProxyChatPayload.kt @@ -0,0 +1,43 @@ +/* + * OKTW Galaxy Project + * Copyright (C) 2018-2024 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package one.oktw.galaxy.network + +import io.netty.buffer.Unpooled.wrappedBuffer +import net.minecraft.network.PacketByteBuf +import net.minecraft.network.codec.PacketCodec +import net.minecraft.network.packet.CustomPayload +import net.minecraft.util.Identifier +import one.oktw.galaxy.proxy.api.ProxyAPI +import one.oktw.galaxy.proxy.api.ProxyAPI.encode +import one.oktw.galaxy.proxy.api.packet.Packet + +@JvmRecord +data class ProxyChatPayload(val packet: Packet) : CustomPayload { + companion object { + private val PROXY_CHAT_IDENTIFIER = Identifier("galaxy", "proxy-chat") + val ID = CustomPayload.Id(PROXY_CHAT_IDENTIFIER) + val CODEC: PacketCodec = PacketCodec.of( + { value, buf -> buf.writeBytes(wrappedBuffer(encode(value.packet))) }, + { buf -> ProxyAPI.decode(buf.nioBuffer()) }) + } + + override fun getId(): CustomPayload.Id { + return ID + } +}