Skip to content

Commit 3f88957

Browse files
committed
update README
1 parent 4f7cb2f commit 3f88957

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

libraries/networking/README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,46 @@ 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.
25+
26+
You are expected to register your packet listeners in your mod initializer.
27+
28+
29+
```java
30+
package com.example;
31+
32+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
33+
import net.ornithemc.osl.entrypoints.api.ModInitializer;
34+
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
35+
import net.ornithemc.osl.networking.api.client.ClientPlayNetworking;
36+
37+
public class ExampleInitializer implements ModInitializer {
38+
39+
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
40+
41+
@Override
42+
public void init() {
43+
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, (context, buffer) -> {
44+
// handle custom payload
45+
});
46+
}
47+
}
48+
```
2449

2550
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.
51+
and must have a public constructor without parameters. An example can be seen below.
2752

2853
```java
2954
package com.example;
3055

3156
import java.io.IOException;
3257

33-
import net.minecraft.network.PacketByteBuf;
34-
3558
import net.ornithemc.osl.networking.api.CustomPayload;
59+
import net.ornithemc.osl.networking.api.PacketBuffer;
3660

3761
public class CookiePayload implements CustomPayload {
3862

@@ -46,12 +70,12 @@ public class CookiePayload implements CustomPayload {
4670
}
4771

4872
@Override
49-
public void read(PacketByteBuf buffer) throws IOException {
73+
public void read(PacketBuffer buffer) throws IOException {
5074
// deserialize data
5175
}
5276

5377
@Override
54-
public void write(PacketByteBuf buffer) throws IOException {
78+
public void write(PacketBuffer buffer) throws IOException {
5579
// serialize data
5680
}
5781
}
@@ -62,14 +86,18 @@ Listeners for these custom payload objects can be registered as follows:
6286
```java
6387
package com.example;
6488

89+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
6590
import net.ornithemc.osl.entrypoints.api.ModInitializer;
91+
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
6692
import net.ornithemc.osl.networking.api.client.ClientPlayNetworking;
6793

6894
public class ExampleInitializer implements ModInitializer {
6995

96+
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
97+
7098
@Override
7199
public void init() {
72-
ClientPlayNetworking.registerListener("Example|Cookie", CookiePayload::new, (minecraft, handler, payload) -> {
100+
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, CookiePayload::new, (context, payload) -> {
73101
// handle custom payload
74102
});
75103
}

0 commit comments

Comments
 (0)