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 b63b6b1ac..01a582035 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 @@ -4,6 +4,7 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import java.util.Objects; import java.util.regex.Pattern; +import net.kyori.minecraft.Key; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -50,6 +51,35 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { return new MinecraftChannelIdentifier(namespace, name); } + /** + * Creates an channel identifier from the specified Minecraft identifier. + * + * @param identifier the Minecraft identifier + * @return a new channel identifier + */ + 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."); + } + String namespace = identifier.substring(0, colonPos); + String name = identifier.substring(colonPos + 1); + return create(namespace, name); + } + + /** + * Creates an channel identifier from the specified Minecraft identifier. + * + * @param key the Minecraft key to use + * @return a new channel identifier + */ + public static MinecraftChannelIdentifier from(Key key) { + return create(key.namespace(), key.value()); + } + public String getNamespace() { return namespace; } @@ -58,6 +88,10 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { return name; } + public Key asKey() { + return Key.of(namespace, name); + } + @Override public String toString() { return namespace + ":" + name + " (modern)"; 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 00699ba5e..dfa738198 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 @@ -1,7 +1,9 @@ package com.velocitypowered.api.proxy.messages; import static com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier.create; +import static com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier.from; import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -26,4 +28,24 @@ class MinecraftChannelIdentifierTest { () -> assertThrows(IllegalArgumentException.class, () -> create("minecraft", null)) ); } + + @Test + void fromIdentifierIsCorrect() { + MinecraftChannelIdentifier expected = MinecraftChannelIdentifier.create("velocity", "test"); + assertEquals(expected, MinecraftChannelIdentifier.from("velocity:test")); + } + + @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("hello::")) + ); + } + + } \ No newline at end of file