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.
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: https://docs.spongepowered.org/en/plugin/lifecycle.html
okay i changed the channel reg to init phase but still have the issue
This is the client code
@Override
public List<String> getChannels()
{
return Arrays.asList(new String[] {packetChannels});
}
@Override
public void onCustomPayload(String channel, PacketBuffer data)
{
try
{
int readableData = data.readableBytes();
if(readableData > 0)
{
byte[] payload = new byte[readableData];
data.readBytes(payload);
System.out.println("===========CLIENT=========");
System.out.println(new String(payload, Charsets.UTF_8));
System.out.println("==========================");
}
else
{
// 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)
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.
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.
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.
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