From b35fd05d2e68c6f1f165169b2ef7c2c110f84f4c Mon Sep 17 00:00:00 2001 From: Crypnotic Date: Sat, 2 Mar 2019 13:25:37 -0600 Subject: [PATCH] Add matchPlayer and matchServer --- .../api/proxy/ProxyServer.java | 16 ++++++++++ .../velocitypowered/proxy/VelocityServer.java | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/ProxyServer.java b/api/src/main/java/com/velocitypowered/api/proxy/ProxyServer.java index 7cde5a176..64584c0d9 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/ProxyServer.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/ProxyServer.java @@ -76,6 +76,22 @@ public interface ProxyServer { */ Collection getAllServers(); + /** + * Matches all {@link Player}s whose names start with the provided partial name. + * + * @param partialName the partial name to check for + * @return a collection of mathed {@link Player}s + */ + Collection matchPlayer(String partialName); + + /** + * Matches all {@link RegisteredServer}s whose names start with the provided partial name. + * + * @param partialName the partial name to check for + * @return a collection of mathed {@link RegisteredServer}s + */ + Collection matchServer(String partialName); + /** * Registers a server with this proxy. A server with this name should not already exist. * diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 81987889b..913b3497c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -50,13 +50,16 @@ import java.nio.file.Paths; import java.security.KeyPair; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.serializer.GsonComponentSerializer; @@ -442,6 +445,34 @@ public class VelocityServer implements ProxyServer { } } + @Override + public Collection matchPlayer(String partialName) { + Objects.requireNonNull(partialName); + + Optional exactMatch = getPlayer(partialName); + if (exactMatch.isPresent()) { + return Collections.singleton(exactMatch.get()); + } + + return getAllPlayers().stream().filter(p -> p.getUsername() + .regionMatches(true, 0, partialName, 0, partialName.length())) + .collect(Collectors.toList()); + } + + @Override + public Collection matchServer(String partialName) { + Objects.requireNonNull(partialName); + + Optional exactMatch = getServer(partialName); + if (exactMatch.isPresent()) { + return Collections.singleton(exactMatch.get()); + } + + return getAllServers().stream().filter(s -> s.getServerInfo().getName() + .regionMatches(true, 0, partialName, 0, partialName.length())) + .collect(Collectors.toList()); + } + @Override public Collection getAllPlayers() { return ImmutableList.copyOf(connectionsByUuid.values());