13
0
geforkt von Mirrors/Velocity

Extend the API with a ProxyServer type.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-05 01:26:07 -04:00
Ursprung 06a6493605
Commit 22d1398f73
5 geänderte Dateien mit 85 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,32 @@
package com.velocitypowered.api.proxy;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
/**
* Represents a Minecraft proxy server that follows the Velocity API.
*/
public interface ProxyServer {
/**
* Retrieves the player currently connected to this proxy by their Minecraft username.
* @param username the username
* @return an {@link Optional} with the player
*/
Optional<Player> getPlayer(@Nonnull String username);
/**
* Retrieves the player currently connected to this proxy by their Minecraft UUID.
* @param uuid the UUID
* @return an {@link Optional} with the player
*/
Optional<Player> getPlayer(@Nonnull UUID uuid);
/**
* Retrieves all players currently connected to this proxy. This call may or may not be a snapshot of all players
* online.
* @return the players online on this proxy
*/
Collection<Player> getAllPlayers();
}

Datei anzeigen

@ -1,10 +1,16 @@
package com.velocitypowered.proxy;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.network.ConnectionManager;
import com.velocitypowered.proxy.config.VelocityConfiguration;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.connection.http.NettyHttpClient;
import com.velocitypowered.api.server.ServerInfo;
import com.velocitypowered.proxy.util.AddressUtil;
@ -16,15 +22,20 @@ import net.kyori.text.serializer.GsonComponentSerializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class VelocityServer {
public class VelocityServer implements ProxyServer {
private static final Logger logger = LogManager.getLogger(VelocityServer.class);
private static final VelocityServer INSTANCE = new VelocityServer();
public static final Gson GSON = new GsonBuilder()
@ -37,6 +48,9 @@ public class VelocityServer {
private KeyPair serverKeyPair;
private final ServerMap servers = new ServerMap();
private final Map<UUID, ConnectedPlayer> connectionsByUuid = new ConcurrentHashMap<>();
private final Map<String, ConnectedPlayer> connectionsByName = new ConcurrentHashMap<>();
private VelocityServer() {
}
@ -103,4 +117,31 @@ public class VelocityServer {
public NettyHttpClient getHttpClient() {
return httpClient;
}
public void registerConnection(ConnectedPlayer connection) {
connectionsByName.put(connection.getUsername(), connection);
connectionsByUuid.put(connection.getUniqueId(), connection);
}
public void unregisterConnection(ConnectedPlayer connection) {
connectionsByName.remove(connection.getUsername(), connection);
connectionsByUuid.remove(connection.getUniqueId(), connection);
}
@Override
public Optional<Player> getPlayer(@Nonnull String username) {
Preconditions.checkNotNull(username, "username");
return Optional.ofNullable(connectionsByName.get(username));
}
@Override
public Optional<Player> getPlayer(@Nonnull UUID uuid) {
Preconditions.checkNotNull(uuid, "uuid");
return Optional.ofNullable(connectionsByUuid.get(uuid));
}
@Override
public Collection<Player> getAllPlayers() {
return ImmutableList.copyOf(connectionsByUuid.values());
}
}

Datei anzeigen

@ -209,6 +209,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
if (connectedServer != null) {
connectedServer.disconnect();
}
VelocityServer.getServer().unregisterConnection(this);
}
@Override

Datei anzeigen

@ -140,6 +140,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
inbound.setAssociation(player);
inbound.setState(StateRegistry.PLAY);
inbound.setSessionHandler(new InitialConnectSessionHandler(player));
VelocityServer.getServer().registerConnection(player);
player.createConnectionRequest(toTry.get()).fireAndForget();
}
}

Datei anzeigen

@ -28,6 +28,15 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder<ByteBuf> {
}
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception {
if (msg.readableBytes() <= threshold) {
return ctx.alloc().directBuffer(msg.readableBytes() + 1);
}
// A reasonable assumption about compression savings
return ctx.alloc().directBuffer(msg.readableBytes() / 3);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
compressor.dispose();