Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libraries/networking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Channels identifiers should be constructed through the `ChannelIdentifieres` cla
and snake case for the identifier.

```java
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie");
public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.register("example", "cookie");
```

You are expected to register your channels through the `ChannelRegistry`.
In 1.13-pre2 and below, the game did not validate channel names, so Minecraft itself used names like `MC|Brand`. If you require a channel name with this convention, you can use the `legacy` factory methods instead.

```java
ChannelRegistry.register(COOKIE_CHANNEL);
ChannelRegistry.register(ChannelIdentifiers.legacy("EXAMPLE", "Cookie")); // -> "EXAMPLE|Cookie"
```

You are expected to register your packet listeners in your mod initializer through `ClientPlayNetworking` and `ServerPlayNetworking`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static NamespacedIdentifier fromIdentifier(Identifier id) {
*/
public static NamespacedIdentifier fromIdentifierOrThrow(Identifier id) {
try {
return ChannelIdentifiers.from(id.getNamespace(), id.getPath());
return ChannelIdentifiers.validate(id);
} catch (ChannelIdentifierException e) {
throw ChannelIdentifierParseException.invalid(id.toString(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.minecraft.resource.Identifier;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -24,10 +23,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload client() {
Expand All @@ -46,7 +42,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -56,7 +52,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static NamespacedIdentifier fromIdentifier(Identifier id) {
*/
public static NamespacedIdentifier fromIdentifierOrThrow(Identifier id) {
try {
return ChannelIdentifiers.from(id.getNamespace(), id.getPath());
return ChannelIdentifiers.validate(id);
} catch (ChannelIdentifierException e) {
throw ChannelIdentifierParseException.invalid(id.toString(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -24,10 +23,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload client() {
Expand All @@ -46,7 +42,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -56,7 +52,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -23,10 +22,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload server() {
Expand All @@ -41,7 +37,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -51,7 +47,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -23,10 +22,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload server() {
Expand All @@ -41,7 +37,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -51,7 +47,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -23,10 +22,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload client() {
Expand All @@ -41,7 +37,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -51,7 +47,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import java.util.Set;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.ChannelIdentifiers;
import net.ornithemc.osl.networking.api.ChannelRegistry;
import net.ornithemc.osl.networking.api.PacketBuffer;
import net.ornithemc.osl.networking.api.PacketPayload;
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class HandshakePayload implements PacketPayload {

public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL);
public static final NamespacedIdentifier CHANNEL = Constants.OSL_HANDSHAKE_CHANNEL;

public byte protocol;
public Set<NamespacedIdentifier> channels;
Expand All @@ -24,10 +23,7 @@ public HandshakePayload() {

public HandshakePayload(Set<NamespacedIdentifier> channels) {
this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL;
// we allow registering listeners on channels that do not conform to OSL spec
// but payloads sent over these channels aren't sent via OSL so we can ignore
// them for the OSL handshake.
this.channels = ChannelIdentifiers.dropInvalid(channels);
this.channels = channels;
}

public static HandshakePayload client() {
Expand All @@ -46,7 +42,7 @@ public void read(PacketBuffer buffer) throws IOException {
int channelCount = buffer.readInt();

for (int i = 0; i < channelCount; i++) {
channels.add(buffer.readNamespacedIdentifier());
channels.add(StringChannelIdentifierParser.fromString(buffer.readString()));
}
}

Expand All @@ -56,7 +52,7 @@ public void write(PacketBuffer buffer) throws IOException {
buffer.writeInt(channels.size());

for (NamespacedIdentifier channel : channels) {
buffer.writeNamespacedIdentifier(channel);
buffer.writeString(StringChannelIdentifierParser.toString(channel));
}
}
}
Loading
Loading