Remove portedPlayers field, let platform handle players

This commit is contained in:
creeper123123321 2020-04-12 15:37:08 -03:00
parent d7027cc0e3
commit ca78bf9851
15 changed files with 231 additions and 47 deletions

View File

@ -11,6 +11,7 @@ import us.myles.ViaVersion.api.ViaAPI;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator;
@ -19,15 +20,18 @@ import us.myles.ViaVersion.bukkit.commands.BukkitCommandSender;
import us.myles.ViaVersion.bukkit.platform.*;
import us.myles.ViaVersion.bukkit.util.NMSUtil;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.GsonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player> {
private static ViaVersionPlugin instance;
private final Map<UUID, UserConnection> clients = new ConcurrentHashMap<>();
private final Set<UserConnection> connections = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final BukkitCommandHandler commandHandler;
private final BukkitViaConfig conf;
private final ViaAPI<Player> api = new BukkitViaAPI(this);
@ -279,4 +283,35 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
public static ViaVersionPlugin getInstance() {
return instance;
}
@Override
public void onLoginSuccess(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.add(connection);
clients.put(id, connection);
}
@Override
public void onDisconnect(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.remove(connection);
clients.remove(id);
}
@Override
public Map<UUID, UserConnection> getConnectedClients() {
return Collections.unmodifiableMap(clients);
}
@Override
public UserConnection getConnectedClient(UUID clientIdentifier) {
return clients.get(clientIdentifier);
}
@Override
public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections);
}
}

View File

@ -108,6 +108,6 @@ public class BukkitViaAPI implements ViaAPI<Player> {
}
public Map<UUID, UserConnection> getPortedPlayers() {
return Via.getManager().getPortedPlayers();
return Via.getManager().getConnectedClients();
}
}

View File

@ -71,7 +71,7 @@ public class BukkitViaLoader implements ViaPlatformLoader {
registerListener(new Listener() {
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
Via.getManager().removePortedClient(e.getPlayer().getUniqueId());
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
});

View File

@ -22,15 +22,16 @@ import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
import us.myles.ViaVersion.bungee.platform.*;
import us.myles.ViaVersion.bungee.service.ProtocolDetectorService;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.GsonUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
private final Map<UUID, UserConnection> clients = new ConcurrentHashMap<>();
private final Set<UserConnection> connections = Collections.newSetFromMap(new ConcurrentHashMap<>());
private BungeeViaAPI api;
private BungeeViaConfig config;
private BungeeCommandHandler commandHandler;
@ -184,13 +185,44 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
return true;
}
@Override
public void onLoginSuccess(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.add(connection);
clients.put(id, connection);
}
@Override
public void onDisconnect(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.remove(connection);
clients.remove(id);
}
@Override
public Map<UUID, UserConnection> getConnectedClients() {
return Collections.unmodifiableMap(clients);
}
@Override
public UserConnection getConnectedClient(UUID clientIdentifier) {
return clients.get(clientIdentifier);
}
@Override
public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections);
}
@EventHandler
public void onQuit(PlayerDisconnectEvent e) {
UserConnection userConnection = Via.getManager().getPortedPlayers().get(e.getPlayer().getUniqueId());
UserConnection userConnection = getConnectedClient(e.getPlayer().getUniqueId());
if (userConnection != null) {
// Only remove if the connection is disconnected (eg. relogin)
if (userConnection.getChannel() == null || !userConnection.getChannel().isOpen()) {
Via.getManager().removePortedClient(e.getPlayer().getUniqueId());
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
}

View File

@ -79,7 +79,7 @@ public class BungeeViaAPI implements ViaAPI<ProxiedPlayer> {
}
public Map<UUID, UserConnection> getPortedPlayers() {
return Via.getManager().getPortedPlayers();
return Via.getManager().getConnectedClients();
}
/**

View File

@ -10,18 +10,16 @@ import us.myles.ViaVersion.api.platform.providers.ViaProviders;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.commands.ViaCommandHandler;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.TabCompleteThread;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.ViaIdleThread;
import us.myles.ViaVersion.update.UpdateUtil;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class ViaManager {
private final Map<UUID, UserConnection> portedPlayers = new ConcurrentHashMap<>();
private final ViaPlatform platform;
private final ViaPlatform<?> platform;
private final ViaProviders providers = new ViaProviders();
private boolean debug;
// Internals
@ -30,7 +28,7 @@ public class ViaManager {
private final ViaPlatformLoader loader;
@Builder
public ViaManager(ViaPlatform platform, ViaInjector injector, ViaCommandHandler commandHandler, ViaPlatformLoader loader) {
public ViaManager(ViaPlatform<?> platform, ViaInjector injector, ViaCommandHandler commandHandler, ViaPlatformLoader loader) {
this.platform = platform;
this.injector = injector;
this.commandHandler = commandHandler;
@ -112,23 +110,35 @@ public class ViaManager {
loader.unload();
}
public void addPortedClient(UserConnection info) {
portedPlayers.put(info.get(ProtocolInfo.class).getUuid(), info);
public Set<UserConnection> getConnections() {
return platform.getConnections();
}
public void removePortedClient(UUID clientID) {
portedPlayers.remove(clientID);
/**
* @deprecated use getConnectedClients()
*/
@Deprecated
public Map<UUID, UserConnection> getPortedClients() {
return getConnectedClients();
}
public UserConnection getConnection(UUID playerUUID) {
return portedPlayers.get(playerUUID);
public Map<UUID, UserConnection> getConnectedClients() {
return platform.getConnectedClients();
}
public Map<UUID, UserConnection> getPortedPlayers() {
return portedPlayers;
public void handleLoginSuccess(UserConnection info) {
platform.onLoginSuccess(info);
}
public ViaPlatform getPlatform() {
public void handleDisconnect(UUID id) {
handleDisconnect(getConnection(id));
}
public void handleDisconnect(UserConnection info) {
platform.onDisconnect(info);
}
public ViaPlatform<?> getPlatform() {
return platform;
}
@ -155,4 +165,8 @@ public class ViaManager {
public ViaPlatformLoader getLoader() {
return loader;
}
public UserConnection getConnection(UUID playerUUID) {
return platform.getConnectedClient(playerUUID);
}
}

View File

@ -5,8 +5,11 @@ import us.myles.ViaVersion.api.ViaAPI;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.UserConnection;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
@ -166,4 +169,18 @@ public interface ViaPlatform<T> {
* @return True if allowed
*/
boolean isOldClientsAllowed();
void onLoginSuccess(UserConnection connection);
void onDisconnect(UserConnection connection);
Map<UUID, UserConnection> getConnectedClients();
UserConnection getConnectedClient(UUID clientIdentifier);
/**
* May contain duplicated UUIDs on multiple ProtocolInfo.
* May contain client-sided connections.
*/
Set<UserConnection> getConnections();
}

View File

@ -133,7 +133,7 @@ public class BaseProtocol1_7 extends Protocol {
String username = wrapper.passthrough(Type.STRING);
info.setUsername(username);
// Add to ported clients
Via.getManager().addPortedClient(wrapper.user());
Via.getManager().handleLoginSuccess(wrapper.user());
if (info.getPipeline().pipes().size() == 2
&& info.getPipeline().pipes().get(1).getClass() == BaseProtocol1_7.class

View File

@ -8,7 +8,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.TabCompleteTra
public class TabCompleteThread implements Runnable {
@Override
public void run() {
for (UserConnection info : Via.getManager().getPortedPlayers().values()) {
for (UserConnection info : Via.getManager().getConnections()) {
if (info.has(ProtocolInfo.class) && info.get(ProtocolInfo.class).getPipeline().contains(Protocol1_13To1_12_2.class)) {
if (info.getChannel().isOpen()) {
info.get(TabCompleteTracker.class).sendPacketToServer();

View File

@ -9,7 +9,7 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker;
public class ViaIdleThread implements Runnable {
@Override
public void run() {
for (UserConnection info : Via.getManager().getPortedPlayers().values()) {
for (UserConnection info : Via.getManager().getConnections()) {
ProtocolInfo protocolInfo = info.get(ProtocolInfo.class);
if (protocolInfo != null && protocolInfo.getPipeline().contains(Protocol1_9To1_8.class)) {
long nextIdleUpdate = info.get(MovementTracker.class).getNextIdlePacket();

View File

@ -20,9 +20,11 @@ import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.sponge.VersionInfo;
import us.myles.ViaVersion.sponge.commands.SpongeCommandHandler;
import us.myles.ViaVersion.sponge.commands.SpongeCommandSender;
@ -31,9 +33,8 @@ import us.myles.ViaVersion.sponge.util.LoggerWrapper;
import us.myles.ViaVersion.util.GsonUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
@Plugin(id = "viaversion",
@ -42,7 +43,10 @@ import java.util.logging.Logger;
authors = {"_MylesC", "creeper123123321", "Gerrygames", "KennyTV", "Matsv"},
description = "Allow newer Minecraft versions to connect to an older server version."
)
public class SpongePlugin implements ViaPlatform {
public class SpongePlugin implements ViaPlatform<Player> {
private final Map<UUID, UserConnection> clients = new ConcurrentHashMap<>();
private final Set<UserConnection> connections = Collections.newSetFromMap(new ConcurrentHashMap<>());
@Inject
private Game game;
@ -50,8 +54,8 @@ public class SpongePlugin implements ViaPlatform {
private PluginContainer container;
@Inject
@DefaultConfig(sharedRoot = false)
private File defaultConfig;
@DefaultConfig(sharedRoot = true)
private File spongeConfig;
@Getter
private SpongeViaAPI api = new SpongeViaAPI();
@ -66,7 +70,7 @@ public class SpongePlugin implements ViaPlatform {
// Setup Logger
logger = new LoggerWrapper(container.getLogger());
// Setup Plugin
conf = new SpongeViaConfig(container, defaultConfig.getParentFile());
conf = new SpongeViaConfig(container, spongeConfig);
SpongeCommandHandler commandHandler = new SpongeCommandHandler();
game.getCommandManager().register(this, commandHandler, "viaversion", "viaver", "vvsponge");
logger.info("ViaVersion " + getPluginVersion() + " is now loaded!");
@ -203,7 +207,7 @@ public class SpongePlugin implements ViaPlatform {
@Override
public File getDataFolder() {
return defaultConfig.getParentFile();
return spongeConfig;
}
@Override
@ -234,4 +238,36 @@ public class SpongePlugin implements ViaPlatform {
public boolean isOldClientsAllowed() {
return true;
}
@Override
public void onLoginSuccess(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.add(connection);
clients.put(id, connection);
}
@Override
public void onDisconnect(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.remove(connection);
clients.remove(id);
}
@Override
public Map<UUID, UserConnection> getConnectedClients() {
return Collections.unmodifiableMap(clients);
}
@Override
public UserConnection getConnectedClient(UUID clientIdentifier) {
return clients.get(clientIdentifier);
}
@Override
public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections);
}
}

View File

@ -7,6 +7,6 @@ import us.myles.ViaVersion.api.Via;
public class ClientLeaveListener {
@Listener
public void onDisconnect(ClientConnectionEvent.Disconnect disconnect) {
Via.getManager().removePortedClient(disconnect.getTargetEntity().getUniqueId());
Via.getManager().handleDisconnect(disconnect.getTargetEntity().getUniqueId());
}
}

View File

@ -74,6 +74,6 @@ public class SpongeViaAPI implements ViaAPI<Player> {
}
public Map<UUID, UserConnection> getPortedPlayers() {
return Via.getManager().getPortedPlayers();
return Via.getManager().getConnectedClients();
}
}

View File

@ -23,6 +23,7 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.GsonUtil;
import us.myles.ViaVersion.velocity.VersionInfo;
import us.myles.ViaVersion.velocity.command.VelocityCommandHandler;
@ -33,9 +34,8 @@ import us.myles.ViaVersion.velocity.util.LoggerWrapper;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@Plugin(
@ -46,19 +46,23 @@ import java.util.concurrent.TimeUnit;
description = "Allow newer Minecraft versions to connect to an older server version.",
url = "https://viaversion.com"
)
@Getter
public class VelocityPlugin implements ViaPlatform<Player> {
private final Map<UUID, UserConnection> clients = new ConcurrentHashMap<>();
private final Set<UserConnection> connections = Collections.newSetFromMap(new ConcurrentHashMap<>());
@Inject
private ProxyServer proxy;
@Inject
public static ProxyServer PROXY;
@Inject
private Logger loggerslf4j;
private java.util.logging.Logger logger;
@Inject
@DataDirectory
private Path configDir;
private VelocityViaAPI api;
private VelocityViaConfig conf;
@Subscribe
@ -84,11 +88,11 @@ public class VelocityPlugin implements ViaPlatform<Player> {
@Subscribe
public void onQuit(DisconnectEvent e) {
UserConnection userConnection = Via.getManager().getPortedPlayers().get(e.getPlayer().getUniqueId());
UserConnection userConnection = getConnectedClient(e.getPlayer().getUniqueId());
if (userConnection != null) {
// Only remove if the connection is disconnected (eg. relogin)
if (userConnection.getChannel() == null || !userConnection.getChannel().isOpen()) {
Via.getManager().removePortedClient(e.getPlayer().getUniqueId());
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
}
}
@ -188,6 +192,16 @@ public class VelocityPlugin implements ViaPlatform<Player> {
return configDir.toFile();
}
@Override
public VelocityViaAPI getApi() {
return api;
}
@Override
public VelocityViaConfig getConf() {
return conf;
}
@Override
public void onReload() {
@ -215,4 +229,40 @@ public class VelocityPlugin implements ViaPlatform<Player> {
public boolean isOldClientsAllowed() {
return true;
}
@Override
public java.util.logging.Logger getLogger() {
return logger;
}
@Override
public void onLoginSuccess(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.add(connection);
clients.put(id, connection);
}
@Override
public void onDisconnect(UserConnection connection) {
Objects.requireNonNull(connection, "connection is null!");
UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.remove(connection);
clients.remove(id);
}
@Override
public Map<UUID, UserConnection> getConnectedClients() {
return Collections.unmodifiableMap(clients);
}
@Override
public UserConnection getConnectedClient(UUID clientIdentifier) {
return clients.get(clientIdentifier);
}
@Override
public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections);
}
}

View File

@ -80,6 +80,6 @@ public class VelocityViaAPI implements ViaAPI<Player> {
}
public Map<UUID, UserConnection> getPortedPlayers() {
return Via.getManager().getPortedPlayers();
return Via.getManager().getConnectedClients();
}
}