From 3f0a85d794e3f401409a013fac6ea48b22a2e3ae Mon Sep 17 00:00:00 2001 From: booky Date: Mon, 14 Apr 2025 19:52:02 +0200 Subject: [PATCH] Fix MinecraftChannelIdentifier parsing to align with vanilla (#1552) --- .../messages/MinecraftChannelIdentifier.java | 24 +++++++------------ .../MinecraftChannelIdentifierTest.java | 22 +++++++++++------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java b/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java index 98967cfd..a271a414 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java @@ -11,7 +11,6 @@ import static com.google.common.base.Preconditions.checkArgument; import com.google.common.base.Strings; import java.util.Objects; -import java.util.regex.Pattern; import net.kyori.adventure.key.Key; import org.checkerframework.checker.nullness.qual.Nullable; @@ -21,8 +20,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public final class MinecraftChannelIdentifier implements ChannelIdentifier { - private static final Pattern VALID_IDENTIFIER_REGEX = Pattern.compile("[a-z0-9/\\-_]*"); - private final String namespace; private final String name; @@ -39,7 +36,7 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { * @return a new channel identifier */ public static MinecraftChannelIdentifier forDefaultNamespace(String name) { - return new MinecraftChannelIdentifier("minecraft", name); + return new MinecraftChannelIdentifier(Key.MINECRAFT_NAMESPACE, name); } /** @@ -52,14 +49,10 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { public static MinecraftChannelIdentifier create(String namespace, String name) { checkArgument(!Strings.isNullOrEmpty(namespace), "namespace is null or empty"); checkArgument(name != null, "namespace is null or empty"); - checkArgument(VALID_IDENTIFIER_REGEX.matcher(namespace).matches(), - "namespace is not valid, must match: %s got %s", - VALID_IDENTIFIER_REGEX.toString(), - namespace); - checkArgument(VALID_IDENTIFIER_REGEX.matcher(name).matches(), - "name is not valid, must match: %s got %s", - VALID_IDENTIFIER_REGEX.toString(), - name); + checkArgument(Key.parseableNamespace(namespace), + "namespace is not valid, must match: [a-z0-9_.-] got %s", namespace); + checkArgument(Key.parseableValue(name), + "name is not valid, must match: [a-z0-9/._-] got %s", name); return new MinecraftChannelIdentifier(namespace, name); } @@ -72,10 +65,9 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { public static MinecraftChannelIdentifier from(String identifier) { int colonPos = identifier.indexOf(':'); if (colonPos == -1) { - throw new IllegalArgumentException("Identifier does not contain a colon."); - } - if (colonPos + 1 == identifier.length()) { - throw new IllegalArgumentException("Identifier is empty."); + return create(Key.MINECRAFT_NAMESPACE, identifier); + } else if (colonPos == 0) { + return create(Key.MINECRAFT_NAMESPACE, identifier.substring(1)); } String namespace = identifier.substring(0, colonPos); String name = identifier.substring(colonPos + 1); diff --git a/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java b/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java index 4c8919f3..9be12fc5 100644 --- a/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java +++ b/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java @@ -47,17 +47,25 @@ class MinecraftChannelIdentifierTest { create("velocity", "test/test2"); } + @Test + void fromIdentifierDefaultNamespace() { + assertEquals("minecraft", from("test").getNamespace()); + assertEquals("minecraft", from(":test").getNamespace()); + } + + @Test + void fromIdentifierAllowsEmptyName() { + from("minecraft:"); + from(":"); + from(""); + } + @Test void fromIdentifierThrowsOnBadValues() { assertAll( - () -> assertThrows(IllegalArgumentException.class, () -> from("")), - () -> assertThrows(IllegalArgumentException.class, () -> from(":")), - () -> assertThrows(IllegalArgumentException.class, () -> from(":a")), - () -> assertThrows(IllegalArgumentException.class, () -> from("a:")), () -> assertThrows(IllegalArgumentException.class, () -> from("hello:$$$$$$")), + () -> assertThrows(IllegalArgumentException.class, () -> from("he/llo:wor/ld")), () -> assertThrows(IllegalArgumentException.class, () -> from("hello::")) ); } - - -} \ No newline at end of file +}