Skip to content

Commit 541a66b

Browse files
committed
update networking readme
1 parent aac45a1 commit 541a66b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

libraries/networking/README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,25 @@ Custom payloads are sent over specific channels. Channels are namespaced identif
2323
Channels identifiers should be constructed through the `ChannelIdentifieres` class. The convention is to use your mod id as the namespace,
2424
and snake case for the identifier.
2525

26-
You are expected to register your packet listeners in your mod initializer.
27-
28-
2926
```java
30-
package com.example;
27+
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
28+
```
3129

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;
30+
You are expected to register your channels through the `ChannelRegistry`.
3631

37-
public class ExampleInitializer implements ModInitializer {
32+
```java
33+
ChannelRegistry.register(COOKIE_CHANNEL);
34+
```
3835

39-
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
36+
You are expected to register your packet listeners in your mod initializer through `ClientPlayNetworking` and `ServerPlayNetworking`.
4037

41-
@Override
42-
public void init() {
43-
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, (context, buffer) -> {
44-
// handle custom payload
45-
});
46-
}
47-
}
38+
```java
39+
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, (context, buffer) -> { });
4840
```
4941

50-
For ease of use data can be wrapped in custom payload objects. These must implement the `CustomPayload` interface
51-
and must have a public constructor without parameters. An example can be seen below.
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.
5245

5346
```java
5447
package com.example;
@@ -81,7 +74,8 @@ public class CookiePayload implements CustomPayload {
8174
}
8275
```
8376

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

8680
```java
8781
package com.example;
@@ -97,7 +91,15 @@ public class ExampleInitializer implements ModInitializer {
9791

9892
@Override
9993
public void init() {
94+
ChannelRegistry.register(COOKIE_CHANNEL, true, false);
95+
}
96+
97+
@Override
98+
public void initClient() {
10099
ClientPlayNetworking.registerListener(COOKIE_CHANNEL, CookiePayload::new, (context, payload) -> {
100+
// ensure this listener is running on the main thread
101+
context.ensureOnMainThread();
102+
101103
// handle custom payload
102104
});
103105
}

0 commit comments

Comments
 (0)