Restore reader index after processing custom payload

This commit is contained in:
IzzelAliz 2021-04-27 16:24:32 +08:00
parent 9a562e1e34
commit d0108af1e6

View File

@ -1684,7 +1684,7 @@ public abstract class ServerPlayNetHandlerMixin implements ServerPlayNetHandlerB
private static final ResourceLocation CUSTOM_REGISTER = new ResourceLocation("register");
private static final ResourceLocation CUSTOM_UNREGISTER = new ResourceLocation("unregister");
@Inject(method = "processCustomPayload", at = @At(value = "INVOKE", remap = false, target = "Lnet/minecraftforge/fml/network/NetworkHooks;onCustomPayload(Lnet/minecraftforge/fml/network/ICustomPacket;Lnet/minecraft/network/NetworkManager;)Z"))
@Inject(method = "processCustomPayload", cancellable = true, at = @At(value = "INVOKE", remap = false, target = "Lnet/minecraftforge/fml/network/NetworkHooks;onCustomPayload(Lnet/minecraftforge/fml/network/ICustomPacket;Lnet/minecraft/network/NetworkManager;)Z"))
private void arclight$customPayload(CCustomPayloadPacket packet, CallbackInfo ci) {
if (packet.channel.equals(CUSTOM_REGISTER)) {
try {
@ -1697,6 +1697,7 @@ public abstract class ServerPlayNetHandlerMixin implements ServerPlayNetHandlerB
} catch (Exception ex) {
LOGGER.error("Couldn't register custom payload", ex);
this.disconnect("Invalid payload REGISTER!");
ci.cancel();
}
} else if (packet.channel.equals(CUSTOM_UNREGISTER)) {
try {
@ -1709,15 +1710,19 @@ public abstract class ServerPlayNetHandlerMixin implements ServerPlayNetHandlerB
} catch (Exception ex) {
LOGGER.error("Couldn't unregister custom payload", ex);
this.disconnect("Invalid payload UNREGISTER!");
ci.cancel();
}
} else {
try {
int readerIndex = packet.data.readerIndex();
final byte[] data = new byte[packet.data.readableBytes()];
packet.data.readBytes(data);
this.server.getMessenger().dispatchIncomingMessage(((ServerPlayerEntityBridge) this.player).bridge$getBukkitEntity(), packet.channel.toString(), data);
packet.data.readerIndex(readerIndex);
} catch (Exception ex) {
LOGGER.error("Couldn't dispatch custom payload", ex);
this.disconnect("Invalid custom payload!");
ci.cancel();
}
}
}