diff --git a/src/mixins/resources/mixins.sponge.core.json b/src/mixins/resources/mixins.sponge.core.json index 164d24399c2..4525b2bf3aa 100644 --- a/src/mixins/resources/mixins.sponge.core.json +++ b/src/mixins/resources/mixins.sponge.core.json @@ -39,7 +39,6 @@ "network.chat.StyleMixin", "network.chat.TranslatableContentsMixin", "network.protocol.game.ClientboundResourcePackPacketMixin", - "network.protocol.login.ServerboundCustomQueryAnswerPacketMixin", "network.syncher.EntityDataAccessorMixin", "network.syncher.SynchedEntityDataMixin", "registries.BuiltInRegistriesMixin", diff --git a/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/ServerCommonPacketListenerImplMixin_Vanilla.java b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/ServerCommonPacketListenerImplMixin_Vanilla.java index 5384da527e6..0fe5541a66e 100644 --- a/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/ServerCommonPacketListenerImplMixin_Vanilla.java +++ b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/ServerCommonPacketListenerImplMixin_Vanilla.java @@ -39,14 +39,13 @@ @Mixin(value = ServerCommonPacketListenerImpl.class, priority = 999) public abstract class ServerCommonPacketListenerImplMixin_Vanilla { + + // @formatter: off @Shadow @Final protected MinecraftServer server; + // @formatter: on @Inject(method = "handleCustomPayload", at = @At(value = "HEAD")) private void vanilla$onHandleCustomPayload(final ServerboundCustomPayloadPacket packet, final CallbackInfo ci) { - // For some reason, "ServerboundCustomPayloadPacket" is released in the processPacket - // method of its class, only applicable to this packet, so just retain here. - // TODO investigate packet.getData().retain(); - final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager(); this.server.execute(() -> channelRegistry.handlePlayPayload((EngineConnection) this, packet.payload())); } diff --git a/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/common/ServerboundCustomPayloadPacketMixin_Vanilla.java b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/common/ServerboundCustomPayloadPacketMixin_Vanilla.java new file mode 100644 index 00000000000..da171154d4b --- /dev/null +++ b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/common/ServerboundCustomPayloadPacketMixin_Vanilla.java @@ -0,0 +1,66 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.vanilla.mixin.core.server.network.protocol.common; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +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; + +@Mixin(ServerboundCustomPayloadPacket.class) +public abstract class ServerboundCustomPayloadPacketMixin_Vanilla { + + // @formatter: off + @Shadow @Final private static int MAX_PAYLOAD_SIZE; + // @formatter: on + + @Inject(method = "readPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/common/ServerboundCustomPayloadPacket;readUnknownPayload(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)Lnet/minecraft/network/protocol/common/custom/DiscardedPayload;"), cancellable = true) + private static void impl$onReadUnknownPayload(ResourceLocation id, FriendlyByteBuf buf, final CallbackInfoReturnable cir) { + int readableBytes = buf.readableBytes(); + if (readableBytes >= 0 && readableBytes <= ServerboundCustomPayloadPacketMixin_Vanilla.MAX_PAYLOAD_SIZE) { + final var payload = new FriendlyByteBuf(buf.readBytes(readableBytes)); + + cir.setReturnValue(new CustomPacketPayload() { + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBytes(payload.copy()); + } + + @Override + public ResourceLocation id() { + return id; + } + }); + } else { + throw new IllegalArgumentException("Payload may not be larger than " + ServerboundCustomPayloadPacketMixin_Vanilla.MAX_PAYLOAD_SIZE + " bytes"); + } + } +} diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin.java b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin_Vanilla.java similarity index 93% rename from src/mixins/java/org/spongepowered/common/mixin/core/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin.java rename to vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin_Vanilla.java index cea96136ab4..643c875c0b6 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin.java +++ b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/server/network/protocol/login/ServerboundCustomQueryAnswerPacketMixin_Vanilla.java @@ -22,7 +22,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.common.mixin.core.network.protocol.login; +package org.spongepowered.vanilla.mixin.core.server.network.protocol.login; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.login.ServerboundCustomQueryAnswerPacket; @@ -33,7 +33,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ServerboundCustomQueryAnswerPacket.class) -public abstract class ServerboundCustomQueryAnswerPacketMixin { +public abstract class ServerboundCustomQueryAnswerPacketMixin_Vanilla { @Inject(method = "readUnknownPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/FriendlyByteBuf;skipBytes(I)Lnet/minecraft/network/FriendlyByteBuf;"), cancellable = true) private static void impl$onReadUnknownPayload(final FriendlyByteBuf $$0, final CallbackInfoReturnable cir) { @@ -41,5 +41,4 @@ public abstract class ServerboundCustomQueryAnswerPacketMixin { cir.setReturnValue(payload == null ? null : buf -> buf.writeBytes(payload.copy())); } - } diff --git a/vanilla/src/mixins/resources/mixins.spongevanilla.core.json b/vanilla/src/mixins/resources/mixins.spongevanilla.core.json index 44c115604a6..7dcc2a71bf0 100644 --- a/vanilla/src/mixins/resources/mixins.spongevanilla.core.json +++ b/vanilla/src/mixins/resources/mixins.spongevanilla.core.json @@ -23,6 +23,8 @@ "server.BootstrapMixin_Vanilla", "server.MinecraftServerMixin_Vanilla", "server.level.ServerPlayerMixin_Vanilla", + "server.network.protocol.common.ServerboundCustomPayloadPacketMixin_Vanilla", + "server.network.protocol.login.ServerboundCustomQueryAnswerPacketMixin_Vanilla", "server.network.ServerCommonPacketListenerImplMixin_Vanilla", "server.network.ServerGamePacketListenerImplMixin_Vanilla", "server.network.ServerLoginPacketListenerImplMixin_Vanilla",