This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| wiki:api:networking [2022/09/12 13:58] – created shedaniel | wiki:api:networking [2022/09/16 15:56] (current) – Deleted shedaniel | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Networking ====== | ||
| - | |||
| - | There are currently a few ways to setup networking with Architectury API: | ||
| - | |||
| - | * [[# | ||
| - | * [[# | ||
| - | |||
| - | ===== Differences between Server and Logical Server ===== | ||
| - | |||
| - | When you join a world in singleplayer, | ||
| - | |||
| - | ====== Receiver Callback ====== | ||
| - | |||
| - | Each type of message is differentiated by a '' | ||
| - | |||
| - | <code java> | ||
| - | public static final ResourceLocation EXAMPLE_PACKET_ID = new ResourceLocation(" | ||
| - | </ | ||
| - | |||
| - | ===== Registering the Handler ===== | ||
| - | |||
| - | Now, depending on the direction of the transmission, | ||
| - | |||
| - | === Client -> Logical Server === | ||
| - | |||
| - | When we send a packet from the client to the server, we will register the handler on the common side. | ||
| - | |||
| - | === Logical Server -> Client === | ||
| - | |||
| - | When we send a packet from the server to the client, we will register the handler on the client side. | ||
| - | |||
| - | <code java> | ||
| - | // We are using S2C here for an example, use C2S instead if this is from the client to the server NetworkManager.registerReceiver(NetworkManager.Side.S2C, | ||
| - | Player player = context.getPlayer(); | ||
| - | // Logic | ||
| - | }); | ||
| - | </ | ||
| - | |||
| - | ===== Sending the Packet ===== | ||
| - | |||
| - | === Client -> Logical Server === | ||
| - | |||
| - | <code java> | ||
| - | FriendlyPacketBuf buf = new FriendlyPacketBuf(Unpooled.buffer()); | ||
| - | </ | ||
| - | |||
| - | === Logical Server -> Client === | ||
| - | |||
| - | <code java> | ||
| - | FriendlyPacketBuf buf = new FriendlyPacketBuf(Unpooled.buffer()); | ||
| - | </ | ||
| - | |||
| - | ===== Handling the Packet ===== | ||
| - | |||
| - | The packet buffer supplied is a stream of bytes sent over the network, you must read it in order of how you sent it. | ||
| - | |||
| - | Let say, we want to send a block position and an item stack to the server. In between creating the packet buf and sending it, we can write our data to it. | ||
| - | |||
| - | <code java> | ||
| - | FriendlyPacketBuf buf = new FriendlyPacketBuf(Unpooled.buffer()); | ||
| - | buf.writeBlockPos(pos); | ||
| - | buf.writeItem(stack); | ||
| - | NetworkManager.sendToServer(EXAMPLE_PACKET_ID, | ||
| - | </ | ||
| - | |||
| - | And then on the server, we can read it. | ||
| - | |||
| - | <code java> | ||
| - | NetworkManager.registerReceiver(NetworkManager.Side.C2S, | ||
| - | BlockPos pos = buf.readBlockPos(); | ||
| - | ItemStack stack = buf.readItem(); | ||
| - | }); | ||
| - | </ | ||
| - | |||
| - | ====== Message Channel ====== | ||
| - | |||
| - | First, we need to register a message channel for our packets to go through. | ||
| - | |||
| - | <code java> | ||
| - | public static final NetworkChannel CHANNEL = NetworkChannel.create(new ResourceLocation(" | ||
| - | </ | ||
| - | |||
| - | Then, we will create a message class like this: | ||
| - | |||
| - | <code java> | ||
| - | public class ExampleMessage { | ||
| - | public ExampleMessage(FriendlyPacketBuf buf) { | ||
| - | // Decode data into a message | ||
| - | } | ||
| - | |||
| - | public ExampleMessage() { | ||
| - | // Message creation | ||
| - | } | ||
| - | |||
| - | public void encode(FriendlyPacketBuf buf) { | ||
| - | // Encode data into the buf | ||
| - | } | ||
| - | |||
| - | public void apply(Supplier< | ||
| - | // On receive | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | After this, we will register the message into the channel. | ||
| - | |||
| - | <code java> | ||
| - | CHANNEL.register(ExampleMessage.class, | ||
| - | </ | ||
| - | |||
| - | ===== Attaching more data with the message ===== | ||
| - | |||
| - | For any data we want to add to the message, we will want to add fields, then add logic to encode, and decode it over the network. | ||
| - | |||
| - | Let say, we want to send a block position and an item stack to the server. | ||
| - | |||
| - | <code java> | ||
| - | public class ExampleMessage { | ||
| - | public final BlockPos pos; | ||
| - | public final ItemStack stack; | ||
| - | |||
| - | public ExampleMessage(FriendlyPacketBuf buf) { | ||
| - | this(buf.readBlockPos(), | ||
| - | } | ||
| - | |||
| - | public ExampleMessage(BlockPos pos, ItemStack stack) { | ||
| - | this.pos = pos; | ||
| - | this.stack = stack; | ||
| - | } | ||
| - | |||
| - | public void encode(FriendlyPacketBuf buf) { | ||
| - | buf.writeBlockPos(pos); | ||
| - | buf.writeItem(stack); | ||
| - | } | ||
| - | |||
| - | public void apply(Supplier< | ||
| - | // Do logic here | ||
| - | } | ||
| - | } | ||
| - | </ | ||