Network Packets
This script shows how to use network packets:
// Listen to a player event, in this case item right-click
events.listen(// This goes in either server or client script, depending on which side you want to send the data packet to
onEvent('item.right_click', functionevent (event)=> {
// Check if item was right-clicked on client or server side
if (event.player.isServer())server) {
// Send data {test: 123} to channel "test_channel_1". Channel ID can be any string, but it's recommended to keep it to snake_case [a-z_0-9].
// Receiving side will either be client (because its fired from server).
event.player.sendData('test_channel_1', { test: 123 })
} else {
// It's not required to use a different channel ID, but it's recommended.
// Receiving side will either be server (because its fired from client).
event.player.sendData('test_channel_2', { test: 456 })
}
})
// Listen to event that gets fired when network packet is received from server.
events.listen(// This goes in a client script
onEvent('player.data_from_server.test_channel_1', functionevent (event)=> {
log.info(event.data.get('test').asInt())test) // Prints 123
})
// Listen to event that gets fired when network packet is received from client.
events.listen(// This goes in a server script
onEvent('player.data_from_client.test_channel_2', functionevent (event)=> {
log.info(event.data.get('test').asInt())test) // Prints 456
})