Jump to content

Recommended Posts

Posted

The documentation at https://docs.minecraftforge.net/en/1.19.x/networking/simpleimpl/ for sending packets states that the code to send a message to a single player looks like this:

    INSTANCE.send(PacketDistributor.PLAYER.with(serverPlayer), new MyMessage());

I have wrapped this in a function in a class so that I can keep INSTANCE private and simplify all code that sends messages to the client. My function is:

    public static <MSG> void sendToClient(ServerPlayer player, MSG msg)
    {
        INSTANCE.send(PacketDistributor.PLAYER.with(player), msg);
    }

 

This fails to compile with the error: incompatible types: ServerPlayer cannot be converted to Supplier<ServerPlayer>

 

I have looked at the documentation for the Supplier class and there are no signs of any constructor or assignment function such as you get with Optional and similar classes. I did notice that it said that Supplier was a functional interface and changed my code to:

    public static <MSG> void sendToClient(ServerPlayer player, MSG msg)
    {
        INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), msg);
    }

This compiles.

 

I suggest you change the documentation to read:

    INSTANCE.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new MyMessage());

 

  • 2 years later...
Posted (edited)

Im having the same issue. 1.20.1
and yes it does effect levelchunks as well because they both use "with". the ones with no arg are fine.
this is because it now takes a "supplier<t>" instead of whatever else


i can probably test a workaround and call the other "sendto" code for instance
however "serverPlayer.connection.getConnection()" is invalid i assume its "serverPlayer.connection.connection"??

 

its been 3 years how has no one had this issue before.

pulled from base mincraft packet distributor
public PacketTarget with(Supplier<T> input) { return new PacketTarget((Consumer)this.functor.apply(this, input), this); }
public PacketTarget noArg() { return new PacketTarget((Consumer)this.functor.apply(this, () -> { return null; }), this); }

Edited by AustinPinheiro
spell check. grammer, formatting
  • 2 weeks later...
Posted

The networking has been far simplified in 1.20.5+

The docs could be old, as Ash has said feel free to submit a PR updating the community docs.

However the only real up-to-date reference would be the code itself. So your best bet is to just read the code and give it want it wants.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.