HomeCommandsExamplesGetting Started With ScriptsGlobal Keywords1.21 Migration Guide
BracketDumpersBracketHandlersBracketValidatorsResourceLocationBracketHandler

Network

Importing the class

If you need to reference this type directly, like when casting an Array, or as a parameter, you will need to import it. Simply add the import at the top of the file.

script.zs
import crafttweaker.api.network.Network;

Description

Allows scripts to send data from the server to the client.
This system provides a way for server side scripts to send data to client side only scripts, and trigger events from the server.
An example of this would be (neoforge only): <code>
// Listens to the ItemTossEvent events.register<ItemTossEvent>(event => { if event.player is ServerPlayer { // If the player is a ServerPlayer, send data to the client using the "toss" id network.sendTo(event.player as ServerPlayer, "toss", {custom: "data"}); } }); // Using the 'onlyif side client' preprocessor, we can ensure script lines are only ran on the Client distribution. #onlyif side client // Listen for data sent to the "toss" id network.onData("toss", (data, context) => { // Prints the data that was received println(data.getAsString()); }); #endif </code>

Members

onData(id as string, receiver as CTNetworkReceiver)
Receives data on the client.
script.zs
// CTNetwork.onData(id as string, receiver as vanilla/api/network/NetworkReceiver);
network.onData(my_notification, (data, context) => println(data.getAsString()););

Parameters:

id Type: string - A string that connects data receivers with data senders.
receiver Type: vanilla/api/network/NetworkReceiver - A receiver that is run when the data is received from the server.
sendTo(player as ServerPlayer, id as string, data as IData)
Sends the given data to the specified player on the specified id.
script.zs
// CTNetwork.sendTo(player as ServerPlayer, id as string, data as IData);
network.sendTo(player, my_notification, { custom: "data" });

Parameters:

player Type: ServerPlayer - The player to send the data to.
id Type: string - A string that connects data receivers with data senders.
data Type: IData - The data to send to the client