[1.12.2] Sending Packets from Forge to Sponge

Hi guys,

I’am trying to send a packet to the server from my forge mod ( client side). And i want to receive this packet in my sponge plugin. So i used ChannelRegistrar etc… BUT! Nothing is happening, nothing is printing in the terminal.
Here my code :

FORGE :
Packet class : https://pastebin.com/175Da9tS
Class where i register the packet : https://pastebin.com/L22Zv0eG
Class where i send the packet: https://pastebin.com/gJ3Uu73V

SPONGE :
Main class where i create the ChannelBinding : https://pastebin.com/2Gy1bX7d
Class where i receive the packet : https://pastebin.com/C9DUHMpX

This is supposed to work and at least show the “works” message in the handler class when i receive my packet (last sponge class that i linked ).
I have no error in my terminal…
Please guys I’m struggling on this issue for few days…

I’ve already read these topics : Communication between spongeforge and forge mod client

I apologize in advance for my English.
Thanks.

Nobody to help me ?

Unfortunately I don’t know the solution, but I want to push this topic because I faced the same challenge this weekend. I would also appreciate any help, because I need to communicate between my plugin and my mod. For example to open a custom GUI on client side and to transfer the result to the server to store the result inside a database and to work with the data in my plugin.

1 Like

Yeah i wanna do the same thing. Please help us :’(

Well, Sponge networking API (or how this thing is named) is somewhy poorly documented, so yeah, it’s easy to stuck there.
I’m going to help you by giving examples of code I use in my plugin. They work normally.
First off: channel registering (done in init, but I think it doesn’t matter when you register your channel)

ChannelBinding.RawDataChannel yourChannel = Sponge.getGame()
                .getChannelRegistrar()
                .createRawChannel(yourPlugin, "yourChannelName");

Then: my listener

public class ExampleListener implements RawDataListener {
    @Override
    public void handlePayload(ChannelBuf buf, RemoteConnection connection, Platform.Type side) {
        if (!(connection instanceof PlayerConnection)) {
            return;
        }

        Player player = ((PlayerConnection) connection).getPlayer();

        // Do your stuff here.
    }
}

And now, simply invoke yourChannel.addListener(new ExampleListener())
That’s it.

1 Like

Hi,
Thanks for your answer.
But i already tried with RawDataChannel, i will try again for being sure.

It’ll be strange if it won’t work. I copied code from my plugin where everything is working fine.

In your plugin, it was a forge packet?

Yeah. I can give you an example of how I send packet from client side to clarify, if you want.

Yeah why not, it will be very cool !

Here you go.
Registering:

NetworkRegistry.INSTANCE.newEventDrivenChannel(channelName).register(yourProxyHere);

Sending:

Minecraft.getMinecraft().player.connection.sendPacket(new CPacketCustomPayload(channelName, new PacketBuffer(buffer)));

Can you show me your buffer please?

Sure

ByteBuf buffer = Unpooled.buffer();
buffer.writeBoolean(true);

Ok thanks i will test right now ! I appreciate your help !

Sorry but what is yourProxyHere

It’s simply an event listener object. You can subscribe to an appropriate event in it to listen for incoming packets.

Oh okey, but if i dont need an event?

Just i need to create newEventDrivenChannel ? or sending my packet is enough?

I think you can ignore registering if you don’t need to listen to anything.

Hi thanks,
now i receive my sysout in my terminal (“received”) but i have an another problem
Stacktrace : https://pastebin.com/P6TD6kEg
My listener in my plugin : https://pastebin.com/BjZ9zR5T
how i send my packet : https://pastebin.com/fRpC7Y6y

How did you got the registration done? I always receive the exception that the channel is already registered on the server/plugin side.

Registration server side: https://pastebin.com/na8FaJnA
Server side event handler/listener: https://pastebin.com/ZLUT6ZCR

Client side registration: https://pastebin.com/WV4BDQg7
Client side packet send: https://pastebin.com/FTC4zrBt