[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 : public class PacketTest implements IMessage { private String text; - Pastebin.com
Class where i register the packet : public class AlbCommon { public static SimpleNetworkWrapper network; - Pastebin.com
Class where i send the packet: @Override protected void actionPerformed(GuiButton button) throws IOExcept - Pastebin.com

SPONGE :
Main class where i create the ChannelBinding : public class Main { public static ChannelBinding.IndexedMessageChannel ch - Pastebin.com
Class where i receive the packet : public class MessageGui implements Message { private String text; - Pastebin.com

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 : [09:40:20] [Netty Server IO #2/INFO] [STDOUT]: [fr.frenchdevz.albhybrid.TestList - Pastebin.com
My listener in my plugin : public class TestListener implements RawDataListener { @Override publi - Pastebin.com
how i send my packet : @Override protected void actionPerformed(GuiButton button) throws IOExcepti - Pastebin.com

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: @Listener public void onServerStart(GameInitializationEvent event) { - Pastebin.com
Server side event handler/listener: public class ModListener implements RawDataListener { @Override publ - Pastebin.com

Client side registration: @EventHandler public static void preInit(FMLPreInitializationEvent event) { - Pastebin.com
Client side packet send: @SideOnly(Side.CLIENT) @SubscribeEvent public void onKeyInput(KeyInputEvent - Pastebin.com