Merge branch 'plugin-messages'

This commit is contained in:
Andrew Steinborn
2018-08-22 00:27:29 -04:00
14 changed files with 238 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package com.velocitypowered.api.proxy;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.messages.ChannelMessageSource;
import com.velocitypowered.api.server.ServerInfo;
import com.velocitypowered.api.util.MessagePosition;
import net.kyori.text.Component;
@@ -12,7 +13,7 @@ import java.util.UUID;
/**
* Represents a player who is connected to the proxy.
*/
public interface Player extends CommandSource, InboundConnection {
public interface Player extends CommandSource, InboundConnection, ChannelMessageSource {
/**
* Returns the player's current username.
* @return the username

View File

@@ -4,6 +4,7 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.plugin.PluginManager;
import com.velocitypowered.api.proxy.messages.ChannelRegistrar;
import com.velocitypowered.api.scheduler.Scheduler;
import com.velocitypowered.api.server.ServerInfo;
@@ -101,4 +102,10 @@ public interface ProxyServer {
* @return the scheduler instance
*/
Scheduler getScheduler();
/**
* Gets the {@link ChannelRegistrar} instance.
* @return the channel registrar
*/
ChannelRegistrar getChannelRegistrar();
}

View File

@@ -1,11 +1,12 @@
package com.velocitypowered.api.proxy;
import com.velocitypowered.api.proxy.messages.ChannelMessageSource;
import com.velocitypowered.api.server.ServerInfo;
/**
* Represents a connection to a backend server from the proxy for a client.
*/
public interface ServerConnection {
public interface ServerConnection extends ChannelMessageSource {
ServerInfo getServerInfo();
Player getPlayer();

View File

@@ -0,0 +1,8 @@
package com.velocitypowered.api.proxy.messages;
/**
* Represents a kind of channel identifier.
*/
public interface ChannelIdentifier {
String getId();
}

View File

@@ -0,0 +1,4 @@
package com.velocitypowered.api.proxy.messages;
public interface ChannelMessageSource {
}

View File

@@ -0,0 +1,11 @@
package com.velocitypowered.api.proxy.messages;
/**
* Represents an interface to register and unregister {@link MessageHandler} instances for handling plugin messages from
* the client or the server.
*/
public interface ChannelRegistrar {
void register(MessageHandler handler, ChannelIdentifier... identifiers);
void unregister(ChannelIdentifier... identifiers);
}

View File

@@ -0,0 +1,6 @@
package com.velocitypowered.api.proxy.messages;
public enum ChannelSide {
FROM_SERVER,
FROM_CLIENT
}

View File

@@ -0,0 +1,48 @@
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}. This class is immutable and safe for multi-threaded use.
*/
public final 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);
}
@Override
public String getId() {
return name;
}
}

View File

@@ -0,0 +1,10 @@
package com.velocitypowered.api.proxy.messages;
public interface MessageHandler {
ForwardStatus handle(ChannelMessageSource source, ChannelSide side, byte[] data);
enum ForwardStatus {
FORWARD,
HANDLED
}
}

View File

@@ -0,0 +1,69 @@
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. This class is immutable and safe for multi-threaded use.
*/
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;
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);
}
@Override
public String getId() {
return namespace + ":" + name;
}
}