Skip to content

Commit 8506609

Browse files
committed
update Networking API README
1 parent 4b04dcc commit 8506609

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

libraries/networking/README.md

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,39 @@ are also fired for connections to integrated servers.
1717
## Networking
1818

1919
Sending and receiving data is done through the `ClientPlayNetworking` and `ServerPlayNetworking` classes.
20-
Mods can register network listeners through the `registerListener` and `registerListenerAsync` methods,
21-
allowing them to receive data through specific channels. Sending data is done through the `send` methods.
20+
Mods can register packet listeners through the `registerListener` methods, and send data through the `send` methods.
2221

23-
Each custom payload is tied to a channel. Only connections that have listeners on that channel will receive the payload, and on the receiving end, the payload will only be handled by the listener on that channel. In Minecraft versions 1.13-pre2 and below, a channel can be any `String` of length 20 or less. In Minecraft versions 1.13-pre4 and above, a channel can be any valid `Identifier`. For `String` channels, the convention is `<(abbreviated) mod id>|<payload id>` (e.g. `Example|Cookie`), while for `Identifier` channels, the convention is `<mod id>:<payload id>` (e.g. `example:cookie`).
22+
Custom payloads are sent over specific channels. Channels are namespaced identifiers, used to identify the payload being sent or received.
23+
Channels identifiers should be constructed through the `ChannelIdentifieres` class. The convention is to use your mod id as the namespace,
24+
and snake case for the identifier.
2425

25-
For ease of use data can be wrapped in custom payload objects. These must implement the `CustomPayload` interface
26-
and must have a public construcor without parameters. An example can be seen below.
26+
```java
27+
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
28+
```
29+
30+
You are expected to register your channels through the `ChannelRegistry`.
31+
32+
```java
33+
ChannelRegistry.register(COOKIE_CHANNEL);
34+
```
35+
36+
You are expected to register your packet listeners in your mod initializer through `ClientPlayNetworking` and `ServerPlayNetworking`.
37+
38+
```java
39+
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, (context, buffer) -> { });
40+
```
41+
42+
For ease of use data can be wrapped in custom payload objects.
43+
These must implement the `CustomPayload` interface and must have a public constructor without parameters.
44+
An example can be seen below.
2745

2846
```java
2947
package com.example;
3048

3149
import java.io.IOException;
3250

33-
import net.minecraft.network.PacketByteBuf;
34-
3551
import net.ornithemc.osl.networking.api.CustomPayload;
52+
import net.ornithemc.osl.networking.api.PacketBuffer;
3653

3754
public class CookiePayload implements CustomPayload {
3855

@@ -46,30 +63,43 @@ public class CookiePayload implements CustomPayload {
4663
}
4764

4865
@Override
49-
public void read(PacketByteBuf buffer) throws IOException {
66+
public void read(PacketBuffer buffer) throws IOException {
5067
// deserialize data
5168
}
5269

5370
@Override
54-
public void write(PacketByteBuf buffer) throws IOException {
71+
public void write(PacketBuffer buffer) throws IOException {
5572
// serialize data
5673
}
5774
}
5875
```
5976

60-
Listeners for these custom payload objects can be registered as follows:
77+
A basic networking setup might look as follows.
78+
6179

6280
```java
6381
package com.example;
6482

83+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
6584
import net.ornithemc.osl.entrypoints.api.ModInitializer;
85+
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
6686
import net.ornithemc.osl.networking.api.client.ClientPlayNetworking;
6787

6888
public class ExampleInitializer implements ModInitializer {
6989

90+
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
91+
7092
@Override
7193
public void init() {
72-
ClientPlayNetworking.registerListener("Example|Cookie", CookiePayload::new, (minecraft, handler, payload) -> {
94+
ChannelRegistry.register(COOKIE_CHANNEL, true, false);
95+
}
96+
97+
@Override
98+
public void initClient() {
99+
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, CookiePayload::new, (context, payload) -> {
100+
// ensure this listener is running on the main thread
101+
context.ensureOnMainThread();
102+
73103
// handle custom payload
74104
});
75105
}

0 commit comments

Comments
 (0)