Fabric API

Fabric API

112M Downloads

The ServerPlayNetworking. canSend() of the Fabric API may not work in some cases

Crystal0404 opened this issue · 11 comments

commented

Version

Minecraft 1.20.4
Fabric Loader 0.15.6
Fabric API 0.95.0+1.20.4

Methods for reproducing errors

This is the code, For the convenience of comparison, I used Fallen_Breath's template

/*
 * fabric_api_version=0.91.0+1.20.1
 * fabric_api_version=0.95.0+1.20.4
 */

package me.fallenbreath.template_mod;

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.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;

public class network implements ServerPlayConnectionEvents.Join{
    public static final Identifier HELLO = new Identifier(TemplateMod.MOD_ID, "hello");

    public static void regS2C(){
        ClientPlayNetworking.registerGlobalReceiver(HELLO, (
                MinecraftClient,
                ClientPlayNetworking,
                PacketByteBuf,
                PacketSender
                ) -> {
            TemplateMod.LOGGER.warn("hello world");
        });
    }

    @Override
    public void onPlayReady(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server) {
        TemplateMod.LOGGER.warn(ServerPlayNetworking.canSend(handler.player, HELLO));
        ServerPlayNetworking.send(handler.player, HELLO, PacketByteBufs.create());
    }
}

This is a partial log output from the 1.20.4 server

[20:12:31] [Server thread/INFO] (Minecraft) Player479[/127.0.0.1:59519] logged in with entity id 190 at (127.5, 63.0, -297.5)
[20:12:31] [Server thread/WARN] (TemplateMod) false
[20:12:31] [Server thread/INFO] (Minecraft) Player479 joined the game
[20:12:39] [Server thread/INFO] (Minecraft) Player479 lost connection: Disconnected
[20:12:39] [Server thread/INFO] (Minecraft) Player479 left the game

This is a partial log output from the 1.20.4 client

[20:12:24] [IO-Worker-1/INFO] (Minecraft) Could not authorize you against Realms server: Invalid session id
[20:12:31] [Render thread/INFO] (Minecraft) Connecting to localhost, 25565
[20:12:31] [Netty Client IO #1/WARN] (TemplateMod) hello world

The client successfully received the packet sent by the server, but returned false, But version 1.20.1 has no errors

This is a partial log output from the 1.20.1 server

[20:26:24] [Server thread/INFO] (Minecraft) Player481[/127.0.0.1:59717] logged in with entity id 192 at (120.5, 65.0, -303.5)
true
[20:26:24] [Server thread/INFO] (Minecraft) Player481 joined the game
[20:26:28] [Server thread/INFO] (Minecraft) Player481 lost connection: Disconnected
[20:26:28] [Server thread/INFO] (Minecraft) Player481 left the game

This is a partial log output from the 1.20.1 client

[20:26:19] [Render thread/INFO] (Minecraft) Created: 128x128x0 minecraft:textures/atlas/mob_effects.png-atlas
[20:26:23] [Render thread/INFO] (Minecraft) Connecting to localhost, 25565
[20:26:24] [Netty Client IO #1/WARN] (TemplateMod) hello world
[20:26:25] [Render thread/INFO] (Minecraft) Loaded 2 advancements

The Boolean values here are output using System. out. println(), as there is a slight error in the log output of the 1.20.1 example code

The attachment size exceeds the limit, so I created a project to store it(The attachment is a template for running the code above me)

commented

Could you turn on debug logging and send the result?

commented

你能打开调试日志记录并发送结果吗?
debug.log
Is this the file? If not, please tell me how to do it. I am not a professional programmer: P

commented

I also noticed that in 1.20.1, if forwarded through Velocity, a successful packet transmission will also appear, but false will be returned.

commented

你能打开调试日志记录并发送结果吗?
debug.log

This is the client's log

commented

@Crystal0404 I couldn't find "(TemplateMod) false" in the server log. Hmm.

commented

libs.zip
This is the jar I built, and its function is to print the return value of ServerPlayNetworking.canSend() on the server. And printing hello world on the client after receiving the data packet may be helpful for you ,When the player enters the server, it will trigger

commented

@Crystal0404 I couldn't find "(TemplateMod) false" in the server log. Hmm.
debug.log

When outputting to the log at 1.20.1, it crashes, so I used System.out.println(); to output it. However, 1.20.4 does not crash, so I printed it into the log(line 1138)

commented

It appears that ClientConfigurationNetworkAddon didn't receive any packets including the c:register packet, this is wrong.

commented

It appears that ClientConfigurationNetworkAddon didn't receive any packets including the c:register packet, this is wrong.
Sdebug.log
Cdebug.log

There is another possible related issue. ServerPlayNetworking. canSend() at 1.20.1 was originally working properly, but when connected to the server through Velocity, it encountered the same error as ServerPlayNetworking. canSend() at 1.20.4

commented

Data needed for that method is available later than in 1.20.1.
On 1.20.2, that method is not usable before S2CPlayChannelEvents.REGISTER event fire (so eg you can not use that method in ServerPlayConnectionEvents.INIT nor in ServerPlayConnectionEvents.JOIN).