Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specific error when encoding packet with no encoder #3734

Open
wants to merge 10 commits into
base: 1.20.5
Choose a base branch
from

Conversation

Moulberry
Copy link

@Moulberry Moulberry commented Apr 26, 2024

Adds a specific error when encoding a packet that doesn't have a registered encoder

To explain a bit more about the issue:
CustomPayload#getCodec returns a dummy decoder using unknownCodecFactory. This is fine when decoding, but not when encoding. When encoding, you'll end up with a confusing exception about casting to DiscardedPayload (or UnknownCustomPayload).

An ideal solution would not use #getCodec for encode, and would instead throw the more helpful exception directly when a codec is missing. Unfortunately I couldn't figure out a good way to do this since map is part of the anonymous class (perhaps someone with more knowledge of mixins could point me in the right direction).

Overwriting the entire #getCodec method is also an option, but I think is generally unfavoured.

For this PR, I just throw the error if the namespace isn't "minecraft", which might be problematic if mods add directly to the map instead of using Fabric's system.

@Patbox
Copy link

Patbox commented Apr 26, 2024

This does break existing mods such as polymer library (it injects into getCodec method) and carpet (adds to vanilla list). It also requires more specific mixins to work around it.

@Moulberry Moulberry marked this pull request as draft April 26, 2024 08:17
@Moulberry Moulberry marked this pull request as ready for review April 26, 2024 14:30
@modmuss50 modmuss50 added the enhancement New feature or request label May 1, 2024
@modmuss50
Copy link
Member

Hi, im just testing out this PR and im not sure its working as expected? Running /networktestcommand sendMissingCodec shows me the following error screen? Putting a breakpoint in the encode doesnt seem to trigger.

Screenshot 2024-05-01 at 09 27 43

@modmuss50
Copy link
Member

You might be able to add a unit test for this, see PayloadTypeRegistryTests for an example.

@Moulberry
Copy link
Author

Moulberry commented May 1, 2024

Hi, im just testing out this PR and im not sure its working as expected? Running /networktestcommand sendMissingCodec shows me the following error screen? Putting a breakpoint in the encode doesnt seem to trigger.

The current behaviour (which may be possible to change) is that attempting to encode a packet without a codec is still an error, so the client will be disconnected.
The difference is that the logs will show the new RuntimeException ("has no registered codec" etc.), instead of a ClassCastException to DiscardedPayload.

The thrown exception is caught somewhere higher up and the client is disconnected with the "Failed to encode packet" error message, with our RuntimeException being a child.

Below is what appears in the logs (at least on my machine) when running /networktestcommand sendMissingCodec:

[20:16:50] [Netty Server IO #1/ERROR] (Minecraft) Error sending packet clientbound/minecraft:custom_payload
io.netty.handler.codec.EncoderException: Failed to encode packet 'clientbound/minecraft:custom_payload' (fabric-networking-api-v1-testmod:no_codec)
        at net.minecraft.network.handler.PacketCodecDispatcher.handler$zcm000$fabric-networking-api-v1$encode(PacketCodecDispatcher.java:547) ~[minecraft-common-38e832ecca-1.20.5-net.fabricmc.yarn.1_20_5.1.20.5+build.1-v2.jar:?]
        at net.minecraft.network.handler.PacketCodecDispatcher.encode(PacketCodecDispatcher.java:55) ~[minecraft-common-38e832ecca-1.20.5-net.fabricmc.yarn.1_20_5.1.20.5+build.1-v2.jar:?]
        ~~SNIP~~
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.97.Final.jar:?]
        at java.base/java.lang.Thread.run(Thread.java:1583) [?:?]
Caused by: java.lang.RuntimeException: Custom payload 'fabric-networking-api-v1-testmod:no_codec' has no registered codec
        at net.minecraft.network.packet.UnknownCustomPayload$Anonymous$a4ca0709779d43999db59af12d303b0c.encode(UnknownCustomPayloadMixin.java:41) ~[?:?]
        at net.minecraft.network.packet.UnknownCustomPayload$Anonymous$a4ca0709779d43999db59af12d303b0c.encode(UnknownCustomPayloadMixin.java:33) ~[?:?]
        ~~SNIP~~
        at net.minecraft.network.handler.PacketCodecDispatcher.encode(PacketCodecDispatcher.java:53) ~[minecraft-common-38e832ecca-1.20.5-net.fabricmc.yarn.1_20_5.1.20.5+build.1-v2.jar:?]
        ... 31 more

I can't say for sure why the breakpoint isn't working, but I'd think it has something to do with the mixin+anonymous class?

You might be able to add a unit test for this, see PayloadTypeRegistryTests for an example.

I'll give it a shot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants