From 2b6786e1faa77812fc11981f9f9ba8b764ef6929 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 21 Aug 2018 22:41:30 -0400 Subject: [PATCH] Started work on plugin messaging API. --- .../api/proxy/messages/ChannelIdentifier.java | 7 ++ .../api/proxy/messages/ChannelRegistrar.java | 4 ++ .../messages/LegacyChannelIdentifier.java | 43 +++++++++++++ .../messages/MinecraftChannelIdentifier.java | 64 +++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelIdentifier.java create mode 100644 api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelRegistrar.java create mode 100644 api/src/main/java/com/velocitypowered/api/proxy/messages/LegacyChannelIdentifier.java create mode 100644 api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java diff --git a/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelIdentifier.java b/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelIdentifier.java new file mode 100644 index 000000000..66d8b65c9 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelIdentifier.java @@ -0,0 +1,7 @@ +package com.velocitypowered.api.proxy.messages; + +/** + * Represents a kind of channel identifier. + */ +public interface ChannelIdentifier { +} diff --git a/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelRegistrar.java b/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelRegistrar.java new file mode 100644 index 000000000..71fa1bb1d --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/ChannelRegistrar.java @@ -0,0 +1,4 @@ +package com.velocitypowered.api.proxy.messages; + +public interface ChannelRegistrar { +} diff --git a/api/src/main/java/com/velocitypowered/api/proxy/messages/LegacyChannelIdentifier.java b/api/src/main/java/com/velocitypowered/api/proxy/messages/LegacyChannelIdentifier.java new file mode 100644 index 000000000..4d534912b --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/LegacyChannelIdentifier.java @@ -0,0 +1,43 @@ +package com.velocitypowered.api.proxy.messages; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +import java.util.Objects; + +/** + * Reperesents a legacy channel identifier (for Minecraft 1.12 and below). For modern 1.13 plugin messages, please see + * {@link MinecraftChannelIdentifier}. + */ +public class LegacyChannelIdentifier implements ChannelIdentifier { + private final String name; + + public LegacyChannelIdentifier(String name) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "provided name is empty"); + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "LegacyChannelIdentifier{" + + "name='" + name + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LegacyChannelIdentifier that = (LegacyChannelIdentifier) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} 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 new file mode 100644 index 000000000..839aadb75 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java @@ -0,0 +1,64 @@ +package com.velocitypowered.api.proxy.messages; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +import java.util.Objects; +import java.util.regex.Pattern; + +/** + * Represents a Minecraft 1.13+ channel identifier. + */ +public class MinecraftChannelIdentifier implements ChannelIdentifier { + private static final Pattern VALID_IDENTIFIER_REGEX = Pattern.compile("[a-z0-9\\-_]+"); + + private final String namespace; + private final String name; + + private MinecraftChannelIdentifier(String namespace, String name) { + this.namespace = namespace; + this.name = name; + } + + public static MinecraftChannelIdentifier forDefaultNamespace(String name) { + return new MinecraftChannelIdentifier("minecraft", name); + } + + public static MinecraftChannelIdentifier create(String namespace, String name) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace is null or empty"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "namespace is null or empty"); + Preconditions.checkArgument(VALID_IDENTIFIER_REGEX.matcher(namespace).matches(), "namespace is not valid"); + Preconditions.checkArgument(VALID_IDENTIFIER_REGEX.matcher(name).matches(), "name is not valid"); + return new MinecraftChannelIdentifier(namespace, name); + } + + public String getNamespace() { + return namespace; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "MinecraftChannelIdentifier{" + + "namespace='" + namespace + '\'' + + ", name='" + name + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MinecraftChannelIdentifier that = (MinecraftChannelIdentifier) o; + return Objects.equals(namespace, that.namespace) && + Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(namespace, name); + } +}