Skip to content

Commit 0101515

Browse files
committed
Fix MinecraftChannelIdentifier parsing to align with vanilla
1 parent 74d0521 commit 0101515

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import com.google.common.base.Strings;
1313
import java.util.Objects;
14-
import java.util.regex.Pattern;
1514
import net.kyori.adventure.key.Key;
1615
import org.checkerframework.checker.nullness.qual.Nullable;
1716

@@ -21,8 +20,6 @@
2120
*/
2221
public final class MinecraftChannelIdentifier implements ChannelIdentifier {
2322

24-
private static final Pattern VALID_IDENTIFIER_REGEX = Pattern.compile("[a-z0-9/\\-_]*");
25-
2623
private final String namespace;
2724
private final String name;
2825

@@ -39,7 +36,7 @@ private MinecraftChannelIdentifier(String namespace, String name) {
3936
* @return a new channel identifier
4037
*/
4138
public static MinecraftChannelIdentifier forDefaultNamespace(String name) {
42-
return new MinecraftChannelIdentifier("minecraft", name);
39+
return new MinecraftChannelIdentifier(Key.MINECRAFT_NAMESPACE, name);
4340
}
4441

4542
/**
@@ -52,14 +49,10 @@ public static MinecraftChannelIdentifier forDefaultNamespace(String name) {
5249
public static MinecraftChannelIdentifier create(String namespace, String name) {
5350
checkArgument(!Strings.isNullOrEmpty(namespace), "namespace is null or empty");
5451
checkArgument(name != null, "namespace is null or empty");
55-
checkArgument(VALID_IDENTIFIER_REGEX.matcher(namespace).matches(),
56-
"namespace is not valid, must match: %s got %s",
57-
VALID_IDENTIFIER_REGEX.toString(),
58-
namespace);
59-
checkArgument(VALID_IDENTIFIER_REGEX.matcher(name).matches(),
60-
"name is not valid, must match: %s got %s",
61-
VALID_IDENTIFIER_REGEX.toString(),
62-
name);
52+
checkArgument(Key.parseableNamespace(namespace),
53+
"namespace is not valid, must match: [a-z0-9_.-] got %s", namespace);
54+
checkArgument(Key.parseableValue(name),
55+
"name is not valid, must match: [a-z0-9/._-] got %s", name);
6356
return new MinecraftChannelIdentifier(namespace, name);
6457
}
6558

@@ -72,10 +65,9 @@ public static MinecraftChannelIdentifier create(String namespace, String name) {
7265
public static MinecraftChannelIdentifier from(String identifier) {
7366
int colonPos = identifier.indexOf(':');
7467
if (colonPos == -1) {
75-
throw new IllegalArgumentException("Identifier does not contain a colon.");
76-
}
77-
if (colonPos + 1 == identifier.length()) {
78-
throw new IllegalArgumentException("Identifier is empty.");
68+
return create(Key.MINECRAFT_NAMESPACE, identifier);
69+
} else if (colonPos == 0) {
70+
return create(Key.MINECRAFT_NAMESPACE, identifier.substring(1));
7971
}
8072
String namespace = identifier.substring(0, colonPos);
8173
String name = identifier.substring(colonPos + 1);

api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,25 @@ void createAllowsSlashes() {
4747
create("velocity", "test/test2");
4848
}
4949

50+
@Test
51+
void fromIdentifierDefaultNamespace() {
52+
assertEquals("minecraft", from("test").getNamespace());
53+
assertEquals("minecraft", from(":test").getNamespace());
54+
}
55+
56+
@Test
57+
void fromIdentifierAllowsEmptyName() {
58+
from("minecraft:");
59+
from(":");
60+
from("");
61+
}
62+
5063
@Test
5164
void fromIdentifierThrowsOnBadValues() {
5265
assertAll(
53-
() -> assertThrows(IllegalArgumentException.class, () -> from("")),
54-
() -> assertThrows(IllegalArgumentException.class, () -> from(":")),
55-
() -> assertThrows(IllegalArgumentException.class, () -> from(":a")),
56-
() -> assertThrows(IllegalArgumentException.class, () -> from("a:")),
5766
() -> assertThrows(IllegalArgumentException.class, () -> from("hello:$$$$$$")),
67+
() -> assertThrows(IllegalArgumentException.class, () -> from("he/llo:wor/ld")),
5868
() -> assertThrows(IllegalArgumentException.class, () -> from("hello::"))
5969
);
6070
}
61-
62-
63-
}
71+
}

0 commit comments

Comments
 (0)