Sending packet on player join

I am having an issue trying to send packets to players when they join the server. The client can send packets to the server and they are received fine, however when i try to send a packet to a client it is not always received.
If a break point is placed on the line to send the packet the client receives the packet.

This is the test code that I am using :

On the client side I am using liteloader using PluginChannelListener

I am running SpongeVanilla (Latest build as of 22/01/16 9:20)

@mumfrey tagged you in case it’s a liteloader issue (also i’ll do you the favor of calling myself a noob)

Thanks ChrisMack

What’s happening when it’s not received?

Nothing, I think it gets to the send packet but the client doesn’t fire the onCustomPayload

Does the client correctly register the channel? What is the code on the client?

Edit: another thing, you’re registering your channel really late on and in fact in the wrong ‘type’ of state. Channel reg is a one-time operation so it should go in an initialization phase and not a running phase. See the difference here:

1 Like

okay i changed the channel reg to init phase but still have the issue

This is the client code

public List<String> getChannels()
	return Arrays.asList(new String[] {packetChannels});

public void onCustomPayload(String channel, PacketBuffer data)
		int readableData = data.readableBytes();
		if(readableData > 0)
			byte[] payload = new byte[readableData];
			System.out.println(new String(payload, Charsets.UTF_8));
			// No data to be read...
	catch (Exception e) {};

I’ve also set packets to be sent onChat event, they work fine, just when the player joins the client doesn’t seem to notice the packet (unless the packet is delayed EDIT: Thread.sleep(100); before the packet is sent seems to give the client enough time to ‘detect’ the packet)

Can you try using SpongeForge? I would like to know if it’s something specific to SpongeVanilla. I tested on SpongeForge and it works

currently cannot clone spongeforge

Missing unknown 5d31470065a3a01feb45f927639f7147734122f0

i’m going to go to sleep so ill try it on forge tomorrow :slightly_smiling:

Thanks for the help @simon816

Oh it looks like this was bumped to a non-existent commit

Was supposed to be

It’s not guaranteed that liteloader will have registered the plugin channel client-side before the player connection event is triggered server-side.

You could use liteloader’s joingame callback to send some ‘mod has joined’ packet on a custom channel, listen for it server-side, and then send your data back to the client.

Or, if you can only change things server-side, you could schedule a task from the connect event to send the info after some delay.

1 Like

I had the same idea using the join game callback as a work around :slight_smile: Just thought i’d see if there was a actual fix first though

i tried to test with sponge forge but got this error :

There’s a problem with the dependencies in your workspace.
Try cleaning the eclipse files and regenerating, use the command gradle cleanEclipse eclipse --refresh-dependencies

okay testong sponge still don’t receive the packet on client but sponge forge puts the message in the console
[00:24:23] [Server thread/DEBUG] [Sponge]: Dropping data sent to EntityPlayerMP[‘ChrisMack’/262, l=‘world’, x=-120.50, y=78.00, z=248.50] on channel “TEST_CHANNEL”

OK that confirms that the client has not registered the channel at that current time.

What you could do is add an event listener to ChannelRegistrationEvent.Register, this event will fire when a channel is registered, you could send your message at this time instead.

Is this correct? becuase the channelRegistrationEvent isn’t being called

@Listener public void onChannelReg(ChannelRegistrationEvent.Register event) { Optional<Player> playerOptional = event.getCause().<Player>first(Player.class); sendPacket(playerOptional.get(), new String[] {"reg event event"}); }

Oh, maybe the event isn’t implemented yet. Temporarily for now just create a scheduled task to execute a bit later on, I’ll see if I can get the Register/Unregister events to fire soon.

Tippy Top thank you very much :smile: :

I think I will probably do what @dags said and respond to a packet sent from the client I only just found the PlayerConnection Interface XD

EDIT : This doesn’t seem to work the packet still gets dropped even if sent after packet has been received :confused:

Yeah sorry, just realised that liteloader calls onJoinGame for litemods before it sends plugin channels to the server. Hadn’t had issues doing this on bukkit, but maybe it doesn’t check to see if a client is registered to a channel before sending the packet, or latency, or something :S

I’ve now implemented the two ChannelRegistrationEvents

1 Like