Add some missing convenience APIs to MinecraftChannelIdentifier

This commit is contained in:
Andrew Steinborn
2020-10-28 19:02:37 -04:00
parent 7bec4b2f12
commit dd23203139
2 changed files with 56 additions and 0 deletions

View File

@@ -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)";