Skip to content

Commit

Permalink
fix: update networking to 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Apr 26, 2024
1 parent 97cc106 commit b11de54
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 31 deletions.
11 changes: 6 additions & 5 deletions src/main/kotlin/one/oktw/galaxy/Main.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
27 changes: 12 additions & 15 deletions src/main/kotlin/one/oktw/galaxy/chat/Exchange.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
)
)
)
Expand Down
15 changes: 4 additions & 11 deletions src/main/kotlin/one/oktw/galaxy/command/commands/Join.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -56,11 +53,7 @@ class Join : Command, CoroutineScope by CoroutineScope(Dispatchers.Default + Sup
val future = CompletableFuture<Suggestions>()
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
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/network/ProxyAPIPayload.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<ProxyAPIPayload>(PROXY_IDENTIFIER)
val CODEC: PacketCodec<PacketByteBuf, ProxyAPIPayload> = PacketCodec.of(
{ value, buf -> buf.writeBytes(wrappedBuffer(encode(value.packet))) },
{ buf -> ProxyAPI.decode(buf.nioBuffer()) })
}

override fun getId(): CustomPayload.Id<out CustomPayload> {
return ID
}
}
43 changes: 43 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/network/ProxyChatPayload.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<ProxyChatPayload>(PROXY_CHAT_IDENTIFIER)
val CODEC: PacketCodec<PacketByteBuf, ProxyChatPayload> = PacketCodec.of(
{ value, buf -> buf.writeBytes(wrappedBuffer(encode(value.packet))) },
{ buf -> ProxyAPI.decode(buf.nioBuffer()) })
}

override fun getId(): CustomPayload.Id<out CustomPayload> {
return ID
}
}

0 comments on commit b11de54

Please sign in to comment.