13
0
geforkt von Mirrors/Velocity

Add some missing convenience APIs to MinecraftChannelIdentifier

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-10-28 19:02:37 -04:00
Ursprung 7bec4b2f12
Commit dd23203139
2 geänderte Dateien mit 56 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.kyori.minecraft.Key;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
@ -50,6 +51,35 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier {
return new MinecraftChannelIdentifier(namespace, name); 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() { public String getNamespace() {
return namespace; return namespace;
} }
@ -58,6 +88,10 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier {
return name; return name;
} }
public Key asKey() {
return Key.of(namespace, name);
}
@Override @Override
public String toString() { public String toString() {
return namespace + ":" + name + " (modern)"; return namespace + ":" + name + " (modern)";

Datei anzeigen

@ -1,7 +1,9 @@
package com.velocitypowered.api.proxy.messages; package com.velocitypowered.api.proxy.messages;
import static com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier.create; 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.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -26,4 +28,24 @@ class MinecraftChannelIdentifierTest {
() -> assertThrows(IllegalArgumentException.class, () -> create("minecraft", null)) () -> 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::"))
);
}
} }