Skip to content

Commit

Permalink
Fix play plugin messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 25, 2023
1 parent 1b64a15 commit 834e186
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/mixins/resources/mixins.sponge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<CustomPacketPayload> 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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,13 +33,12 @@
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<CustomQueryAnswerPayload> cir) {
final var payload = $$0.readNullable(buf -> new FriendlyByteBuf(buf.readBytes(buf.readableBytes())));

cir.setReturnValue(payload == null ? null : buf -> buf.writeBytes(payload.copy()));
}

}
2 changes: 2 additions & 0 deletions vanilla/src/mixins/resources/mixins.spongevanilla.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 834e186

Please sign in to comment.