From a6473c372287c2e4644f8e6774a446eba7dcfe8b Mon Sep 17 00:00:00 2001 From: OroArmor Date: Mon, 2 Sep 2024 03:08:44 -0700 Subject: [PATCH] Add core:networking --- fabric-networking-api-v1/build.gradle | 4 + .../v1/C2SConfigurationChannelEvents.java | 31 +- .../networking/v1/C2SPlayChannelEvents.java | 30 +- .../ClientConfigurationConnectionEvents.java | 56 ++- .../v1/ClientConfigurationNetworking.java | 115 +++-- .../v1/ClientLoginConnectionEvents.java | 33 +- .../networking/v1/ClientLoginNetworking.java | 53 ++- .../v1/ClientPlayConnectionEvents.java | 34 +- .../networking/v1/ClientPlayNetworking.java | 111 +++-- .../client/networking/v1/package-info.java | 5 +- .../client/ClientCommonNetworkAddon.java | 80 ---- .../ClientConfigurationNetworkAddon.java | 137 ------ .../client/ClientLoginNetworkAddon.java | 115 ----- .../client/ClientNetworkingImpl.java | 181 -------- .../client/ClientPlayNetworkAddon.java | 109 ----- .../ClientCommonNetworkHandlerMixin.java | 51 --- ...lientConfigurationNetworkHandlerMixin.java | 63 --- .../ClientLoginNetworkHandlerMixin.java | 71 --- .../client/ClientPlayNetworkHandlerMixin.java | 63 --- ...entConfigurationNetworkAddonAccessor.java} | 10 +- .../ClientLoginNetworkHandlerAccessor.java | 29 -- ...va => ClientPlayNetworkAddonAccessor.java} | 12 +- .../accessor/MinecraftClientAccessor.java | 31 -- ...abric-networking-api-v1.client.mixins.json | 11 +- .../networking/v1/EntityTrackingEvents.java | 24 +- ...bricServerConfigurationNetworkHandler.java | 6 +- .../api/networking/v1/PacketByteBufs.java | 57 +-- .../networking/v1/PayloadTypeRegistry.java | 14 +- .../api/networking/v1/PlayerLookup.java | 78 +--- .../v1/S2CConfigurationChannelEvents.java | 25 +- .../networking/v1/S2CPlayChannelEvents.java | 25 +- .../ServerConfigurationConnectionEvents.java | 36 +- .../v1/ServerConfigurationNetworking.java | 82 ++-- .../v1/ServerLoginConnectionEvents.java | 34 +- .../networking/v1/ServerLoginNetworking.java | 47 +- .../v1/ServerPlayConnectionEvents.java | 34 +- .../networking/v1/ServerPlayNetworking.java | 94 ++-- .../api/networking/v1/package-info.java | 4 + .../AbstractChanneledNetworkAddon.java | 238 ---------- .../impl/networking/AbstractNetworkAddon.java | 175 -------- .../impl/networking/ChannelInfoHolder.java | 29 -- .../impl/networking/CommonPacketHandler.java | 27 -- .../impl/networking/CommonPacketsImpl.java | 150 ------- .../networking/CommonRegisterPayload.java | 52 --- .../impl/networking/CommonVersionPayload.java | 40 -- .../networking/CustomPayloadTypeProvider.java | 25 -- .../Fabric2QuiltLoginPacketSender.java | 52 +++ .../networking/Fabric2QuiltPacketSender.java | 46 ++ .../FabricCustomPayloadPacketCodec.java | 23 - .../networking/GlobalReceiverRegistry.java | 229 ---------- .../networking/NetworkHandlerExtensions.java | 21 - .../impl/networking/NetworkingImpl.java | 54 --- .../networking/PacketCallbackListener.java | 28 -- .../networking/PayloadTypeRegistryImpl.java | 83 ---- .../Quilt2FabricLoginPacketSender.java | 67 +++ .../networking/Quilt2FabricPacketSender.java | 65 +++ .../fabric/impl/networking/QuiltUtil.java | 96 +++++ .../impl/networking/RegistrationPayload.java | 94 ---- ...PacketByteBufLoginQueryRequestPayload.java | 28 -- .../PacketByteBufLoginQueryResponse.java | 27 -- .../networking/payload/PayloadHelper.java | 44 -- .../networking/server/QueryIdFactory.java | 38 -- .../ServerConfigurationNetworkAddon.java | 190 -------- .../server/ServerLoginNetworkAddon.java | 207 --------- .../server/ServerNetworkingImpl.java | 61 --- .../server/ServerPlayNetworkAddon.java | 144 ------- .../networking/ClientConnectionMixin.java | 90 ---- .../mixin/networking/CommandManagerMixin.java | 55 --- .../CustomPayloadC2SPacketMixin.java | 57 --- .../CustomPayloadPacketCodecMixin.java | 63 --- .../CustomPayloadS2CPacketMixin.java | 66 --- .../networking/EntityTrackerEntryMixin.java | 47 -- .../LoginQueryRequestS2CPacketMixin.java | 44 -- .../LoginQueryResponseC2SPacketMixin.java | 50 --- .../PacketCodecDispatcherMixin.java | 50 --- .../mixin/networking/PlayerManagerMixin.java | 37 -- .../ServerCommonNetworkHandlerMixin.java | 58 --- ...erverConfigurationNetworkHandlerMixin.java | 141 +----- .../ServerLoginNetworkHandlerMixin.java | 90 ---- .../ServerPlayNetworkHandlerMixin.java | 67 --- .../accessor/EntityTrackerAccessor.java | 30 -- .../ServerChunkLoadingManagerAccessor.java | 29 -- .../ServerCommonNetworkHandlerAccessor.java | 33 -- .../ServerLoginNetworkAddonAccessor.java} | 10 +- .../ServerLoginNetworkHandlerAccessor.java | 33 -- .../fabric-networking-api-v1.mixins.json | 20 +- .../networking/unit/CommonPacketTests.java | 407 ------------------ 87 files changed, 869 insertions(+), 4936 deletions(-) delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientCommonNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientCommonNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientLoginNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java rename fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/{ClientConfigurationNetworkHandlerAccessor.java => ClientConfigurationNetworkAddonAccessor.java} (74%) delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientLoginNetworkHandlerAccessor.java rename fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/{ClientCommonNetworkHandlerAccessor.java => ClientPlayNetworkAddonAccessor.java} (71%) delete mode 100644 fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/MinecraftClientAccessor.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractChanneledNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/ChannelInfoHolder.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketHandler.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketsImpl.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonRegisterPayload.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonVersionPayload.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomPayloadTypeProvider.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltLoginPacketSender.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltPacketSender.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/FabricCustomPayloadPacketCodec.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/GlobalReceiverRegistry.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkHandlerExtensions.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkingImpl.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PacketCallbackListener.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PayloadTypeRegistryImpl.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricLoginPacketSender.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricPacketSender.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/QuiltUtil.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/RegistrationPayload.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/QueryIdFactory.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerNetworkingImpl.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ClientConnectionMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CommandManagerMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadPacketCodecMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryRequestS2CPacketMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PacketCodecDispatcherMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PlayerManagerMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerLoginNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/EntityTrackerAccessor.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerChunkLoadingManagerAccessor.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerCommonNetworkHandlerAccessor.java rename fabric-networking-api-v1/src/{client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ConnectScreenAccessor.java => main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkAddonAccessor.java} (75%) delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkHandlerAccessor.java delete mode 100644 fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java diff --git a/fabric-networking-api-v1/build.gradle b/fabric-networking-api-v1/build.gradle index a66181f704d..52ee72a8603 100644 --- a/fabric-networking-api-v1/build.gradle +++ b/fabric-networking-api-v1/build.gradle @@ -11,3 +11,7 @@ testDependencies(project, [ loom { accessWidenerPath = file('src/main/resources/fabric-networking-api-v1.accesswidener') } + +dependencies { + modApi getQslModule("core", "networking") +} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SConfigurationChannelEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SConfigurationChannelEvents.java index 0c56453e0cc..dd401a7b52d 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SConfigurationChannelEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SConfigurationChannelEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,32 +24,40 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the indication of a connected server's ability to receive packets in certain channels. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SConfigurationChannelEvents C2SConfigurationChannelEvents} */ +@Deprecated public final class C2SConfigurationChannelEvents { /** * An event for the client configuration network handler receiving an update indicating the connected server's ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SConfigurationChannelEvents#REGISTER REGISTER} */ - public static final Event REGISTER = EventFactory.createArrayBacked(Register.class, callbacks -> (handler, sender, client, channels) -> { - for (Register callback : callbacks) { - callback.onChannelRegister(handler, sender, client, channels); - } - }); + @Deprecated + public static final Event REGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.C2SConfigurationChannelEvents.REGISTER, + register -> (handler, sender, client, channels) -> register.onChannelRegister(handler, QuiltUtil.toFabricSender(sender), client, QuiltUtil.toIdentifiers(channels)), + invokerGetter -> (handler, sender, client, channels) -> invokerGetter.get().onChannelRegister(handler, QuiltUtil.toQuiltSender(sender), client, QuiltUtil.toPayloadIds(channels)) + ); /** * An event for the client configuration network handler receiving an update indicating the connected server's lack of ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SConfigurationChannelEvents#UNREGISTER UNREGISTER} */ - public static final Event UNREGISTER = EventFactory.createArrayBacked(Unregister.class, callbacks -> (handler, sender, client, channels) -> { - for (Unregister callback : callbacks) { - callback.onChannelUnregister(handler, sender, client, channels); - } - }); + @Deprecated + public static final Event UNREGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.C2SConfigurationChannelEvents.UNREGISTER, + register -> (handler, sender, client, channels) -> register.onChannelUnregister(handler, QuiltUtil.toFabricSender(sender), client, QuiltUtil.toIdentifiers(channels)), + invokerGetter -> (handler, sender, client, channels) -> invokerGetter.get().onChannelUnregister(handler, QuiltUtil.toQuiltSender(sender), client, QuiltUtil.toPayloadIds(channels)) + ); private C2SConfigurationChannelEvents() { } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SPlayChannelEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SPlayChannelEvents.java index f938fe21283..311ba100a16 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SPlayChannelEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/C2SPlayChannelEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,30 +26,39 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the indication of a connected server's ability to receive packets in certain channels. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SPlayChannelEvents C2SPlayChannelEvents} */ +@Deprecated public final class C2SPlayChannelEvents { /** * An event for the client play network handler receiving an update indicating the connected server's ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SPlayChannelEvents#REGISTER REGISTER} */ - public static final Event REGISTER = EventFactory.createArrayBacked(Register.class, callbacks -> (handler, sender, client, channels) -> { - for (Register callback : callbacks) { - callback.onChannelRegister(handler, sender, client, channels); - } - }); + @Deprecated + public static final Event REGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.C2SPlayChannelEvents.REGISTER, + register -> (handler, sender, client, channels) -> register.onChannelRegister(handler, QuiltUtil.toFabricSender(sender), client, QuiltUtil.toIdentifiers(channels)), + invokerGetter -> (handler, sender, client, channels) -> invokerGetter.get().onChannelRegister(handler, QuiltUtil.toQuiltSender(sender), client, QuiltUtil.toPayloadIds(channels)) + ); /** * An event for the client play network handler receiving an update indicating the connected server's lack of ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.C2SPlayChannelEvents#REGISTER REGISTER} */ - public static final Event UNREGISTER = EventFactory.createArrayBacked(Unregister.class, callbacks -> (handler, sender, client, channels) -> { - for (Unregister callback : callbacks) { - callback.onChannelUnregister(handler, sender, client, channels); - } - }); + @Deprecated + public static final Event UNREGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.C2SPlayChannelEvents.UNREGISTER, + register -> (handler, sender, client, channels) -> register.onChannelUnregister(handler, QuiltUtil.toFabricSender(sender), client, QuiltUtil.toIdentifiers(channels)), + invokerGetter -> (handler, sender, client, channels) -> invokerGetter.get().onChannelUnregister(handler, QuiltUtil.toQuiltSender(sender), client, QuiltUtil.toPayloadIds(channels)) + ); private C2SPlayChannelEvents() { } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java index 4ca3bd05bc5..f13bbb9ad92 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +17,23 @@ package net.fabricmc.fabric.api.client.networking.v1; +import org.quiltmc.qsl.networking.impl.AbstractChanneledNetworkAddon; +import org.quiltmc.qsl.networking.impl.NetworkHandlerExtensions; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientConfigurationNetworkHandler; import net.minecraft.network.packet.CustomPayload; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; /** * Offers access to events related to the configuration connection to a server on a logical client. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents ClientConfigurationConnectionEvents} */ +@Deprecated public final class ClientConfigurationConnectionEvents { /** * Event indicating a connection entering the CONFIGURATION state, ready for registering channel handlers. @@ -33,45 +41,53 @@ public final class ClientConfigurationConnectionEvents { *

No packets should be sent when this event is invoked. * * @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ClientConfigurationNetworking.ConfigurationPayloadHandler) + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents#INIT INIT} */ - public static final Event INIT = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Init.class, callbacks -> (handler, client) -> { - for (ClientConfigurationConnectionEvents.Init callback : callbacks) { - callback.onConfigurationInit(handler, client); - } - }); + @Deprecated + public static final Event INIT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents.INIT, + init -> init::onConfigurationInit, + invokerGetter -> invokerGetter.get()::onConfigurationInit + ); /** * An event called after the connection has been initialized and is ready to start sending and receiving configuration packets. * *

Packets may be sent during this event. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents#START START} */ - public static final Event START = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Start.class, callbacks -> (handler, client) -> { - for (ClientConfigurationConnectionEvents.Start callback : callbacks) { - callback.onConfigurationStart(handler, client); - } - }); + @Deprecated + public static final Event START = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents.START, + init -> (handler, sender, client) -> init.onConfigurationStart(handler, client), + invokerGetter -> (handler, client) -> invokerGetter.get().onConfigurationStart(handler, ((AbstractChanneledNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon()), client) + ); /** * An event called after the ReadyS2CPacket has been received, just before switching to the PLAY state. * *

No packets should be sent when this event is invoked. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents#CONFIGURED CONFIGURED} */ - public static final Event COMPLETE = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Complete.class, callbacks -> (handler, client) -> { - for (ClientConfigurationConnectionEvents.Complete callback : callbacks) { - callback.onConfigurationComplete(handler, client); - } - }); + @Deprecated + public static final Event COMPLETE = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents.CONFIGURED, + init -> init::onConfigurationComplete, + invokerGetter -> invokerGetter.get()::onConfigured + ); /** * An event for the disconnection of the client configuration network handler. * *

No packets should be sent when this event is invoked. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents#DISCONNECT DISCONNECT} */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(ClientConfigurationConnectionEvents.Disconnect.class, callbacks -> (handler, client) -> { - for (ClientConfigurationConnectionEvents.Disconnect callback : callbacks) { - callback.onConfigurationDisconnect(handler, client); - } - }); + @Deprecated + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientConfigurationConnectionEvents.DISCONNECT, + init -> init::onConfigurationDisconnect, + invokerGetter -> invokerGetter.get()::onConfigurationDisconnect + ); private ClientConfigurationConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java index 4bf48a99482..8c1d0d00608 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientConfigurationNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +17,14 @@ package net.fabricmc.fabric.api.client.networking.v1; -import java.util.Objects; import java.util.Set; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.networking.impl.client.ClientConfigurationNetworkAddon; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientConfigurationNetworkHandler; import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; import net.minecraft.util.thread.ThreadExecutor; @@ -31,8 +33,8 @@ import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; +import net.fabricmc.fabric.impl.networking.QuiltUtil; +import net.fabricmc.fabric.mixin.networking.client.accessor.ClientConfigurationNetworkAddonAccessor; /** * Offers access to configuration stage client-side networking functionalities. @@ -51,7 +53,10 @@ * object-based API. * * @see ServerConfigurationNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking ClientConfigurationNetworking} */ +@Deprecated public final class ClientConfigurationNetworking { /** * Registers a handler for a packet type. @@ -68,7 +73,7 @@ public final class ClientConfigurationNetworking { * @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ConfigurationPayloadHandler) */ public static boolean registerGlobalReceiver(CustomPayload.Id type, ConfigurationPayloadHandler handler) { - return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.id(), handler); + return org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.registerGlobalReceiver(type, handler); } /** @@ -85,7 +90,9 @@ public static boolean registerGlobalReceiver(CustomPay */ @Nullable public static ClientConfigurationNetworking.ConfigurationPayloadHandler unregisterGlobalReceiver(CustomPayload.Id id) { - return ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(id.id()); + var old = org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.unregisterGlobalReceiver(id); + + return convertToFabricHandler(old); } /** @@ -95,7 +102,7 @@ public static ClientConfigurationNetworking.ConfigurationPayloadHandler unreg * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ClientNetworkingImpl.CONFIGURATION.getChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.getGlobalReceivers()); } /** @@ -115,13 +122,7 @@ public static Set getGlobalReceivers() { * @see ClientPlayConnectionEvents#INIT */ public static boolean registerReceiver(CustomPayload.Id id, ConfigurationPayloadHandler handler) { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon.registerChannel(id.id(), handler); - } - - throw new IllegalStateException("Cannot register receiver while not configuring!"); + return org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.registerReceiver(id, handler); } /** @@ -136,13 +137,9 @@ public static boolean registerReceiver(CustomPayload.I */ @Nullable public static ClientConfigurationNetworking.ConfigurationPayloadHandler unregisterReceiver(Identifier id) { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon.unregisterChannel(id); - } + var old = org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.unregisterReceiver(new CustomPayload.Id<>(id)); - throw new IllegalStateException("Cannot unregister receiver while not configuring!"); + return convertToFabricHandler(old); } /** @@ -152,13 +149,7 @@ public static ClientConfigurationNetworking.ConfigurationPayloadHandler unreg * @throws IllegalStateException if the client is not connected to a server */ public static Set getReceived() throws IllegalStateException { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon.getReceivableChannels(); - } - - throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not configuring!"); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.getReceived()); } /** @@ -168,13 +159,7 @@ public static Set getReceived() throws IllegalStateException { * @throws IllegalStateException if the client is not connected to a server */ public static Set getSendable() throws IllegalStateException { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon.getSendableChannels(); - } - - throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not configuring!"); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.getSendable()); } /** @@ -185,13 +170,7 @@ public static Set getSendable() throws IllegalStateException { * False if the client is not in game. */ public static boolean canSend(Identifier channelName) throws IllegalArgumentException { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon.getSendableChannels().contains(channelName); - } - - throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not configuring!"); + return org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.canSend(new CustomPayload.Id<>(channelName)); } /** @@ -202,7 +181,7 @@ public static boolean canSend(Identifier channelName) throws IllegalArgumentExce * @return {@code true} if the connected server has declared the ability to receive a packet on the specified channel */ public static boolean canSend(CustomPayload.Id type) { - return canSend(type.id()); + return org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.canSend(type); } /** @@ -212,13 +191,7 @@ public static boolean canSend(CustomPayload.Id type) { * @throws IllegalStateException if the client is not connected to a server */ public static PacketSender getSender() throws IllegalStateException { - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - return addon; - } - - throw new IllegalStateException("Cannot get PacketSender while not configuring!"); + return QuiltUtil.toFabricSender(org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.getSender()); } /** @@ -230,17 +203,7 @@ public static PacketSender getSender() throws IllegalStateException { * @throws IllegalStateException if the client is not connected to a server */ public static void send(CustomPayload payload) { - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (addon != null) { - addon.sendPacket(payload); - return; - } - - throw new IllegalStateException("Cannot send packet while not configuring!"); + org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.send(payload); } private ClientConfigurationNetworking() { @@ -251,7 +214,7 @@ private ClientConfigurationNetworking() { * @param the type of the packet */ @FunctionalInterface - public interface ConfigurationPayloadHandler { + public interface ConfigurationPayloadHandler extends org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.CustomChannelReceiver { /** * Handles the incoming packet. * @@ -271,6 +234,12 @@ public interface ConfigurationPayloadHandler { * @see CustomPayload */ void receive(T payload, Context context); + + @Override + default void receive(MinecraftClient client, ClientConfigurationNetworkHandler handler, T payload, org.quiltmc.qsl.networking.api.PacketSender responseSender) { + record ContextImpl(MinecraftClient client, PacketSender responseSender) implements Context {} + this.receive(payload, new ContextImpl(client, QuiltUtil.toFabricSender(responseSender))); + } } @ApiStatus.NonExtendable @@ -285,4 +254,30 @@ public interface Context { */ PacketSender responseSender(); } + + + private static @Nullable ConfigurationPayloadHandler convertToFabricHandler(org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.CustomChannelReceiver old) { + if (old instanceof ConfigurationPayloadHandler fabric) { + return fabric; + } else if (old != null) { + return (payload, context) -> { + if (old instanceof org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking.CustomChannelReceiver r) { + org.quiltmc.qsl.networking.api.PacketSender sender = QuiltUtil.toQuiltSender(context.responseSender()); + r.receive(context.client(), getHandler(sender), payload, sender); + } else { + // This should never happen! + throw new UnsupportedOperationException("Unknown receiver type: " + old.getClass().getName()); + } + }; + } else { + return null; + } + } + + private static ClientConfigurationNetworkHandler getHandler(org.quiltmc.qsl.networking.api.PacketSender sender) { + if (sender instanceof ClientConfigurationNetworkAddon addon) { + return ((ClientConfigurationNetworkAddonAccessor) (Object) addon).getHandler(); + } + throw new IllegalStateException("Packet Sender was not from a ClientConfigurationNetworkAddon!"); + } } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginConnectionEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginConnectionEvents.java index e5a3e685dc5..20f5413f4e8 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginConnectionEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,11 +22,14 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; /** * Offers access to events related to the connection to a server on the client while the server is processing the client's login request. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientLoginConnectionEvents ClientLoginConnectionEvents} */ +@Deprecated public final class ClientLoginConnectionEvents { /** * Event indicating a connection entered the LOGIN state, ready for registering query request handlers. @@ -34,11 +38,10 @@ public final class ClientLoginConnectionEvents { * * @see ClientLoginNetworking#registerReceiver(Identifier, ClientLoginNetworking.LoginQueryRequestHandler) */ - public static final Event INIT = EventFactory.createArrayBacked(Init.class, callbacks -> (handler, client) -> { - for (Init callback : callbacks) { - callback.onLoginStart(handler, client); - } - }); + public static final Event INIT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientLoginConnectionEvents.INIT, + init -> init::onLoginStart, + invokerGetter -> invokerGetter.get()::onLoginStart + ); /** * An event for when the client has started receiving login queries. @@ -54,22 +57,20 @@ public final class ClientLoginConnectionEvents { * *

No packets should be sent when this event is invoked. */ - public static final Event QUERY_START = EventFactory.createArrayBacked(QueryStart.class, callbacks -> (handler, client) -> { - for (QueryStart callback : callbacks) { - callback.onLoginQueryStart(handler, client); - } - }); + public static final Event QUERY_START = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientLoginConnectionEvents.QUERY_START, + disconnect -> disconnect::onLoginQueryStart, + invokerGetter -> invokerGetter.get()::onLoginQueryStart + ); /** * An event for when the client's login process has ended due to disconnection. * *

No packets should be sent when this event is invoked. */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(Disconnect.class, callbacks -> (handler, client) -> { - for (Disconnect callback : callbacks) { - callback.onLoginDisconnect(handler, client); - } - }); + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientLoginConnectionEvents.DISCONNECT, + disconnect -> disconnect::onLoginDisconnect, + invokerGetter -> invokerGetter.get()::onLoginDisconnect + ); private ClientLoginConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginNetworking.java index 42b43545b90..4f34cbf85b9 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientLoginNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,14 +25,11 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientLoginNetworkHandler; -import net.minecraft.network.ClientConnection; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.listener.PacketListener; import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; /** * Offers access to login stage client-side networking functionalities. @@ -40,7 +38,10 @@ * * @see ClientPlayNetworking * @see ServerLoginNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientLoginNetworking ClientLoginNetworking} */ +@Deprecated public final class ClientLoginNetworking { /** * Registers a handler to a query request channel. @@ -56,7 +57,7 @@ public final class ClientLoginNetworking { * @see ClientLoginNetworking#registerReceiver(Identifier, LoginQueryRequestHandler) */ public static boolean registerGlobalReceiver(Identifier channelName, LoginQueryRequestHandler queryHandler) { - return ClientNetworkingImpl.LOGIN.registerGlobalReceiver(channelName, queryHandler); + return org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.registerGlobalReceiver(channelName, queryHandler); } /** @@ -72,7 +73,15 @@ public static boolean registerGlobalReceiver(Identifier channelName, LoginQueryR */ @Nullable public static ClientLoginNetworking.LoginQueryRequestHandler unregisterGlobalReceiver(Identifier channelName) { - return ClientNetworkingImpl.LOGIN.unregisterGlobalReceiver(channelName); + var old = org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.unregisterGlobalReceiver(channelName); + + if (old instanceof LoginQueryRequestHandler fabric) { + return fabric; + } else if (old != null) { + return old::receive; + } else { + return null; + } } /** @@ -82,7 +91,7 @@ public static ClientLoginNetworking.LoginQueryRequestHandler unregisterGlobalRec * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ClientNetworkingImpl.LOGIN.getChannels(); + return org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.getGlobalReceivers(); } /** @@ -97,17 +106,7 @@ public static Set getGlobalReceivers() { * @throws IllegalStateException if the client is not logging in */ public static boolean registerReceiver(Identifier channelName, LoginQueryRequestHandler queryHandler) throws IllegalStateException { - final ClientConnection connection = ClientNetworkingImpl.getLoginConnection(); - - if (connection != null) { - final PacketListener packetListener = connection.getPacketListener(); - - if (packetListener instanceof ClientLoginNetworkHandler) { - return ClientNetworkingImpl.getAddon(((ClientLoginNetworkHandler) packetListener)).registerChannel(channelName, queryHandler); - } - } - - throw new IllegalStateException("Cannot register receiver while client is not logging in!"); + return org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.registerReceiver(channelName, queryHandler); } /** @@ -121,24 +120,22 @@ public static boolean registerReceiver(Identifier channelName, LoginQueryRequest */ @Nullable public static LoginQueryRequestHandler unregisterReceiver(Identifier channelName) throws IllegalStateException { - final ClientConnection connection = ClientNetworkingImpl.getLoginConnection(); - - if (connection != null) { - final PacketListener packetListener = connection.getPacketListener(); - - if (packetListener instanceof ClientLoginNetworkHandler) { - return ClientNetworkingImpl.getAddon(((ClientLoginNetworkHandler) packetListener)).unregisterChannel(channelName); - } + var old = org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.unregisterReceiver(channelName); + + if (old instanceof LoginQueryRequestHandler fabric) { + return fabric; + } else if (old != null) { + return old::receive; + } else { + return null; } - - throw new IllegalStateException("Cannot unregister receiver while client is not logging in!"); } private ClientLoginNetworking() { } @FunctionalInterface - public interface LoginQueryRequestHandler { + public interface LoginQueryRequestHandler extends org.quiltmc.qsl.networking.api.client.ClientLoginNetworking.QueryRequestReceiver { /** * Handles an incoming query request from a server. * diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayConnectionEvents.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayConnectionEvents.java index 22bb68bfbe5..3aabda09b5b 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayConnectionEvents.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,23 +22,26 @@ import net.minecraft.network.packet.CustomPayload; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the connection to a server on a logical client. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientPlayConnectionEvents ClientPlayConnectionEvents} */ +@Deprecated public final class ClientPlayConnectionEvents { /** * Event indicating a connection entered the PLAY state, ready for registering channel handlers. * * @see ClientPlayNetworking#registerReceiver(CustomPayload.Id, ClientPlayNetworking.PlayPayloadHandler) */ - public static final Event INIT = EventFactory.createArrayBacked(Init.class, callbacks -> (handler, client) -> { - for (Init callback : callbacks) { - callback.onPlayInit(handler, client); - } - }); + public static final Event INIT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientPlayConnectionEvents.INIT, + init -> init::onPlayInit, + invokerGetter -> invokerGetter.get()::onPlayInit + ); /** * An event for notification when the client play network handler is ready to send packets to the server. @@ -45,22 +49,20 @@ public final class ClientPlayConnectionEvents { *

At this stage, the network handler is ready to send packets to the server. * Since the client's local state has been set up. */ - public static final Event JOIN = EventFactory.createArrayBacked(Join.class, callbacks -> (handler, sender, client) -> { - for (Join callback : callbacks) { - callback.onPlayReady(handler, sender, client); - } - }); + public static final Event JOIN = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientPlayConnectionEvents.JOIN, + join -> (handler, sender, client) -> join.onPlayReady(handler, QuiltUtil.toFabricSender(sender), client), + invokerGetter -> (handler, sender, client) -> invokerGetter.get().onPlayReady(handler, QuiltUtil.toQuiltSender(sender), client) + ); /** * An event for the disconnection of the client play network handler. * *

No packets should be sent when this event is invoked. */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(Disconnect.class, callbacks -> (handler, client) -> { - for (Disconnect callback : callbacks) { - callback.onPlayDisconnect(handler, client); - } - }); + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.client.ClientPlayConnectionEvents.DISCONNECT, + disconnect -> disconnect::onPlayDisconnect, + invokerGetter -> invokerGetter.get()::onPlayDisconnect + ); private ClientPlayConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java index a0ceaec567d..e39f33ad4c5 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/ClientPlayNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +17,13 @@ package net.fabricmc.fabric.api.client.networking.v1; -import java.util.Objects; import java.util.Set; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.network.listener.ServerCommonPacketListener; import net.minecraft.network.packet.CustomPayload; @@ -32,8 +33,8 @@ import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; -import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon; +import net.fabricmc.fabric.impl.networking.QuiltUtil; +import net.fabricmc.fabric.mixin.networking.client.accessor.ClientPlayNetworkAddonAccessor; /** * Offers access to play stage client-side networking functionalities. @@ -52,7 +53,10 @@ * @see ClientLoginNetworking * @see ClientConfigurationNetworking * @see ServerPlayNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client.ClientPlayNetworking ClientPlayNetworking} */ +@Deprecated public final class ClientPlayNetworking { /** * Registers a handler for a payload type. @@ -69,7 +73,7 @@ public final class ClientPlayNetworking { * @see ClientPlayNetworking#registerReceiver(CustomPayload.Id, PlayPayloadHandler) */ public static boolean registerGlobalReceiver(CustomPayload.Id type, PlayPayloadHandler handler) { - return ClientNetworkingImpl.PLAY.registerGlobalReceiver(type.id(), handler); + return org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.registerGlobalReceiver(type, handler); } /** @@ -86,7 +90,8 @@ public static boolean registerGlobalReceiver(CustomPay */ @Nullable public static ClientPlayNetworking.PlayPayloadHandler unregisterGlobalReceiver(Identifier id) { - return ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(id); + var old = org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.unregisterGlobalReceiver(new CustomPayload.Id<>(id)); + return convertToFabricHandler(old); } /** @@ -96,7 +101,7 @@ public static ClientPlayNetworking.PlayPayloadHandler unregisterGlobalReceive * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ClientNetworkingImpl.PLAY.getChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.getGlobalReceivers()); } /** @@ -116,13 +121,7 @@ public static Set getGlobalReceivers() { * @see ClientPlayConnectionEvents#INIT */ public static boolean registerReceiver(CustomPayload.Id type, PlayPayloadHandler handler) { - final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon(); - - if (addon != null) { - return addon.registerChannel(type.id(), handler); - } - - throw new IllegalStateException("Cannot register receiver while not in game!"); + return org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.registerReceiver(type, handler); } /** @@ -137,13 +136,9 @@ public static boolean registerReceiver(CustomPayload.I */ @Nullable public static ClientPlayNetworking.PlayPayloadHandler unregisterReceiver(Identifier id) { - final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon(); + var old = org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.unregisterReceiver(new CustomPayload.Id<>(id)); - if (addon != null) { - return addon.unregisterChannel(id); - } - - throw new IllegalStateException("Cannot unregister receiver while not in game!"); + return convertToFabricHandler(old); } /** @@ -153,13 +148,7 @@ public static ClientPlayNetworking.PlayPayloadHandler unregisterReceiver(Iden * @throws IllegalStateException if the client is not connected to a server */ public static Set getReceived() throws IllegalStateException { - final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon(); - - if (addon != null) { - return addon.getReceivableChannels(); - } - - throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not in game!"); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.getReceived()); } /** @@ -169,13 +158,7 @@ public static Set getReceived() throws IllegalStateException { * @throws IllegalStateException if the client is not connected to a server */ public static Set getSendable() throws IllegalStateException { - final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon(); - - if (addon != null) { - return addon.getSendableChannels(); - } - - throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not in game!"); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.getSendable()); } /** @@ -186,12 +169,7 @@ public static Set getSendable() throws IllegalStateException { * False if the client is not in game. */ public static boolean canSend(Identifier channelName) throws IllegalArgumentException { - // You cant send without a client player, so this is fine - if (MinecraftClient.getInstance().getNetworkHandler() != null) { - return ClientNetworkingImpl.getAddon(MinecraftClient.getInstance().getNetworkHandler()).getSendableChannels().contains(channelName); - } - - return false; + return org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.canSend(new CustomPayload.Id<>(channelName)); } /** @@ -202,7 +180,7 @@ public static boolean canSend(Identifier channelName) throws IllegalArgumentExce * @return {@code true} if the connected server has declared the ability to receive a payload on the specified channel */ public static boolean canSend(CustomPayload.Id type) { - return canSend(type.id()); + return org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.canSend(type); } /** @@ -212,7 +190,7 @@ public static boolean canSend(CustomPayload.Id type) { * @return a new payload */ public static Packet createC2SPacket(T packet) { - return ClientNetworkingImpl.createC2SPacket(packet); + return org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.createC2SPacket(packet); } /** @@ -222,12 +200,7 @@ public static Packet creat * @throws IllegalStateException if the client is not connected to a server */ public static PacketSender getSender() throws IllegalStateException { - // You cant send without a client player, so this is fine - if (MinecraftClient.getInstance().getNetworkHandler() != null) { - return ClientNetworkingImpl.getAddon(MinecraftClient.getInstance().getNetworkHandler()); - } - - throw new IllegalStateException("Cannot get payload sender when not in game!"); + return QuiltUtil.toFabricSender(org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.getSender()); } /** @@ -239,16 +212,7 @@ public static PacketSender getSender() throws IllegalStateException { * @throws IllegalStateException if the client is not connected to a server */ public static void send(CustomPayload payload) { - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - // You cant send without a client player, so this is fine - if (MinecraftClient.getInstance().getNetworkHandler() != null) { - MinecraftClient.getInstance().getNetworkHandler().sendPacket(createC2SPacket(payload)); - return; - } - - throw new IllegalStateException("Cannot send packets when not in game!"); + org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.send(payload); } private ClientPlayNetworking() { @@ -259,7 +223,7 @@ private ClientPlayNetworking() { * @param the type of the payload */ @FunctionalInterface - public interface PlayPayloadHandler { + public interface PlayPayloadHandler extends org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.CustomChannelReceiver { /** * Handles the incoming payload. This is called on the render thread, and can safely * call client methods. @@ -279,6 +243,12 @@ public interface PlayPayloadHandler { * @see CustomPayload */ void receive(T payload, Context context); + + @Override + default void receive(MinecraftClient client, ClientPlayNetworkHandler handler, T payload, org.quiltmc.qsl.networking.api.PacketSender responseSender) { + record ContextImpl(MinecraftClient client, ClientPlayerEntity player, PacketSender responseSender) implements Context {} + this.receive(payload, new ContextImpl(client, client.player, QuiltUtil.toFabricSender(responseSender))); + } } @ApiStatus.NonExtendable @@ -298,4 +268,29 @@ public interface Context { */ PacketSender responseSender(); } + + private static @Nullable PlayPayloadHandler convertToFabricHandler(org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.CustomChannelReceiver old) { + if (old instanceof PlayPayloadHandler fabric) { + return fabric; + } else if (old != null) { + return (payload, context) -> { + if (old instanceof org.quiltmc.qsl.networking.api.client.ClientPlayNetworking.CustomChannelReceiver r) { + org.quiltmc.qsl.networking.api.PacketSender sender = QuiltUtil.toQuiltSender(context.responseSender()); + r.receive(context.client(), getHandler(sender), payload, sender); + } else { + // This should never happen! + throw new UnsupportedOperationException("Unknown receiver type: " + old.getClass().getName()); + } + }; + } else { + return null; + } + } + + private static ClientPlayNetworkHandler getHandler(org.quiltmc.qsl.networking.api.PacketSender sender) { + if (sender instanceof org.quiltmc.qsl.networking.impl.client.ClientPlayNetworkAddon addon) { + return ((ClientPlayNetworkAddonAccessor) (Object) addon).getHandler(); + } + throw new IllegalStateException("Packet Sender was not from a ClientConfigurationNetworkAddon!"); + } } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/package-info.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/package-info.java index db0f27caf4a..9489e52091a 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/package-info.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/api/client/networking/v1/package-info.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +43,8 @@ * and {@link net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents}, and related methods are found at * {@link net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking}. * + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.client} */ - +@Deprecated package net.fabricmc.fabric.api.client.networking.v1; diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientCommonNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientCommonNetworkAddon.java deleted file mode 100644 index 3033de5da13..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientCommonNetworkAddon.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.client; - -import java.util.Collections; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientCommonNetworkHandler; -import net.minecraft.network.ClientConnection; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon; -import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry; -import net.fabricmc.fabric.impl.networking.NetworkingImpl; -import net.fabricmc.fabric.impl.networking.RegistrationPayload; - -abstract class ClientCommonNetworkAddon extends AbstractChanneledNetworkAddon { - protected final T handler; - protected final MinecraftClient client; - - protected boolean isServerReady = false; - - protected ClientCommonNetworkAddon(GlobalReceiverRegistry receiver, ClientConnection connection, String description, T handler, MinecraftClient client) { - super(receiver, connection, description); - this.handler = handler; - this.client = client; - } - - public void onServerReady() { - this.isServerReady = true; - } - - @Override - protected void handleRegistration(Identifier channelName) { - // If we can already send packets, immediately send the register packet for this channel - if (this.isServerReady) { - final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.REGISTER, Collections.singleton(channelName)); - - if (payload != null) { - this.sendPacket(payload); - } - } - } - - @Override - protected void handleUnregistration(Identifier channelName) { - // If we can already send packets, immediately send the unregister packet for this channel - if (this.isServerReady) { - final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.UNREGISTER, Collections.singleton(channelName)); - - if (payload != null) { - this.sendPacket(payload); - } - } - } - - @Override - protected boolean isReservedChannel(Identifier channelName) { - return NetworkingImpl.isReservedCommonChannel(channelName); - } - - @Override - protected void schedule(Runnable task) { - client.execute(task); - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java deleted file mode 100644 index d391223bb32..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientConfigurationNetworkAddon.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.client; - -import java.util.List; -import java.util.Objects; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientConfigurationNetworkHandler; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.packet.BrandCustomPayload; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.client.networking.v1.C2SConfigurationChannelEvents; -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.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; -import net.fabricmc.fabric.impl.networking.RegistrationPayload; -import net.fabricmc.fabric.mixin.networking.client.accessor.ClientCommonNetworkHandlerAccessor; -import net.fabricmc.fabric.mixin.networking.client.accessor.ClientConfigurationNetworkHandlerAccessor; - -public final class ClientConfigurationNetworkAddon extends ClientCommonNetworkAddon, ClientConfigurationNetworkHandler> { - private final ContextImpl context; - private boolean sentInitialRegisterPacket; - private boolean hasStarted; - - public ClientConfigurationNetworkAddon(ClientConfigurationNetworkHandler handler, MinecraftClient client) { - super(ClientNetworkingImpl.CONFIGURATION, ((ClientCommonNetworkHandlerAccessor) handler).getConnection(), "ClientPlayNetworkAddon for " + ((ClientConfigurationNetworkHandlerAccessor) handler).getProfile().getName(), handler, client); - this.context = new ContextImpl(client, this); - - // Must register pending channels via lateinit - this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); - } - - @Override - protected void invokeInitEvent() { - ClientConfigurationConnectionEvents.INIT.invoker().onConfigurationInit(this.handler, this.client); - } - - @Override - public void onServerReady() { - super.onServerReady(); - invokeStartEvent(); - } - - @Override - protected void receiveRegistration(boolean register, RegistrationPayload payload) { - super.receiveRegistration(register, payload); - - if (register && !this.sentInitialRegisterPacket) { - this.sendInitialChannelRegistrationPacket(); - this.sentInitialRegisterPacket = true; - - this.onServerReady(); - } - } - - @Override - public boolean handle(CustomPayload payload) { - boolean result = super.handle(payload); - - if (payload instanceof BrandCustomPayload) { - // If we have received this without first receiving the registration packet, its likely a vanilla server. - invokeStartEvent(); - } - - return result; - } - - private void invokeStartEvent() { - if (!hasStarted) { - hasStarted = true; - ClientConfigurationConnectionEvents.START.invoker().onConfigurationStart(this.handler, this.client); - } - } - - @Override - protected void receive(ClientConfigurationNetworking.ConfigurationPayloadHandler handler, CustomPayload payload) { - ((ClientConfigurationNetworking.ConfigurationPayloadHandler) handler).receive(payload, this.context); - } - - // impl details - @Override - public Packet createPacket(CustomPayload packet) { - return ClientPlayNetworking.createC2SPacket(packet); - } - - @Override - protected void invokeRegisterEvent(List ids) { - C2SConfigurationChannelEvents.REGISTER.invoker().onChannelRegister(this.handler, this, this.client, ids); - } - - @Override - protected void invokeUnregisterEvent(List ids) { - C2SConfigurationChannelEvents.UNREGISTER.invoker().onChannelUnregister(this.handler, this, this.client, ids); - } - - public void handleComplete() { - ClientConfigurationConnectionEvents.COMPLETE.invoker().onConfigurationComplete(this.handler, this.client); - ClientConfigurationConnectionEvents.READY.invoker().onConfigurationReady(this.handler, this.client); - ClientNetworkingImpl.setClientConfigurationAddon(null); - } - - @Override - protected void invokeDisconnectEvent() { - ClientConfigurationConnectionEvents.DISCONNECT.invoker().onConfigurationDisconnect(this.handler, this.client); - } - - public ChannelInfoHolder getChannelInfoHolder() { - return (ChannelInfoHolder) ((ClientCommonNetworkHandlerAccessor) handler).getConnection(); - } - - private record ContextImpl(MinecraftClient client, PacketSender responseSender) implements ClientConfigurationNetworking.Context { - private ContextImpl { - Objects.requireNonNull(client, "client"); - Objects.requireNonNull(responseSender, "responseSender"); - } - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java deleted file mode 100644 index e050c0326c2..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientLoginNetworkAddon.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.client; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientLoginNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents; -import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; -import net.fabricmc.fabric.impl.networking.AbstractNetworkAddon; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; -import net.fabricmc.fabric.mixin.networking.client.accessor.ClientLoginNetworkHandlerAccessor; - -public final class ClientLoginNetworkAddon extends AbstractNetworkAddon { - private final ClientLoginNetworkHandler handler; - private final MinecraftClient client; - private boolean firstResponse = true; - - public ClientLoginNetworkAddon(ClientLoginNetworkHandler handler, MinecraftClient client) { - super(ClientNetworkingImpl.LOGIN, "ClientLoginNetworkAddon for Client"); - this.handler = handler; - this.client = client; - } - - @Override - protected void invokeInitEvent() { - ClientLoginConnectionEvents.INIT.invoker().onLoginStart(this.handler, this.client); - } - - public boolean handlePacket(LoginQueryRequestS2CPacket packet) { - PacketByteBufLoginQueryRequestPayload payload = (PacketByteBufLoginQueryRequestPayload) packet.payload(); - return handlePacket(packet.queryId(), packet.payload().id(), payload.data()); - } - - private boolean handlePacket(int queryId, Identifier channelName, PacketByteBuf originalBuf) { - this.logger.debug("Handling inbound login response with id {} and channel with name {}", queryId, channelName); - - if (this.firstResponse) { - ClientLoginConnectionEvents.QUERY_START.invoker().onLoginQueryStart(this.handler, this.client); - this.firstResponse = false; - } - - @Nullable ClientLoginNetworking.LoginQueryRequestHandler handler = this.getHandler(channelName); - - if (handler == null) { - return false; - } - - PacketByteBuf buf = PacketByteBufs.slice(originalBuf); - List callbacks = new ArrayList<>(); - - try { - CompletableFuture<@Nullable PacketByteBuf> future = handler.receive(this.client, this.handler, buf, callbacks::add); - future.thenAccept(result -> { - LoginQueryResponseC2SPacket packet = new LoginQueryResponseC2SPacket(queryId, result == null ? null : new PacketByteBufLoginQueryResponse(result)); - ((ClientLoginNetworkHandlerAccessor) this.handler).getConnection().send(packet, new PacketCallbacks() { - @Override - public void onSuccess() { - callbacks.forEach(PacketCallbacks::onSuccess); - } - }); - }); - } catch (Throwable ex) { - this.logger.error("Encountered exception while handling in channel with name \"{}\"", channelName, ex); - throw ex; - } - - return true; - } - - @Override - protected void handleRegistration(Identifier channelName) { - } - - @Override - protected void handleUnregistration(Identifier channelName) { - } - - @Override - protected void invokeDisconnectEvent() { - ClientLoginConnectionEvents.DISCONNECT.invoker().onLoginDisconnect(this.handler, this.client); - } - - @Override - protected boolean isReservedChannel(Identifier channelName) { - return false; - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java deleted file mode 100644 index c2f5bea6a2b..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientNetworkingImpl.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.client; - -import java.util.Objects; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.multiplayer.ConnectScreen; -import net.minecraft.client.network.ClientConfigurationNetworkHandler; -import net.minecraft.client.network.ClientLoginNetworkHandler; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.network.ClientConnection; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.NetworkSide; -import net.minecraft.network.listener.ServerCommonPacketListener; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; - -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.ClientLoginNetworking; -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.PacketSender; -import net.fabricmc.fabric.impl.networking.CommonPacketsImpl; -import net.fabricmc.fabric.impl.networking.CommonRegisterPayload; -import net.fabricmc.fabric.impl.networking.CommonVersionPayload; -import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry; -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.NetworkingImpl; -import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; -import net.fabricmc.fabric.mixin.networking.client.accessor.ConnectScreenAccessor; -import net.fabricmc.fabric.mixin.networking.client.accessor.MinecraftClientAccessor; - -public final class ClientNetworkingImpl { - public static final GlobalReceiverRegistry LOGIN = new GlobalReceiverRegistry<>(NetworkSide.CLIENTBOUND, NetworkPhase.LOGIN, null); - public static final GlobalReceiverRegistry> CONFIGURATION = new GlobalReceiverRegistry<>(NetworkSide.CLIENTBOUND, NetworkPhase.CONFIGURATION, PayloadTypeRegistryImpl.CONFIGURATION_S2C); - public static final GlobalReceiverRegistry> PLAY = new GlobalReceiverRegistry<>(NetworkSide.CLIENTBOUND, NetworkPhase.PLAY, PayloadTypeRegistryImpl.PLAY_S2C); - - private static ClientPlayNetworkAddon currentPlayAddon; - private static ClientConfigurationNetworkAddon currentConfigurationAddon; - - public static ClientPlayNetworkAddon getAddon(ClientPlayNetworkHandler handler) { - return (ClientPlayNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static ClientConfigurationNetworkAddon getAddon(ClientConfigurationNetworkHandler handler) { - return (ClientConfigurationNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static ClientLoginNetworkAddon getAddon(ClientLoginNetworkHandler handler) { - return (ClientLoginNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static Packet createC2SPacket(CustomPayload payload) { - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - return new CustomPayloadC2SPacket(payload); - } - - /** - * Due to the way logging into an integrated or remote dedicated server will differ, we need to obtain the login client connection differently. - */ - @Nullable - public static ClientConnection getLoginConnection() { - final ClientConnection connection = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getConnection(); - - // Check if we are connecting to an integrated server. This will set the field on MinecraftClient - if (connection != null) { - return connection; - } else { - // We are probably connecting to a remote server. - // Check if the ConnectScreen is the currentScreen to determine that: - if (MinecraftClient.getInstance().currentScreen instanceof ConnectScreen) { - return ((ConnectScreenAccessor) MinecraftClient.getInstance().currentScreen).getConnection(); - } - } - - // We are not connected to a server at all. - return null; - } - - @Nullable - public static ClientConfigurationNetworkAddon getClientConfigurationAddon() { - return currentConfigurationAddon; - } - - @Nullable - public static ClientPlayNetworkAddon getClientPlayAddon() { - // Since Minecraft can be a bit weird, we need to check for the play addon in a few ways: - // If the client's player is set this will work - if (MinecraftClient.getInstance().getNetworkHandler() != null) { - currentPlayAddon = null; // Shouldn't need this anymore - return getAddon(MinecraftClient.getInstance().getNetworkHandler()); - } - - // We haven't hit the end of onGameJoin yet, use our backing field here to access the network handler - if (currentPlayAddon != null) { - return currentPlayAddon; - } - - // We are not in play stage - return null; - } - - public static void setClientPlayAddon(ClientPlayNetworkAddon addon) { - assert addon == null || currentConfigurationAddon == null; - currentPlayAddon = addon; - } - - public static void setClientConfigurationAddon(ClientConfigurationNetworkAddon addon) { - currentConfigurationAddon = addon; - } - - public static void clientInit() { - // Reference cleanup for the locally stored addon if we are disconnected - ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> { - currentPlayAddon = null; - }); - - ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client) -> { - currentConfigurationAddon = null; - }); - - // Version packet - ClientConfigurationNetworking.registerGlobalReceiver(CommonVersionPayload.ID, (payload, context) -> { - int negotiatedVersion = handleVersionPacket(payload, context.responseSender()); - ClientNetworkingImpl.getClientConfigurationAddon().onCommonVersionPacket(negotiatedVersion); - }); - - // Register packet - ClientConfigurationNetworking.registerGlobalReceiver(CommonRegisterPayload.ID, (payload, context) -> { - ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon(); - - if (CommonRegisterPayload.PLAY_PHASE.equals(payload.phase())) { - if (payload.version() != addon.getNegotiatedVersion()) { - throw new IllegalStateException("Negotiated common packet version: %d but received packet with version: %d".formatted(addon.getNegotiatedVersion(), payload.version())); - } - - addon.getChannelInfoHolder().fabric_getPendingChannelsNames(NetworkPhase.PLAY).addAll(payload.channels()); - NetworkingImpl.LOGGER.debug("Received accepted channels from the server"); - context.responseSender().sendPacket(new CommonRegisterPayload(addon.getNegotiatedVersion(), CommonRegisterPayload.PLAY_PHASE, ClientPlayNetworking.getGlobalReceivers())); - } else { - addon.onCommonRegisterPacket(payload); - context.responseSender().sendPacket(addon.createRegisterPayload()); - } - }); - } - - // Disconnect if there are no commonly supported versions. - // Client responds with the intersection of supported versions. - // Return the highest supported version - private static int handleVersionPacket(CommonVersionPayload payload, PacketSender packetSender) { - int version = CommonPacketsImpl.getHighestCommonVersion(payload.versions(), CommonPacketsImpl.SUPPORTED_COMMON_PACKET_VERSIONS); - - if (version <= 0) { - throw new UnsupportedOperationException("Client does not support any requested versions from server"); - } - - packetSender.sendPacket(new CommonVersionPayload(new int[]{ version })); - return version; - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java deleted file mode 100644 index b9942064234..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/impl/networking/client/ClientPlayNetworkAddon.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.client; - -import java.util.List; -import java.util.Objects; - -import com.mojang.logging.LogUtils; -import org.slf4j.Logger; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents; -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.PacketSender; -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; - -public final class ClientPlayNetworkAddon extends ClientCommonNetworkAddon, ClientPlayNetworkHandler> { - private final ContextImpl context; - - private static final Logger LOGGER = LogUtils.getLogger(); - - public ClientPlayNetworkAddon(ClientPlayNetworkHandler handler, MinecraftClient client) { - super(ClientNetworkingImpl.PLAY, handler.getConnection(), "ClientPlayNetworkAddon for " + handler.getProfile().getName(), handler, client); - this.context = new ContextImpl(client, this); - - // Must register pending channels via lateinit - this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY); - } - - @Override - protected void invokeInitEvent() { - ClientPlayConnectionEvents.INIT.invoker().onPlayInit(this.handler, this.client); - } - - @Override - public void onServerReady() { - try { - ClientPlayConnectionEvents.JOIN.invoker().onPlayReady(this.handler, this, this.client); - } catch (RuntimeException e) { - LOGGER.error("Exception thrown while invoking ClientPlayConnectionEvents.JOIN", e); - } - - // The client cannot send any packets, including `minecraft:register` until after GameJoinS2CPacket is received. - this.sendInitialChannelRegistrationPacket(); - super.onServerReady(); - } - - @Override - protected void receive(ClientPlayNetworking.PlayPayloadHandler handler, CustomPayload payload) { - this.client.execute(() -> { - ((ClientPlayNetworking.PlayPayloadHandler) handler).receive(payload, context); - }); - } - - // impl details - @Override - public Packet createPacket(CustomPayload packet) { - return ClientPlayNetworking.createC2SPacket(packet); - } - - @Override - protected void invokeRegisterEvent(List ids) { - C2SPlayChannelEvents.REGISTER.invoker().onChannelRegister(this.handler, this, this.client, ids); - } - - @Override - protected void invokeUnregisterEvent(List ids) { - C2SPlayChannelEvents.UNREGISTER.invoker().onChannelUnregister(this.handler, this, this.client, ids); - } - - @Override - protected void invokeDisconnectEvent() { - ClientPlayConnectionEvents.DISCONNECT.invoker().onPlayDisconnect(this.handler, this.client); - } - - private record ContextImpl(MinecraftClient client, PacketSender responseSender) implements ClientPlayNetworking.Context { - private ContextImpl { - Objects.requireNonNull(client, "client"); - Objects.requireNonNull(responseSender, "responseSender"); - } - - @Override - public ClientPlayerEntity player() { - return Objects.requireNonNull(client.player, "player"); - } - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientCommonNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientCommonNetworkHandlerMixin.java deleted file mode 100644 index 62d03d6a713..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientCommonNetworkHandlerMixin.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.network.ClientCommonNetworkHandler; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon; - -@Mixin(ClientCommonNetworkHandler.class) -public abstract class ClientCommonNetworkHandlerMixin implements NetworkHandlerExtensions { - @Inject(method = "onCustomPayload(Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;)V", at = @At("HEAD"), cancellable = true) - public void onCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - final CustomPayload payload = packet.payload(); - boolean handled; - - if (this.getAddon() instanceof ClientPlayNetworkAddon addon) { - handled = addon.handle(payload); - } else if (this.getAddon() instanceof ClientConfigurationNetworkAddon addon) { - handled = addon.handle(payload); - } else { - throw new IllegalStateException("Unknown network addon"); - } - - if (handled) { - ci.cancel(); - } - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java deleted file mode 100644 index 2261d9a3b02..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientConfigurationNetworkHandlerMixin.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientCommonNetworkHandler; -import net.minecraft.client.network.ClientConfigurationNetworkHandler; -import net.minecraft.client.network.ClientConnectionState; -import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.s2c.config.ReadyS2CPacket; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; - -// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues -@Mixin(value = ClientConfigurationNetworkHandler.class, priority = 999) -public abstract class ClientConfigurationNetworkHandlerMixin extends ClientCommonNetworkHandler implements NetworkHandlerExtensions { - @Unique - private ClientConfigurationNetworkAddon addon; - - protected ClientConfigurationNetworkHandlerMixin(MinecraftClient client, ClientConnection connection, ClientConnectionState connectionState) { - super(client, connection, connectionState); - } - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ClientConfigurationNetworkAddon((ClientConfigurationNetworkHandler) (Object) this, this.client); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - ClientNetworkingImpl.setClientConfigurationAddon(this.addon); - this.addon.lateInit(); - } - - @Inject(method = "onReady", at = @At(value = "NEW", target = "(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/network/ClientConnection;Lnet/minecraft/client/network/ClientConnectionState;)Lnet/minecraft/client/network/ClientPlayNetworkHandler;")) - public void handleComplete(ReadyS2CPacket packet, CallbackInfo ci) { - this.addon.handleComplete(); - } - - @Override - public ClientConfigurationNetworkAddon getAddon() { - return addon; - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientLoginNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientLoginNetworkHandlerMixin.java deleted file mode 100644 index d6aef39f14e..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientLoginNetworkHandlerMixin.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientLoginNetworkHandler; -import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.client.ClientLoginNetworkAddon; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; - -@Mixin(ClientLoginNetworkHandler.class) -abstract class ClientLoginNetworkHandlerMixin implements NetworkHandlerExtensions { - @Shadow - @Final - private MinecraftClient client; - - @Shadow - @Final - private ClientConnection connection; - - @Unique - private ClientLoginNetworkAddon addon; - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ClientLoginNetworkAddon((ClientLoginNetworkHandler) (Object) this, this.client); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - this.addon.lateInit(); - } - - @Inject(method = "onQueryRequest", at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V", remap = false, shift = At.Shift.AFTER), cancellable = true) - private void handleQueryRequest(LoginQueryRequestS2CPacket packet, CallbackInfo ci) { - if (packet.payload() instanceof PacketByteBufLoginQueryRequestPayload payload) { - if (this.addon.handlePacket(packet)) { - ci.cancel(); - } else { - payload.data().skipBytes(payload.data().readableBytes()); - } - } - } - - @Override - public ClientLoginNetworkAddon getAddon() { - return this.addon; - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java deleted file mode 100644 index 0fb8f697631..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/ClientPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientCommonNetworkHandler; -import net.minecraft.client.network.ClientConnectionState; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; -import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon; - -// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues -@Mixin(value = ClientPlayNetworkHandler.class, priority = 999) -abstract class ClientPlayNetworkHandlerMixin extends ClientCommonNetworkHandler implements NetworkHandlerExtensions { - @Unique - private ClientPlayNetworkAddon addon; - - protected ClientPlayNetworkHandlerMixin(MinecraftClient client, ClientConnection connection, ClientConnectionState connectionState) { - super(client, connection, connectionState); - } - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ClientPlayNetworkAddon((ClientPlayNetworkHandler) (Object) this, this.client); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - ClientNetworkingImpl.setClientPlayAddon(this.addon); - this.addon.lateInit(); - } - - @Inject(method = "onGameJoin", at = @At("RETURN")) - private void handleServerPlayReady(GameJoinS2CPacket packet, CallbackInfo ci) { - this.addon.onServerReady(); - } - - @Override - public ClientPlayNetworkAddon getAddon() { - return this.addon; - } -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkHandlerAccessor.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkAddonAccessor.java similarity index 74% rename from fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkHandlerAccessor.java rename to fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkAddonAccessor.java index 23bb8539db2..f9928b40590 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkHandlerAccessor.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientConfigurationNetworkAddonAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,14 @@ package net.fabricmc.fabric.mixin.networking.client.accessor; -import com.mojang.authlib.GameProfile; +import org.quiltmc.qsl.networking.impl.client.ClientConfigurationNetworkAddon; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import net.minecraft.client.network.ClientConfigurationNetworkHandler; -@Mixin(ClientConfigurationNetworkHandler.class) -public interface ClientConfigurationNetworkHandlerAccessor { +@Mixin(ClientConfigurationNetworkAddon.class) +public interface ClientConfigurationNetworkAddonAccessor { @Accessor - GameProfile getProfile(); + ClientConfigurationNetworkHandler getHandler(); } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientLoginNetworkHandlerAccessor.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientLoginNetworkHandlerAccessor.java deleted file mode 100644 index 0f128d10dc4..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientLoginNetworkHandlerAccessor.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client.accessor; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.network.ClientLoginNetworkHandler; -import net.minecraft.network.ClientConnection; - -@Mixin(ClientLoginNetworkHandler.class) -public interface ClientLoginNetworkHandlerAccessor { - @Accessor - ClientConnection getConnection(); -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientCommonNetworkHandlerAccessor.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientPlayNetworkAddonAccessor.java similarity index 71% rename from fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientCommonNetworkHandlerAccessor.java rename to fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientPlayNetworkAddonAccessor.java index 7400e6bb994..bc95bc3059e 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientCommonNetworkHandlerAccessor.java +++ b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ClientPlayNetworkAddonAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,14 @@ package net.fabricmc.fabric.mixin.networking.client.accessor; +import org.quiltmc.qsl.networking.impl.client.ClientPlayNetworkAddon; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; -import net.minecraft.client.network.ClientCommonNetworkHandler; -import net.minecraft.network.ClientConnection; +import net.minecraft.client.network.ClientPlayNetworkHandler; -@Mixin(ClientCommonNetworkHandler.class) -public interface ClientCommonNetworkHandlerAccessor { +@Mixin(ClientPlayNetworkAddon.class) +public interface ClientPlayNetworkAddonAccessor { @Accessor - ClientConnection getConnection(); + ClientPlayNetworkHandler getHandler(); } diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/MinecraftClientAccessor.java b/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/MinecraftClientAccessor.java deleted file mode 100644 index 7f7f760e4d3..00000000000 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/MinecraftClientAccessor.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.client.accessor; - -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.network.ClientConnection; - -@Mixin(MinecraftClient.class) -public interface MinecraftClientAccessor { - @Nullable - @Accessor("integratedServerConnection") - ClientConnection getConnection(); -} diff --git a/fabric-networking-api-v1/src/client/resources/fabric-networking-api-v1.client.mixins.json b/fabric-networking-api-v1/src/client/resources/fabric-networking-api-v1.client.mixins.json index 114fd6087bc..1f99263f40b 100644 --- a/fabric-networking-api-v1/src/client/resources/fabric-networking-api-v1.client.mixins.json +++ b/fabric-networking-api-v1/src/client/resources/fabric-networking-api-v1.client.mixins.json @@ -3,15 +3,8 @@ "package": "net.fabricmc.fabric.mixin.networking.client", "compatibilityLevel": "JAVA_17", "client": [ - "accessor.ClientCommonNetworkHandlerAccessor", - "accessor.ClientConfigurationNetworkHandlerAccessor", - "accessor.ClientLoginNetworkHandlerAccessor", - "accessor.ConnectScreenAccessor", - "accessor.MinecraftClientAccessor", - "ClientCommonNetworkHandlerMixin", - "ClientConfigurationNetworkHandlerMixin", - "ClientLoginNetworkHandlerMixin", - "ClientPlayNetworkHandlerMixin" + "accessor.ClientConfigurationNetworkAddonAccessor", + "accessor.ClientPlayNetworkAddonAccessor" ], "injectors": { "defaultRequire": 1 diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java index 6e93cff1fcf..df5209ba260 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,33 +21,34 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; /** * Events related to a tracking entities within a player's view distance. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.EntityTrackingEvents EntityTrackingEvents} */ +@Deprecated public final class EntityTrackingEvents { /** * An event that is called before player starts tracking an entity. * Typically, this occurs when an entity enters a client's view distance. * This event is called before the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}. */ - public static final Event START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> { - for (StartTracking callback : callbacks) { - callback.onStartTracking(trackedEntity, player); - } - }); + public static final Event START_TRACKING = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.EntityTrackingEvents.BEFORE_START_TRACKING, + startTracking -> startTracking::onStartTracking, + invokerGetter -> invokerGetter.get()::beforeStartTracking + ); /** * An event that is called after a player has stopped tracking an entity. * The client at this point was sent a packet to {@link net.minecraft.network.packet.s2c.play.EntitiesDestroyS2CPacket destroy} the entity on the client. * The entity still exists on the server. */ - public static final Event STOP_TRACKING = EventFactory.createArrayBacked(StopTracking.class, callbacks -> (trackedEntity, player) -> { - for (StopTracking callback : callbacks) { - callback.onStopTracking(trackedEntity, player); - } - }); + public static final Event STOP_TRACKING = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.EntityTrackingEvents.STOP_TRACKING, + stopTracking -> stopTracking::onStopTracking, + invokerGetter -> invokerGetter.get()::onStopTracking + ); @FunctionalInterface public interface StartTracking { diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/FabricServerConfigurationNetworkHandler.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/FabricServerConfigurationNetworkHandler.java index 69ee4ee6cad..d6b52b03690 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/FabricServerConfigurationNetworkHandler.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/FabricServerConfigurationNetworkHandler.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +24,11 @@ /** * Fabric-provided extensions for {@link ServerConfigurationNetworkHandler}. * This interface is automatically implemented via Mixin and interface injection. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerConfigurationTaskManager} */ -public interface FabricServerConfigurationNetworkHandler { +@Deprecated +public interface FabricServerConfigurationNetworkHandler extends org.quiltmc.qsl.networking.api.server.ServerConfigurationTaskManager { /** * Enqueues a {@link ServerPlayerConfigurationTask} task to be processed. * diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PacketByteBufs.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PacketByteBufs.java index 5a30554272f..01f33afeb9b 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PacketByteBufs.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PacketByteBufs.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,26 +17,24 @@ package net.fabricmc.fabric.api.networking.v1; -import java.util.Objects; - import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import net.minecraft.network.PacketByteBuf; /** * Helper methods for working with and creating {@link PacketByteBuf}s. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.PacketByteBufs PacketByteBufs} */ +@Deprecated public final class PacketByteBufs { - private static final PacketByteBuf EMPTY_PACKET_BYTE_BUF = new PacketByteBuf(Unpooled.EMPTY_BUFFER); - /** * Returns an empty instance of packet byte buf. * * @return an empty buf */ public static PacketByteBuf empty() { - return EMPTY_PACKET_BYTE_BUF; + return org.quiltmc.qsl.networking.api.PacketByteBufs.empty(); } /** @@ -44,7 +43,7 @@ public static PacketByteBuf empty() { * @return a new buf */ public static PacketByteBuf create() { - return new PacketByteBuf(Unpooled.buffer()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.create(); } // Convenience methods for byte buf methods that return a new byte buf @@ -58,9 +57,7 @@ public static PacketByteBuf create() { * @see ByteBuf#readBytes(int) */ public static PacketByteBuf readBytes(ByteBuf buf, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.readBytes(length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.readBytes(buf, length); } /** @@ -72,9 +69,7 @@ public static PacketByteBuf readBytes(ByteBuf buf, int length) { * @see ByteBuf#readSlice(int) */ public static PacketByteBuf readSlice(ByteBuf buf, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.readSlice(length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.readSlice(buf, length); } /** @@ -86,9 +81,7 @@ public static PacketByteBuf readSlice(ByteBuf buf, int length) { * @see ByteBuf#readRetainedSlice(int) */ public static PacketByteBuf readRetainedSlice(ByteBuf buf, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.readRetainedSlice(length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.readRetainedSlice(buf, length); } /** @@ -99,9 +92,7 @@ public static PacketByteBuf readRetainedSlice(ByteBuf buf, int length) { * @see ByteBuf#copy() */ public static PacketByteBuf copy(ByteBuf buf) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.copy()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.copy(buf); } /** @@ -114,9 +105,7 @@ public static PacketByteBuf copy(ByteBuf buf) { * @see ByteBuf#copy(int, int) */ public static PacketByteBuf copy(ByteBuf buf, int index, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.copy(index, length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.copy(buf, index, length); } /** @@ -127,9 +116,7 @@ public static PacketByteBuf copy(ByteBuf buf, int index, int length) { * @see ByteBuf#slice() */ public static PacketByteBuf slice(ByteBuf buf) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.slice()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.slice(buf); } /** @@ -140,9 +127,7 @@ public static PacketByteBuf slice(ByteBuf buf) { * @see ByteBuf#retainedSlice() */ public static PacketByteBuf retainedSlice(ByteBuf buf) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.retainedSlice()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.retainedSlice(buf); } /** @@ -155,9 +140,7 @@ public static PacketByteBuf retainedSlice(ByteBuf buf) { * @see ByteBuf#slice(int, int) */ public static PacketByteBuf slice(ByteBuf buf, int index, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.slice(index, length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.slice(buf, index, length); } /** @@ -170,9 +153,7 @@ public static PacketByteBuf slice(ByteBuf buf, int index, int length) { * @see ByteBuf#retainedSlice(int, int) */ public static PacketByteBuf retainedSlice(ByteBuf buf, int index, int length) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.retainedSlice(index, length)); + return org.quiltmc.qsl.networking.api.PacketByteBufs.retainedSlice(buf, index, length); } /** @@ -183,9 +164,7 @@ public static PacketByteBuf retainedSlice(ByteBuf buf, int index, int length) { * @see ByteBuf#duplicate() */ public static PacketByteBuf duplicate(ByteBuf buf) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.duplicate()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.duplicate(buf); } /** @@ -196,9 +175,7 @@ public static PacketByteBuf duplicate(ByteBuf buf) { * @see ByteBuf#retainedDuplicate() */ public static PacketByteBuf retainedDuplicate(ByteBuf buf) { - Objects.requireNonNull(buf, "ByteBuf cannot be null"); - - return new PacketByteBuf(buf.retainedDuplicate()); + return org.quiltmc.qsl.networking.api.PacketByteBufs.retainedDuplicate(buf); } private PacketByteBufs() { diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PayloadTypeRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PayloadTypeRegistry.java index 0b8bb7b2023..281a96f13dc 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PayloadTypeRegistry.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PayloadTypeRegistry.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,11 +24,14 @@ import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; -import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * A registry for payload types. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.PayloadTypeRegistry PayloadTypeRegistry} */ +@Deprecated @ApiStatus.NonExtendable public interface PayloadTypeRegistry { /** @@ -47,27 +51,27 @@ public interface PayloadTypeRegistry { * @return the {@link PayloadTypeRegistry} instance for the client to server configuration channel. */ static PayloadTypeRegistry configurationC2S() { - return PayloadTypeRegistryImpl.CONFIGURATION_C2S; + return QuiltUtil.fromQuilt(org.quiltmc.qsl.networking.api.PayloadTypeRegistry.configurationC2S()); } /** * @return the {@link PayloadTypeRegistry} instance for the server to client configuration channel. */ static PayloadTypeRegistry configurationS2C() { - return PayloadTypeRegistryImpl.CONFIGURATION_S2C; + return QuiltUtil.fromQuilt(org.quiltmc.qsl.networking.api.PayloadTypeRegistry.configurationS2C()); } /** * @return the {@link PayloadTypeRegistry} instance for the client to server play channel. */ static PayloadTypeRegistry playC2S() { - return PayloadTypeRegistryImpl.PLAY_C2S; + return QuiltUtil.fromQuilt(org.quiltmc.qsl.networking.api.PayloadTypeRegistry.playC2S()); } /** * @return the {@link PayloadTypeRegistry} instance for the server to client play channel. */ static PayloadTypeRegistry playS2C() { - return PayloadTypeRegistryImpl.PLAY_S2C; + return QuiltUtil.fromQuilt(org.quiltmc.qsl.networking.api.PayloadTypeRegistry.playS2C()); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PlayerLookup.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PlayerLookup.java index 5543cb8a81b..b15de6f9ba5 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PlayerLookup.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PlayerLookup.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,26 +18,16 @@ package net.fabricmc.fabric.api.networking.v1; import java.util.Collection; -import java.util.Collections; -import java.util.Objects; -import java.util.stream.Collectors; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.Entity; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.PlayerAssociatedNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.server.world.ServerChunkLoadingManager; -import net.minecraft.server.world.ServerChunkManager; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3i; -import net.minecraft.world.chunk.ChunkManager; - -import net.fabricmc.fabric.mixin.networking.accessor.EntityTrackerAccessor; -import net.fabricmc.fabric.mixin.networking.accessor.ServerChunkLoadingManagerAccessor; /** * Helper methods to lookup players in a server. @@ -44,7 +35,10 @@ *

The word "tracking" means that an entity/chunk on the server is known to a player's client (within in view distance) and the (block) entity should notify tracking clients of changes. * *

These methods should only be called on the server thread and only be used on logical a server. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.PlayerLookup PlayerLookup} */ +@Deprecated public final class PlayerLookup { /** * Gets all the players on the minecraft server. @@ -55,14 +49,7 @@ public final class PlayerLookup { * @return all players on the server */ public static Collection all(MinecraftServer server) { - Objects.requireNonNull(server, "The server cannot be null"); - - // return an immutable collection to guard against accidental removals. - if (server.getPlayerManager() != null) { - return Collections.unmodifiableCollection(server.getPlayerManager().getPlayerList()); - } - - return Collections.emptyList(); + return org.quiltmc.qsl.networking.api.PlayerLookup.all(server); } /** @@ -74,10 +61,7 @@ public static Collection all(MinecraftServer server) { * @return the players in the server world */ public static Collection world(ServerWorld world) { - Objects.requireNonNull(world, "The world cannot be null"); - - // return an immutable collection to guard against accidental removals. - return Collections.unmodifiableCollection(world.getPlayers()); + return org.quiltmc.qsl.networking.api.PlayerLookup.world(world); } /** @@ -88,10 +72,7 @@ public static Collection world(ServerWorld world) { * @return the players tracking the chunk */ public static Collection tracking(ServerWorld world, ChunkPos pos) { - Objects.requireNonNull(world, "The world cannot be null"); - Objects.requireNonNull(pos, "The chunk pos cannot be null"); - - return world.getChunkManager().chunkLoadingManager.getPlayersWatchingChunk(pos, false); + return org.quiltmc.qsl.networking.api.PlayerLookup.tracking(world, pos); } /** @@ -108,23 +89,7 @@ public static Collection tracking(ServerWorld world, ChunkPo * @throws IllegalArgumentException if the entity is not in a server world */ public static Collection tracking(Entity entity) { - Objects.requireNonNull(entity, "Entity cannot be null"); - ChunkManager manager = entity.getWorld().getChunkManager(); - - if (manager instanceof ServerChunkManager) { - ServerChunkLoadingManager chunkLoadingManager = ((ServerChunkManager) manager).chunkLoadingManager; - EntityTrackerAccessor tracker = ((ServerChunkLoadingManagerAccessor) chunkLoadingManager).getEntityTrackers().get(entity.getId()); - - // return an immutable collection to guard against accidental removals. - if (tracker != null) { - return tracker.getPlayersTracking() - .stream().map(PlayerAssociatedNetworkHandler::getPlayer).collect(Collectors.toUnmodifiableSet()); - } - - return Collections.emptySet(); - } - - throw new IllegalArgumentException("Only supported on server worlds!"); + return org.quiltmc.qsl.networking.api.PlayerLookup.tracking(entity); } /** @@ -135,14 +100,7 @@ public static Collection tracking(Entity entity) { * @throws IllegalArgumentException if the block entity is not in a server world */ public static Collection tracking(BlockEntity blockEntity) { - Objects.requireNonNull(blockEntity, "BlockEntity cannot be null"); - - //noinspection ConstantConditions - IJ intrinsics don't know hasWorld == true will result in no null - if (!blockEntity.hasWorld() || blockEntity.getWorld().isClient()) { - throw new IllegalArgumentException("Only supported on server worlds!"); - } - - return tracking((ServerWorld) blockEntity.getWorld(), blockEntity.getPos()); + return org.quiltmc.qsl.networking.api.PlayerLookup.tracking(blockEntity); } /** @@ -153,9 +111,7 @@ public static Collection tracking(BlockEntity blockEntity) { * @return the players tracking the block position */ public static Collection tracking(ServerWorld world, BlockPos pos) { - Objects.requireNonNull(pos, "BlockPos cannot be null"); - - return tracking(world, new ChunkPos(pos)); + return org.quiltmc.qsl.networking.api.PlayerLookup.tracking(world, pos); } /** @@ -169,12 +125,7 @@ public static Collection tracking(ServerWorld world, BlockPo * @return the players around the position */ public static Collection around(ServerWorld world, Vec3d pos, double radius) { - double radiusSq = radius * radius; - - return world(world) - .stream() - .filter((p) -> p.squaredDistanceTo(pos) <= radiusSq) - .collect(Collectors.toList()); + return org.quiltmc.qsl.networking.api.PlayerLookup.around(world, pos, radius); } /** @@ -188,12 +139,7 @@ public static Collection around(ServerWorld world, Vec3d pos * @return the players around the position */ public static Collection around(ServerWorld world, Vec3i pos, double radius) { - double radiusSq = radius * radius; - - return world(world) - .stream() - .filter((p) -> p.squaredDistanceTo(pos.getX(), pos.getY(), pos.getZ()) <= radiusSq) - .collect(Collectors.toList()); + return org.quiltmc.qsl.networking.api.PlayerLookup.around(world, pos, radius); } private PlayerLookup() { diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CConfigurationChannelEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CConfigurationChannelEvents.java index ba05d648ffc..b2ac5daf1b7 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CConfigurationChannelEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CConfigurationChannelEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,31 +24,33 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the indication of a connected client's ability to receive packets in certain channels. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.S2CConfigurationChannelEvents S2CConfigurationChannelEvents} */ +@Deprecated public final class S2CConfigurationChannelEvents { /** * An event for the server configuration network handler receiving an update indicating the connected client's ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. */ - public static final Event REGISTER = EventFactory.createArrayBacked(Register.class, callbacks -> (handler, sender, server, channels) -> { - for (Register callback : callbacks) { - callback.onChannelRegister(handler, sender, server, channels); - } - }); + public static final Event REGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.S2CConfigurationChannelEvents.REGISTER, + register -> (handler, sender, server, channels) -> register.onChannelRegister(handler, QuiltUtil.toFabricSender(sender), server, QuiltUtil.toIdentifiers(channels)), + register -> (handler, sender, server, channels) -> register.get().onChannelRegister(handler, QuiltUtil.toQuiltSender(sender), server, QuiltUtil.toPayloadIds(channels)) + ); /** * An event for the server configuration network handler receiving an update indicating the connected client's lack of ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. */ - public static final Event UNREGISTER = EventFactory.createArrayBacked(Unregister.class, callbacks -> (handler, sender, server, channels) -> { - for (Unregister callback : callbacks) { - callback.onChannelUnregister(handler, sender, server, channels); - } - }); + public static final Event UNREGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.S2CConfigurationChannelEvents.UNREGISTER, + register -> (handler, sender, server, channels) -> register.onChannelUnregister(handler, QuiltUtil.toFabricSender(sender), server, QuiltUtil.toIdentifiers(channels)), + register -> (handler, sender, server, channels) -> register.get().onChannelUnregister(handler, QuiltUtil.toQuiltSender(sender), server, QuiltUtil.toPayloadIds(channels)) + ); private S2CConfigurationChannelEvents() { } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CPlayChannelEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CPlayChannelEvents.java index 93d2881b23d..30759ae721c 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CPlayChannelEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/S2CPlayChannelEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,31 +24,33 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the indication of a connected client's ability to receive packets in certain channels. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.S2CPlayChannelEvents S2CPlayChannelEvents} */ +@Deprecated public final class S2CPlayChannelEvents { /** * An event for the server play network handler receiving an update indicating the connected client's ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. */ - public static final Event REGISTER = EventFactory.createArrayBacked(Register.class, callbacks -> (handler, sender, server, channels) -> { - for (Register callback : callbacks) { - callback.onChannelRegister(handler, sender, server, channels); - } - }); + public static final Event REGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.S2CPlayChannelEvents.REGISTER, + register -> (handler, sender, server, channels) -> register.onChannelRegister(handler, QuiltUtil.toFabricSender(sender), server, QuiltUtil.toIdentifiers(channels)), + register -> (handler, sender, server, channels) -> register.get().onChannelRegister(handler, QuiltUtil.toQuiltSender(sender), server, QuiltUtil.toPayloadIds(channels)) + ); /** * An event for the server play network handler receiving an update indicating the connected client's lack of ability to receive packets in certain channels. * This event may be invoked at any time after login and up to disconnection. */ - public static final Event UNREGISTER = EventFactory.createArrayBacked(Unregister.class, callbacks -> (handler, sender, server, channels) -> { - for (Unregister callback : callbacks) { - callback.onChannelUnregister(handler, sender, server, channels); - } - }); + public static final Event UNREGISTER = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.S2CPlayChannelEvents.UNREGISTER, + register -> (handler, sender, server, channels) -> register.onChannelUnregister(handler, QuiltUtil.toFabricSender(sender), server, QuiltUtil.toIdentifiers(channels)), + register -> (handler, sender, server, channels) -> register.get().onChannelUnregister(handler, QuiltUtil.toQuiltSender(sender), server, QuiltUtil.toPayloadIds(channels)) + ); private S2CPlayChannelEvents() { } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationConnectionEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationConnectionEvents.java index 3a1452f99df..520d5c19a44 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationConnectionEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +21,14 @@ import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; /** * Offers access to events related to the connection to a client on a logical server while a client is configuring. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerConfigurationConnectionEvents ServerConfigurationConnectionEvents} */ +@Deprecated public final class ServerConfigurationConnectionEvents { /** * Event fired before any vanilla configuration has taken place. @@ -33,11 +37,11 @@ public final class ServerConfigurationConnectionEvents { * *

Task queued during this event will complete before vanilla configuration starts. */ - public static final Event BEFORE_CONFIGURE = EventFactory.createArrayBacked(Configure.class, callbacks -> (handler, server) -> { - for (Configure callback : callbacks) { - callback.onSendConfiguration(handler, server); - } - }); + public static final Event BEFORE_CONFIGURE = QuiltCompatEvent.fromQuilt( + org.quiltmc.qsl.networking.api.server.ServerConfigurationConnectionEvents.INIT, + configure -> configure::onSendConfiguration, + invokerGetter -> invokerGetter.get()::onConfigurationInit + ); /** * Event fired during vanilla configuration. @@ -56,22 +60,22 @@ public final class ServerConfigurationConnectionEvents { * }); * } */ - public static final Event CONFIGURE = EventFactory.createArrayBacked(Configure.class, callbacks -> (handler, server) -> { - for (Configure callback : callbacks) { - callback.onSendConfiguration(handler, server); - } - }); + public static final Event CONFIGURE = QuiltCompatEvent.fromQuilt( + org.quiltmc.qsl.networking.api.server.ServerConfigurationConnectionEvents.ADD_TASKS, + configure -> configure::onSendConfiguration, + invokerGetter -> invokerGetter.get()::onAddTasks + ); /** * An event for the disconnection of the server configuration network handler. * *

No packets should be sent when this event is invoked. */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(ServerConfigurationConnectionEvents.Disconnect.class, callbacks -> (handler, server) -> { - for (ServerConfigurationConnectionEvents.Disconnect callback : callbacks) { - callback.onConfigureDisconnect(handler, server); - } - }); + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt( + org.quiltmc.qsl.networking.api.server.ServerConfigurationConnectionEvents.DISCONNECT, + configure -> configure::onConfigureDisconnect, + invokerGetter -> invokerGetter.get()::onConfigurationDisconnect + ); private ServerConfigurationConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java index 7f6c36cef83..1892dfdadf0 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerConfigurationNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +17,6 @@ package net.fabricmc.fabric.api.networking.v1; -import java.util.Objects; import java.util.Set; import org.jetbrains.annotations.ApiStatus; @@ -30,8 +30,7 @@ import net.minecraft.util.Identifier; import net.minecraft.util.thread.ThreadExecutor; -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; -import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to configuration stage server-side networking functionalities. @@ -49,7 +48,10 @@ * * @see ServerLoginNetworking * @see ServerConfigurationNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking ServerConfigurationNetworking} */ +@Deprecated public final class ServerConfigurationNetworking { /** * Registers a handler for a payload type. @@ -66,7 +68,7 @@ public final class ServerConfigurationNetworking { * @see ServerConfigurationNetworking#registerReceiver(ServerConfigurationNetworkHandler, CustomPayload.Id, ConfigurationPacketHandler) */ public static boolean registerGlobalReceiver(CustomPayload.Id type, ConfigurationPacketHandler handler) { - return ServerNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.id(), handler); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.registerGlobalReceiver(type, handler); } /** @@ -83,7 +85,8 @@ public static boolean registerGlobalReceiver(CustomPay */ @Nullable public static ServerConfigurationNetworking.ConfigurationPacketHandler unregisterGlobalReceiver(Identifier id) { - return ServerNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(id); + var old = org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.unregisterGlobalReceiver(new CustomPayload.Id<>(id)); + return convertToFabricHandler(old); } /** @@ -93,7 +96,7 @@ public static ServerConfigurationNetworking.ConfigurationPacketHandler unregi * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ServerNetworkingImpl.CONFIGURATION.getChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.getGlobalReceivers()); } /** @@ -112,7 +115,7 @@ public static Set getGlobalReceivers() { * @see ServerPlayConnectionEvents#INIT */ public static boolean registerReceiver(ServerConfigurationNetworkHandler networkHandler, CustomPayload.Id type, ConfigurationPacketHandler handler) { - return ServerNetworkingImpl.getAddon(networkHandler).registerChannel(type.id(), handler); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.registerReceiver(networkHandler, type, handler); } /** @@ -126,7 +129,8 @@ public static boolean registerReceiver(ServerConfigura */ @Nullable public static ServerConfigurationNetworking.ConfigurationPacketHandler unregisterReceiver(ServerConfigurationNetworkHandler networkHandler, Identifier id) { - return ServerNetworkingImpl.getAddon(networkHandler).unregisterChannel(id); + var old = org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.unregisterReceiver(networkHandler, new CustomPayload.Id<>(id)); + return convertToFabricHandler(old); } /** @@ -136,9 +140,7 @@ public static ServerConfigurationNetworking.ConfigurationPacketHandler unregi * @return All the channel names that the server can receive packets on */ public static Set getReceived(ServerConfigurationNetworkHandler handler) { - Objects.requireNonNull(handler, "Server configuration network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getReceivableChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.getReceived(handler)); } /** @@ -148,9 +150,7 @@ public static Set getReceived(ServerConfigurationNetworkHandler hand * @return {@code true} if the connected client has declared the ability to receive a packet on the specified channel */ public static Set getSendable(ServerConfigurationNetworkHandler handler) { - Objects.requireNonNull(handler, "Server configuration network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.getSendable(handler)); } /** @@ -161,10 +161,7 @@ public static Set getSendable(ServerConfigurationNetworkHandler hand * @return {@code true} if the connected client has declared the ability to receive a packet on the specified channel */ public static boolean canSend(ServerConfigurationNetworkHandler handler, Identifier channelName) { - Objects.requireNonNull(handler, "Server configuration network handler cannot be null"); - Objects.requireNonNull(channelName, "Channel name cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels().contains(channelName); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.canSend(handler, new CustomPayload.Id<>(channelName)); } /** @@ -175,10 +172,7 @@ public static boolean canSend(ServerConfigurationNetworkHandler handler, Identif * @return {@code true} if the connected client has declared the ability to receive a specific type of packet */ public static boolean canSend(ServerConfigurationNetworkHandler handler, CustomPayload.Id id) { - Objects.requireNonNull(handler, "Server configuration network handler cannot be null"); - Objects.requireNonNull(id, "Payload id cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels().contains(id.id()); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.canSend(handler, id); } /** @@ -188,10 +182,7 @@ public static boolean canSend(ServerConfigurationNetworkHandler handler, CustomP * @return a new packet */ public static Packet createS2CPacket(CustomPayload payload) { - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - return ServerNetworkingImpl.createS2CPacket(payload); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.createS2CPacket(payload); } /** @@ -201,9 +192,7 @@ public static Packet createS2CPacket(CustomPayload p * @return the packet sender */ public static PacketSender getSender(ServerConfigurationNetworkHandler handler) { - Objects.requireNonNull(handler, "Server configuration network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler); + return QuiltUtil.toFabricSender(org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.getSender(handler)); } /** @@ -215,11 +204,7 @@ public static PacketSender getSender(ServerConfigurationNetworkHandler handler) * @param payload to be sent */ public static void send(ServerConfigurationNetworkHandler handler, CustomPayload payload) { - Objects.requireNonNull(handler, "Server configuration handler cannot be null"); - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - handler.sendPacket(createS2CPacket(payload)); + org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.send(handler, payload); } // Helper methods @@ -230,9 +215,7 @@ public static void send(ServerConfigurationNetworkHandler handler, CustomPayload * @param handler the server configuration network handler */ public static MinecraftServer getServer(ServerConfigurationNetworkHandler handler) { - Objects.requireNonNull(handler, "Network handler cannot be null"); - - return ((ServerCommonNetworkHandlerAccessor) handler).getServer(); + return org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.getServer(handler); } private ServerConfigurationNetworking() { @@ -243,7 +226,7 @@ private ServerConfigurationNetworking() { * @param the type of the packet */ @FunctionalInterface - public interface ConfigurationPacketHandler { + public interface ConfigurationPacketHandler extends org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.CustomChannelReceiver { /** * Handles an incoming packet. * @@ -264,6 +247,12 @@ public interface ConfigurationPacketHandler { * @see CustomPayload */ void receive(T payload, Context context); + + @Override + default void receive(MinecraftServer server, ServerConfigurationNetworkHandler handler, T payload, org.quiltmc.qsl.networking.api.PacketSender responseSender) { + record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHandler networkHandler, PacketSender responseSender) implements Context {} + this.receive(payload, new ContextImpl(server, handler, QuiltUtil.toFabricSender(responseSender))); + } } @ApiStatus.NonExtendable @@ -283,4 +272,21 @@ public interface Context { */ PacketSender responseSender(); } + + private static @Nullable ConfigurationPacketHandler convertToFabricHandler(org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.CustomChannelReceiver old) { + if (old instanceof ConfigurationPacketHandler) { + return (ConfigurationPacketHandler) old; + } else if (old != null) { + return (payload, context) -> { + if (old instanceof org.quiltmc.qsl.networking.api.server.ServerConfigurationNetworking.CustomChannelReceiver r) { + org.quiltmc.qsl.networking.api.PacketSender sender = QuiltUtil.toQuiltSender(context.responseSender()); + r.receive(context.server(), context.networkHandler(), payload, sender); + } else { + // This should never happen! + throw new UnsupportedOperationException("Unknown receiver type: " + old.getClass().getName()); + } + }; + } + return null; + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginConnectionEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginConnectionEvents.java index 935f747aeda..fe268049c9f 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginConnectionEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,22 +22,25 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the connection to a client on a logical server while a client is logging in. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerLoginConnectionEvents ServerLoginConnectionEvents} */ +@Deprecated public final class ServerLoginConnectionEvents { /** * Event indicating a connection entered the LOGIN state, ready for registering query response handlers. * * @see ServerLoginNetworking#registerReceiver(ServerLoginNetworkHandler, Identifier, ServerLoginNetworking.LoginQueryResponseHandler) */ - public static final Event INIT = EventFactory.createArrayBacked(Init.class, callbacks -> (handler, server) -> { - for (Init callback : callbacks) { - callback.onLoginInit(handler, server); - } - }); + public static final Event INIT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerLoginConnectionEvents.INIT, + init -> init::onLoginInit, + invokerGetter -> invokerGetter.get()::onLoginInit + ); /** * An event for the start of login queries of the server login network handler. @@ -46,22 +50,20 @@ public final class ServerLoginConnectionEvents { * *

You may send login queries to the connected client using the provided {@link LoginPacketSender}. */ - public static final Event QUERY_START = EventFactory.createArrayBacked(QueryStart.class, callbacks -> (handler, server, sender, synchronizer) -> { - for (QueryStart callback : callbacks) { - callback.onLoginStart(handler, server, sender, synchronizer); - } - }); + public static final Event QUERY_START = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerLoginConnectionEvents.QUERY_START, + queryStart -> (handler, server, sender, synchronizer) -> queryStart.onLoginStart(handler, server, QuiltUtil.toFabricLoginSender(sender), synchronizer::waitFor), + invokerGetter -> (handler, server, sender, synchronizer) -> invokerGetter.get().onLoginStart(handler, server, QuiltUtil.toQuiltLoginSender(sender), synchronizer) + ); /** * An event for the disconnection of the server login network handler. * *

No packets should be sent when this event is invoked. */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(Disconnect.class, callbacks -> (handler, server) -> { - for (Disconnect callback : callbacks) { - callback.onLoginDisconnect(handler, server); - } - }); + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerLoginConnectionEvents.DISCONNECT, + disconnect -> disconnect::onLoginDisconnect, + invokerGetter -> invokerGetter.get()::onLoginDisconnect + ); private ServerLoginConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginNetworking.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginNetworking.java index 4723f83c1a1..9e7bd62738b 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginNetworking.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerLoginNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +17,6 @@ package net.fabricmc.fabric.api.networking.v1; -import java.util.Objects; import java.util.Set; import java.util.concurrent.Future; @@ -28,8 +28,7 @@ import net.minecraft.server.network.ServerLoginNetworkHandler; import net.minecraft.util.Identifier; -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; -import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkHandlerAccessor; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to login stage server-side networking functionalities. @@ -38,7 +37,10 @@ * * @see ServerPlayNetworking * @see ServerConfigurationNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerLoginNetworking ServerLoginNetworking} */ +@Deprecated public final class ServerLoginNetworking { /** * Registers a handler to a query response channel. @@ -54,7 +56,7 @@ public final class ServerLoginNetworking { * @see ServerLoginNetworking#registerReceiver(ServerLoginNetworkHandler, Identifier, LoginQueryResponseHandler) */ public static boolean registerGlobalReceiver(Identifier channelName, LoginQueryResponseHandler channelHandler) { - return ServerNetworkingImpl.LOGIN.registerGlobalReceiver(channelName, channelHandler); + return org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.registerGlobalReceiver(channelName, channelHandler); } /** @@ -70,7 +72,9 @@ public static boolean registerGlobalReceiver(Identifier channelName, LoginQueryR */ @Nullable public static ServerLoginNetworking.LoginQueryResponseHandler unregisterGlobalReceiver(Identifier channelName) { - return ServerNetworkingImpl.LOGIN.unregisterGlobalReceiver(channelName); + var old = org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.unregisterGlobalReceiver(channelName); + + return convertToFabricHandler(old); } /** @@ -80,7 +84,7 @@ public static ServerLoginNetworking.LoginQueryResponseHandler unregisterGlobalRe * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ServerNetworkingImpl.LOGIN.getChannels(); + return org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.getGlobalReceivers(); } /** @@ -95,9 +99,7 @@ public static Set getGlobalReceivers() { * @return false if a handler is already registered to the channel name */ public static boolean registerReceiver(ServerLoginNetworkHandler networkHandler, Identifier channelName, LoginQueryResponseHandler responseHandler) { - Objects.requireNonNull(networkHandler, "Network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(networkHandler).registerChannel(channelName, responseHandler); + return org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.registerReceiver(networkHandler, channelName, responseHandler); } /** @@ -110,9 +112,9 @@ public static boolean registerReceiver(ServerLoginNetworkHandler networkHandler, */ @Nullable public static ServerLoginNetworking.LoginQueryResponseHandler unregisterReceiver(ServerLoginNetworkHandler networkHandler, Identifier channelName) { - Objects.requireNonNull(networkHandler, "Network handler cannot be null"); + var old = org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.unregisterReceiver(networkHandler, channelName); - return ServerNetworkingImpl.getAddon(networkHandler).unregisterChannel(channelName); + return convertToFabricHandler(old); } // Helper methods @@ -123,16 +125,14 @@ public static ServerLoginNetworking.LoginQueryResponseHandler unregisterReceiver * @param handler the server login network handler */ public static MinecraftServer getServer(ServerLoginNetworkHandler handler) { - Objects.requireNonNull(handler, "Network handler cannot be null"); - - return ((ServerLoginNetworkHandlerAccessor) handler).getServer(); + return org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.getServer(handler); } private ServerLoginNetworking() { } @FunctionalInterface - public interface LoginQueryResponseHandler { + public interface LoginQueryResponseHandler extends org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.QueryResponseReceiver { /** * Handles an incoming query response from a client. * @@ -148,6 +148,11 @@ public interface LoginQueryResponseHandler { * @param responseSender the packet sender */ void receive(MinecraftServer server, ServerLoginNetworkHandler handler, boolean understood, PacketByteBuf buf, LoginSynchronizer synchronizer, PacketSender responseSender); + + @Override + default void receive(MinecraftServer server, ServerLoginNetworkHandler handler, boolean understood, PacketByteBuf buf, org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.LoginSynchronizer synchronizer, org.quiltmc.qsl.networking.api.LoginPacketSender responseSender) { + this.receive(server, handler, understood, buf, synchronizer::waitFor, QuiltUtil.toFabricSender(responseSender)); + } } /** @@ -155,7 +160,7 @@ public interface LoginQueryResponseHandler { */ @FunctionalInterface @ApiStatus.NonExtendable - public interface LoginSynchronizer { + public interface LoginSynchronizer extends org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.LoginSynchronizer { /** * Allows blocking client log-in until the {@code future} is {@link Future#isDone() done}. * @@ -194,4 +199,14 @@ public interface LoginSynchronizer { */ void waitFor(Future future); } + + private static @Nullable ServerLoginNetworking.LoginQueryResponseHandler convertToFabricHandler(org.quiltmc.qsl.networking.api.server.ServerLoginNetworking.QueryResponseReceiver old) { + if (old instanceof LoginQueryResponseHandler fabric) { + return fabric; + } else if (old != null) { + return (server, handler, understood, buf, sync, sender) -> old.receive(server, handler, understood, buf, sync, QuiltUtil.toQuiltLoginSender(sender)); + } else { + return null; + } + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayConnectionEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayConnectionEvents.java index fc93a8dc774..997b07f0a20 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayConnectionEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayConnectionEvents.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,44 +22,45 @@ import net.minecraft.server.network.ServerPlayNetworkHandler; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to events related to the connection to a client on a logical server while a client is in game. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerPlayConnectionEvents} */ +@Deprecated public final class ServerPlayConnectionEvents { /** * Event indicating a connection entered the PLAY state, ready for registering channel handlers. * * @see ServerPlayNetworking#registerReceiver(ServerPlayNetworkHandler, CustomPayload.Id, ServerPlayNetworking.PlayPayloadHandler) */ - public static final Event INIT = EventFactory.createArrayBacked(Init.class, callbacks -> (handler, server) -> { - for (Init callback : callbacks) { - callback.onPlayInit(handler, server); - } - }); + public static final Event INIT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerPlayConnectionEvents.INIT, + init -> init::onPlayInit, + invokerGetter -> invokerGetter.get()::onPlayInit + ); /** * An event for notification when the server play network handler is ready to send packets to the client. * *

At this stage, the network handler is ready to send packets to the client. */ - public static final Event JOIN = EventFactory.createArrayBacked(Join.class, callbacks -> (handler, sender, server) -> { - for (Join callback : callbacks) { - callback.onPlayReady(handler, sender, server); - } - }); + public static final Event JOIN = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerPlayConnectionEvents.JOIN, + join -> (handler, sender, server) -> join.onPlayReady(handler, QuiltUtil.toFabricSender(sender), server), + invokerGetter -> (handler, sender, server) -> invokerGetter.get().onPlayReady(handler, QuiltUtil.toQuiltSender(sender), server) + ); /** * An event for the disconnection of the server play network handler. * *

No packets should be sent when this event is invoked. */ - public static final Event DISCONNECT = EventFactory.createArrayBacked(Disconnect.class, callbacks -> (handler, server) -> { - for (Disconnect callback : callbacks) { - callback.onPlayDisconnect(handler, server); - } - }); + public static final Event DISCONNECT = QuiltCompatEvent.fromQuilt(org.quiltmc.qsl.networking.api.server.ServerPlayConnectionEvents.DISCONNECT, + disconnect -> disconnect::onPlayDisconnect, + invokerGetter -> invokerGetter.get()::onPlayDisconnect + ); private ServerPlayConnectionEvents() { } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java index 23d415919b0..1f35a8e8f55 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/ServerPlayNetworking.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +17,6 @@ package net.fabricmc.fabric.api.networking.v1; -import java.util.Objects; import java.util.Set; import org.jetbrains.annotations.ApiStatus; @@ -30,7 +30,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; +import net.fabricmc.fabric.impl.networking.QuiltUtil; /** * Offers access to play stage server-side networking functionalities. @@ -59,7 +59,10 @@ * * @see ServerLoginNetworking * @see ServerConfigurationNetworking + * + * @deprecated see {@link org.quiltmc.qsl.networking.api.server.ServerPlayNetworking ServerPlayNetworking} */ +@Deprecated public final class ServerPlayNetworking { /** * Registers a handler for a payload type. @@ -75,7 +78,7 @@ public final class ServerPlayNetworking { * @see ServerPlayNetworking#unregisterGlobalReceiver(Identifier) */ public static boolean registerGlobalReceiver(CustomPayload.Id type, PlayPayloadHandler handler) { - return ServerNetworkingImpl.PLAY.registerGlobalReceiver(type.id(), handler); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.registerGlobalReceiver(type, handler); } /** @@ -92,7 +95,8 @@ public static boolean registerGlobalReceiver(CustomPay */ @Nullable public static ServerPlayNetworking.PlayPayloadHandler unregisterGlobalReceiver(Identifier id) { - return ServerNetworkingImpl.PLAY.unregisterGlobalReceiver(id); + var old = org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.unregisterGlobalReceiver(new CustomPayload.Id<>(id)); + return convertToFabricHandler(old); } /** @@ -102,7 +106,7 @@ public static ServerPlayNetworking.PlayPayloadHandler unregisterGlobalReceive * @return all channel names which global receivers are registered for. */ public static Set getGlobalReceivers() { - return ServerNetworkingImpl.PLAY.getChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getGlobalReceivers()); } /** @@ -124,7 +128,7 @@ public static Set getGlobalReceivers() { * @see ServerPlayConnectionEvents#INIT */ public static boolean registerReceiver(ServerPlayNetworkHandler networkHandler, CustomPayload.Id type, PlayPayloadHandler handler) { - return ServerNetworkingImpl.getAddon(networkHandler).registerChannel(type.id(), handler); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.registerReceiver(networkHandler, type, handler); } /** @@ -138,7 +142,8 @@ public static boolean registerReceiver(ServerPlayNetwo */ @Nullable public static ServerPlayNetworking.PlayPayloadHandler unregisterReceiver(ServerPlayNetworkHandler networkHandler, Identifier id) { - return ServerNetworkingImpl.getAddon(networkHandler).unregisterChannel(id); + var old = org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.unregisterReceiver(networkHandler, new CustomPayload.Id<>(id)); + return convertToFabricHandler(old); } /** @@ -148,9 +153,7 @@ public static ServerPlayNetworking.PlayPayloadHandler unregisterReceiver(Serv * @return All the channel names that the server can receive packets on */ public static Set getReceived(ServerPlayerEntity player) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - - return getReceived(player.networkHandler); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getReceived(player)); } /** @@ -160,9 +163,7 @@ public static Set getReceived(ServerPlayerEntity player) { * @return All the channel names that the server can receive packets on */ public static Set getReceived(ServerPlayNetworkHandler handler) { - Objects.requireNonNull(handler, "Server play network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getReceivableChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getReceived(handler)); } /** @@ -172,9 +173,7 @@ public static Set getReceived(ServerPlayNetworkHandler handler) { * @return All the channel names the connected client declared the ability to receive a packets on */ public static Set getSendable(ServerPlayerEntity player) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - - return getSendable(player.networkHandler); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getSendable(player)); } /** @@ -184,9 +183,7 @@ public static Set getSendable(ServerPlayerEntity player) { * @return {@code true} if the connected client has declared the ability to receive a packet on the specified channel */ public static Set getSendable(ServerPlayNetworkHandler handler) { - Objects.requireNonNull(handler, "Server play network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels(); + return QuiltUtil.toIdentifiers(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getSendable(handler)); } /** @@ -197,9 +194,7 @@ public static Set getSendable(ServerPlayNetworkHandler handler) { * @return {@code true} if the connected client has declared the ability to receive a packet on the specified channel */ public static boolean canSend(ServerPlayerEntity player, Identifier channelName) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - - return canSend(player.networkHandler, channelName); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.canSend(player, new CustomPayload.Id<>(channelName)); } /** @@ -210,9 +205,7 @@ public static boolean canSend(ServerPlayerEntity player, Identifier channelName) * @return {@code true} if the connected client has declared the ability to receive a specific type of packet */ public static boolean canSend(ServerPlayerEntity player, CustomPayload.Id type) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - - return canSend(player.networkHandler, type.id()); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.canSend(player, type); } /** @@ -223,10 +216,7 @@ public static boolean canSend(ServerPlayerEntity player, CustomPayload.Id typ * @return {@code true} if the connected client has declared the ability to receive a packet on the specified channel */ public static boolean canSend(ServerPlayNetworkHandler handler, Identifier channelName) { - Objects.requireNonNull(handler, "Server play network handler cannot be null"); - Objects.requireNonNull(channelName, "Channel name cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels().contains(channelName); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.canSend(handler, new CustomPayload.Id<>(channelName)); } /** @@ -237,10 +227,7 @@ public static boolean canSend(ServerPlayNetworkHandler handler, Identifier chann * @return {@code true} if the connected client has declared the ability to receive a specific type of packet */ public static boolean canSend(ServerPlayNetworkHandler handler, CustomPayload.Id type) { - Objects.requireNonNull(handler, "Server play network handler cannot be null"); - Objects.requireNonNull(type, "Packet type cannot be null"); - - return ServerNetworkingImpl.getAddon(handler).getSendableChannels().contains(type.id()); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.canSend(handler, type); } /** @@ -250,7 +237,7 @@ public static boolean canSend(ServerPlayNetworkHandler handler, CustomPayload.Id * @return a new packet */ public static Packet createS2CPacket(T packet) { - return ServerNetworkingImpl.createS2CPacket(packet); + return org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.createS2CPacket(packet); } /** @@ -260,9 +247,7 @@ public static Packet creat * @return the packet sender */ public static PacketSender getSender(ServerPlayerEntity player) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - - return getSender(player.networkHandler); + return QuiltUtil.toFabricSender(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getSender(player)); } /** @@ -272,9 +257,7 @@ public static PacketSender getSender(ServerPlayerEntity player) { * @return the packet sender */ public static PacketSender getSender(ServerPlayNetworkHandler handler) { - Objects.requireNonNull(handler, "Server play network handler cannot be null"); - - return ServerNetworkingImpl.getAddon(handler); + return QuiltUtil.toFabricSender(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.getSender(handler)); } /** @@ -286,11 +269,7 @@ public static PacketSender getSender(ServerPlayNetworkHandler handler) { * @param payload the payload to send */ public static void send(ServerPlayerEntity player, CustomPayload payload) { - Objects.requireNonNull(player, "Server player entity cannot be null"); - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - player.networkHandler.sendPacket(createS2CPacket(payload)); + org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.send(player, payload); } private ServerPlayNetworking() { @@ -301,7 +280,7 @@ private ServerPlayNetworking() { * @param the type of the packet */ @FunctionalInterface - public interface PlayPayloadHandler { + public interface PlayPayloadHandler extends org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.CustomChannelReceiver { /** * Handles the incoming packet. This is called on the server thread, and can safely * manipulate the world. @@ -321,6 +300,12 @@ public interface PlayPayloadHandler { * @see CustomPayload */ void receive(T payload, Context context); + + @Override + default void receive(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, T payload, org.quiltmc.qsl.networking.api.PacketSender responseSender) { + record ContextImpl(MinecraftServer server, ServerPlayerEntity player, PacketSender responseSender) implements Context {} + this.receive(payload, new ContextImpl(server, player, QuiltUtil.toFabricSender(responseSender))); + } } @ApiStatus.NonExtendable @@ -340,4 +325,21 @@ public interface Context { */ PacketSender responseSender(); } + + private static @Nullable PlayPayloadHandler convertToFabricHandler(org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.CustomChannelReceiver old) { + if (old instanceof PlayPayloadHandler) { + return (PlayPayloadHandler) old; + } else if (old != null) { + return (payload, context) -> { + if (old instanceof org.quiltmc.qsl.networking.api.server.ServerPlayNetworking.CustomChannelReceiver r) { + org.quiltmc.qsl.networking.api.PacketSender sender = QuiltUtil.toQuiltSender(context.responseSender()); + r.receive(context.server(), context.player(), context.player().networkHandler, payload, sender); + } else { + // This should never happen! + throw new UnsupportedOperationException("Unknown receiver type: " + old.getClass().getName()); + } + }; + } + return null; + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/package-info.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/package-info.java index cdba7d5dbb6..9b35962b374 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/package-info.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/package-info.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +47,9 @@ *

In addition, this API includes helpers for {@linkplain * net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry registering custom packet payloads} and {@linkplain * net.fabricmc.fabric.api.networking.v1.PlayerLookup player lookups}. + * + * @deprecated see {@link org.quiltmc.qsl.networking.api} and {@link org.quiltmc.qsl.networking.api.server} */ +@Deprecated package net.fabricmc.fabric.api.networking.v1; diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractChanneledNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractChanneledNetworkAddon.java deleted file mode 100644 index efae4a474bf..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractChanneledNetworkAddon.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.network.ClientConnection; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.text.Text; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.PacketSender; - -/** - * A network addon which is aware of the channels the other side may receive. - * - * @param the channel handler type - */ -public abstract class AbstractChanneledNetworkAddon extends AbstractNetworkAddon implements PacketSender, CommonPacketHandler { - // The maximum number of channels that a connecting client can register. - private static final int MAX_CHANNELS = Integer.getInteger("fabric.networking.maxChannels", 8192); - // The maximum length of a channel name a connecting client can use, 128 is the default and minimum value. - private static final int MAX_CHANNEL_NAME_LENGTH = Math.max(Integer.getInteger("fabric.networking.maxChannelNameLength", GlobalReceiverRegistry.DEFAULT_CHANNEL_NAME_MAX_LENGTH), GlobalReceiverRegistry.DEFAULT_CHANNEL_NAME_MAX_LENGTH); - - protected final ClientConnection connection; - protected final GlobalReceiverRegistry receiver; - protected final Set sendableChannels; - - protected int commonVersion = -1; - - protected AbstractChanneledNetworkAddon(GlobalReceiverRegistry receiver, ClientConnection connection, String description) { - super(receiver, description); - this.connection = connection; - this.receiver = receiver; - this.sendableChannels = Collections.synchronizedSet(new HashSet<>()); - } - - protected void registerPendingChannels(ChannelInfoHolder holder, NetworkPhase state) { - final Collection pending = holder.fabric_getPendingChannelsNames(state); - - if (!pending.isEmpty()) { - register(new ArrayList<>(pending)); - pending.clear(); - } - } - - // always supposed to handle async! - public boolean handle(CustomPayload payload) { - final Identifier channelName = payload.getId().id(); - this.logger.debug("Handling inbound packet from channel with name \"{}\"", channelName); - - // Handle reserved packets - if (payload instanceof RegistrationPayload registrationPayload) { - if (NetworkingImpl.REGISTER_CHANNEL.equals(channelName)) { - this.receiveRegistration(true, registrationPayload); - return true; - } - - if (NetworkingImpl.UNREGISTER_CHANNEL.equals(channelName)) { - this.receiveRegistration(false, registrationPayload); - return true; - } - } - - @Nullable H handler = this.getHandler(channelName); - - if (handler == null) { - return false; - } - - try { - this.receive(handler, payload); - } catch (Throwable ex) { - this.logger.error("Encountered exception while handling in channel with name \"{}\"", channelName, ex); - throw ex; - } - - return true; - } - - protected abstract void receive(H handler, CustomPayload payload); - - protected void sendInitialChannelRegistrationPacket() { - final RegistrationPayload payload = createRegistrationPayload(RegistrationPayload.REGISTER, this.getReceivableChannels()); - - if (payload != null) { - this.sendPacket(payload); - } - } - - @Nullable - protected RegistrationPayload createRegistrationPayload(CustomPayload.Id id, Collection channels) { - if (channels.isEmpty()) { - return null; - } - - return new RegistrationPayload(id, new ArrayList<>(channels)); - } - - // wrap in try with res (buf) - protected void receiveRegistration(boolean register, RegistrationPayload payload) { - if (register) { - register(payload.channels()); - } else { - unregister(payload.channels()); - } - } - - void register(List ids) { - ids.forEach(this::registerChannel); - schedule(() -> this.invokeRegisterEvent(ids)); - } - - private void registerChannel(Identifier id) { - if (this.sendableChannels.size() >= MAX_CHANNELS) { - throw new IllegalArgumentException("Cannot register more than " + MAX_CHANNELS + " channels"); - } - - if (id.toString().length() > MAX_CHANNEL_NAME_LENGTH) { - throw new IllegalArgumentException("Channel name is too long"); - } - - this.sendableChannels.add(id); - } - - void unregister(List ids) { - this.sendableChannels.removeAll(ids); - schedule(() -> this.invokeUnregisterEvent(ids)); - } - - @Override - public void sendPacket(Packet packet, PacketCallbacks callback) { - Objects.requireNonNull(packet, "Packet cannot be null"); - - this.connection.send(packet, callback); - } - - @Override - public void disconnect(Text disconnectReason) { - Objects.requireNonNull(disconnectReason, "Disconnect reason cannot be null"); - - this.connection.disconnect(disconnectReason); - } - - /** - * Schedules a task to run on the main thread. - */ - protected abstract void schedule(Runnable task); - - protected abstract void invokeRegisterEvent(List ids); - - protected abstract void invokeUnregisterEvent(List ids); - - public Set getSendableChannels() { - return Collections.unmodifiableSet(this.sendableChannels); - } - - // Common packet handlers - - @Override - public void onCommonVersionPacket(int negotiatedVersion) { - assert negotiatedVersion == 1; // We only support version 1 for now - - commonVersion = negotiatedVersion; - this.logger.debug("Negotiated common packet version {}", commonVersion); - } - - @Override - public void onCommonRegisterPacket(CommonRegisterPayload payload) { - if (payload.version() != getNegotiatedVersion()) { - throw new IllegalStateException("Negotiated common packet version: %d but received packet with version: %d".formatted(commonVersion, payload.version())); - } - - final String currentPhase = getPhase(); - - if (currentPhase == null) { - // We don't support receiving the register packet during this phase. See getPhase() for supported phases. - // The normal case where the play channels are sent during configuration is handled in the client/common configuration packet handlers. - logger.warn("Received common register packet for phase {} in network state: {}", payload.phase(), receiver.getPhase()); - return; - } - - if (!payload.phase().equals(currentPhase)) { - // We need to handle receiving the play phase during configuration! - throw new IllegalStateException("Register packet received for phase (%s) on handler for phase(%s)".formatted(payload.phase(), currentPhase)); - } - - register(new ArrayList<>(payload.channels())); - } - - @Override - public CommonRegisterPayload createRegisterPayload() { - return new CommonRegisterPayload(getNegotiatedVersion(), getPhase(), this.getReceivableChannels()); - } - - @Override - public int getNegotiatedVersion() { - if (commonVersion == -1) { - throw new IllegalStateException("Not yet negotiated common packet version"); - } - - return commonVersion; - } - - @Nullable - private String getPhase() { - return switch (receiver.getPhase()) { - case PLAY -> CommonRegisterPayload.PLAY_PHASE; - case CONFIGURATION -> CommonRegisterPayload.CONFIGURATION_PHASE; - default -> null; // We don't support receiving this packet on any other phase - }; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java deleted file mode 100644 index 3ce0a675c99..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import net.minecraft.util.Identifier; - -/** - * A network addon is a simple abstraction to hold information about a player's registered channels. - * - * @param the channel handler type - */ -public abstract class AbstractNetworkAddon { - protected final GlobalReceiverRegistry receiver; - protected final Logger logger; - // A lock is used due to possible access on netty's event loops and game thread at same times such as during dynamic registration - private final ReadWriteLock lock = new ReentrantReadWriteLock(); - // Sync map should be fine as there is little read write competition - // All access to this map is guarded by the lock - private final Map handlers = new HashMap<>(); - private final AtomicBoolean disconnected = new AtomicBoolean(); // blocks redundant disconnect notifications - - protected AbstractNetworkAddon(GlobalReceiverRegistry receiver, String description) { - this.receiver = receiver; - this.logger = LoggerFactory.getLogger(description); - } - - public final void lateInit() { - this.receiver.startSession(this); - invokeInitEvent(); - } - - protected abstract void invokeInitEvent(); - - public final void endSession() { - this.receiver.endSession(this); - } - - @Nullable - public H getHandler(Identifier channel) { - Lock lock = this.lock.readLock(); - lock.lock(); - - try { - return this.handlers.get(channel); - } finally { - lock.unlock(); - } - } - - private void assertNotReserved(Identifier channel) { - if (this.isReservedChannel(channel)) { - throw new IllegalArgumentException(String.format("Cannot (un)register handler for reserved channel with name \"%s\"", channel)); - } - } - - public void registerChannels(Map map) { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - for (Map.Entry entry : map.entrySet()) { - assertNotReserved(entry.getKey()); - - boolean unique = this.handlers.putIfAbsent(entry.getKey(), entry.getValue()) == null; - if (unique) handleRegistration(entry.getKey()); - } - } finally { - lock.unlock(); - } - } - - public boolean registerChannel(Identifier channelName, H handler) { - Objects.requireNonNull(channelName, "Channel name cannot be null"); - Objects.requireNonNull(handler, "Packet handler cannot be null"); - assertNotReserved(channelName); - - receiver.assertPayloadType(channelName); - - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - final boolean replaced = this.handlers.putIfAbsent(channelName, handler) == null; - - if (replaced) { - this.handleRegistration(channelName); - } - - return replaced; - } finally { - lock.unlock(); - } - } - - public H unregisterChannel(Identifier channelName) { - Objects.requireNonNull(channelName, "Channel name cannot be null"); - assertNotReserved(channelName); - - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - final H removed = this.handlers.remove(channelName); - - if (removed != null) { - this.handleUnregistration(channelName); - } - - return removed; - } finally { - lock.unlock(); - } - } - - public Set getReceivableChannels() { - Lock lock = this.lock.readLock(); - lock.lock(); - - try { - return new HashSet<>(this.handlers.keySet()); - } finally { - lock.unlock(); - } - } - - protected abstract void handleRegistration(Identifier channelName); - - protected abstract void handleUnregistration(Identifier channelName); - - public final void handleDisconnect() { - if (disconnected.compareAndSet(false, true)) { - invokeDisconnectEvent(); - endSession(); - } - } - - protected abstract void invokeDisconnectEvent(); - - /** - * Checks if a channel is considered a "reserved" channel. - * A reserved channel such as "minecraft:(un)register" has special handling and should not have any channel handlers registered for it. - * - * @param channelName the channel name - * @return whether the channel is reserved - */ - protected abstract boolean isReservedChannel(Identifier channelName); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/ChannelInfoHolder.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/ChannelInfoHolder.java deleted file mode 100644 index 3a744519e73..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/ChannelInfoHolder.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.Collection; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.util.Identifier; - -public interface ChannelInfoHolder { - /** - * @return Channels which are declared as receivable by the other side but have not been declared yet. - */ - Collection fabric_getPendingChannelsNames(NetworkPhase state); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketHandler.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketHandler.java deleted file mode 100644 index 96b90c95934..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -public interface CommonPacketHandler { - void onCommonVersionPacket(int negotiatedVersion); - - void onCommonRegisterPacket(CommonRegisterPayload payload); - - CommonRegisterPayload createRegisterPayload(); - - int getNegotiatedVersion(); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketsImpl.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketsImpl.java deleted file mode 100644 index 289e5d2e447..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonPacketsImpl.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.Arrays; -import java.util.function.Consumer; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.network.ServerPlayerConfigurationTask; - -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; - -public class CommonPacketsImpl { - public static final int PACKET_VERSION_1 = 1; - public static final int[] SUPPORTED_COMMON_PACKET_VERSIONS = new int[]{ PACKET_VERSION_1 }; - - public static void init() { - PayloadTypeRegistry.configurationC2S().register(CommonVersionPayload.ID, CommonVersionPayload.CODEC); - PayloadTypeRegistry.configurationS2C().register(CommonVersionPayload.ID, CommonVersionPayload.CODEC); - PayloadTypeRegistry.playC2S().register(CommonVersionPayload.ID, CommonVersionPayload.CODEC); - PayloadTypeRegistry.playS2C().register(CommonVersionPayload.ID, CommonVersionPayload.CODEC); - PayloadTypeRegistry.configurationC2S().register(CommonRegisterPayload.ID, CommonRegisterPayload.CODEC); - PayloadTypeRegistry.configurationS2C().register(CommonRegisterPayload.ID, CommonRegisterPayload.CODEC); - PayloadTypeRegistry.playC2S().register(CommonRegisterPayload.ID, CommonRegisterPayload.CODEC); - PayloadTypeRegistry.playS2C().register(CommonRegisterPayload.ID, CommonRegisterPayload.CODEC); - - ServerConfigurationNetworking.registerGlobalReceiver(CommonVersionPayload.ID, (payload, context) -> { - ServerConfigurationNetworkAddon addon = ServerNetworkingImpl.getAddon(context.networkHandler()); - addon.onCommonVersionPacket(getNegotiatedVersion(payload)); - context.networkHandler().completeTask(CommonVersionConfigurationTask.KEY); - }); - - ServerConfigurationNetworking.registerGlobalReceiver(CommonRegisterPayload.ID, (payload, context) -> { - ServerConfigurationNetworkAddon addon = ServerNetworkingImpl.getAddon(context.networkHandler()); - - if (CommonRegisterPayload.PLAY_PHASE.equals(payload.phase())) { - if (payload.version() != addon.getNegotiatedVersion()) { - throw new IllegalStateException("Negotiated common packet version: %d but received packet with version: %d".formatted(addon.getNegotiatedVersion(), payload.version())); - } - - // Play phase hasnt started yet, add them to the pending names. - addon.getChannelInfoHolder().fabric_getPendingChannelsNames(NetworkPhase.PLAY).addAll(payload.channels()); - NetworkingImpl.LOGGER.debug("Received accepted channels from the client for play phase"); - } else { - addon.onCommonRegisterPacket(payload); - } - - context.networkHandler().completeTask(CommonRegisterConfigurationTask.KEY); - }); - - // Create a configuration task to send and receive the common packets - ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { - final ServerConfigurationNetworkAddon addon = ServerNetworkingImpl.getAddon(handler); - - if (ServerConfigurationNetworking.canSend(handler, CommonVersionPayload.ID)) { - // Tasks are processed in order. - handler.addTask(new CommonVersionConfigurationTask(addon)); - - if (ServerConfigurationNetworking.canSend(handler, CommonRegisterPayload.ID)) { - handler.addTask(new CommonRegisterConfigurationTask(addon)); - } - } - }); - } - - // A configuration phase task to send and receive the version packets. - private record CommonVersionConfigurationTask(ServerConfigurationNetworkAddon addon) implements ServerPlayerConfigurationTask { - public static final Key KEY = new Key(CommonVersionPayload.ID.id().toString()); - - @Override - public void sendPacket(Consumer> sender) { - addon.sendPacket(new CommonVersionPayload(SUPPORTED_COMMON_PACKET_VERSIONS)); - } - - @Override - public Key getKey() { - return KEY; - } - } - - // A configuration phase task to send and receive the registration packets. - private record CommonRegisterConfigurationTask(ServerConfigurationNetworkAddon addon) implements ServerPlayerConfigurationTask { - public static final Key KEY = new Key(CommonRegisterPayload.ID.id().toString()); - - @Override - public void sendPacket(Consumer> sender) { - addon.sendPacket(new CommonRegisterPayload(addon.getNegotiatedVersion(), CommonRegisterPayload.PLAY_PHASE, ServerPlayNetworking.getGlobalReceivers())); - } - - @Override - public Key getKey() { - return KEY; - } - } - - private static int getNegotiatedVersion(CommonVersionPayload payload) { - int version = getHighestCommonVersion(payload.versions(), SUPPORTED_COMMON_PACKET_VERSIONS); - - if (version <= 0) { - throw new UnsupportedOperationException("server does not support any requested versions from client"); - } - - return version; - } - - public static int getHighestCommonVersion(int[] a, int[] b) { - int[] as = a.clone(); - int[] bs = b.clone(); - - Arrays.sort(as); - Arrays.sort(bs); - - int ap = as.length - 1; - int bp = bs.length - 1; - - while (ap >= 0 && bp >= 0) { - if (as[ap] == bs[bp]) { - return as[ap]; - } - - if (as[ap] > bs[bp]) { - ap--; - } else { - bp--; - } - } - - return -1; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonRegisterPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonRegisterPayload.java deleted file mode 100644 index 7a91613101f..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonRegisterPayload.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.HashSet; -import java.util.Set; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; - -public record CommonRegisterPayload(int version, String phase, Set channels) implements CustomPayload { - public static final CustomPayload.Id ID = new Id<>(Identifier.of("c:register")); - public static final PacketCodec CODEC = CustomPayload.codecOf(CommonRegisterPayload::write, CommonRegisterPayload::new); - - public static final String PLAY_PHASE = "play"; - public static final String CONFIGURATION_PHASE = "configuration"; - - private CommonRegisterPayload(PacketByteBuf buf) { - this( - buf.readVarInt(), - buf.readString(), - buf.readCollection(HashSet::new, PacketByteBuf::readIdentifier) - ); - } - - public void write(PacketByteBuf buf) { - buf.writeVarInt(version); - buf.writeString(phase); - buf.writeCollection(channels, PacketByteBuf::writeIdentifier); - } - - @Override - public Id getId() { - return ID; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonVersionPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonVersionPayload.java deleted file mode 100644 index 8acbb1dfc81..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CommonVersionPayload.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; - -public record CommonVersionPayload(int[] versions) implements CustomPayload { - public static final PacketCodec CODEC = CustomPayload.codecOf(CommonVersionPayload::write, CommonVersionPayload::new); - public static final CustomPayload.Id ID = new Id<>(Identifier.of("c:version")); - - private CommonVersionPayload(PacketByteBuf buf) { - this(buf.readIntArray()); - } - - public void write(PacketByteBuf buf) { - buf.writeIntArray(versions); - } - - @Override - public Id getId() { - return ID; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomPayloadTypeProvider.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomPayloadTypeProvider.java deleted file mode 100644 index 157a63d2d46..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomPayloadTypeProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; - -public interface CustomPayloadTypeProvider { - CustomPayload.Type get(B packetByteBuf, Identifier identifier); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltLoginPacketSender.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltLoginPacketSender.java new file mode 100644 index 00000000000..cde233d880a --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltLoginPacketSender.java @@ -0,0 +1,52 @@ +/* + * Copyright 2024 The Quilt Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.networking.api.LoginPacketSender; +import org.quiltmc.qsl.networking.api.PacketSender; + +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.s2c.login.LoginQueryRequestPayload; + +class Fabric2QuiltLoginPacketSender implements LoginPacketSender { + final net.fabricmc.fabric.api.networking.v1.PacketSender sender; + + protected Fabric2QuiltLoginPacketSender(net.fabricmc.fabric.api.networking.v1.PacketSender fabric) { + this.sender = fabric; + } + + @Override + public Packet createPacket(LoginQueryRequestPayload payload) { + if (this.sender instanceof Quilt2FabricLoginPacketSender quilt) { + return quilt.sender.createPacket(payload); + } + + throw new IllegalStateException("I don't think this is reachable!"); + } + + @Override + public void sendPacket(Packet packet) { + this.sender.sendPacket(packet); + } + + @Override + public void sendPacket(Packet packet, @Nullable PacketCallbacks listener) { + this.sender.sendPacket(packet, listener); + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltPacketSender.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltPacketSender.java new file mode 100644 index 00000000000..089cc833197 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Fabric2QuiltPacketSender.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 The Quilt Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.networking.api.PacketSender; + +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.packet.Packet; + +class Fabric2QuiltPacketSender implements PacketSender { + final net.fabricmc.fabric.api.networking.v1.PacketSender sender; + + protected Fabric2QuiltPacketSender(net.fabricmc.fabric.api.networking.v1.PacketSender fabric) { + this.sender = fabric; + } + + @Override + public Packet createPacket(C payload) { + throw new UnsupportedOperationException("This should not be called!"); + } + + @Override + public void sendPacket(Packet packet) { + this.sender.sendPacket(packet); + } + + @Override + public void sendPacket(Packet packet, @Nullable PacketCallbacks listener) { + this.sender.sendPacket(packet, listener); + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/FabricCustomPayloadPacketCodec.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/FabricCustomPayloadPacketCodec.java deleted file mode 100644 index 40382170ea3..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/FabricCustomPayloadPacketCodec.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import net.minecraft.network.PacketByteBuf; - -public interface FabricCustomPayloadPacketCodec { - void fabric_setPacketCodecProvider(CustomPayloadTypeProvider customPayloadTypeProvider); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/GlobalReceiverRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/GlobalReceiverRegistry.java deleted file mode 100644 index eff31522e04..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/GlobalReceiverRegistry.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.NetworkSide; -import net.minecraft.util.Identifier; - -public final class GlobalReceiverRegistry { - public static final int DEFAULT_CHANNEL_NAME_MAX_LENGTH = 128; - private static final Logger LOGGER = LoggerFactory.getLogger(GlobalReceiverRegistry.class); - - private final NetworkSide side; - private final NetworkPhase phase; - @Nullable - private final PayloadTypeRegistryImpl payloadTypeRegistry; - - private final ReadWriteLock lock = new ReentrantReadWriteLock(); - private final Map handlers = new HashMap<>(); - private final Set> trackedAddons = new HashSet<>(); - - public GlobalReceiverRegistry(NetworkSide side, NetworkPhase phase, @Nullable PayloadTypeRegistryImpl payloadTypeRegistry) { - this.side = side; - this.phase = phase; - this.payloadTypeRegistry = payloadTypeRegistry; - - if (payloadTypeRegistry != null) { - assert phase == payloadTypeRegistry.getPhase(); - assert side == payloadTypeRegistry.getSide(); - } - } - - @Nullable - public H getHandler(Identifier channelName) { - Lock lock = this.lock.readLock(); - lock.lock(); - - try { - return this.handlers.get(channelName); - } finally { - lock.unlock(); - } - } - - public boolean registerGlobalReceiver(Identifier channelName, H handler) { - Objects.requireNonNull(channelName, "Channel name cannot be null"); - Objects.requireNonNull(handler, "Channel handler cannot be null"); - - if (NetworkingImpl.isReservedCommonChannel(channelName)) { - throw new IllegalArgumentException(String.format("Cannot register handler for reserved channel with name \"%s\"", channelName)); - } - - assertPayloadType(channelName); - - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - final boolean replaced = this.handlers.putIfAbsent(channelName, handler) == null; - - if (replaced) { - this.handleRegistration(channelName, handler); - } - - return replaced; - } finally { - lock.unlock(); - } - } - - @Nullable - public H unregisterGlobalReceiver(Identifier channelName) { - Objects.requireNonNull(channelName, "Channel name cannot be null"); - - if (NetworkingImpl.isReservedCommonChannel(channelName)) { - throw new IllegalArgumentException(String.format("Cannot unregister packet handler for reserved channel with name \"%s\"", channelName)); - } - - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - final H removed = this.handlers.remove(channelName); - - if (removed != null) { - this.handleUnregistration(channelName); - } - - return removed; - } finally { - lock.unlock(); - } - } - - public Map getHandlers() { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - return new HashMap<>(this.handlers); - } finally { - lock.unlock(); - } - } - - public Set getChannels() { - Lock lock = this.lock.readLock(); - lock.lock(); - - try { - return new HashSet<>(this.handlers.keySet()); - } finally { - lock.unlock(); - } - } - - // State tracking methods - - public void startSession(AbstractNetworkAddon addon) { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - if (this.trackedAddons.add(addon)) { - addon.registerChannels(handlers); - } - - this.logTrackedAddonSize(); - } finally { - lock.unlock(); - } - } - - public void endSession(AbstractNetworkAddon addon) { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - this.logTrackedAddonSize(); - this.trackedAddons.remove(addon); - } finally { - lock.unlock(); - } - } - - /** - * In practice, trackedAddons should never contain more than the number of players. - */ - private void logTrackedAddonSize() { - if (LOGGER.isTraceEnabled() && this.trackedAddons.size() > 1) { - LOGGER.trace("{} receiver registry tracks {} addon instances", phase.getId(), trackedAddons.size()); - } - } - - private void handleRegistration(Identifier channelName, H handler) { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - this.logTrackedAddonSize(); - - for (AbstractNetworkAddon addon : this.trackedAddons) { - addon.registerChannel(channelName, handler); - } - } finally { - lock.unlock(); - } - } - - private void handleUnregistration(Identifier channelName) { - Lock lock = this.lock.writeLock(); - lock.lock(); - - try { - this.logTrackedAddonSize(); - - for (AbstractNetworkAddon addon : this.trackedAddons) { - addon.unregisterChannel(channelName); - } - } finally { - lock.unlock(); - } - } - - public void assertPayloadType(Identifier channelName) { - if (payloadTypeRegistry == null) { - return; - } - - if (payloadTypeRegistry.get(channelName) == null) { - throw new IllegalArgumentException(String.format("Cannot register handler as no payload type has been registered with name \"%s\" for %s %s", channelName, side, phase)); - } - - if (channelName.toString().length() > DEFAULT_CHANNEL_NAME_MAX_LENGTH) { - throw new IllegalArgumentException(String.format("Cannot register handler for channel with name \"%s\" as it exceeds the maximum length of 128 characters", channelName)); - } - } - - public NetworkPhase getPhase() { - return phase; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkHandlerExtensions.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkHandlerExtensions.java deleted file mode 100644 index 99b1a2a0493..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkHandlerExtensions.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -public interface NetworkHandlerExtensions { - AbstractNetworkAddon getAddon(); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkingImpl.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkingImpl.java deleted file mode 100644 index b6c67cbf2b6..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/NetworkingImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; - -public final class NetworkingImpl { - public static final String MOD_ID = "fabric-networking-api-v1"; - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - - /** - * Id of packet used to register supported channels. - */ - public static final Identifier REGISTER_CHANNEL = Identifier.ofVanilla("register"); - - /** - * Id of packet used to unregister supported channels. - */ - public static final Identifier UNREGISTER_CHANNEL = Identifier.ofVanilla("unregister"); - - public static boolean isReservedCommonChannel(Identifier channelName) { - return channelName.equals(REGISTER_CHANNEL) || channelName.equals(UNREGISTER_CHANNEL); - } - - public static void init() { - PayloadTypeRegistry.configurationS2C().register(RegistrationPayload.REGISTER, RegistrationPayload.REGISTER_CODEC); - PayloadTypeRegistry.configurationS2C().register(RegistrationPayload.UNREGISTER, RegistrationPayload.UNREGISTER_CODEC); - PayloadTypeRegistry.configurationC2S().register(RegistrationPayload.REGISTER, RegistrationPayload.REGISTER_CODEC); - PayloadTypeRegistry.configurationC2S().register(RegistrationPayload.UNREGISTER, RegistrationPayload.UNREGISTER_CODEC); - PayloadTypeRegistry.playS2C().register(RegistrationPayload.REGISTER, RegistrationPayload.REGISTER_CODEC); - PayloadTypeRegistry.playS2C().register(RegistrationPayload.UNREGISTER, RegistrationPayload.UNREGISTER_CODEC); - PayloadTypeRegistry.playC2S().register(RegistrationPayload.REGISTER, RegistrationPayload.REGISTER_CODEC); - PayloadTypeRegistry.playC2S().register(RegistrationPayload.UNREGISTER, RegistrationPayload.UNREGISTER_CODEC); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PacketCallbackListener.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PacketCallbackListener.java deleted file mode 100644 index d00742ac893..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PacketCallbackListener.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import net.minecraft.network.packet.Packet; - -public interface PacketCallbackListener { - /** - * Called after a packet has been sent. - * - * @param packet the packet - */ - void sent(Packet packet); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PayloadTypeRegistryImpl.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PayloadTypeRegistryImpl.java deleted file mode 100644 index 4b749a01c40..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PayloadTypeRegistryImpl.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.NetworkSide; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; - -public class PayloadTypeRegistryImpl implements PayloadTypeRegistry { - public static final PayloadTypeRegistryImpl CONFIGURATION_C2S = new PayloadTypeRegistryImpl<>(NetworkPhase.CONFIGURATION, NetworkSide.SERVERBOUND); - public static final PayloadTypeRegistryImpl CONFIGURATION_S2C = new PayloadTypeRegistryImpl<>(NetworkPhase.CONFIGURATION, NetworkSide.CLIENTBOUND); - public static final PayloadTypeRegistryImpl PLAY_C2S = new PayloadTypeRegistryImpl<>(NetworkPhase.PLAY, NetworkSide.SERVERBOUND); - public static final PayloadTypeRegistryImpl PLAY_S2C = new PayloadTypeRegistryImpl<>(NetworkPhase.PLAY, NetworkSide.CLIENTBOUND); - - private final Map> packetTypes = new HashMap<>(); - private final NetworkPhase state; - private final NetworkSide side; - - private PayloadTypeRegistryImpl(NetworkPhase state, NetworkSide side) { - this.state = state; - this.side = side; - } - - @Override - public CustomPayload.Type register(CustomPayload.Id id, PacketCodec codec) { - Objects.requireNonNull(id, "id"); - Objects.requireNonNull(codec, "codec"); - - final CustomPayload.Type payloadType = new CustomPayload.Type<>(id, codec.cast()); - - if (packetTypes.containsKey(id.id())) { - throw new IllegalArgumentException("Packet type " + id + " is already registered!"); - } - - packetTypes.put(id.id(), payloadType); - return payloadType; - } - - @Nullable - public CustomPayload.Type get(Identifier id) { - return packetTypes.get(id); - } - - @Nullable - public CustomPayload.Type get(CustomPayload.Id id) { - //noinspection unchecked - return (CustomPayload.Type) packetTypes.get(id.id()); - } - - public NetworkPhase getPhase() { - return state; - } - - public NetworkSide getSide() { - return side; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricLoginPacketSender.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricLoginPacketSender.java new file mode 100644 index 00000000000..7eee0b0932b --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricLoginPacketSender.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 The Quilt Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.networking.impl.payload.PacketByteBufLoginQueryRequestPayload; +import org.quiltmc.qsl.networking.impl.server.ServerLoginNetworkAddon; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.network.packet.Packet; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.networking.v1.LoginPacketSender; +import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkAddonAccessor; + +final class Quilt2FabricLoginPacketSender implements LoginPacketSender { + final org.quiltmc.qsl.networking.api.LoginPacketSender sender; + + Quilt2FabricLoginPacketSender(org.quiltmc.qsl.networking.api.LoginPacketSender sender) { + this.sender = sender; + } + + @Override + public Packet createPacket(Identifier channelName, PacketByteBuf buf) { + // Copied from org.quiltmc.qsl.networking.api.LoginPacketSender + return this.sender.createPacket(new PacketByteBufLoginQueryRequestPayload(channelName, buf)); + } + + @Override + public Packet createPacket(CustomPayload payload) { + throw new UnsupportedOperationException("Cannot send CustomPayload during login"); + } + + @Override + public void sendPacket(Packet packet) { + this.sender.sendPacket(packet); + } + + @Override + public void sendPacket(Packet packet, @Nullable PacketCallbacks callback) { + this.sender.sendPacket(packet, callback); + } + + @Override + public void disconnect(Text disconnectReason) { + if (this.sender instanceof ServerLoginNetworkAddon loginAddon) { + ((ServerLoginNetworkAddonAccessor) (Object) loginAddon).getConnection().disconnect(disconnectReason); + } + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricPacketSender.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricPacketSender.java new file mode 100644 index 00000000000..46e1790ba04 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/Quilt2FabricPacketSender.java @@ -0,0 +1,65 @@ +/* + * Copyright 2022 The Quilt Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.networking.impl.server.ServerLoginNetworkAddon; + +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.s2c.login.LoginQueryRequestPayload; +import net.minecraft.text.Text; + +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkAddonAccessor; + +final class Quilt2FabricPacketSender implements PacketSender { + final org.quiltmc.qsl.networking.api.PacketSender sender; + + Quilt2FabricPacketSender(org.quiltmc.qsl.networking.api.PacketSender sender) { + this.sender = sender; + } + + @Override + public Packet createPacket(CustomPayload payload) { + if (payload instanceof LoginQueryRequestPayload) { + throw new UnsupportedOperationException("Cannot send CustomPayload during login"); + } + + return this.sender.createPacket((C) payload); + } + + @Override + public void sendPacket(Packet packet) { + this.sender.sendPacket(packet); + } + + @Override + public void sendPacket(Packet packet, @Nullable PacketCallbacks callback) { + this.sender.sendPacket(packet, callback); + } + + @Override + public void disconnect(Text disconnectReason) { + if (this.sender instanceof AbstractChanneledNetworkAddon channeledAddon) { + channeledAddon.disconnect(disconnectReason); + } else if (this.sender instanceof ServerLoginNetworkAddon loginAddon) { + ((ServerLoginNetworkAddonAccessor) (Object) loginAddon).getConnection().disconnect(disconnectReason); + } + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/QuiltUtil.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/QuiltUtil.java new file mode 100644 index 00000000000..467dd3e5e7b --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/QuiltUtil.java @@ -0,0 +1,96 @@ +/* + * Copyright 2022 The Quilt Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.jetbrains.annotations.NotNull; +import org.quiltmc.qsl.networking.api.LoginPacketSender; +import org.quiltmc.qsl.networking.api.PacketSender; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; + +public class QuiltUtil { + + public static PacketSender toQuiltSender(net.fabricmc.fabric.api.networking.v1.PacketSender sender) { + if (sender instanceof Quilt2FabricPacketSender s) { + return (PacketSender) s.sender; + } else { + return new Fabric2QuiltPacketSender<>(sender); + } + } + + public static net.fabricmc.fabric.api.networking.v1.PacketSender toFabricSender(PacketSender sender) { + if (sender instanceof Fabric2QuiltPacketSender s) { + return s.sender; + } else { + return new Quilt2FabricPacketSender<>(sender); + } + } + + public static LoginPacketSender toQuiltLoginSender(net.fabricmc.fabric.api.networking.v1.PacketSender sender) { + if (sender instanceof Quilt2FabricLoginPacketSender s) { + return s.sender; + } else { + return new Fabric2QuiltLoginPacketSender(sender); + } + } + + public static net.fabricmc.fabric.api.networking.v1.LoginPacketSender toFabricLoginSender(LoginPacketSender sender) { + if (sender instanceof net.fabricmc.fabric.api.networking.v1.LoginPacketSender) { + return ((net.fabricmc.fabric.api.networking.v1.LoginPacketSender) sender); + } else { + return new Quilt2FabricLoginPacketSender(sender); + } + } + + public static PayloadTypeRegistry fromQuilt(org.quiltmc.qsl.networking.api.PayloadTypeRegistry quilt) { + record Impl(org.quiltmc.qsl.networking.api.PayloadTypeRegistry quilt) implements PayloadTypeRegistry { + + @Override + public CustomPayload.Type register(CustomPayload.Id id, PacketCodec codec) { + return Impl.this.quilt.register(id, codec); + } + } + return new Impl<>(quilt); + } + + public static @NotNull List toIdentifiers(List> channels) { + return channels.stream().map(CustomPayload.Id::id).toList(); + } + + public static @NotNull Set toIdentifiers(Set> channels) { + return channels.stream().map(CustomPayload.Id::id).collect(Collectors.toSet()); + } + + public static @NotNull List> toPayloadIds(List channels) { + List> ids = new ArrayList<>(); + for (Identifier channel : channels) { + CustomPayload.Id id = new CustomPayload.Id<>(channel); + ids.add(id); + } + return ids; + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/RegistrationPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/RegistrationPayload.java deleted file mode 100644 index e498eb41e4a..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/RegistrationPayload.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking; - -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import io.netty.util.AsciiString; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; -import net.minecraft.util.InvalidIdentifierException; - -public record RegistrationPayload(Id id, List channels) implements CustomPayload { - public static final CustomPayload.Id REGISTER = new CustomPayload.Id<>(NetworkingImpl.REGISTER_CHANNEL); - public static final CustomPayload.Id UNREGISTER = new CustomPayload.Id<>(NetworkingImpl.UNREGISTER_CHANNEL); - public static final PacketCodec REGISTER_CODEC = codec(REGISTER); - public static final PacketCodec UNREGISTER_CODEC = codec(UNREGISTER); - - private RegistrationPayload(Id id, PacketByteBuf buf) { - this(id, read(buf)); - } - - private void write(PacketByteBuf buf) { - boolean first = true; - - for (Identifier channel : channels) { - if (first) { - first = false; - } else { - buf.writeByte(0); - } - - buf.writeBytes(channel.toString().getBytes(StandardCharsets.US_ASCII)); - } - } - - private static List read(PacketByteBuf buf) { - List ids = new ArrayList<>(); - StringBuilder active = new StringBuilder(); - - while (buf.isReadable()) { - byte b = buf.readByte(); - - if (b != 0) { - active.append(AsciiString.b2c(b)); - } else { - addId(ids, active); - active = new StringBuilder(); - } - } - - addId(ids, active); - - return Collections.unmodifiableList(ids); - } - - private static void addId(List ids, StringBuilder sb) { - String literal = sb.toString(); - - try { - ids.add(Identifier.of(literal)); - } catch (InvalidIdentifierException ex) { - NetworkingImpl.LOGGER.warn("Received invalid channel identifier \"{}\"", literal); - } - } - - @Override - public Id getId() { - return id; - } - - private static PacketCodec codec(Id id) { - return CustomPayload.codecOf(RegistrationPayload::write, buf -> new RegistrationPayload(id, buf)); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java deleted file mode 100644 index 2e50bc9e3c5..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryRequestPayload.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.payload; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestPayload; -import net.minecraft.util.Identifier; - -public record PacketByteBufLoginQueryRequestPayload(Identifier id, PacketByteBuf data) implements LoginQueryRequestPayload { - @Override - public void write(PacketByteBuf buf) { - PayloadHelper.write(buf, data()); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java deleted file mode 100644 index 08d79fb58e4..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PacketByteBufLoginQueryResponse.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.payload; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.c2s.login.LoginQueryResponsePayload; - -public record PacketByteBufLoginQueryResponse(PacketByteBuf data) implements LoginQueryResponsePayload { - @Override - public void write(PacketByteBuf buf) { - PayloadHelper.write(buf, data()); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java deleted file mode 100644 index 7a02f4b4ed7..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/payload/PayloadHelper.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.payload; - -import net.minecraft.network.PacketByteBuf; - -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; - -public class PayloadHelper { - public static void write(PacketByteBuf byteBuf, PacketByteBuf data) { - byteBuf.writeBytes(data.copy()); - } - - public static PacketByteBuf read(PacketByteBuf byteBuf, int maxSize) { - assertSize(byteBuf, maxSize); - - PacketByteBuf newBuf = PacketByteBufs.create(); - newBuf.writeBytes(byteBuf.copy()); - byteBuf.skipBytes(byteBuf.readableBytes()); - return newBuf; - } - - private static void assertSize(PacketByteBuf buf, int maxSize) { - int size = buf.readableBytes(); - - if (size < 0 || size > maxSize) { - throw new IllegalArgumentException("Payload may not be larger than " + maxSize + " bytes"); - } - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/QueryIdFactory.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/QueryIdFactory.java deleted file mode 100644 index bf1e1bb2f8f..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/QueryIdFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.server; - -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Tracks the current query id used for login query responses. - */ -interface QueryIdFactory { - static QueryIdFactory create() { - return new QueryIdFactory() { - private final AtomicInteger currentId = new AtomicInteger(); - - @Override - public int nextId() { - return this.currentId.getAndIncrement(); - } - }; - } - - // called async prob. - int nextId(); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java deleted file mode 100644 index c70e7dbdc77..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.server; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.s2c.common.CommonPingS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.fabricmc.fabric.api.networking.v1.S2CConfigurationChannelEvents; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon; -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; -import net.fabricmc.fabric.impl.networking.NetworkingImpl; -import net.fabricmc.fabric.impl.networking.RegistrationPayload; -import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor; - -public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetworkAddon> { - private final ServerConfigurationNetworkHandler handler; - private final MinecraftServer server; - private final ServerConfigurationNetworking.Context context; - private RegisterState registerState = RegisterState.NOT_SENT; - - public ServerConfigurationNetworkAddon(ServerConfigurationNetworkHandler handler, MinecraftServer server) { - super(ServerNetworkingImpl.CONFIGURATION, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerConfigurationNetworkAddon for " + handler.getDebugProfile().getName()); - this.handler = handler; - this.server = server; - this.context = new ContextImpl(server, handler, this); - - // Must register pending channels via lateinit - this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.CONFIGURATION); - } - - @Override - protected void invokeInitEvent() { - } - - public void preConfiguration() { - ServerConfigurationConnectionEvents.BEFORE_CONFIGURE.invoker().onSendConfiguration(handler, server); - } - - public void configuration() { - ServerConfigurationConnectionEvents.CONFIGURE.invoker().onSendConfiguration(handler, server); - } - - public boolean startConfiguration() { - if (this.registerState == RegisterState.NOT_SENT) { - // Send the registration packet, followed by a ping - this.sendInitialChannelRegistrationPacket(); - this.sendPacket(new CommonPingS2CPacket(0xFAB71C)); - - this.registerState = RegisterState.SENT; - - // Cancel the configuration for now, the response from the ping or registration packet will continue. - return true; - } - - // We should have received a response - assert registerState == RegisterState.RECEIVED || registerState == RegisterState.NOT_RECEIVED; - return false; - } - - @Override - protected void receiveRegistration(boolean register, RegistrationPayload resolvable) { - super.receiveRegistration(register, resolvable); - - if (register && registerState == RegisterState.SENT) { - // We received the registration packet, thus we know this is a modded client, continue with configuration. - registerState = RegisterState.RECEIVED; - handler.sendConfigurations(); - } - } - - public void onPong(int parameter) { - if (registerState == RegisterState.SENT) { - // We did not receive the registration packet, thus we think this is a vanilla client, continue with configuration. - registerState = RegisterState.NOT_RECEIVED; - handler.sendConfigurations(); - } - } - - @Override - protected void receive(ServerConfigurationNetworking.ConfigurationPacketHandler handler, CustomPayload payload) { - ((ServerConfigurationNetworking.ConfigurationPacketHandler) handler).receive(payload, this.context); - } - - // impl details - - @Override - protected void schedule(Runnable task) { - this.server.execute(task); - } - - @Override - public Packet createPacket(CustomPayload packet) { - return ServerConfigurationNetworking.createS2CPacket(packet); - } - - @Override - protected void invokeRegisterEvent(List ids) { - S2CConfigurationChannelEvents.REGISTER.invoker().onChannelRegister(this.handler, this, this.server, ids); - } - - @Override - protected void invokeUnregisterEvent(List ids) { - S2CConfigurationChannelEvents.UNREGISTER.invoker().onChannelUnregister(this.handler, this, this.server, ids); - } - - @Override - protected void handleRegistration(Identifier channelName) { - // If we can already send packets, immediately send the register packet for this channel - if (this.registerState != RegisterState.NOT_SENT) { - RegistrationPayload registrationPayload = this.createRegistrationPayload(RegistrationPayload.REGISTER, Collections.singleton(channelName)); - - if (registrationPayload != null) { - this.sendPacket(registrationPayload); - } - } - } - - @Override - protected void handleUnregistration(Identifier channelName) { - // If we can already send packets, immediately send the unregister packet for this channel - if (this.registerState != RegisterState.NOT_SENT) { - RegistrationPayload registrationPayload = this.createRegistrationPayload(RegistrationPayload.UNREGISTER, Collections.singleton(channelName)); - - if (registrationPayload != null) { - this.sendPacket(registrationPayload); - } - } - } - - @Override - protected void invokeDisconnectEvent() { - ServerConfigurationConnectionEvents.DISCONNECT.invoker().onConfigureDisconnect(handler, server); - } - - @Override - protected boolean isReservedChannel(Identifier channelName) { - return NetworkingImpl.isReservedCommonChannel(channelName); - } - - @Override - public void sendPacket(Packet packet, PacketCallbacks callback) { - handler.send(packet, callback); - } - - private enum RegisterState { - NOT_SENT, - SENT, - RECEIVED, - NOT_RECEIVED - } - - public ChannelInfoHolder getChannelInfoHolder() { - return (ChannelInfoHolder) ((ServerCommonNetworkHandlerAccessor) handler).getConnection(); - } - - private record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHandler networkHandler, PacketSender responseSender) implements ServerConfigurationNetworking.Context { - private ContextImpl { - Objects.requireNonNull(server, "server"); - Objects.requireNonNull(networkHandler, "networkHandler"); - Objects.requireNonNull(responseSender, "responseSender"); - } - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java deleted file mode 100644 index 8615467f45b..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerLoginNetworkAddon.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.server; - -import java.util.Collection; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.CancellationException; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicReference; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.network.ClientConnection; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket; -import net.minecraft.network.packet.s2c.login.LoginCompressionS2CPacket; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerLoginNetworkHandler; -import net.minecraft.text.Text; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.LoginPacketSender; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; -import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking; -import net.fabricmc.fabric.impl.networking.AbstractNetworkAddon; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; -import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkHandlerAccessor; - -public final class ServerLoginNetworkAddon extends AbstractNetworkAddon implements LoginPacketSender { - private final ClientConnection connection; - private final ServerLoginNetworkHandler handler; - private final MinecraftServer server; - private final QueryIdFactory queryIdFactory; - private final Collection> waits = new ConcurrentLinkedQueue<>(); - private final Map channels = new ConcurrentHashMap<>(); - private boolean firstQueryTick = true; - - public ServerLoginNetworkAddon(ServerLoginNetworkHandler handler) { - super(ServerNetworkingImpl.LOGIN, "ServerLoginNetworkAddon for " + handler.getConnectionInfo()); - this.connection = ((ServerLoginNetworkHandlerAccessor) handler).getConnection(); - this.handler = handler; - this.server = ((ServerLoginNetworkHandlerAccessor) handler).getServer(); - this.queryIdFactory = QueryIdFactory.create(); - } - - @Override - protected void invokeInitEvent() { - ServerLoginConnectionEvents.INIT.invoker().onLoginInit(handler, this.server); - } - - // return true if no longer ticks query - public boolean queryTick() { - if (this.firstQueryTick) { - // Send the compression packet now so clients receive compressed login queries - this.sendCompressionPacket(); - - ServerLoginConnectionEvents.QUERY_START.invoker().onLoginStart(this.handler, this.server, this, this.waits::add); - this.firstQueryTick = false; - } - - AtomicReference error = new AtomicReference<>(); - this.waits.removeIf(future -> { - if (!future.isDone()) { - return false; - } - - try { - future.get(); - } catch (ExecutionException ex) { - Throwable caught = ex.getCause(); - error.getAndUpdate(oldEx -> { - if (oldEx == null) { - return caught; - } - - oldEx.addSuppressed(caught); - return oldEx; - }); - } catch (InterruptedException | CancellationException ignored) { - // ignore - } - - return true; - }); - - return this.channels.isEmpty() && this.waits.isEmpty(); - } - - private void sendCompressionPacket() { - // Compression is not needed for local transport - if (this.server.getNetworkCompressionThreshold() >= 0 && !this.connection.isLocal()) { - this.connection.send(new LoginCompressionS2CPacket(this.server.getNetworkCompressionThreshold()), - PacketCallbacks.always(() -> connection.setCompressionThreshold(server.getNetworkCompressionThreshold(), true)) - ); - } - } - - /** - * Handles an incoming query response during login. - * - * @param packet the packet to handle - * @return true if the packet was handled - */ - public boolean handle(LoginQueryResponseC2SPacket packet) { - PacketByteBufLoginQueryResponse response = (PacketByteBufLoginQueryResponse) packet.response(); - return handle(packet.queryId(), response == null ? null : response.data()); - } - - private boolean handle(int queryId, @Nullable PacketByteBuf originalBuf) { - this.logger.debug("Handling inbound login query with id {}", queryId); - Identifier channel = this.channels.remove(queryId); - - if (channel == null) { - this.logger.warn("Query ID {} was received but no query has been associated in {}!", queryId, this.connection); - return false; - } - - boolean understood = originalBuf != null; - @Nullable ServerLoginNetworking.LoginQueryResponseHandler handler = this.getHandler(channel); - - if (handler == null) { - return false; - } - - PacketByteBuf buf = understood ? PacketByteBufs.slice(originalBuf) : PacketByteBufs.empty(); - - try { - handler.receive(this.server, this.handler, understood, buf, this.waits::add, this); - } catch (Throwable ex) { - this.logger.error("Encountered exception while handling in channel \"{}\"", channel, ex); - throw ex; - } - - return true; - } - - @Override - public Packet createPacket(CustomPayload packet) { - throw new UnsupportedOperationException("Cannot send CustomPayload during login"); - } - - @Override - public Packet createPacket(Identifier channelName, PacketByteBuf buf) { - int queryId = this.queryIdFactory.nextId(); - return new LoginQueryRequestS2CPacket(queryId, new PacketByteBufLoginQueryRequestPayload(channelName, buf)); - } - - @Override - public void sendPacket(Packet packet, PacketCallbacks callback) { - Objects.requireNonNull(packet, "Packet cannot be null"); - - this.connection.send(packet, callback); - } - - @Override - public void disconnect(Text disconnectReason) { - Objects.requireNonNull(disconnectReason, "Disconnect reason cannot be null"); - - this.connection.disconnect(disconnectReason); - } - - public void registerOutgoingPacket(LoginQueryRequestS2CPacket packet) { - this.channels.put(packet.queryId(), packet.payload().id()); - } - - @Override - protected void handleRegistration(Identifier channelName) { - } - - @Override - protected void handleUnregistration(Identifier channelName) { - } - - @Override - protected void invokeDisconnectEvent() { - ServerLoginConnectionEvents.DISCONNECT.invoker().onLoginDisconnect(this.handler, this.server); - } - - @Override - protected boolean isReservedChannel(Identifier channelName) { - return false; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerNetworkingImpl.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerNetworkingImpl.java deleted file mode 100644 index 72234acd2ab..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerNetworkingImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.server; - -import java.util.Objects; - -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.NetworkSide; -import net.minecraft.network.listener.ClientCommonPacketListener; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; -import net.minecraft.server.network.ServerLoginNetworkHandler; -import net.minecraft.server.network.ServerPlayNetworkHandler; - -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry; -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; - -public final class ServerNetworkingImpl { - public static final GlobalReceiverRegistry LOGIN = new GlobalReceiverRegistry<>(NetworkSide.SERVERBOUND, NetworkPhase.LOGIN, null); - public static final GlobalReceiverRegistry> CONFIGURATION = new GlobalReceiverRegistry<>(NetworkSide.SERVERBOUND, NetworkPhase.CONFIGURATION, PayloadTypeRegistryImpl.CONFIGURATION_C2S); - public static final GlobalReceiverRegistry> PLAY = new GlobalReceiverRegistry<>(NetworkSide.SERVERBOUND, NetworkPhase.PLAY, PayloadTypeRegistryImpl.PLAY_C2S); - - public static ServerPlayNetworkAddon getAddon(ServerPlayNetworkHandler handler) { - return (ServerPlayNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static ServerLoginNetworkAddon getAddon(ServerLoginNetworkHandler handler) { - return (ServerLoginNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static ServerConfigurationNetworkAddon getAddon(ServerConfigurationNetworkHandler handler) { - return (ServerConfigurationNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon(); - } - - public static Packet createS2CPacket(CustomPayload payload) { - Objects.requireNonNull(payload, "Payload cannot be null"); - Objects.requireNonNull(payload.getId(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass()); - - return new CustomPayloadS2CPacket(payload); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java deleted file mode 100644 index 2406133a096..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.networking.server; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -import net.minecraft.network.ClientConnection; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.fabricmc.fabric.api.networking.v1.S2CPlayChannelEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon; -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; -import net.fabricmc.fabric.impl.networking.NetworkingImpl; -import net.fabricmc.fabric.impl.networking.RegistrationPayload; - -public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon> { - private final ServerPlayNetworkHandler handler; - private final MinecraftServer server; - private boolean sentInitialRegisterPacket; - private final ServerPlayNetworking.Context context; - - public ServerPlayNetworkAddon(ServerPlayNetworkHandler handler, ClientConnection connection, MinecraftServer server) { - super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getDisplayName()); - this.handler = handler; - this.server = server; - this.context = new ContextImpl(server, handler, this); - - // Must register pending channels via lateinit - this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY); - } - - @Override - protected void invokeInitEvent() { - ServerPlayConnectionEvents.INIT.invoker().onPlayInit(this.handler, this.server); - } - - public void onClientReady() { - ServerPlayConnectionEvents.JOIN.invoker().onPlayReady(this.handler, this, this.server); - - this.sendInitialChannelRegistrationPacket(); - this.sentInitialRegisterPacket = true; - } - - @Override - protected void receive(ServerPlayNetworking.PlayPayloadHandler payloadHandler, CustomPayload payload) { - this.server.execute(() -> { - ((ServerPlayNetworking.PlayPayloadHandler) payloadHandler).receive(payload, ServerPlayNetworkAddon.this.context); - }); - } - - // impl details - - @Override - protected void schedule(Runnable task) { - this.handler.player.server.execute(task); - } - - @Override - public Packet createPacket(CustomPayload packet) { - return ServerPlayNetworking.createS2CPacket(packet); - } - - @Override - protected void invokeRegisterEvent(List ids) { - S2CPlayChannelEvents.REGISTER.invoker().onChannelRegister(this.handler, this, this.server, ids); - } - - @Override - protected void invokeUnregisterEvent(List ids) { - S2CPlayChannelEvents.UNREGISTER.invoker().onChannelUnregister(this.handler, this, this.server, ids); - } - - @Override - protected void handleRegistration(Identifier channelName) { - // If we can already send packets, immediately send the register packet for this channel - if (this.sentInitialRegisterPacket) { - RegistrationPayload registrationPayload = this.createRegistrationPayload(RegistrationPayload.REGISTER, Collections.singleton(channelName)); - - if (registrationPayload != null) { - this.sendPacket(registrationPayload); - } - } - } - - @Override - protected void handleUnregistration(Identifier channelName) { - // If we can already send packets, immediately send the unregister packet for this channel - if (this.sentInitialRegisterPacket) { - RegistrationPayload registrationPayload = this.createRegistrationPayload(RegistrationPayload.UNREGISTER, Collections.singleton(channelName)); - - if (registrationPayload != null) { - this.sendPacket(registrationPayload); - } - } - } - - @Override - protected void invokeDisconnectEvent() { - ServerPlayConnectionEvents.DISCONNECT.invoker().onPlayDisconnect(this.handler, this.server); - } - - @Override - protected boolean isReservedChannel(Identifier channelName) { - return NetworkingImpl.isReservedCommonChannel(channelName); - } - - private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { - private ContextImpl { - Objects.requireNonNull(server, "server"); - Objects.requireNonNull(handler, "handler"); - Objects.requireNonNull(responseSender, "responseSender"); - } - - @Override - public ServerPlayerEntity player() { - return handler.getPlayer(); - } - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ClientConnectionMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ClientConnectionMixin.java deleted file mode 100644 index 27ad5a6ffb3..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ClientConnectionMixin.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import io.netty.channel.ChannelHandlerContext; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.ClientConnection; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.NetworkSide; -import net.minecraft.network.NetworkState; -import net.minecraft.network.PacketCallbacks; -import net.minecraft.network.listener.PacketListener; -import net.minecraft.network.packet.Packet; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.PacketCallbackListener; - -@Mixin(ClientConnection.class) -abstract class ClientConnectionMixin implements ChannelInfoHolder { - @Shadow - private PacketListener packetListener; - - @Unique - private Map> playChannels; - - @Inject(method = "", at = @At("RETURN")) - private void initAddedFields(NetworkSide side, CallbackInfo ci) { - this.playChannels = new ConcurrentHashMap<>(); - } - - @Inject(method = "sendImmediately", at = @At(value = "FIELD", target = "Lnet/minecraft/network/ClientConnection;packetsSentCounter:I")) - private void checkPacket(Packet packet, PacketCallbacks callback, boolean flush, CallbackInfo ci) { - if (this.packetListener instanceof PacketCallbackListener) { - ((PacketCallbackListener) this.packetListener).sent(packet); - } - } - - @Inject(method = "setPacketListener", at = @At("HEAD")) - private void unwatchAddon(NetworkState state, PacketListener listener, CallbackInfo ci) { - if (this.packetListener instanceof NetworkHandlerExtensions oldListener) { - oldListener.getAddon().endSession(); - } - } - - @Inject(method = "channelInactive", at = @At("HEAD")) - private void disconnectAddon(ChannelHandlerContext channelHandlerContext, CallbackInfo ci) { - if (packetListener instanceof NetworkHandlerExtensions extension) { - extension.getAddon().handleDisconnect(); - } - } - - @Inject(method = "handleDisconnection", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/listener/PacketListener;onDisconnected(Lnet/minecraft/network/DisconnectionInfo;)V")) - private void disconnectAddon(CallbackInfo ci) { - if (packetListener instanceof NetworkHandlerExtensions extension) { - extension.getAddon().handleDisconnect(); - } - } - - @Override - public Collection fabric_getPendingChannelsNames(NetworkPhase state) { - return this.playChannels.computeIfAbsent(state, (key) -> Collections.newSetFromMap(new ConcurrentHashMap<>())); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CommandManagerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CommandManagerMixin.java deleted file mode 100644 index dfdc4e00489..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CommandManagerMixin.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import com.mojang.brigadier.CommandDispatcher; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.SharedConstants; -import net.minecraft.command.CommandRegistryAccess; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.command.DebugConfigCommand; -import net.minecraft.server.command.ServerCommandSource; - -import net.fabricmc.loader.api.FabricLoader; - -@Mixin(CommandManager.class) -public class CommandManagerMixin { - @Shadow - @Final - private CommandDispatcher dispatcher; - - @Inject(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/dedicated/command/BanIpCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V")) - private void init(CommandManager.RegistrationEnvironment environment, CommandRegistryAccess commandRegistryAccess, CallbackInfo ci) { - if (SharedConstants.isDevelopment) { - // Command is registered when isDevelopment is set. - return; - } - - if (!FabricLoader.getInstance().isDevelopmentEnvironment()) { - // Only register this command in a dev env - return; - } - - DebugConfigCommand.register(this.dispatcher); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java deleted file mode 100644 index 34b48c6b7dd..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadC2SPacketMixin.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import java.util.List; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; - -import net.fabricmc.fabric.impl.networking.FabricCustomPayloadPacketCodec; -import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; - -@Mixin(CustomPayloadC2SPacket.class) -public class CustomPayloadC2SPacketMixin { - @WrapOperation( - method = "", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/network/packet/CustomPayload;createCodec(Lnet/minecraft/network/packet/CustomPayload$CodecFactory;Ljava/util/List;)Lnet/minecraft/network/codec/PacketCodec;" - ) - ) - private static PacketCodec wrapCodec(CustomPayload.CodecFactory unknownCodecFactory, List> types, Operation> original) { - PacketCodec codec = original.call(unknownCodecFactory, types); - FabricCustomPayloadPacketCodec fabricCodec = (FabricCustomPayloadPacketCodec) codec; - fabricCodec.fabric_setPacketCodecProvider((packetByteBuf, identifier) -> { - // CustomPayloadC2SPacket does not have a separate codec for play/configuration. We know if the packetByteBuf is a PacketByteBuf we are in the play phase. - if (packetByteBuf instanceof RegistryByteBuf) { - return (CustomPayload.Type) (Object) PayloadTypeRegistryImpl.PLAY_C2S.get(identifier); - } - - return PayloadTypeRegistryImpl.CONFIGURATION_C2S.get(identifier); - }); - return codec; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadPacketCodecMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadPacketCodecMixin.java deleted file mode 100644 index 539264541ec..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadPacketCodecMixin.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Coerce; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.impl.networking.CustomPayloadTypeProvider; -import net.fabricmc.fabric.impl.networking.FabricCustomPayloadPacketCodec; - -@Mixin(targets = "net/minecraft/network/packet/CustomPayload$1") -public abstract class CustomPayloadPacketCodecMixin implements PacketCodec, FabricCustomPayloadPacketCodec { - @Unique - private CustomPayloadTypeProvider customPayloadTypeProvider; - - @Override - public void fabric_setPacketCodecProvider(CustomPayloadTypeProvider customPayloadTypeProvider) { - if (this.customPayloadTypeProvider != null) { - throw new IllegalStateException("Payload codec provider is already set!"); - } - - this.customPayloadTypeProvider = customPayloadTypeProvider; - } - - @WrapOperation(method = { - "encode(Lnet/minecraft/network/PacketByteBuf;Lnet/minecraft/network/packet/CustomPayload$Id;Lnet/minecraft/network/packet/CustomPayload;)V", - "decode(Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/CustomPayload;" - }, at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/CustomPayload$1;getCodec(Lnet/minecraft/util/Identifier;)Lnet/minecraft/network/codec/PacketCodec;")) - private PacketCodec wrapGetCodec(@Coerce PacketCodec instance, Identifier identifier, Operation> original, B packetByteBuf) { - if (customPayloadTypeProvider != null) { - CustomPayload.Type payloadType = customPayloadTypeProvider.get(packetByteBuf, identifier); - - if (payloadType != null) { - return payloadType.codec(); - } - } - - return original.call(instance, identifier); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java deleted file mode 100644 index 2be96e8ca9f..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadS2CPacketMixin.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import java.util.List; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; - -import net.fabricmc.fabric.impl.networking.FabricCustomPayloadPacketCodec; -import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; - -@Mixin(CustomPayloadS2CPacket.class) -public class CustomPayloadS2CPacketMixin { - @WrapOperation( - method = "", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/network/packet/CustomPayload;createCodec(Lnet/minecraft/network/packet/CustomPayload$CodecFactory;Ljava/util/List;)Lnet/minecraft/network/codec/PacketCodec;", - ordinal = 0 - ) - ) - private static PacketCodec wrapPlayCodec(CustomPayload.CodecFactory unknownCodecFactory, List> types, Operation> original) { - PacketCodec codec = original.call(unknownCodecFactory, types); - FabricCustomPayloadPacketCodec fabricCodec = (FabricCustomPayloadPacketCodec) codec; - fabricCodec.fabric_setPacketCodecProvider((packetByteBuf, identifier) -> PayloadTypeRegistryImpl.PLAY_S2C.get(identifier)); - return codec; - } - - @WrapOperation( - method = "", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/network/packet/CustomPayload;createCodec(Lnet/minecraft/network/packet/CustomPayload$CodecFactory;Ljava/util/List;)Lnet/minecraft/network/codec/PacketCodec;", - ordinal = 1 - ) - ) - private static PacketCodec wrapConfigCodec(CustomPayload.CodecFactory unknownCodecFactory, List> types, Operation> original) { - PacketCodec codec = original.call(unknownCodecFactory, types); - FabricCustomPayloadPacketCodec fabricCodec = (FabricCustomPayloadPacketCodec) codec; - fabricCodec.fabric_setPacketCodecProvider((packetByteBuf, identifier) -> PayloadTypeRegistryImpl.CONFIGURATION_S2C.get(identifier)); - return codec; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java deleted file mode 100644 index 7e908734ef3..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.entity.Entity; -import net.minecraft.server.network.EntityTrackerEntry; -import net.minecraft.server.network.ServerPlayerEntity; - -import net.fabricmc.fabric.api.networking.v1.EntityTrackingEvents; - -@Mixin(EntityTrackerEntry.class) -abstract class EntityTrackerEntryMixin { - @Shadow - @Final - private Entity entity; - - @Inject(method = "startTracking", at = @At("HEAD")) - private void onStartTracking(ServerPlayerEntity player, CallbackInfo ci) { - EntityTrackingEvents.START_TRACKING.invoker().onStartTracking(this.entity, player); - } - - @Inject(method = "stopTracking", at = @At("TAIL")) - private void onStopTracking(ServerPlayerEntity player, CallbackInfo ci) { - EntityTrackingEvents.STOP_TRACKING.invoker().onStopTracking(this.entity, player); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryRequestS2CPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryRequestS2CPacketMixin.java deleted file mode 100644 index 181543f812e..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryRequestS2CPacketMixin.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestPayload; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload; -import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; - -@Mixin(LoginQueryRequestS2CPacket.class) -public class LoginQueryRequestS2CPacketMixin { - @Shadow - @Final - private static int MAX_PAYLOAD_SIZE; - - @Inject(method = "readPayload", at = @At("HEAD"), cancellable = true) - private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable cir) { - cir.setReturnValue(new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE))); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java deleted file mode 100644 index e3ab52d22ee..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/LoginQueryResponseC2SPacketMixin.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket; -import net.minecraft.network.packet.c2s.login.LoginQueryResponsePayload; - -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; -import net.fabricmc.fabric.impl.networking.payload.PayloadHelper; - -@Mixin(LoginQueryResponseC2SPacket.class) -public class LoginQueryResponseC2SPacketMixin { - @Shadow - @Final - private static int MAX_PAYLOAD_SIZE; - - @Inject(method = "readPayload", at = @At("HEAD"), cancellable = true) - private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoReturnable cir) { - boolean hasPayload = buf.readBoolean(); - - if (!hasPayload) { - cir.setReturnValue(null); - return; - } - - cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf, MAX_PAYLOAD_SIZE))); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PacketCodecDispatcherMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PacketCodecDispatcherMixin.java deleted file mode 100644 index 267abf119ec..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PacketCodecDispatcherMixin.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import com.llamalad7.mixinextras.sugar.Local; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.EncoderException; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.handler.PacketCodecDispatcher; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; - -@Mixin(PacketCodecDispatcher.class) -public abstract class PacketCodecDispatcherMixin implements PacketCodec { - // Add the custom payload id to the error message - @Inject(method = "encode(Lio/netty/buffer/ByteBuf;Ljava/lang/Object;)V", at = @At(value = "NEW", target = "(Ljava/lang/String;Ljava/lang/Throwable;)Lio/netty/handler/codec/EncoderException;")) - public void encode(B byteBuf, V packet, CallbackInfo ci, @Local(ordinal = 1) T packetId, @Local Exception e) { - CustomPayload payload = null; - - if (packet instanceof CustomPayloadC2SPacket customPayloadC2SPacket) { - payload = customPayloadC2SPacket.payload(); - } else if (packet instanceof CustomPayloadS2CPacket customPayloadS2CPacket) { - payload = customPayloadS2CPacket.payload(); - } - - if (payload != null && payload.getId() != null) { - throw new EncoderException("Failed to encode packet '%s' (%s)".formatted(packetId, payload.getId().id().toString()), e); - } - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PlayerManagerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PlayerManagerMixin.java deleted file mode 100644 index beacc1f51fa..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/PlayerManagerMixin.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.ClientConnection; -import net.minecraft.server.PlayerManager; -import net.minecraft.server.network.ConnectedClientData; -import net.minecraft.server.network.ServerPlayerEntity; - -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; - -@Mixin(PlayerManager.class) -abstract class PlayerManagerMixin { - @Inject(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/PlayerAbilitiesS2CPacket;(Lnet/minecraft/entity/player/PlayerAbilities;)V")) - private void handlePlayerConnection(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData arg, CallbackInfo ci) { - ServerNetworkingImpl.getAddon(player.networkHandler).onClientReady(); - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java deleted file mode 100644 index 18dff3b2037..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.network.packet.c2s.common.CommonPongC2SPacket; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; -import net.minecraft.server.network.ServerCommonNetworkHandler; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon; - -@Mixin(ServerCommonNetworkHandler.class) -public abstract class ServerCommonNetworkHandlerMixin implements NetworkHandlerExtensions { - @Inject(method = "onCustomPayload", at = @At("HEAD"), cancellable = true) - private void handleCustomPayloadReceivedAsync(CustomPayloadC2SPacket packet, CallbackInfo ci) { - final CustomPayload payload = packet.payload(); - - boolean handled; - - if (getAddon() instanceof ServerConfigurationNetworkAddon addon) { - handled = addon.handle(payload); - } else { - // Play should be handled in ServerPlayNetworkHandlerMixin - throw new IllegalStateException("Unknown addon"); - } - - if (handled) { - ci.cancel(); - } - } - - @Inject(method = "onPong", at = @At("HEAD")) - private void onPlayPong(CommonPongC2SPacket packet, CallbackInfo ci) { - if (getAddon() instanceof ServerConfigurationNetworkAddon addon) { - addon.onPong(packet.getParameter()); - } - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerConfigurationNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerConfigurationNetworkHandlerMixin.java index 8989f66287a..f8bbb24a24a 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerConfigurationNetworkHandlerMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerConfigurationNetworkHandlerMixin.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,151 +17,17 @@ package net.fabricmc.fabric.mixin.networking; -import java.util.Queue; - -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import net.minecraft.network.ClientConnection; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ConnectedClientData; -import net.minecraft.server.network.ServerCommonNetworkHandler; import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.server.network.ServerPlayerConfigurationTask; import net.fabricmc.fabric.api.networking.v1.FabricServerConfigurationNetworkHandler; -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon; - -// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues -@Mixin(value = ServerConfigurationNetworkHandler.class, priority = 900) -public abstract class ServerConfigurationNetworkHandlerMixin extends ServerCommonNetworkHandler implements NetworkHandlerExtensions, FabricServerConfigurationNetworkHandler { - @Shadow - @Nullable - private ServerPlayerConfigurationTask currentTask; - - @Shadow - protected abstract void onTaskFinished(ServerPlayerConfigurationTask.Key key); - - @Shadow - @Final - private Queue tasks; - - @Shadow - public abstract boolean isConnectionOpen(); - - @Shadow - public abstract void sendConfigurations(); - - @Unique - private ServerConfigurationNetworkAddon addon; - - @Unique - private boolean sentConfiguration; - - @Unique - private boolean earlyTaskExecution; - - public ServerConfigurationNetworkHandlerMixin(MinecraftServer server, ClientConnection connection, ConnectedClientData arg) { - super(server, connection, arg); - } - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ServerConfigurationNetworkAddon((ServerConfigurationNetworkHandler) (Object) this, this.server); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - this.addon.lateInit(); - } - - @Inject(method = "sendConfigurations", at = @At("HEAD"), cancellable = true) - private void onClientReady(CallbackInfo ci) { - // Send the initial channel registration packet - if (this.addon.startConfiguration()) { - assert currentTask == null; - ci.cancel(); - return; - } - - // Ready to start sending packets - if (!sentConfiguration) { - this.addon.preConfiguration(); - sentConfiguration = true; - earlyTaskExecution = true; - } - - // Run the early tasks - if (earlyTaskExecution) { - if (pollEarlyTasks()) { - ci.cancel(); - return; - } else { - earlyTaskExecution = false; - } - } - - // All early tasks should have been completed - assert currentTask == null; - assert tasks.isEmpty(); - - // Run the vanilla tasks. - this.addon.configuration(); - } - - @Unique - private boolean pollEarlyTasks() { - if (!earlyTaskExecution) { - throw new IllegalStateException("Early task execution has finished"); - } - - if (this.currentTask != null) { - throw new IllegalStateException("Task " + this.currentTask.getKey().id() + " has not finished yet"); - } - - if (!this.isConnectionOpen()) { - return false; - } - - final ServerPlayerConfigurationTask task = this.tasks.poll(); - - if (task != null) { - this.currentTask = task; - task.sendPacket(this::sendPacket); - return true; - } - - return false; - } - - @Override - public ServerConfigurationNetworkAddon getAddon() { - return addon; - } - - @Override - public void addTask(ServerPlayerConfigurationTask task) { - tasks.add(task); - } +@Mixin(ServerConfigurationNetworkHandler.class) +public abstract class ServerConfigurationNetworkHandlerMixin implements FabricServerConfigurationNetworkHandler { @Override public void completeTask(ServerPlayerConfigurationTask.Key key) { - if (!earlyTaskExecution) { - onTaskFinished(key); - return; - } - - final ServerPlayerConfigurationTask.Key currentKey = this.currentTask != null ? this.currentTask.getKey() : null; - - if (!key.equals(currentKey)) { - throw new IllegalStateException("Unexpected request for task finish, current task: " + currentKey + ", requested: " + key); - } - - this.currentTask = null; - sendConfigurations(); + this.finishTask(key); } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerLoginNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerLoginNetworkHandlerMixin.java deleted file mode 100644 index 04f310ea7c5..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerLoginNetworkHandlerMixin.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import com.mojang.authlib.GameProfile; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket; -import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerLoginNetworkHandler; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.PacketCallbackListener; -import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse; -import net.fabricmc.fabric.impl.networking.server.ServerLoginNetworkAddon; - -@Mixin(ServerLoginNetworkHandler.class) -abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerExtensions, PacketCallbackListener { - @Shadow - protected abstract void tickVerify(GameProfile profile); - - @Unique - private ServerLoginNetworkAddon addon; - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ServerLoginNetworkAddon((ServerLoginNetworkHandler) (Object) this); - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - this.addon.lateInit(); - } - - @Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerLoginNetworkHandler;tickVerify(Lcom/mojang/authlib/GameProfile;)V")) - private void handlePlayerJoin(ServerLoginNetworkHandler instance, GameProfile profile) { - // Do not accept the player, thereby moving into play stage until all login futures being waited on are completed - if (this.addon.queryTick()) { - this.tickVerify(profile); - } - } - - @Inject(method = "onQueryResponse", at = @At("HEAD"), cancellable = true) - private void handleCustomPayloadReceivedAsync(LoginQueryResponseC2SPacket packet, CallbackInfo ci) { - // Handle queries - if (this.addon.handle(packet)) { - ci.cancel(); - } else { - if (packet.response() instanceof PacketByteBufLoginQueryResponse response) { - response.data().skipBytes(response.data().readableBytes()); - } - } - } - - @Redirect(method = "tickVerify", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;getNetworkCompressionThreshold()I", ordinal = 0)) - private int removeLateCompressionPacketSending(MinecraftServer server) { - return -1; - } - - @Override - public void sent(Packet packet) { - if (packet instanceof LoginQueryRequestS2CPacket) { - this.addon.registerOutgoingPacket((LoginQueryRequestS2CPacket) packet); - } - } - - @Override - public ServerLoginNetworkAddon getAddon() { - return this.addon; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java deleted file mode 100644 index 7fae773f1d1..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ConnectedClientData; -import net.minecraft.server.network.ServerCommonNetworkHandler; -import net.minecraft.server.network.ServerPlayNetworkHandler; - -import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions; -import net.fabricmc.fabric.impl.networking.UntrackedNetworkHandler; -import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon; - -// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues -@Mixin(value = ServerPlayNetworkHandler.class, priority = 999) -abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler implements NetworkHandlerExtensions { - @Unique - private ServerPlayNetworkAddon addon; - - ServerPlayNetworkHandlerMixin(MinecraftServer server, ClientConnection connection, ConnectedClientData arg) { - super(server, connection, arg); - } - - @Inject(method = "", at = @At("RETURN")) - private void initAddon(CallbackInfo ci) { - this.addon = new ServerPlayNetworkAddon((ServerPlayNetworkHandler) (Object) this, connection, server); - - if (!(this instanceof UntrackedNetworkHandler)) { - // A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field - this.addon.lateInit(); - } - } - - @Inject(method = "onCustomPayload", at = @At("HEAD"), cancellable = true) - private void handleCustomPayloadReceivedAsync(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (getAddon().handle(packet.payload())) { - ci.cancel(); - } - } - - @Override - public ServerPlayNetworkAddon getAddon() { - return this.addon; - } -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/EntityTrackerAccessor.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/EntityTrackerAccessor.java deleted file mode 100644 index c52d424b09b..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/EntityTrackerAccessor.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.accessor; - -import java.util.Set; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.server.network.PlayerAssociatedNetworkHandler; - -@Mixin(targets = "net/minecraft/server/world/ServerChunkLoadingManager$EntityTracker") -public interface EntityTrackerAccessor { - @Accessor("listeners") - Set getPlayersTracking(); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerChunkLoadingManagerAccessor.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerChunkLoadingManagerAccessor.java deleted file mode 100644 index 6c8b498df90..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerChunkLoadingManagerAccessor.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.accessor; - -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.server.world.ServerChunkLoadingManager; - -@Mixin(ServerChunkLoadingManager.class) -public interface ServerChunkLoadingManagerAccessor { - @Accessor - Int2ObjectMap getEntityTrackers(); -} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerCommonNetworkHandlerAccessor.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerCommonNetworkHandlerAccessor.java deleted file mode 100644 index 23012e3c84d..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerCommonNetworkHandlerAccessor.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.accessor; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.network.ClientConnection; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerCommonNetworkHandler; - -@Mixin(ServerCommonNetworkHandler.class) -public interface ServerCommonNetworkHandlerAccessor { - @Accessor - ClientConnection getConnection(); - - @Accessor - MinecraftServer getServer(); -} diff --git a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ConnectScreenAccessor.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkAddonAccessor.java similarity index 75% rename from fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ConnectScreenAccessor.java rename to fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkAddonAccessor.java index d6dc4268047..f55459603e6 100644 --- a/fabric-networking-api-v1/src/client/java/net/fabricmc/fabric/mixin/networking/client/accessor/ConnectScreenAccessor.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkAddonAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2024 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,16 +14,16 @@ * limitations under the License. */ -package net.fabricmc.fabric.mixin.networking.client.accessor; +package net.fabricmc.fabric.mixin.networking.accessor; +import org.quiltmc.qsl.networking.impl.server.ServerLoginNetworkAddon; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; -import net.minecraft.client.gui.screen.multiplayer.ConnectScreen; import net.minecraft.network.ClientConnection; -@Mixin(ConnectScreen.class) -public interface ConnectScreenAccessor { +@Mixin(ServerLoginNetworkAddon.class) +public interface ServerLoginNetworkAddonAccessor { @Accessor ClientConnection getConnection(); } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkHandlerAccessor.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkHandlerAccessor.java deleted file mode 100644 index 244ad6de2aa..00000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/accessor/ServerLoginNetworkHandlerAccessor.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.networking.accessor; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.network.ClientConnection; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerLoginNetworkHandler; - -@Mixin(ServerLoginNetworkHandler.class) -public interface ServerLoginNetworkHandlerAccessor { - @Accessor - MinecraftServer getServer(); - - @Accessor - ClientConnection getConnection(); -} diff --git a/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json b/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json index 4ec0e45e812..60f8aa57374 100644 --- a/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json +++ b/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json @@ -3,24 +3,8 @@ "package": "net.fabricmc.fabric.mixin.networking", "compatibilityLevel": "JAVA_17", "mixins": [ - "PacketCodecDispatcherMixin", - "ClientConnectionMixin", - "CommandManagerMixin", - "CustomPayloadC2SPacketMixin", - "CustomPayloadS2CPacketMixin", - "CustomPayloadPacketCodecMixin", - "EntityTrackerEntryMixin", - "LoginQueryRequestS2CPacketMixin", - "LoginQueryResponseC2SPacketMixin", - "PlayerManagerMixin", - "ServerCommonNetworkHandlerMixin", - "ServerConfigurationNetworkHandlerMixin", - "ServerLoginNetworkHandlerMixin", - "ServerPlayNetworkHandlerMixin", - "accessor.EntityTrackerAccessor", - "accessor.ServerCommonNetworkHandlerAccessor", - "accessor.ServerLoginNetworkHandlerAccessor", - "accessor.ServerChunkLoadingManagerAccessor" + "accessor.ServerLoginNetworkAddonAccessor", + "ServerConfigurationNetworkHandlerMixin" ], "injectors": { "defaultRequire": 1 diff --git a/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java b/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java deleted file mode 100644 index 1674ef82cc3..00000000000 --- a/fabric-networking-api-v1/src/test/java/net/fabricmc/fabric/test/networking/unit/CommonPacketTests.java +++ /dev/null @@ -1,407 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.test.networking.unit; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertIterableEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientConfigurationNetworkHandler; -import net.minecraft.network.NetworkPhase; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; -import net.minecraft.util.Identifier; - -import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; -import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; -import net.fabricmc.fabric.impl.networking.CommonPacketHandler; -import net.fabricmc.fabric.impl.networking.CommonPacketsImpl; -import net.fabricmc.fabric.impl.networking.CommonRegisterPayload; -import net.fabricmc.fabric.impl.networking.CommonVersionPayload; -import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl; -import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon; -import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl; - -public class CommonPacketTests { - private static final CustomPayload.Type VERSION_PAYLOAD_TYPE = new CustomPayload.Type<>(CommonVersionPayload.ID, CommonVersionPayload.CODEC); - private static final CustomPayload.Type REGISTER_PAYLOAD_TYPE = new CustomPayload.Type<>(CommonRegisterPayload.ID, CommonRegisterPayload.CODEC); - - private PacketSender packetSender; - private ChannelInfoHolder channelInfoHolder; - - private ClientConfigurationNetworkHandler clientNetworkHandler; - private ClientConfigurationNetworkAddon clientAddon; - - private ServerConfigurationNetworkHandler serverNetworkHandler; - private ServerConfigurationNetworkAddon serverAddon; - - private ClientConfigurationNetworking.Context clientContext; - private ServerConfigurationNetworking.Context serverContext; - - @BeforeAll - static void beforeAll() { - CommonPacketsImpl.init(); - ClientNetworkingImpl.clientInit(); - - // Register the packet codec on both sides - PayloadTypeRegistry.playS2C().register(TestPayload.ID, TestPayload.CODEC); - - // Listen for the payload on the client - ClientPlayNetworking.registerGlobalReceiver(TestPayload.ID, (payload, context) -> { - System.out.println(payload.data()); - }); - } - - private record TestPayload(String data) implements CustomPayload { - static final CustomPayload.Id ID = new CustomPayload.Id<>(Identifier.of("fabric", "global_client")); - static final PacketCodec CODEC = CustomPayload.codecOf(TestPayload::write, TestPayload::new); - - TestPayload(RegistryByteBuf buf) { - this(buf.readString()); - } - - private void write(RegistryByteBuf buf) { - buf.writeString(data); - } - - @Override - public Id getId() { - return ID; - } - } - - @BeforeEach - void setUp() { - packetSender = mock(PacketSender.class); - channelInfoHolder = new MockChannelInfoHolder(); - - clientNetworkHandler = mock(ClientConfigurationNetworkHandler.class); - clientAddon = mock(ClientConfigurationNetworkAddon.class); - when(ClientNetworkingImpl.getAddon(clientNetworkHandler)).thenReturn(clientAddon); - when(clientAddon.getChannelInfoHolder()).thenReturn(channelInfoHolder); - - serverNetworkHandler = mock(ServerConfigurationNetworkHandler.class); - serverAddon = mock(ServerConfigurationNetworkAddon.class); - when(ServerNetworkingImpl.getAddon(serverNetworkHandler)).thenReturn(serverAddon); - when(serverAddon.getChannelInfoHolder()).thenReturn(channelInfoHolder); - - ClientNetworkingImpl.setClientConfigurationAddon(clientAddon); - - clientContext = new ClientConfigurationNetworking.Context() { - @Override - public MinecraftClient client() { - return null; - } - - @Override - public PacketSender responseSender() { - return packetSender; - } - }; - serverContext = new ServerConfigurationNetworking.Context() { - @Override - public MinecraftServer server() { - return null; - } - - @Override - public ServerConfigurationNetworkHandler networkHandler() { - return serverNetworkHandler; - } - - @Override - public PacketSender responseSender() { - return packetSender; - } - }; - } - - // Test handling the version packet on the client - @Test - void handleVersionPacketClient() { - ClientConfigurationNetworking.ConfigurationPayloadHandler packetHandler = (ClientConfigurationNetworking.ConfigurationPayloadHandler) ClientNetworkingImpl.CONFIGURATION.getHandler(CommonVersionPayload.ID.id()); - assertNotNull(packetHandler); - - // Receive a packet from the server - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeIntArray(new int[]{1, 2, 3}); - - CommonVersionPayload payload = CommonVersionPayload.CODEC.decode(buf); - packetHandler.receive(payload, clientContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - - // Check the response we are sending back to the server - PacketByteBuf response = readResponse(packetSender, VERSION_PAYLOAD_TYPE); - assertArrayEquals(new int[]{1}, response.readIntArray()); - assertEquals(0, response.readableBytes()); - - assertEquals(1, getNegotiatedVersion(clientAddon)); - } - - // Test handling the version packet on the client, when the server sends unsupported versions - @Test - void handleVersionPacketClientUnsupported() { - ClientConfigurationNetworking.ConfigurationPayloadHandler packetHandler = (ClientConfigurationNetworking.ConfigurationPayloadHandler) ClientNetworkingImpl.CONFIGURATION.getHandler(CommonVersionPayload.ID.id()); - assertNotNull(packetHandler); - - // Receive a packet from the server - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeIntArray(new int[]{2, 3}); // We only support version 1 - - assertThrows(UnsupportedOperationException.class, () -> { - CommonVersionPayload payload = CommonVersionPayload.CODEC.decode(buf); - packetHandler.receive(payload, clientContext); - }); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - } - - // Test handling the version packet on the server - @Test - void handleVersionPacketServer() { - ServerConfigurationNetworking.ConfigurationPacketHandler packetHandler = (ServerConfigurationNetworking.ConfigurationPacketHandler) ServerNetworkingImpl.CONFIGURATION.getHandler(CommonVersionPayload.ID.id()); - assertNotNull(packetHandler); - - // Receive a packet from the client - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeIntArray(new int[]{1, 2, 3}); - - CommonVersionPayload payload = CommonVersionPayload.CODEC.decode(buf); - packetHandler.receive(payload, serverContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - assertEquals(1, getNegotiatedVersion(serverAddon)); - } - - // Test handling the version packet on the server unsupported version - @Test - void handleVersionPacketServerUnsupported() { - ServerConfigurationNetworking.ConfigurationPacketHandler packetHandler = (ServerConfigurationNetworking.ConfigurationPacketHandler) ServerNetworkingImpl.CONFIGURATION.getHandler(CommonVersionPayload.ID.id()); - assertNotNull(packetHandler); - - // Receive a packet from the client - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeIntArray(new int[]{3}); // Server only supports version 1 - - assertThrows(UnsupportedOperationException.class, () -> { - CommonVersionPayload payload = CommonVersionPayload.CODEC.decode(buf); - packetHandler.receive(payload, serverContext); - }); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - } - - // Test handing the play registry packet on the client configuration handler - @Test - void handlePlayRegistryClient() { - ClientConfigurationNetworking.ConfigurationPayloadHandler packetHandler = (ClientConfigurationNetworking.ConfigurationPayloadHandler) ClientNetworkingImpl.CONFIGURATION.getHandler(CommonRegisterPayload.ID.id()); - assertNotNull(packetHandler); - - when(clientAddon.getNegotiatedVersion()).thenReturn(1); - - // Receive a packet from the server - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeVarInt(1); // Version - buf.writeString("play"); // Target phase - buf.writeCollection(List.of(Identifier.of("fabric", "test")), PacketByteBuf::writeIdentifier); - - CommonRegisterPayload payload = CommonRegisterPayload.CODEC.decode(buf); - packetHandler.receive(payload, clientContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - assertIterableEquals(List.of(Identifier.of("fabric", "test")), channelInfoHolder.fabric_getPendingChannelsNames(NetworkPhase.PLAY)); - - // Check the response we are sending back to the server - PacketByteBuf response = readResponse(packetSender, REGISTER_PAYLOAD_TYPE); - assertEquals(1, response.readVarInt()); - assertEquals("play", response.readString()); - assertIterableEquals(List.of(Identifier.of("fabric", "global_client")), response.readCollection(HashSet::new, PacketByteBuf::readIdentifier)); - assertEquals(0, response.readableBytes()); - } - - // Test handling the configuration registry packet on the client configuration handler - @Test - void handleConfigurationRegistryClient() { - ClientConfigurationNetworking.ConfigurationPayloadHandler packetHandler = (ClientConfigurationNetworking.ConfigurationPayloadHandler) ClientNetworkingImpl.CONFIGURATION.getHandler(CommonRegisterPayload.ID.id()); - assertNotNull(packetHandler); - - when(clientAddon.getNegotiatedVersion()).thenReturn(1); - when(clientAddon.createRegisterPayload()).thenAnswer(i -> new CommonRegisterPayload(1, "configuration", Set.of(Identifier.of("fabric", "global_configuration_client")))); - - // Receive a packet from the server - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeVarInt(1); // Version - buf.writeString("configuration"); // Target phase - buf.writeCollection(List.of(Identifier.of("fabric", "test")), PacketByteBuf::writeIdentifier); - - CommonRegisterPayload payload = CommonRegisterPayload.CODEC.decode(buf); - packetHandler.receive(payload, clientContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - verify(clientAddon, times(1)).onCommonRegisterPacket(any()); - - // Check the response we are sending back to the server - PacketByteBuf response = readResponse(packetSender, REGISTER_PAYLOAD_TYPE); - assertEquals(1, response.readVarInt()); - assertEquals("configuration", response.readString()); - assertIterableEquals(List.of(Identifier.of("fabric", "global_configuration_client")), response.readCollection(HashSet::new, PacketByteBuf::readIdentifier)); - assertEquals(0, response.readableBytes()); - } - - // Test handing the play registry packet on the server configuration handler - @Test - void handlePlayRegistryServer() { - ServerConfigurationNetworking.ConfigurationPacketHandler packetHandler = (ServerConfigurationNetworking.ConfigurationPacketHandler) ServerNetworkingImpl.CONFIGURATION.getHandler(CommonRegisterPayload.ID.id()); - assertNotNull(packetHandler); - - when(serverAddon.getNegotiatedVersion()).thenReturn(1); - - // Receive a packet from the client - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeVarInt(1); // Version - buf.writeString("play"); // Target phase - buf.writeCollection(List.of(Identifier.of("fabric", "test")), PacketByteBuf::writeIdentifier); - - CommonRegisterPayload payload = CommonRegisterPayload.CODEC.decode(buf); - packetHandler.receive(payload, serverContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - assertIterableEquals(List.of(Identifier.of("fabric", "test")), channelInfoHolder.fabric_getPendingChannelsNames(NetworkPhase.PLAY)); - } - - // Test handing the configuration registry packet on the server configuration handler - @Test - void handleConfigurationRegistryServer() { - ServerConfigurationNetworking.ConfigurationPacketHandler packetHandler = (ServerConfigurationNetworking.ConfigurationPacketHandler) ServerNetworkingImpl.CONFIGURATION.getHandler(CommonRegisterPayload.ID.id()); - assertNotNull(packetHandler); - - when(serverAddon.getNegotiatedVersion()).thenReturn(1); - - // Receive a packet from the client - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeVarInt(1); // Version - buf.writeString("configuration"); // Target phase - buf.writeCollection(List.of(Identifier.of("fabric", "test")), PacketByteBuf::writeIdentifier); - - CommonRegisterPayload payload = CommonRegisterPayload.CODEC.decode(buf); - packetHandler.receive(payload, serverContext); - - // Assert the entire packet was read - assertEquals(0, buf.readableBytes()); - verify(serverAddon, times(1)).onCommonRegisterPacket(any()); - } - - @Test - public void testHighestCommonVersionWithCommonElement() { - int[] a = {1, 2, 3}; - int[] b = {1, 2}; - assertEquals(2, CommonPacketsImpl.getHighestCommonVersion(a, b)); - } - - @Test - public void testHighestCommonVersionWithoutCommonElement() { - int[] a = {1, 3, 5}; - int[] b = {2, 4, 6}; - assertEquals(-1, CommonPacketsImpl.getHighestCommonVersion(a, b)); - } - - @Test - public void testHighestCommonVersionWithOneEmptyArray() { - int[] a = {1, 3, 5}; - int[] b = {}; - assertEquals(-1, CommonPacketsImpl.getHighestCommonVersion(a, b)); - } - - @Test - public void testHighestCommonVersionWithBothEmptyArrays() { - int[] a = {}; - int[] b = {}; - assertEquals(-1, CommonPacketsImpl.getHighestCommonVersion(a, b)); - } - - @Test - public void testHighestCommonVersionWithIdenticalArrays() { - int[] a = {1, 2, 3}; - int[] b = {1, 2, 3}; - assertEquals(3, CommonPacketsImpl.getHighestCommonVersion(a, b)); - } - - private static PacketByteBuf readResponse(PacketSender packetSender, CustomPayload.Type type) { - ArgumentCaptor responseCaptor = ArgumentCaptor.forClass(CustomPayload.class); - verify(packetSender, times(1)).sendPacket(responseCaptor.capture()); - - final T payload = (T) responseCaptor.getValue(); - final PacketByteBuf buf = PacketByteBufs.create(); - type.codec().encode(buf, payload); - - return buf; - } - - private static int getNegotiatedVersion(CommonPacketHandler packetHandler) { - ArgumentCaptor responseCaptor = ArgumentCaptor.forClass(Integer.class); - verify(packetHandler, times(1)).onCommonVersionPacket(responseCaptor.capture()); - return responseCaptor.getValue(); - } - - private static class MockChannelInfoHolder implements ChannelInfoHolder { - private final Map> playChannels = new ConcurrentHashMap<>(); - - @Override - public Collection fabric_getPendingChannelsNames(NetworkPhase state) { - return this.playChannels.computeIfAbsent(state, (key) -> Collections.newSetFromMap(new ConcurrentHashMap<>())); - } - } -}