Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.14.2] [Solved] How do I send packets to the client?


Recommended Posts

Since the IMessage interface was taken out (at least to my knowledge, the simpleimpl package is no longer in net.minecraftforge.fml.network.simple), what is the new way to send packets from the server to the client? I know that it has something to do with a MSG class from the docs, but it doesn't look like any of the MSG classes relate to Minecraft Forge (I also may not have eyes)

 

(I have capabilities and I want to tell the client the values, they're set up correctly)

Edited by soravoid
Solved
Link to post
Share on other sites
  • Thanks 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to post
Share on other sites

@Cadiboo

 

Looked through the section that you send me, I should probably say that I do have a PacketHandler with the SimpleChannel stuff.

Going through the ProjectE packets, they all still implement IMessage, so I'm still not sure what to do. I digged through the Forge files a bit more and see a pretty promising "IndexedMessageCodec", so I'll post back here once I try using that.

Link to post
Share on other sites

UPDATE: So, the class I mentioned above wasn't the correct thing to try and extend, but I've found an interface called IPacket. That seems promising but I have no idea what I should put in the triangular brackets (T in IPacket<T>). Does anyone have any ideas?

Link to post
Share on other sites

IPacket is a vanilla class, do not use it.

SimpleChannel no longer requires your packets to implement a certain interface. Instead you need to supply the following things:

  • An encoder (BiConsumer<MSG, PacketBuffer>). This will be called with your packet instance and a PacketBuffer to write the packet data to the byte stream. This used to be toBytes in IMessage.
  • A decoder (Function<PacketBuffer, MSG>). This will be called with the byte stream data and must decode it into an instance of your packet. This used to be done via a reflective constructor call and fromBytes.
  • A handler (BiConsumer<MSG, Supplier<NetworkEvent.Context>>). This will be called to handle your message. This used to be IMessageHandler.

Using method references for these makes your packet code very clean:

class MyPacket {
    private final int data;
    MyPacket(PacketBuffer buf) {
        this.data = buf.readInt();
    }

    MyPacket(int data) {
        this.data = data;
    }

    void encode(PacketBuffer buf) {
        buf.writeInt(data);
    }

    void handle(Supplier<NetworkEvent.Context> context) {
    }
}

channel.registerMessage(0, MyPacket.class, MyPacket::encode, MyPacket::new, MyPacket::handle);

 

  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.