From 63356207a3704aedf34d070d9c6c3a55f8c6879e Mon Sep 17 00:00:00 2001 From: KennyTV Date: Wed, 24 Mar 2021 15:21:18 +0100 Subject: [PATCH] ProtocolRegistry -> ProtocolManager interface --- .../java/us/myles/ViaVersion/ViaManager.java | 45 +-- .../java/us/myles/ViaVersion/api/Via.java | 15 +- .../ViaVersion/api/platform/ViaPlatform.java | 11 +- .../api/protocol/ProtocolManager.java | 166 ++++++++++ .../api/protocol/ProtocolPipeline.java | 4 +- .../api/protocol/ProtocolRegistry.java | 128 ++++++++ .../us/myles/ViaVersion/ViaVersionPlugin.java | 7 - .../multiversion/PlayerSneakListener.java | 4 +- .../bukkit/platform/BukkitViaAPI.java | 2 +- .../bukkit/platform/BukkitViaLoader.java | 17 +- .../BukkitInventoryQuickMoveProvider.java | 11 +- .../us/myles/ViaVersion/BungeePlugin.java | 7 - .../bungee/handlers/BungeeServerHandler.java | 13 +- .../bungee/listeners/ElytraPatch.java | 2 +- .../bungee/platform/BungeeViaLoader.java | 11 +- .../us/myles/ViaVersion/ViaManagerImpl.java | 103 +++---- .../us/myles/ViaVersion/api/ViaAPIBase.java | 13 +- .../us/myles/ViaVersion/api/ViaListener.java | 2 +- ...Registry.java => ProtocolManagerImpl.java} | 284 ++++++++---------- .../us/myles/ViaVersion/boss/CommonBoss.java | 6 +- .../commands/defaultsubs/DumpSubCmd.java | 5 +- .../commands/defaultsubs/PPSSubCmd.java | 2 +- .../protocols/base/BaseProtocol.java | 7 +- .../protocols/base/BaseProtocol1_7.java | 13 +- .../protocols/base/BaseVersionProvider.java | 5 +- .../TabCompleteThread.java | 2 +- .../protocol1_9to1_8/ViaIdleThread.java | 2 +- .../us/myles/ViaVersion/SpongePlugin.java | 7 - .../handlers/SpongeChannelInitializer.java | 4 +- .../sponge/platform/SpongeViaLoader.java | 5 +- .../us/myles/ViaVersion/VelocityPlugin.java | 8 - .../velocity/platform/VelocityViaLoader.java | 3 +- 32 files changed, 536 insertions(+), 378 deletions(-) create mode 100644 api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManager.java create mode 100644 api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java rename common/src/main/java/us/myles/ViaVersion/api/protocol/{ProtocolRegistry.java => ProtocolManagerImpl.java} (70%) diff --git a/api/src/main/java/us/myles/ViaVersion/ViaManager.java b/api/src/main/java/us/myles/ViaVersion/ViaManager.java index d001d3112..233071202 100644 --- a/api/src/main/java/us/myles/ViaVersion/ViaManager.java +++ b/api/src/main/java/us/myles/ViaVersion/ViaManager.java @@ -22,55 +22,36 @@ */ package us.myles.ViaVersion; -import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.command.ViaVersionCommand; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.api.platform.providers.ViaProviders; -import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.protocol.ProtocolManager; -import java.util.Map; import java.util.Set; -import java.util.UUID; public interface ViaManager { - Set getConnections(); - - /** - * @deprecated use getConnectedClients() - */ - @Deprecated - Map getPortedPlayers(); - - Map getConnectedClients(); - - UUID getConnectedClientId(UserConnection conn); - - /** - * @see ViaConnectionManager#isClientConnected(UUID) - */ - boolean isClientConnected(UUID player); - - void handleLoginSuccess(UserConnection info); + ProtocolManager getProtocolManager(); ViaPlatform getPlatform(); + ViaConnectionManager getConnectionManager(); + ViaProviders getProviders(); - boolean isDebug(); - - void setDebug(boolean debug); - ViaInjector getInjector(); ViaVersionCommand getCommandHandler(); ViaPlatformLoader getLoader(); + boolean isDebug(); + + void setDebug(boolean debug); + /** * Returns a mutable set of self-added subplatform version strings. * This set is expanded by the subplatform itself (e.g. ViaBackwards), and may not contain all running ones. @@ -79,20 +60,10 @@ public interface ViaManager { */ Set getSubPlatforms(); - /** - * @see ViaConnectionManager#getConnectedClient(UUID) - */ - @Nullable - UserConnection getConnection(UUID playerUUID); - /** * Adds a runnable to be executed when ViaVersion has finished its init before the full server load. * * @param runnable runnable to be executed */ void addEnableListener(Runnable runnable); - - Protocol getBaseProtocol(); - - boolean isBaseProtocol(Protocol protocol); } diff --git a/api/src/main/java/us/myles/ViaVersion/api/Via.java b/api/src/main/java/us/myles/ViaVersion/api/Via.java index 11f643650..00d8521d0 100644 --- a/api/src/main/java/us/myles/ViaVersion/api/Via.java +++ b/api/src/main/java/us/myles/ViaVersion/api/Via.java @@ -27,7 +27,6 @@ import us.myles.ViaVersion.ViaManager; import us.myles.ViaVersion.api.platform.ViaPlatform; public class Via { - private static ViaPlatform platform; private static ViaManager manager; /** @@ -37,19 +36,17 @@ public class Via { */ public static void init(ViaManager viaManager) { Preconditions.checkArgument(manager == null, "ViaManager is already set"); - - Via.platform = viaManager.getPlatform(); Via.manager = viaManager; } /** - * Get the API associated with the current platform. + * Returns the API associated with the current platform. * * @return API instance */ public static ViaAPI getAPI() { - Preconditions.checkArgument(platform != null, "ViaVersion has not loaded the Platform"); - return Via.platform.getApi(); + Preconditions.checkArgument(manager != null, "ViaVersion has not loaded the platform yet"); + return manager.getPlatform().getApi(); } /** @@ -58,12 +55,12 @@ public class Via { * @return Config instance */ public static ViaVersionConfig getConfig() { - Preconditions.checkArgument(platform != null, "ViaVersion has not loaded the Platform"); - return Via.platform.getConf(); + Preconditions.checkArgument(manager != null, "ViaVersion has not loaded the platform yet"); + return manager.getPlatform().getConf(); } public static ViaPlatform getPlatform() { - return platform; + return manager.getPlatform(); } public static ViaManager getManager() { diff --git a/api/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java b/api/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java index 7cc0e1e88..fde8274c2 100644 --- a/api/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java +++ b/api/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java @@ -23,6 +23,8 @@ package us.myles.ViaVersion.api.platform; import com.google.gson.JsonObject; +import us.myles.ViaVersion.ViaManager; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; @@ -216,9 +218,10 @@ public interface ViaPlatform { boolean isOldClientsAllowed(); /** - * Returns the connection manager holding and managing user connections. - * - * @return connection manager + * @deprecated use {@link ViaManager#getConnectionManager()} */ - ViaConnectionManager getConnectionManager(); + @Deprecated + default ViaConnectionManager getConnectionManager() { + return Via.getManager().getConnectionManager(); + } } diff --git a/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManager.java b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManager.java new file mode 100644 index 000000000..904d8fa94 --- /dev/null +++ b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManager.java @@ -0,0 +1,166 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package us.myles.ViaVersion.api.protocol; + +import com.google.common.collect.Range; +import org.jetbrains.annotations.Nullable; +import us.myles.ViaVersion.api.Pair; + +import java.util.List; +import java.util.SortedSet; +import java.util.concurrent.CompletableFuture; + +public interface ProtocolManager { + + /** + * Returns the server protocol version, or -1 if not set. + * In the case of proxies, this returns the lowest supported protocol version. + * + * @return server protocol version, or -1 if not set + */ + int getServerProtocol(); + + /** + * Returns a protocol instance by its class. + * + * @param protocolClass class of the protocol + * @return protocol if present + */ + @Nullable + Protocol getProtocol(Class protocolClass); + + Protocol getBaseProtocol(); + + Protocol getBaseProtocol(int serverVersion); + + boolean isBaseProtocol(Protocol protocol); + + /** + * Register a protocol. + * + * @param protocol protocol to register + * @param supported supported client versions + * @param output output server version the protocol converts to + */ + void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output); + + /** + * Register a protocol. + * + * @param protocol protocol to register + * @param supported supported client versions + * @param output output server version the protocol converts to + */ + void registerProtocol(Protocol protocol, List supported, int output); + + /** + * Registers a base protocol. Base Protocols registered later have higher priority. + * Only base protocol will always be added to pipeline. + * + * @param baseProtocol base protocol to register + * @param supportedProtocols versions supported by the base protocol + */ + void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols); + + /** + * Calculates a path from a client version to server version. + * + * @param clientVersion input client version + * @param serverVersion desired output server version + * @return path it generated, null if it failed + */ + @Nullable + List> getProtocolPath(int clientVersion, int serverVersion); + + /** + * Returns the maximum protocol path size applied to {@link #getProtocolPath(int, int)}. + * + * @return maximum protocol path size + */ + int getMaxProtocolPathSize(); + + /** + * Sets the maximum protocol path size applied to {@link #getProtocolPath(int, int)}. + * Its default it 50. + * + * @param maxProtocolPathSize maximum protocol path size + */ + void setMaxProtocolPathSize(int maxProtocolPathSize); + + /** + * Returns the versions compatible with the server. + * + * @return sorted, immutable set of supported versions + */ + SortedSet getSupportedVersions(); + + /** + * Check if this plugin is useful to the server. + * + * @return true if there is a useful pipe + */ + boolean isWorkingPipe(); + + /** + * Ensure that mapping data for that protocol has already been loaded, completes it otherwise. + * + * @param protocolClass protocol class + */ + void completeMappingDataLoading(Class protocolClass) throws Exception; + + /** + * Shuts down the executor and uncaches mappings if all futures have been completed. + * + * @return true if the executor has now been shut down + */ + boolean checkForMappingCompletion(); + + /** + * Executes the given runnable asynchronously, adding a {@link CompletableFuture} + * to the list of data to load bound to their protocols. + * + * @param protocolClass protocol class + * @param runnable runnable to be executed asynchronously + */ + void addMappingLoaderFuture(Class protocolClass, Runnable runnable); + + /** + * Executes the given runnable asynchronously after the other protocol has finished its data loading, + * adding a {@link CompletableFuture} to the list of data to load bound to their protocols. + * + * @param protocolClass protocol class + * @param dependsOn class of the protocol that the data loading depends on + * @param runnable runnable to be executed asynchronously + */ + void addMappingLoaderFuture(Class protocolClass, Class dependsOn, Runnable runnable); + + /** + * Returns the data loading future bound to the protocol, or null if all loading is complete. + * The future may or may not have already been completed. + * + * @param protocolClass protocol class + * @return data loading future bound to the protocol, or null if all loading is complete + */ + @Nullable + CompletableFuture getMappingLoaderFuture(Class protocolClass); +} diff --git a/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index 868f29813..c3772ff87 100644 --- a/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -47,7 +47,7 @@ public class ProtocolPipeline extends SimpleProtocol { protected void registerPackets() { protocolList = new CopyOnWriteArrayList<>(); // This is a pipeline so we register basic pipes - protocolList.add(Via.getManager().getBaseProtocol()); + protocolList.add(Via.getManager().getProtocolManager().getBaseProtocol()); } @Override @@ -78,7 +78,7 @@ public class ProtocolPipeline extends SimpleProtocol { // Move base Protocols to the end, so the login packets can be modified by other protocols List toMove = new ArrayList<>(); for (Protocol p : protocolList) { - if (Via.getManager().isBaseProtocol(p)) { + if (Via.getManager().getProtocolManager().isBaseProtocol(p)) { toMove.add(p); } } diff --git a/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java new file mode 100644 index 000000000..64af827e7 --- /dev/null +++ b/api/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -0,0 +1,128 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package us.myles.ViaVersion.api.protocol; + +import com.google.common.collect.Range; +import org.jetbrains.annotations.Nullable; +import us.myles.ViaVersion.ViaManager; +import us.myles.ViaVersion.api.Pair; +import us.myles.ViaVersion.api.Via; + +import java.util.Collections; +import java.util.List; +import java.util.SortedSet; + +/** + * @see ViaManager#getProtocolManager() + * @deprecated use {@link ProtocolManager} + */ +@Deprecated +public class ProtocolRegistry { + @Deprecated + public static int SERVER_PROTOCOL = -1; + @Deprecated + public static int maxProtocolPathSize = 50; + + /** + * Register a protocol + * + * @param protocol The protocol to register. + * @param supported Supported client versions. + * @param output The output server version it converts to. + */ + public static void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output) { + registerProtocol(protocol, Collections.singletonList(supported.getVersion()), output.getVersion()); + } + + /** + * Register a protocol + * + * @param protocol The protocol to register. + * @param supported Supported client versions. + * @param output The output server version it converts to. + */ + public static void registerProtocol(Protocol protocol, List supported, int output) { + Via.getManager().getProtocolManager().registerProtocol(protocol, supported, output); + } + + /** + * Registers a base protocol. + * Base Protocols registered later have higher priority + * Only one base protocol will be added to pipeline + * + * @param baseProtocol Base Protocol to register + * @param supportedProtocols Versions that baseProtocol supports + */ + public static void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { + Via.getManager().getProtocolManager().registerBaseProtocol(baseProtocol, supportedProtocols); + } + + /** + * Get the versions compatible with the server. + * + * @return Read-only set of the versions. + */ + public static SortedSet getSupportedVersions() { + return Via.getManager().getProtocolManager().getSupportedVersions(); + } + + /** + * Check if this plugin is useful to the server. + * + * @return True if there is a useful pipe + */ + public static boolean isWorkingPipe() { + return Via.getManager().getProtocolManager().isWorkingPipe(); + } + + /** + * Calculate a path from a client version to server version. + * + * @param clientVersion The input client version + * @param serverVersion The desired output server version + * @return The path it generated, null if it failed. + */ + @Nullable + public static List> getProtocolPath(int clientVersion, int serverVersion) { + return Via.getManager().getProtocolManager().getProtocolPath(clientVersion, serverVersion); + } + + /** + * Returns a protocol instance by its class. + * + * @param protocolClass class of the protocol + * @return protocol if present + */ + @Nullable + public static Protocol getProtocol(Class protocolClass) { + return Via.getManager().getProtocolManager().getProtocol(protocolClass); + } + + public static Protocol getBaseProtocol(int serverVersion) { + return Via.getManager().getProtocolManager().getBaseProtocol(serverVersion); + } + + public static boolean isBaseProtocol(Protocol protocol) { + return Via.getManager().getProtocolManager().isBaseProtocol(protocol); + } +} diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 02621f75e..756e1ab38 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -29,7 +29,6 @@ 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.platform.TaskId; -import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator; import us.myles.ViaVersion.bukkit.commands.BukkitCommandHandler; @@ -50,7 +49,6 @@ import java.util.UUID; public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { private static ViaVersionPlugin instance; - private final ViaConnectionManager connectionManager = new ViaConnectionManager(); private final BukkitCommandHandler commandHandler; private final BukkitViaConfig conf; private final ViaAPI api = new BukkitViaAPI(this); @@ -311,9 +309,4 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform public static ViaVersionPlugin getInstance() { return instance; } - - @Override - public ViaConnectionManager getConnectionManager() { - return connectionManager; - } } diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/multiversion/PlayerSneakListener.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/multiversion/PlayerSneakListener.java index 376bb0276..5ddfa0b4a 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/multiversion/PlayerSneakListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/multiversion/PlayerSneakListener.java @@ -24,8 +24,8 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.base.ProtocolInfo; @@ -68,7 +68,7 @@ public class PlayerSneakListener extends ViaBukkitListener { // From 1.9 upwards the server hitbox is set in every entity tick, so we have to reset it everytime - if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() >= ProtocolVersion.v1_9.getVersion()) { sneaking = new WeakHashMap<>(); useCache = true; plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() { diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java index ab5f5016f..8d24d9e10 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java @@ -46,7 +46,7 @@ public class BukkitViaAPI extends ViaAPIBase { @Override public int getPlayerVersion(UUID uuid) { - UserConnection connection = Via.getManager().getConnection(uuid); + UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid); if (connection == null) { Player player = Bukkit.getPlayer(uuid); if (player != null && isProtocolSupport()) { diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaLoader.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaLoader.java index 28847ba3e..9352fb70c 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaLoader.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaLoader.java @@ -27,7 +27,6 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator; import us.myles.ViaVersion.bukkit.listeners.UpdateListener; @@ -88,7 +87,7 @@ public class BukkitViaLoader implements ViaPlatformLoader { ClassGenerator.registerPSConnectListener(plugin); /* 1.9 client to 1.8 server */ - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { storeListener(new ArmorListener(plugin)).register(); storeListener(new DeathListener(plugin)).register(); storeListener(new BlockListener(plugin)).register(); @@ -99,8 +98,8 @@ public class BukkitViaLoader implements ViaPlatformLoader { } } - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_14.getVersion()) { - boolean use1_9Fix = plugin.getConf().is1_9HitboxFix() && ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion(); + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_14.getVersion()) { + boolean use1_9Fix = plugin.getConf().is1_9HitboxFix() && Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion(); if (use1_9Fix || plugin.getConf().is1_14HitboxFix()) { try { storeListener(new PlayerSneakListener(plugin, use1_9Fix, plugin.getConf().is1_14HitboxFix())).register(); @@ -111,7 +110,7 @@ public class BukkitViaLoader implements ViaPlatformLoader { } } - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_15.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_15.getVersion()) { try { Class.forName("org.bukkit.event.entity.EntityToggleGlideEvent"); storeListener(new EntityToggleGlideListener(plugin)).register(); @@ -122,13 +121,13 @@ public class BukkitViaLoader implements ViaPlatformLoader { if ((Bukkit.getVersion().toLowerCase(Locale.ROOT).contains("paper") || Bukkit.getVersion().toLowerCase(Locale.ROOT).contains("taco") || Bukkit.getVersion().toLowerCase(Locale.ROOT).contains("torch")) - && ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_12.getVersion()) { + && Via.getAPI().getServerVersion() < ProtocolVersion.v1_12.getVersion()) { plugin.getLogger().info("Enabling Paper/TacoSpigot/Torch patch: Fixes block placement."); storeListener(new PaperPatch(plugin)).register(); } /* Providers */ - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { Via.getManager().getProviders().use(BulkChunkTranslatorProvider.class, new BukkitViaBulkChunkTranslator()); Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BukkitViaMovementTransmitter()); @@ -157,12 +156,12 @@ public class BukkitViaLoader implements ViaPlatformLoader { }); } - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_12.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_12.getVersion()) { if (plugin.getConf().is1_12QuickMoveActionFix()) { Via.getManager().getProviders().use(InventoryQuickMoveProvider.class, new BukkitInventoryQuickMoveProvider()); } } - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_13.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_13.getVersion()) { if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("world")) { BukkitBlockConnectionProvider blockConnectionProvider = new BukkitBlockConnectionProvider(); Via.getManager().getProviders().use(BlockConnectionProvider.class, blockConnectionProvider); diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitInventoryQuickMoveProvider.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitInventoryQuickMoveProvider.java index ab771ad15..f11722892 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitInventoryQuickMoveProvider.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitInventoryQuickMoveProvider.java @@ -24,7 +24,6 @@ import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.bukkit.tasks.protocol1_12to1_11_1.BukkitInventoryUpdateTask; import us.myles.ViaVersion.bukkit.util.NMSUtil; @@ -71,7 +70,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider // windowId is always 0 for player inventory. // This has almost definitely something to do with the offhand slot. if (slotId >= 36 && slotId <= 44) { - int protocolId = ProtocolRegistry.SERVER_PROTOCOL; + int protocolId = Via.getAPI().getServerVersion(); // this seems to be working just fine. if (protocolId == ProtocolVersion.v1_8.getVersion()) { return false; @@ -103,7 +102,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider Inventory tinv = inv.getTopInventory(); InventoryType tinvtype = tinv == null ? null : tinv.getType(); // can this even be null? if (tinvtype != null) { - int protocolId = ProtocolRegistry.SERVER_PROTOCOL; + int protocolId = Via.getAPI().getServerVersion(); if (protocolId == ProtocolVersion.v1_8.getVersion()) { if (tinvtype == InventoryType.BREWING) { // 1.9 added the blaze powder slot to brewing stand fix for 1.8 servers @@ -132,7 +131,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider ReflectionUtil.set(packet, "button", 0); // shift + left mouse click ReflectionUtil.set(packet, "d", storage.getActionId()); ReflectionUtil.set(packet, "item", nmsItem); - int protocolId = ProtocolRegistry.SERVER_PROTOCOL; + int protocolId = Via.getAPI().getServerVersion(); if (protocolId == ProtocolVersion.v1_8.getVersion()) { ReflectionUtil.set(packet, "shift", 1); } else if (protocolId >= ProtocolVersion.v1_9.getVersion()) { // 1.9+ @@ -171,7 +170,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider } try { this.windowClickPacketClass = NMSUtil.nms("PacketPlayInWindowClick"); - int protocolId = ProtocolRegistry.SERVER_PROTOCOL; + int protocolId = Via.getAPI().getServerVersion(); if (protocolId >= ProtocolVersion.v1_9.getVersion()) { Class eclassz = NMSUtil.nms("InventoryClickType"); Object[] constants = eclassz.getEnumConstants(); @@ -200,7 +199,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider } private boolean isSupported() { - int protocolId = ProtocolRegistry.SERVER_PROTOCOL; + int protocolId = Via.getAPI().getServerVersion(); if (protocolId >= ProtocolVersion.v1_8.getVersion() && protocolId <= ProtocolVersion.v1_11_1.getVersion()) { return true; // 1.8-1.11.2 } diff --git a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java index f1b3ef438..8cfc5f047 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java +++ b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java @@ -29,7 +29,6 @@ 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.platform.TaskId; -import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.bungee.commands.BungeeCommand; import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler; @@ -51,7 +50,6 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; public class BungeePlugin extends Plugin implements ViaPlatform, Listener { - private final ViaConnectionManager connectionManager = new ViaConnectionManager(); private BungeeViaAPI api; private BungeeViaConfig config; @@ -216,9 +214,4 @@ public class BungeePlugin extends Plugin implements ViaPlatform, public boolean isOldClientsAllowed() { return true; } - - @Override - public ViaConnectionManager getConnectionManager() { - return connectionManager; - } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java index 285e81994..fae6f112f 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java @@ -33,7 +33,6 @@ import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; @@ -86,14 +85,14 @@ public class BungeeServerHandler implements Listener { return; } - UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId()); + UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); if (user == null) return; if (!user.has(BungeeStorage.class)) { user.put(new BungeeStorage(user, e.getPlayer())); } int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName()); - List> protocols = ProtocolRegistry.getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId); + List> protocols = Via.getManager().getProtocolManager().getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId); // Check if ViaVersion can support that version try { @@ -108,7 +107,7 @@ public class BungeeServerHandler implements Listener { @EventHandler(priority = -120) public void onServerConnected(ServerConnectedEvent e) { try { - checkServerChange(e, Via.getManager().getConnection(e.getPlayer().getUniqueId())); + checkServerChange(e, Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId())); } catch (Exception e1) { e1.printStackTrace(); } @@ -117,7 +116,7 @@ public class BungeeServerHandler implements Listener { @EventHandler(priority = -120) public void onServerSwitch(ServerSwitchEvent e) { // Update entity id - UserConnection userConnection = Via.getManager().getConnection(e.getPlayer().getUniqueId()); + UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); if (userConnection == null) return; int playerId; try { @@ -176,7 +175,7 @@ public class BungeeServerHandler implements Listener { int previousServerProtocol = info.getServerProtocolVersion(); // Refresh the pipes - List> protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocolId); + List> protocols = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); user.clearStoredObjects(); pipeline.cleanPipes(); @@ -191,7 +190,7 @@ public class BungeeServerHandler implements Listener { info.setServerProtocolVersion(protocolId); // Add version-specific base Protocol - pipeline.add(ProtocolRegistry.getBaseProtocol(protocolId)); + pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId)); // Workaround 1.13 server change Object relayMessages = getRelayMessages.invoke(e.getPlayer().getPendingConnection()); diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java index 01ed33918..d6b09744f 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java @@ -40,7 +40,7 @@ public class ElytraPatch implements Listener { @EventHandler(priority = EventPriority.HIGH) public void onServerConnected(ServerConnectedEvent event) { - UserConnection user = Via.getManager().getConnection(event.getPlayer().getUniqueId()); + UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId()); if (user == null) return; try { diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java index 85a380977..ab62220cb 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java @@ -23,12 +23,15 @@ import net.md_5.bungee.api.scheduler.ScheduledTask; import us.myles.ViaVersion.BungeePlugin; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.bungee.handlers.BungeeServerHandler; import us.myles.ViaVersion.bungee.listeners.ElytraPatch; import us.myles.ViaVersion.bungee.listeners.UpdateListener; -import us.myles.ViaVersion.bungee.providers.*; +import us.myles.ViaVersion.bungee.providers.BungeeBossBarProvider; +import us.myles.ViaVersion.bungee.providers.BungeeEntityIdProvider; +import us.myles.ViaVersion.bungee.providers.BungeeMainHandProvider; +import us.myles.ViaVersion.bungee.providers.BungeeMovementTransmitter; +import us.myles.ViaVersion.bungee.providers.BungeeVersionProvider; import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider; @@ -62,7 +65,7 @@ public class BungeeViaLoader implements ViaPlatformLoader { registerListener(new UpdateListener()); registerListener(new BungeeServerHandler()); - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { registerListener(new ElytraPatch()); } @@ -70,7 +73,7 @@ public class BungeeViaLoader implements ViaPlatformLoader { Via.getManager().getProviders().use(VersionProvider.class, new BungeeVersionProvider()); Via.getManager().getProviders().use(EntityIdProvider.class, new BungeeEntityIdProvider()); - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter()); Via.getManager().getProviders().use(BossBarProvider.class, new BungeeBossBarProvider()); Via.getManager().getProviders().use(MainHandProvider.class, new BungeeMainHandProvider()); diff --git a/common/src/main/java/us/myles/ViaVersion/ViaManagerImpl.java b/common/src/main/java/us/myles/ViaVersion/ViaManagerImpl.java index 13677d163..f0aca6e36 100644 --- a/common/src/main/java/us/myles/ViaVersion/ViaManagerImpl.java +++ b/common/src/main/java/us/myles/ViaVersion/ViaManagerImpl.java @@ -17,17 +17,15 @@ */ package us.myles.ViaVersion; -import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.api.platform.providers.ViaProviders; -import us.myles.ViaVersion.api.protocol.Protocol; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; +import us.myles.ViaVersion.api.protocol.ProtocolManager; +import us.myles.ViaVersion.api.protocol.ProtocolManagerImpl; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.commands.ViaCommandHandler; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.TabCompleteThread; @@ -37,14 +35,13 @@ import us.myles.ViaVersion.update.UpdateUtil; import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.UUID; public class ViaManagerImpl implements ViaManager { - private final ViaPlatform platform; + private final ProtocolManagerImpl protocolManager = new ProtocolManagerImpl(); + private final ViaConnectionManager connectionManager = new ViaConnectionManager(); private final ViaProviders providers = new ViaProviders(); - // Internals + private final ViaPlatform platform; private final ViaInjector injector; private final ViaCommandHandler commandHandler; private final ViaPlatformLoader loader; @@ -74,8 +71,8 @@ public class ViaManagerImpl implements ViaManager { UpdateUtil.sendUpdateMessage(); } - // Force class load - ProtocolRegistry.init(); + // Register protocols + protocolManager.registerProtocols(); // Inject try { @@ -101,22 +98,22 @@ public class ViaManagerImpl implements ViaManager { public void onServerLoaded() { // Load Server Protocol try { - ProtocolRegistry.SERVER_PROTOCOL = ProtocolVersion.getProtocol(injector.getServerProtocolVersion()).getVersion(); + protocolManager.setServerProtocol(ProtocolVersion.getProtocol(injector.getServerProtocolVersion()).getVersion()); } catch (Exception e) { platform.getLogger().severe("ViaVersion failed to get the server protocol!"); e.printStackTrace(); } // Check if there are any pipes to this version - if (ProtocolRegistry.SERVER_PROTOCOL != -1) { - platform.getLogger().info("ViaVersion detected server version: " + ProtocolVersion.getProtocol(ProtocolRegistry.SERVER_PROTOCOL)); - if (!ProtocolRegistry.isWorkingPipe() && !platform.isProxy()) { + if (protocolManager.getServerProtocol() != -1) { + platform.getLogger().info("ViaVersion detected server version: " + ProtocolVersion.getProtocol(protocolManager.getServerProtocol())); + if (!protocolManager.isWorkingPipe() && !platform.isProxy()) { platform.getLogger().warning("ViaVersion does not have any compatible versions for this server version!"); platform.getLogger().warning("Please remember that ViaVersion only adds support for versions newer than the server version."); platform.getLogger().warning("If you need support for older versions you may need to use one or more ViaVersion addons too."); platform.getLogger().warning("In that case please read the ViaVersion resource page carefully or use https://jo0001.github.io/ViaSetup"); platform.getLogger().warning("and if you're still unsure, feel free to join our Discord-Server for further assistance."); - } else if (ProtocolRegistry.SERVER_PROTOCOL <= ProtocolVersion.v1_12_2.getVersion() && !platform.isProxy()) { + } else if (protocolManager.getServerProtocol() <= ProtocolVersion.v1_12_2.getVersion() && !platform.isProxy()) { platform.getLogger().warning("This version of Minecraft is extremely outdated and support for it has reached its end of life. " + "You will still be able to run Via on this version, but we are unlikely to provide any further fixes or help with problems specific to legacy versions. " + "Please consider updating to give your players a better experience and to avoid issues that have long been fixed."); @@ -124,30 +121,30 @@ public class ViaManagerImpl implements ViaManager { } // Load Listeners / Tasks - ProtocolRegistry.onServerLoaded(); + protocolManager.onServerLoaded(); // Load Platform loader.load(); // Common tasks mappingLoadingTask = Via.getPlatform().runRepeatingSync(() -> { - if (ProtocolRegistry.checkForMappingCompletion()) { + if (protocolManager.checkForMappingCompletion()) { platform.cancelTask(mappingLoadingTask); mappingLoadingTask = null; } }, 10L); - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (protocolManager.getServerProtocol() < ProtocolVersion.v1_9.getVersion()) { if (Via.getConfig().isSimulatePlayerTick()) { Via.getPlatform().runRepeatingSync(new ViaIdleThread(), 1L); } } - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_13.getVersion()) { + if (protocolManager.getServerProtocol() < ProtocolVersion.v1_13.getVersion()) { if (Via.getConfig().get1_13TabCompleteDelay() > 0) { Via.getPlatform().runRepeatingSync(new TabCompleteThread(), 1L); } } // Refresh Versions - ProtocolRegistry.refreshVersions(); + protocolManager.refreshVersions(); } public void destroy() { @@ -164,61 +161,47 @@ public class ViaManagerImpl implements ViaManager { loader.unload(); } - public Set getConnections() { - return platform.getConnectionManager().getConnections(); - } - - /** - * @deprecated use getConnectedClients() - */ - @Deprecated - public Map getPortedPlayers() { - return getConnectedClients(); - } - - public Map getConnectedClients() { - return platform.getConnectionManager().getConnectedClients(); - } - - public UUID getConnectedClientId(UserConnection conn) { - return platform.getConnectionManager().getConnectedClientId(conn); - } - - /** - * @see ViaConnectionManager#isClientConnected(UUID) - */ - public boolean isClientConnected(UUID player) { - return platform.getConnectionManager().isClientConnected(player); - } - - public void handleLoginSuccess(UserConnection info) { - platform.getConnectionManager().onLoginSuccess(info); - } - + @Override public ViaPlatform getPlatform() { return platform; } + @Override + public ViaConnectionManager getConnectionManager() { + return connectionManager; + } + + @Override + public ProtocolManager getProtocolManager() { + return protocolManager; + } + + @Override public ViaProviders getProviders() { return providers; } + @Override public boolean isDebug() { return debug; } + @Override public void setDebug(boolean debug) { this.debug = debug; } + @Override public ViaInjector getInjector() { return injector; } + @Override public ViaCommandHandler getCommandHandler() { return commandHandler; } + @Override public ViaPlatformLoader getLoader() { return loader; } @@ -233,14 +216,6 @@ public class ViaManagerImpl implements ViaManager { return subPlatforms; } - /** - * @see ViaConnectionManager#getConnectedClient(UUID) - */ - @Nullable - public UserConnection getConnection(UUID playerUUID) { - return platform.getConnectionManager().getConnectedClient(playerUUID); - } - /** * Adds a runnable to be executed when ViaVersion has finished its init before the full server load. * @@ -250,16 +225,6 @@ public class ViaManagerImpl implements ViaManager { enableListeners.add(runnable); } - @Override - public Protocol getBaseProtocol() { - return ProtocolRegistry.BASE_PROTOCOL; - } - - @Override - public boolean isBaseProtocol(Protocol protocol) { - return ProtocolRegistry.isBaseProtocol(protocol); - } - public static final class ViaManagerBuilder { private ViaPlatform platform; private ViaInjector injector; diff --git a/common/src/main/java/us/myles/ViaVersion/api/ViaAPIBase.java b/common/src/main/java/us/myles/ViaVersion/api/ViaAPIBase.java index 5b6a54dfc..b49f45a4f 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/ViaAPIBase.java +++ b/common/src/main/java/us/myles/ViaVersion/api/ViaAPIBase.java @@ -19,7 +19,6 @@ package us.myles.ViaVersion.api; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import java.util.SortedSet; import java.util.TreeSet; @@ -29,12 +28,12 @@ public abstract class ViaAPIBase implements ViaAPI { @Override public int getServerVersion() { - return ProtocolRegistry.SERVER_PROTOCOL; + return Via.getManager().getProtocolManager().getServerProtocol(); } @Override public int getPlayerVersion(UUID uuid) { - UserConnection connection = Via.getManager().getConnection(uuid); + UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid); return connection != null ? connection.getProtocolInfo().getProtocolVersion() : -1; } @@ -45,7 +44,7 @@ public abstract class ViaAPIBase implements ViaAPI { @Override public boolean isInjected(UUID playerUUID) { - return Via.getManager().isClientConnected(playerUUID); + return Via.getManager().getConnectionManager().isClientConnected(playerUUID); } @Override @@ -54,19 +53,19 @@ public abstract class ViaAPIBase implements ViaAPI { throw new IllegalArgumentException("This player is not controlled by ViaVersion!"); } - UserConnection user = Via.getManager().getConnection(uuid); + UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(uuid); user.sendRawPacket(packet); } @Override public SortedSet getSupportedVersions() { - SortedSet outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions()); + SortedSet outputSet = new TreeSet<>(Via.getManager().getProtocolManager().getSupportedVersions()); outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols()); return outputSet; } @Override public SortedSet getFullSupportedVersions() { - return ProtocolRegistry.getSupportedVersions(); + return Via.getManager().getProtocolManager().getSupportedVersions(); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java b/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java index e64f9fdc8..713d4b6b2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java +++ b/common/src/main/java/us/myles/ViaVersion/api/ViaListener.java @@ -39,7 +39,7 @@ public abstract class ViaListener { */ @Nullable protected UserConnection getUserConnection(UUID uuid) { - return Via.getManager().getConnection(uuid); + return Via.getManager().getConnectionManager().getConnectedClient(uuid); } /** diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManagerImpl.java similarity index 70% rename from common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java rename to common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManagerImpl.java index 85581d5a4..b56520187 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolManagerImpl.java @@ -79,28 +79,32 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Function; -public class ProtocolRegistry { +public class ProtocolManagerImpl implements ProtocolManager { public static final Protocol BASE_PROTOCOL = new BaseProtocol(); - public static int SERVER_PROTOCOL = -1; - public static int maxProtocolPathSize = 50; + // Input Version -> Output Version & Protocol (Allows fast lookup) - private static final Int2ObjectMap> registryMap = new Int2ObjectOpenHashMap<>(32); - private static final Map, Protocol> protocols = new HashMap<>(); - private static final Map, List>> pathCache = new ConcurrentHashMap<>(); - private static final Set supportedVersions = new HashSet<>(); - private static final List, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList(); - private static final List registerList = new ArrayList<>(); + private final Int2ObjectMap> registryMap = new Int2ObjectOpenHashMap<>(32); + private final Map, Protocol> protocols = new HashMap<>(); + private final Map, List>> pathCache = new ConcurrentHashMap<>(); + private final Set supportedVersions = new HashSet<>(); + private final List, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList(); + private final List registerList = new ArrayList<>(); - private static final ReadWriteLock MAPPING_LOADER_LOCK = new ReentrantReadWriteLock(); - private static Map, CompletableFuture> mappingLoaderFutures = new HashMap<>(); - private static ThreadPoolExecutor mappingLoaderExecutor; - private static boolean mappingsLoaded; + private final ReadWriteLock mappingLoaderLock = new ReentrantReadWriteLock(); + private Map, CompletableFuture> mappingLoaderFutures = new HashMap<>(); + private ThreadPoolExecutor mappingLoaderExecutor; + private boolean mappingsLoaded; - static { + private int maxProtocolPathSize = 50; + private int serverProtocol = -1; + + public ProtocolManagerImpl() { ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Via-Mappingloader-%d").build(); mappingLoaderExecutor = new ThreadPoolExecutor(5, 16, 45L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory); mappingLoaderExecutor.allowCoreThreadTimeOut(true); + } + public void registerProtocols() { // Base Protocol registerBaseProtocol(BASE_PROTOCOL, Range.lessThan(Integer.MIN_VALUE)); registerBaseProtocol(new BaseProtocol1_7(), Range.lessThan(ProtocolVersion.v1_16.getVersion())); @@ -144,29 +148,13 @@ public class ProtocolRegistry { registerProtocol(new Protocol1_17To1_16_4(), ProtocolVersion.v1_17, ProtocolVersion.v1_16_4); } - public static void init() { - // Empty method to trigger static initializer once - } - - /** - * Register a protocol - * - * @param protocol The protocol to register. - * @param supported Supported client versions. - * @param output The output server version it converts to. - */ - public static void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output) { + @Override + public void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output) { registerProtocol(protocol, Collections.singletonList(supported.getVersion()), output.getVersion()); } - /** - * Register a protocol - * - * @param protocol The protocol to register. - * @param supported Supported client versions. - * @param output The output server version it converts to. - */ - public static void registerProtocol(Protocol protocol, List supported, int output) { + @Override + public void registerProtocol(Protocol protocol, List supported, int output) { // Clear cache as this may make new routes. if (!pathCache.isEmpty()) { pathCache.clear(); @@ -197,15 +185,8 @@ public class ProtocolRegistry { } } - /** - * Registers a base protocol. - * Base Protocols registered later have higher priority - * Only one base protocol will be added to pipeline - * - * @param baseProtocol Base Protocol to register - * @param supportedProtocols Versions that baseProtocol supports - */ - public static void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { + @Override + public void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { baseProtocols.add(new Pair<>(supportedProtocols, baseProtocol)); if (Via.getPlatform().isPluginEnabled()) { baseProtocol.register(Via.getManager().getProviders()); @@ -215,12 +196,12 @@ public class ProtocolRegistry { } } - public static void refreshVersions() { + public void refreshVersions() { supportedVersions.clear(); - supportedVersions.add(ProtocolRegistry.SERVER_PROTOCOL); + supportedVersions.add(serverProtocol); for (ProtocolVersion versions : ProtocolVersion.getProtocols()) { - List> paths = getProtocolPath(versions.getVersion(), ProtocolRegistry.SERVER_PROTOCOL); + List> paths = getProtocolPath(versions.getVersion(), serverProtocol); if (paths == null) continue; supportedVersions.add(versions.getVersion()); for (Pair path : paths) { @@ -229,47 +210,34 @@ public class ProtocolRegistry { } } - /** - * Get the versions compatible with the server. - * - * @return Read-only set of the versions. - */ - public static SortedSet getSupportedVersions() { - return Collections.unmodifiableSortedSet(new TreeSet<>(supportedVersions)); - } - - /** - * Check if this plugin is useful to the server. - * - * @return True if there is a useful pipe - */ - public static boolean isWorkingPipe() { - for (Int2ObjectMap map : registryMap.values()) { - if (map.containsKey(SERVER_PROTOCOL)) return true; + @Nullable + @Override + public List> getProtocolPath(int clientVersion, int serverVersion) { + Pair protocolKey = new Pair<>(clientVersion, serverVersion); + // Check cache + List> protocolList = pathCache.get(protocolKey); + if (protocolList != null) { + return protocolList; } - return false; // No destination for protocol - } - - /** - * Called when the server is enabled, to register any non registered listeners. - */ - public static void onServerLoaded() { - for (Protocol protocol : registerList) { - protocol.register(Via.getManager().getProviders()); + // Generate path + List> outputPath = getProtocolPath(new ArrayList<>(), clientVersion, serverVersion); + // If it found a path, cache it. + if (outputPath != null) { + pathCache.put(protocolKey, outputPath); } - registerList.clear(); + return outputPath; } /** - * Calculate a path to get from an input protocol to the servers protocol. + * Calculates a path to get from an input protocol to the server's protocol. * - * @param current The current items in the path - * @param clientVersion The current input version - * @param serverVersion The desired output version - * @return The path which has been generated, null if failed. + * @param current current items in the path + * @param clientVersion current input version + * @param serverVersion desired output version + * @return path that has been generated, null if failed */ @Nullable - private static List> getProtocolPath(List> current, int clientVersion, int serverVersion) { + private List> getProtocolPath(List> current, int clientVersion, int serverVersion) { if (clientVersion == serverVersion) return null; // We're already there if (current.size() > maxProtocolPathSize) return null; // Fail safe, protocol too complicated. @@ -312,42 +280,14 @@ public class ProtocolRegistry { return shortest; // null if none found } - /** - * Calculate a path from a client version to server version. - * - * @param clientVersion The input client version - * @param serverVersion The desired output server version - * @return The path it generated, null if it failed. - */ @Nullable - public static List> getProtocolPath(int clientVersion, int serverVersion) { - Pair protocolKey = new Pair<>(clientVersion, serverVersion); - // Check cache - List> protocolList = pathCache.get(protocolKey); - if (protocolList != null) { - return protocolList; - } - // Generate path - List> outputPath = getProtocolPath(new ArrayList<>(), clientVersion, serverVersion); - // If it found a path, cache it. - if (outputPath != null) { - pathCache.put(protocolKey, outputPath); - } - return outputPath; - } - - /** - * Returns a protocol instance by its class. - * - * @param protocolClass class of the protocol - * @return protocol if present - */ - @Nullable - public static Protocol getProtocol(Class protocolClass) { + @Override + public Protocol getProtocol(Class protocolClass) { return protocols.get(protocolClass); } - public static Protocol getBaseProtocol(int serverVersion) { + @Override + public Protocol getBaseProtocol(int serverVersion) { for (Pair, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) { if (rangeProtocol.getKey().contains(serverVersion)) { return rangeProtocol.getValue(); @@ -356,7 +296,8 @@ public class ProtocolRegistry { throw new IllegalStateException("No Base Protocol for " + serverVersion); } - public static boolean isBaseProtocol(Protocol protocol) { + @Override + public boolean isBaseProtocol(Protocol protocol) { for (Pair, Protocol> p : baseProtocols) { if (p.getValue() == protocol) { return true; @@ -365,12 +306,46 @@ public class ProtocolRegistry { return false; } - /** - * Ensure that mapping data for that protocol has already been loaded, completes it otherwise. - * - * @param protocolClass protocol class - */ - public static void completeMappingDataLoading(Class protocolClass) throws Exception { + @Override + public int getServerProtocol() { + return serverProtocol; + } + + public void setServerProtocol(int serverProtocol) { + this.serverProtocol = serverProtocol; + ProtocolRegistry.SERVER_PROTOCOL = serverProtocol; + } + + @Override + public boolean isWorkingPipe() { + for (Int2ObjectMap map : registryMap.values()) { + if (map.containsKey(serverProtocol)) return true; + } + return false; // No destination for protocol + } + + @Override + public SortedSet getSupportedVersions() { + return Collections.unmodifiableSortedSet(new TreeSet<>(supportedVersions)); + } + + @Override + public int getMaxProtocolPathSize() { + return maxProtocolPathSize; + } + + @Override + public void setMaxProtocolPathSize(int maxProtocolPathSize) { + this.maxProtocolPathSize = maxProtocolPathSize; + } + + @Override + public Protocol getBaseProtocol() { + return BASE_PROTOCOL; + } + + @Override + public void completeMappingDataLoading(Class protocolClass) throws Exception { if (mappingsLoaded) return; CompletableFuture future = getMappingLoaderFuture(protocolClass); @@ -380,13 +355,9 @@ public class ProtocolRegistry { } } - /** - * Shuts down the executor and uncaches mappings if all futures have been completed. - * - * @return true if the executor has now been shut down - */ - public static boolean checkForMappingCompletion() { - MAPPING_LOADER_LOCK.readLock().lock(); + @Override + public boolean checkForMappingCompletion() { + mappingLoaderLock.readLock().lock(); try { if (mappingsLoaded) return false; @@ -400,66 +371,57 @@ public class ProtocolRegistry { shutdownLoaderExecutor(); return true; } finally { - MAPPING_LOADER_LOCK.readLock().unlock(); + mappingLoaderLock.readLock().unlock(); } } - /** - * Executes the given runnable asynchronously, adding a {@link CompletableFuture} - * to the list of data to load bound to their protocols. - * - * @param protocolClass protocol class - * @param runnable runnable to be executed asynchronously - */ - public static void addMappingLoaderFuture(Class protocolClass, Runnable runnable) { + @Override + public void addMappingLoaderFuture(Class protocolClass, Runnable runnable) { CompletableFuture future = CompletableFuture.runAsync(runnable, mappingLoaderExecutor).exceptionally(mappingLoaderThrowable(protocolClass)); - MAPPING_LOADER_LOCK.writeLock().lock(); + mappingLoaderLock.writeLock().lock(); try { mappingLoaderFutures.put(protocolClass, future); } finally { - MAPPING_LOADER_LOCK.writeLock().unlock(); + mappingLoaderLock.writeLock().unlock(); } } - /** - * Executes the given runnable asynchronously after the other protocol has finished its data loading, - * adding a {@link CompletableFuture} to the list of data to load bound to their protocols. - * - * @param protocolClass protocol class - * @param dependsOn class of the protocol that the data loading depends on - * @param runnable runnable to be executed asynchronously - */ - public static void addMappingLoaderFuture(Class protocolClass, Class dependsOn, Runnable runnable) { + @Override + public void addMappingLoaderFuture(Class protocolClass, Class dependsOn, Runnable runnable) { CompletableFuture future = getMappingLoaderFuture(dependsOn) .whenCompleteAsync((v, throwable) -> runnable.run(), mappingLoaderExecutor).exceptionally(mappingLoaderThrowable(protocolClass)); - MAPPING_LOADER_LOCK.writeLock().lock(); + mappingLoaderLock.writeLock().lock(); try { mappingLoaderFutures.put(protocolClass, future); } finally { - MAPPING_LOADER_LOCK.writeLock().unlock(); + mappingLoaderLock.writeLock().unlock(); + } + } + + @Nullable + @Override + public CompletableFuture getMappingLoaderFuture(Class protocolClass) { + mappingLoaderLock.readLock().lock(); + try { + return mappingsLoaded ? null : mappingLoaderFutures.get(protocolClass); + } finally { + mappingLoaderLock.readLock().unlock(); } } /** - * Returns the data loading future bound to the protocol, or null if all loading is complete. - * The future may or may not have already been completed. - * - * @param protocolClass protocol class - * @return data loading future bound to the protocol, or null if all loading is complete + * Called when the server is enabled, to register any non-registered listeners. */ - @Nullable - public static CompletableFuture getMappingLoaderFuture(Class protocolClass) { - MAPPING_LOADER_LOCK.readLock().lock(); - try { - return mappingsLoaded ? null : mappingLoaderFutures.get(protocolClass); - } finally { - MAPPING_LOADER_LOCK.readLock().unlock(); + public void onServerLoaded() { + for (Protocol protocol : registerList) { + protocol.register(Via.getManager().getProviders()); } + registerList.clear(); } - private static void shutdownLoaderExecutor() { + private void shutdownLoaderExecutor() { Preconditions.checkArgument(!mappingsLoaded); Via.getPlatform().getLogger().info("Finished mapping loading, shutting down loader executor!"); @@ -473,7 +435,7 @@ public class ProtocolRegistry { } } - private static Function mappingLoaderThrowable(Class protocolClass) { + private Function mappingLoaderThrowable(Class protocolClass) { return throwable -> { Via.getPlatform().getLogger().severe("Error during mapping loading of " + protocolClass.getSimpleName()); throwable.printStackTrace(); diff --git a/common/src/main/java/us/myles/ViaVersion/boss/CommonBoss.java b/common/src/main/java/us/myles/ViaVersion/boss/CommonBoss.java index ab060b030..ad313e518 100644 --- a/common/src/main/java/us/myles/ViaVersion/boss/CommonBoss.java +++ b/common/src/main/java/us/myles/ViaVersion/boss/CommonBoss.java @@ -94,7 +94,7 @@ public abstract class CommonBoss extends BossBar { @Override public BossBar addPlayer(UUID player) { - return addConnection(Via.getManager().getConnection(player)); + return addConnection(Via.getManager().getConnectionManager().getConnectedClient(player)); } @Override @@ -107,7 +107,7 @@ public abstract class CommonBoss extends BossBar { @Override public BossBar removePlayer(UUID uuid) { - return removeConnection(Via.getManager().getConnection(uuid)); + return removeConnection(Via.getManager().getConnectionManager().getConnectedClient(uuid)); } @Override @@ -144,7 +144,7 @@ public abstract class CommonBoss extends BossBar { @Override public Set getPlayers() { - return connections.stream().map(conn -> Via.getManager().getConnectedClientId(conn)).filter(Objects::nonNull) + return connections.stream().map(conn -> Via.getManager().getConnectionManager().getConnectedClientId(conn)).filter(Objects::nonNull) .collect(Collectors.toSet()); } diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java index 1c5762e24..a36b91ae8 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java @@ -22,7 +22,6 @@ import com.google.gson.JsonObject; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaSubCommand; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.dump.DumpTemplate; import us.myles.ViaVersion.dump.VersionInfo; import us.myles.ViaVersion.util.GsonUtil; @@ -54,8 +53,8 @@ public class DumpSubCmd extends ViaSubCommand { VersionInfo version = new VersionInfo( System.getProperty("java.version"), System.getProperty("os.name"), - ProtocolRegistry.SERVER_PROTOCOL, - ProtocolRegistry.getSupportedVersions(), + Via.getAPI().getServerVersion(), + Via.getManager().getProtocolManager().getSupportedVersions(), Via.getPlatform().getPlatformName(), Via.getPlatform().getPlatformVersion(), Via.getPlatform().getPluginVersion(), diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/PPSSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/PPSSubCmd.java index 1d210f999..f18b50f49 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/PPSSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/PPSSubCmd.java @@ -52,7 +52,7 @@ public class PPSSubCmd extends ViaSubCommand { int playerVersion = Via.getAPI().getPlayerVersion(p.getUUID()); if (!playerVersions.containsKey(playerVersion)) playerVersions.put(playerVersion, new HashSet()); - UserConnection uc = Via.getManager().getConnection(p.getUUID()); + UserConnection uc = Via.getManager().getConnectionManager().getConnectedClient(p.getUUID()); if (uc != null && uc.getPacketsPerSecond() > -1) { playerVersions.get(playerVersion).add(p.getName() + " (" + uc.getPacketsPerSecond() + " PPS)"); totalPackets += uc.getPacketsPerSecond(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java index e53780117..76c80c2af 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java @@ -24,7 +24,6 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.SimpleProtocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -65,7 +64,7 @@ public class BaseProtocol extends SimpleProtocol { // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it) if (info.getProtocolVersion() >= serverProtocol || Via.getPlatform().isOldClientsAllowed()) { - protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), serverProtocol); + protocols = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), serverProtocol); } ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline(); @@ -73,7 +72,7 @@ public class BaseProtocol extends SimpleProtocol { for (Pair prot : protocols) { pipeline.add(prot.getValue()); // Ensure mapping data has already been loaded - ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass()); + Via.getManager().getProtocolManager().completeMappingDataLoading(prot.getValue().getClass()); } // Set the original snapshot version if present @@ -82,7 +81,7 @@ public class BaseProtocol extends SimpleProtocol { } // Add Base Protocol - pipeline.add(ProtocolRegistry.getBaseProtocol(serverProtocol)); + pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(serverProtocol)); // Change state if (state == 1) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java index 288b7d231..695cbfddd 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java @@ -26,7 +26,7 @@ import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.protocol.Protocol; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; +import us.myles.ViaVersion.api.protocol.ProtocolManagerImpl; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.SimpleProtocol; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -81,8 +81,9 @@ public class BaseProtocol1_7 extends SimpleProtocol { version.add("supportedVersions", GsonUtil.getGson().toJsonTree(Via.getAPI().getSupportedVersions())); } - if (ProtocolRegistry.SERVER_PROTOCOL == -1) { // Set the Server protocol if the detection on startup failed - ProtocolRegistry.SERVER_PROTOCOL = ProtocolVersion.getProtocol(protocolVersion).getVersion(); + if (Via.getAPI().getServerVersion() == -1) { // Set the Server protocol if the detection on startup failed + ProtocolManagerImpl protocolManager = (ProtocolManagerImpl) Via.getManager().getProtocolManager(); + protocolManager.setServerProtocol(ProtocolVersion.getProtocol(protocolVersion).getVersion()); } // Ensure the server has a version provider @@ -97,7 +98,7 @@ public class BaseProtocol1_7 extends SimpleProtocol { // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it) if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) { - protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol); + protocols = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocol); } if (protocols != null) { @@ -144,9 +145,9 @@ public class BaseProtocol1_7 extends SimpleProtocol { String username = wrapper.passthrough(Type.STRING); info.setUsername(username); // Add to ported clients - Via.getManager().handleLoginSuccess(wrapper.user()); + Via.getManager().getConnectionManager().onLoginSuccess(wrapper.user()); - if (info.getPipeline().pipes().stream().allMatch(ProtocolRegistry::isBaseProtocol)) { // Only base protocol + if (info.getPipeline().pipes().stream().allMatch(Via.getManager().getProtocolManager()::isBaseProtocol)) { // Only base protocol wrapper.user().setActive(false); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseVersionProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseVersionProvider.java index e2f511c24..67d6d6613 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseVersionProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseVersionProvider.java @@ -17,13 +17,12 @@ */ package us.myles.ViaVersion.protocols.base; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.platform.providers.Provider; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; public class BaseVersionProvider implements VersionProvider { public int getServerProtocol(UserConnection connection) throws Exception { - return ProtocolRegistry.SERVER_PROTOCOL; + return Via.getAPI().getServerVersion(); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java index 56e088ddc..961055395 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/TabCompleteThread.java @@ -24,7 +24,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().getConnections()) { + for (UserConnection info : Via.getManager().getConnectionManager().getConnections()) { if (info.getProtocolInfo() == null) continue; if (info.getProtocolInfo().getPipeline().contains(Protocol1_13To1_12_2.class)) { if (info.getChannel().isOpen()) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ViaIdleThread.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ViaIdleThread.java index 0d405b4fd..2dfe49e25 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ViaIdleThread.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ViaIdleThread.java @@ -27,7 +27,7 @@ public class ViaIdleThread implements Runnable { @Override public void run() { - for (UserConnection info : Via.getManager().getConnections()) { + for (UserConnection info : Via.getManager().getConnectionManager().getConnections()) { ProtocolInfo protocolInfo = info.getProtocolInfo(); if (protocolInfo == null || !protocolInfo.getPipeline().contains(Protocol1_9To1_8.class)) continue; diff --git a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java index 565fad0db..8ccc62143 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java +++ b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java @@ -35,7 +35,6 @@ 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.platform.TaskId; -import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.sponge.commands.SpongeCommandHandler; @@ -72,7 +71,6 @@ public class SpongePlugin implements ViaPlatform { private File spongeConfig; public static final LegacyComponentSerializer COMPONENT_SERIALIZER = LegacyComponentSerializer.builder().character('ยง').extractUrls().build(); - private final ViaConnectionManager connectionManager = new ViaConnectionManager(); private final SpongeViaAPI api = new SpongeViaAPI(); private SpongeViaConfig conf; private Logger logger; @@ -243,11 +241,6 @@ public class SpongePlugin implements ViaPlatform { return true; } - @Override - public ViaConnectionManager getConnectionManager() { - return connectionManager; - } - @Override public SpongeViaAPI getApi() { return api; diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java index c33439765..5ac1044b6 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java @@ -22,9 +22,9 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import java.lang.reflect.Method; @@ -47,7 +47,7 @@ public class SpongeChannelInitializer extends ChannelInitializer { @Override protected void initChannel(Channel channel) throws Exception { // Ensure ViaVersion is loaded - if (ProtocolRegistry.SERVER_PROTOCOL != -1 + if (Via.getAPI().getServerVersion() != -1 && channel instanceof SocketChannel) { // channel can be LocalChannel on internal server UserConnection info = new UserConnection((SocketChannel) channel); // init protocol diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaLoader.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaLoader.java index a3b7ec3a0..d3ec83dee 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaLoader.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaLoader.java @@ -24,7 +24,6 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; @@ -67,7 +66,7 @@ public class SpongeViaLoader implements ViaPlatformLoader { registerListener(new UpdateListener()); /* 1.9 client to 1.8 server */ - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { try { Class.forName("org.spongepowered.api.event.entity.DisplaceEntityEvent"); storeListener(new Sponge4ArmorListener()).register(); @@ -84,7 +83,7 @@ public class SpongeViaLoader implements ViaPlatformLoader { } /* Providers */ - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { Via.getManager().getProviders().use(BulkChunkTranslatorProvider.class, new SpongeViaBulkChunkTranslator()); Via.getManager().getProviders().use(MovementTransmitterProvider.class, new SpongeViaMovementTransmitter()); diff --git a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java index 63dfdb81f..65c66dd07 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java +++ b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java @@ -34,7 +34,6 @@ 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.platform.TaskId; -import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.util.GsonUtil; @@ -79,7 +78,6 @@ public class VelocityPlugin implements ViaPlatform { private VelocityViaAPI api; private java.util.logging.Logger logger; private VelocityViaConfig conf; - private ViaConnectionManager connectionManager; @Subscribe public void onProxyInit(ProxyInitializeEvent e) { @@ -89,7 +87,6 @@ public class VelocityPlugin implements ViaPlatform { api = new VelocityViaAPI(); conf = new VelocityViaConfig(configDir.toFile()); logger = new LoggerWrapper(loggerslf4j); - connectionManager = new ViaConnectionManager(); Via.init(ViaManagerImpl.builder() .platform(this) .commandHandler(commandHandler) @@ -240,9 +237,4 @@ public class VelocityPlugin implements ViaPlatform { public java.util.logging.Logger getLogger() { return logger; } - - @Override - public ViaConnectionManager getConnectionManager() { - return connectionManager; - } } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java index f4a9336ff..aecf15fa3 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java @@ -21,7 +21,6 @@ import com.velocitypowered.api.plugin.PluginContainer; import us.myles.ViaVersion.VelocityPlugin; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider; @@ -38,7 +37,7 @@ public class VelocityViaLoader implements ViaPlatformLoader { Object plugin = VelocityPlugin.PROXY.getPluginManager() .getPlugin("viaversion").flatMap(PluginContainer::getInstance).get(); - if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getVersion()) { + if (Via.getAPI().getServerVersion() < ProtocolVersion.v1_9.getVersion()) { Via.getManager().getProviders().use(MovementTransmitterProvider.class, new VelocityMovementTransmitter()); Via.getManager().getProviders().use(BossBarProvider.class, new VelocityBossBarProvider()); }