From 302716054de78de956d56a854e1657d665cc85ef Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:56:28 +0100 Subject: [PATCH] Port of internals and API to ProtocolVersion (#3694) --- .../com/viaversion/viaversion/api/ViaAPI.java | 45 ++++++++++- .../api/configuration/ViaVersionConfig.java | 11 --- .../api/platform/ProtocolDetectorService.java | 3 +- .../api/protocol/ProtocolManager.java | 30 +++---- .../api/protocol/ProtocolPathEntry.java | 13 +-- .../api/protocol/ProtocolPathKey.java | 15 +--- .../version/BlockedProtocolVersions.java | 10 +-- .../bukkit/util/ProtocolSupportUtil.java | 15 ++-- .../BlockBreakListener.java | 4 +- .../bukkit/platform/BukkitViaAPI.java | 11 +-- .../BukkitInventoryQuickMoveProvider.java | 22 +++--- .../bungee/handlers/BungeeServerHandler.java | 13 ++- .../bungee/platform/BungeeViaAPI.java | 5 +- .../providers/BungeeVersionProvider.java | 8 +- .../service/ProtocolDetectorService.java | 5 +- .../com/viaversion/viaversion/ViaAPIBase.java | 11 +-- .../configuration/AbstractViaConfig.java | 38 ++++----- .../viaversion/dump/VersionInfo.java | 11 +-- .../AbstractProtocolDetectorService.java | 12 +-- .../protocol/BlockedProtocolVersionsImpl.java | 23 +++--- .../protocol/ProtocolManagerImpl.java | 79 ++++++++++--------- .../protocol/ProtocolPathEntryImpl.java | 12 +-- .../protocol/ProtocolPathKeyImpl.java | 16 ++-- .../VersionedPacketTransformerImpl.java | 8 +- .../protocols/base/BaseProtocol.java | 5 +- .../protocols/base/BaseProtocol1_7.java | 16 ++-- .../viaversion/viaversion/util/DumpUtil.java | 2 +- .../viaversion/common/dummy/TestPlatform.java | 5 +- .../sponge/platform/SpongeViaAPI.java | 5 +- .../velocity/platform/VelocityViaAPI.java | 5 +- .../providers/VelocityVersionProvider.java | 3 +- .../service/ProtocolDetectorService.java | 10 +-- 32 files changed, 245 insertions(+), 226 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java index a3b19bc66..5b7c2b89c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java +++ b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java @@ -27,10 +27,13 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.legacy.LegacyViaAPI; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.api.protocol.ProtocolManager; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion; import io.netty.buffer.ByteBuf; import java.util.SortedSet; +import java.util.TreeSet; import java.util.UUID; +import java.util.stream.Collectors; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -80,7 +83,18 @@ public interface ViaAPI { * @param player the platform's player object, e.g. Bukkit this is Player * @return protocol version, for example (47=1.8-1.8.8, 107=1.9, 108=1.9.1), or -1 if no longer connected */ - int getPlayerVersion(T player); + default int getPlayerVersion(T player) { + return getPlayerProtocolVersion(player).getVersion(); + } + + /** + * Returns the protocol version of a player. + * This will also retrieve the version from ProtocolSupport if it's being used. + * + * @param player the platform's player object, e.g. Bukkit this is Player + * @return the protocol version object (see {@link ProtocolVersion}), or ProtocolVersion.unknown if not connected + */ + ProtocolVersion getPlayerProtocolVersion(T player); /** * Returns the protocol version of a player. @@ -88,7 +102,19 @@ public interface ViaAPI { * @param uuid UUID of a player * @return protocol version, for example (47=1.8-1.8.8, 107=1.9, 108=1.9.1), or -1 if not connected */ - int getPlayerVersion(UUID uuid); + default int getPlayerVersion(UUID uuid) { + return getPlayerProtocolVersion(uuid).getVersion(); + } + + + /** + * Returns the protocol version of a player. + * This will also retrieve the version from ProtocolSupport if it's being used. + * + * @param uuid UUID of a player + * @return the protocol version object (see {@link ProtocolVersion}), or ProtocolVersion.unknown if not connected + */ + ProtocolVersion getPlayerProtocolVersion(UUID uuid); /** * Returns whether Via injected into this player connection. @@ -131,6 +157,16 @@ public interface ViaAPI { */ void sendRawPacket(UUID uuid, ByteBuf packet); + @Deprecated + default SortedSet getSupportedVersions() { + return getSupportedProtocolVersions().stream().map(ProtocolVersion::getVersion).collect(Collectors.toCollection(TreeSet::new)); + } + + @Deprecated + default SortedSet getFullSupportedVersions() { + return getFullSupportedProtocolVersions().stream().map(ProtocolVersion::getVersion).collect(Collectors.toCollection(TreeSet::new)); + } + /** * Returns the supported protocol versions. * This method removes any blocked protocol versions. @@ -138,14 +174,15 @@ public interface ViaAPI { * @return a sorted set of protocol versions * @see #getFullSupportedVersions() for a full list */ - SortedSet getSupportedVersions(); + SortedSet getSupportedProtocolVersions(); + /** * Returns the supported protocol versions, including blocked protocols. * * @return a sorted set of protocol versions */ - SortedSet getFullSupportedVersions(); + SortedSet getFullSupportedProtocolVersions(); /** * Returns legacy api only applicable on/to legacy versions. diff --git a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java index f2920cd28..c80737fe3 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java +++ b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java @@ -272,17 +272,6 @@ public interface ViaVersionConfig extends Config { */ BlockedProtocolVersions blockedProtocolVersions(); - /** - * Get the blocked protocols - * - * @return An Integer list - * @deprecated use {@link #blockedProtocolVersions()} - */ - @Deprecated/*(forRemoval = true)*/ - default IntSet getBlockedProtocols() { - return blockedProtocolVersions().singleBlockedVersions(); - } - /** * Get the custom disconnect message * diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/ProtocolDetectorService.java b/api/src/main/java/com/viaversion/viaversion/api/platform/ProtocolDetectorService.java index 1887157a0..5f58e7d38 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/ProtocolDetectorService.java +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/ProtocolDetectorService.java @@ -22,6 +22,7 @@ */ package com.viaversion.viaversion.api.platform; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import it.unimi.dsi.fastutil.objects.Object2IntMap; public interface ProtocolDetectorService { @@ -32,7 +33,7 @@ public interface ProtocolDetectorService { * @param serverName name of the proxied server * @return protocol version of the proxied server, or -1 if unknown */ - int serverProtocolVersion(String serverName); + ProtocolVersion serverProtocolVersion(String serverName); /** * Probes all registered proxied servers for their protocol version. diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolManager.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolManager.java index 35e0923c0..af27abd68 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolManager.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolManager.java @@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.VersionedPacketTransformer; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion; +import com.viaversion.viaversion.api.protocol.version.VersionType; import io.netty.buffer.ByteBuf; import java.util.Collection; import java.util.List; @@ -62,21 +63,8 @@ public interface ProtocolManager { * @param clientVersion client protocol version * @param serverVersion server protocol version * @return protocol if present, else null - * @see #getProtocolPath(int, int) to get a full path of Protocols between a larger gap of versions */ - default @Nullable Protocol getProtocol(ProtocolVersion clientVersion, ProtocolVersion serverVersion) { - return getProtocol(clientVersion.getVersion(), serverVersion.getVersion()); - } - - /** - * Returns a protocol transforming packets for server version to the given client version. - * - * @param clientVersion client protocol version - * @param serverVersion server protocol version - * @return protocol if present, else null - * @see #getProtocolPath(int, int) to get a full path of Protocols between a larger gap of versions - */ - @Nullable Protocol getProtocol(int clientVersion, int serverVersion); + @Nullable Protocol getProtocol(ProtocolVersion clientVersion, ProtocolVersion serverVersion); /** * Returns the base protocol handling serverbound handshake packets. @@ -93,7 +81,7 @@ public interface ProtocolManager { * @return base protocol for the given server protocol version * @throws IllegalStateException if no base protocol could be found */ - Protocol getBaseProtocol(int serverVersion); + Protocol getBaseProtocol(ProtocolVersion serverVersion); /** * Returns an immutable collection of registered protocols. @@ -128,7 +116,7 @@ public interface ProtocolManager { * @param serverVersion output server protocol version the protocol converts to * @throws IllegalArgumentException if a supported client protocol version is equal to the server protocol version */ - void registerProtocol(Protocol protocol, List supportedClientVersion, int serverVersion); + void registerProtocol(Protocol protocol, List supportedClientVersion, ProtocolVersion serverVersion); /** * Registers and initializes a base protocol. Base Protocols registered later have higher priority. @@ -138,7 +126,7 @@ public interface ProtocolManager { * @param supportedProtocols protocol versions supported by the base protocol * @throws IllegalArgumentException if the protocol is not a base protocol as given by {@link Protocol#isBaseProtocol()} */ - void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols); + void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols); /** * Calculates and returns the protocol path from a client protocol version to server protocol version. @@ -148,7 +136,7 @@ public interface ProtocolManager { * @param serverVersion desired output server protocol version * @return path generated, or null if not supported or the length exceeds {@link #getMaxProtocolPathSize()} */ - @Nullable List getProtocolPath(int clientVersion, int serverVersion); + @Nullable List getProtocolPath(ProtocolVersion clientVersion, ProtocolVersion serverVersion); /** * Returns a versioned packet transformer to transform and send packets from a given base version to any client version supported by Via. @@ -226,14 +214,14 @@ public interface ProtocolManager { } /** - * Returns the maximum protocol path size applied to {@link #getProtocolPath(int, int)}. + * Returns the maximum protocol path size applied to {@link #getProtocolPath(ProtocolVersion, ProtocolVersion)}. * * @return maximum protocol path size */ int getMaxProtocolPathSize(); /** - * Sets the maximum protocol path size applied to {@link #getProtocolPath(int, int)}. + * Sets the maximum protocol path size applied to {@link #getProtocolPath(ProtocolVersion, ProtocolVersion)}. * Its default is 50. * * @param maxProtocolPathSize maximum protocol path size @@ -245,7 +233,7 @@ public interface ProtocolManager { * * @return sorted, immutable set of supported protocol versions */ - SortedSet getSupportedVersions(); + SortedSet getSupportedVersions(); /** * Check if this plugin is useful to the server. diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathEntry.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathEntry.java index 2fef63453..376c1df8c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathEntry.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathEntry.java @@ -22,6 +22,8 @@ */ package com.viaversion.viaversion.api.protocol; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; + public interface ProtocolPathEntry { /** @@ -30,7 +32,7 @@ public interface ProtocolPathEntry { * * @return output protocol version after transformation */ - int outputProtocolVersion(); + ProtocolVersion outputProtocolVersion(); /** * Returns the protocol to be applied with this entry. @@ -39,13 +41,4 @@ public interface ProtocolPathEntry { */ Protocol protocol(); - @Deprecated/*(forRemoval = true)*/ - default int getOutputProtocolVersion() { - return outputProtocolVersion(); - } - - @Deprecated/*(forRemoval = true)*/ - default Protocol getProtocol() { - return protocol(); - } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathKey.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathKey.java index 7ac0b1a4e..5d11f13ac 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathKey.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/ProtocolPathKey.java @@ -22,6 +22,8 @@ */ package com.viaversion.viaversion.api.protocol; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; + public interface ProtocolPathKey { /** @@ -29,22 +31,13 @@ public interface ProtocolPathKey { * * @return client protocol version */ - int clientProtocolVersion(); + ProtocolVersion clientProtocolVersion(); /** * Returns the server protocol version. * * @return server protocol version */ - int serverProtocolVersion(); + ProtocolVersion serverProtocolVersion(); - @Deprecated/*(forRemoval = true)*/ - default int getClientProtocolVersion() { - return clientProtocolVersion(); - } - - @Deprecated/*(forRemoval = true)*/ - default int getServerProtocolVersion() { - return serverProtocolVersion(); - } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/BlockedProtocolVersions.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/BlockedProtocolVersions.java index bb4d17ff8..a7aa1d83a 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/BlockedProtocolVersions.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/BlockedProtocolVersions.java @@ -22,7 +22,7 @@ */ package com.viaversion.viaversion.api.protocol.version; -import it.unimi.dsi.fastutil.ints.IntSet; +import java.util.Set; public interface BlockedProtocolVersions { @@ -32,26 +32,26 @@ public interface BlockedProtocolVersions { * @param protocolVersion protocol version * @return whether the given protocol version is blocked */ - boolean contains(int protocolVersion); + boolean contains(ProtocolVersion protocolVersion); /** * Returns the boundary below which protocol versions are blocked, or -1 if none is set. * * @return exclusive boundary below which protocol versions are blocked, or -1 if none */ - int blocksBelow(); + ProtocolVersion blocksBelow(); /** * Returns the boundary above which protocol versions are blocked, or -1 if none is set. * * @return exclusive boundary above which protocol versions are blocked, or -1 if none */ - int blocksAbove(); + ProtocolVersion blocksAbove(); /** * Returns a set of blocked protocol versions between the outer block ranges. * * @return set of blocked protocol versions between the outer block ranges */ - IntSet singleBlockedVersions(); + Set singleBlockedVersions(); } diff --git a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java index b49b91e3e..986e026ef 100644 --- a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java +++ b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java @@ -19,6 +19,8 @@ package com.viaversion.viaversion.bukkit.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import com.viaversion.viaversion.api.protocol.version.VersionType; import org.bukkit.entity.Player; public final class ProtocolSupportUtil { @@ -38,16 +40,19 @@ public final class ProtocolSupportUtil { GET_ID_METHOD = getIdMethod; } - public static int getProtocolVersion(Player player) { + public static ProtocolVersion getProtocolVersion(Player player) { if (PROTOCOL_VERSION_METHOD == null) { - return -1; + return ProtocolVersion.unknown; } try { - Object version = PROTOCOL_VERSION_METHOD.invoke(null, player); - return (int) GET_ID_METHOD.invoke(version); + final Object version = PROTOCOL_VERSION_METHOD.invoke(null, player); + final int id = (int) GET_ID_METHOD.invoke(version); + final boolean preNetty = id == 78 || id == 74 || id == 73 || id == 61 || id == 60 || id == 51; + + return ProtocolVersion.getProtocol(preNetty ? VersionType.RELEASE_INITIAL : VersionType.RELEASE, id); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } - return -1; + return ProtocolVersion.unknown; } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19to1_18_2/BlockBreakListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19to1_18_2/BlockBreakListener.java index 47e7ff1a8..29e8f1663 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19to1_18_2/BlockBreakListener.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19to1_18_2/BlockBreakListener.java @@ -53,8 +53,8 @@ public final class BlockBreakListener extends ViaBukkitListener { } // We need to resend the block entity data after an ack has been sent out - final int serverProtocolVersion = Via.getAPI().getServerVersion().highestSupportedVersion(); - final long delay = serverProtocolVersion > ProtocolVersion.v1_8.getVersion() && serverProtocolVersion < ProtocolVersion.v1_14.getVersion() ? 2 : 1; + final ProtocolVersion serverProtocolVersion = Via.getAPI().getServerVersion().highestSupportedProtocolVersion(); + final long delay = serverProtocolVersion.newerThan(ProtocolVersion.v1_8) && serverProtocolVersion.olderThan(ProtocolVersion.v1_14) ? 2 : 1; getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> { final BlockState state = block.getState(); if (isBlockEntity(state)) { diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java index a7ec6a704..ec05c1817 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java @@ -21,6 +21,7 @@ import com.viaversion.viaversion.ViaAPIBase; import com.viaversion.viaversion.ViaVersionPlugin; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.bukkit.util.ProtocolSupportUtil; import io.netty.buffer.ByteBuf; import java.util.UUID; @@ -35,15 +36,15 @@ public class BukkitViaAPI extends ViaAPIBase { } @Override - public int getPlayerVersion(Player player) { - return getPlayerVersion(player.getUniqueId()); + public ProtocolVersion getPlayerProtocolVersion(Player player) { + return getPlayerProtocolVersion(player.getUniqueId()); } @Override - public int getPlayerVersion(UUID uuid) { + public ProtocolVersion getPlayerProtocolVersion(UUID uuid) { UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid); if (connection != null) { - return connection.getProtocolInfo().protocolVersion().getVersion(); + return connection.getProtocolInfo().protocolVersion(); } if (isProtocolSupport()) { @@ -52,7 +53,7 @@ public class BukkitViaAPI extends ViaAPIBase { return ProtocolSupportUtil.getProtocolVersion(player); } } - return -1; + return ProtocolVersion.unknown; } @Override diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitInventoryQuickMoveProvider.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitInventoryQuickMoveProvider.java index 11d432118..d08907ae5 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitInventoryQuickMoveProvider.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitInventoryQuickMoveProvider.java @@ -69,9 +69,9 @@ 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 <= 45) { - int protocolId = Via.getAPI().getServerVersion().lowestSupportedVersion(); + final ProtocolVersion protocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); // this seems to be working just fine. - if (protocolId == ProtocolVersion.v1_8.getVersion()) { + if (protocol.equalTo(ProtocolVersion.v1_8)) { return false; } } @@ -101,8 +101,8 @@ 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 = Via.getAPI().getServerVersion().lowestSupportedVersion(); - if (protocolId == ProtocolVersion.v1_8.getVersion()) { + final ProtocolVersion protocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); + if (protocol.equalTo(ProtocolVersion.v1_8)) { if (tinvtype == InventoryType.BREWING) { // 1.9 added the blaze powder slot to brewing stand fix for 1.8 servers if (slotId >= 5 && slotId <= 40) { @@ -130,10 +130,10 @@ 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 = Via.getAPI().getServerVersion().lowestSupportedVersion(); - if (protocolId == ProtocolVersion.v1_8.getVersion()) { + final ProtocolVersion protocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); + if (protocol.equalTo(ProtocolVersion.v1_8)) { ReflectionUtil.set(packet, "shift", 1); - } else if (protocolId >= ProtocolVersion.v1_9.getVersion()) { // 1.9+ + } else if (protocol.newerThanOrEquals(ProtocolVersion.v1_9)) { ReflectionUtil.set(packet, "shift", clickTypeEnum); } } catch (Exception e) { @@ -169,8 +169,8 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider } try { this.windowClickPacketClass = NMSUtil.nms("PacketPlayInWindowClick"); - int protocolId = Via.getAPI().getServerVersion().lowestSupportedVersion(); - if (protocolId >= ProtocolVersion.v1_9.getVersion()) { + final ProtocolVersion protocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); + if (protocol.newerThanOrEquals(ProtocolVersion.v1_9)) { Class eclassz = NMSUtil.nms("InventoryClickType"); Object[] constants = eclassz.getEnumConstants(); this.clickTypeEnum = constants[1]; // QUICK_MOVE @@ -198,7 +198,7 @@ public class BukkitInventoryQuickMoveProvider extends InventoryQuickMoveProvider } private boolean isSupported() { - int protocolId = Via.getAPI().getServerVersion().lowestSupportedVersion(); - return protocolId >= ProtocolVersion.v1_8.getVersion() && protocolId <= ProtocolVersion.v1_11_1.getVersion(); // 1.8-1.11.2, not needed with 1.12 + final ProtocolVersion protocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); + return protocol.newerThanOrEquals(ProtocolVersion.v1_8) && protocol.olderThanOrEquals(ProtocolVersion.v1_11_1); } } \ No newline at end of file diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java index ed23ed064..529cd5385 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java @@ -99,14 +99,14 @@ public class BungeeServerHandler implements Listener { user.put(new BungeeStorage(event.getPlayer())); } - int serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(event.getTarget().getName()); - int clientProtocolVersion = user.getProtocolInfo().protocolVersion().getVersion(); + ProtocolVersion serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(event.getTarget().getName()); + ProtocolVersion clientProtocolVersion = user.getProtocolInfo().protocolVersion(); List protocols = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion); // Check if ViaVersion can support that version try { Object handshake = getHandshake.invoke(event.getPlayer().getPendingConnection()); - setProtocol.invoke(handshake, protocols == null ? clientProtocolVersion : serverProtocolVersion); + setProtocol.invoke(handshake, protocols == null ? clientProtocolVersion.getVersion() : serverProtocolVersion.getVersion()); } catch (InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } @@ -172,7 +172,7 @@ public class BungeeServerHandler implements Listener { String serverName = server.getInfo().getName(); storage.setCurrentServer(serverName); - ProtocolVersion serverProtocolVersion = ProtocolVersion.getProtocol(Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName)); + ProtocolVersion serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); if (serverProtocolVersion.olderThanOrEquals(ProtocolVersion.v1_8) && storage.getBossbar() != null) { // 1.8 doesn't have BossBar packet // This ensures we can encode it properly as only the 1.9 protocol is currently implemented. if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { @@ -190,8 +190,7 @@ public class BungeeServerHandler implements Listener { ProtocolVersion previousServerProtocol = info.serverProtocolVersion(); // Refresh the pipes - List protocolPath = Via.getManager().getProtocolManager() - .getProtocolPath(info.protocolVersion().getVersion(), serverProtocolVersion.getVersion()); + List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.protocolVersion(), serverProtocolVersion); ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); user.clearStoredObjects(true); pipeline.cleanPipes(); @@ -208,7 +207,7 @@ public class BungeeServerHandler implements Listener { info.setServerProtocolVersion(serverProtocolVersion); // Add version-specific base Protocol - pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(serverProtocolVersion.getVersion())); + pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(serverProtocolVersion)); // Workaround 1.13 server change boolean toNewId = previousServerProtocol.olderThan(ProtocolVersion.v1_13) && serverProtocolVersion.newerThanOrEquals(ProtocolVersion.v1_13); diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeViaAPI.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeViaAPI.java index 46ce5648f..ad31125e6 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeViaAPI.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeViaAPI.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.bungee.platform; import com.viaversion.viaversion.ViaAPIBase; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.bungee.service.ProtocolDetectorService; import io.netty.buffer.ByteBuf; import net.md_5.bungee.api.config.ServerInfo; @@ -27,8 +28,8 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; public class BungeeViaAPI extends ViaAPIBase { @Override - public int getPlayerVersion(ProxiedPlayer player) { - return getPlayerVersion(player.getUniqueId()); + public ProtocolVersion getPlayerProtocolVersion(ProxiedPlayer player) { + return getPlayerProtocolVersion(player.getUniqueId()); } @Override diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/providers/BungeeVersionProvider.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/providers/BungeeVersionProvider.java index 4cb08ee67..ccc773337 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/providers/BungeeVersionProvider.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/providers/BungeeVersionProvider.java @@ -50,7 +50,7 @@ public class BungeeVersionProvider extends BaseVersionProvider { // Older than bungee supports, get the lowest version if (clientProtocolVersion.getVersion() < sorted.get(0)) { - return ProtocolVersion.getProtocol(getLowestSupportedVersion()); + return getLowestSupportedVersion(); } // Loop through all protocols to get the closest protocol id that bungee supports (and that viaversion does too) @@ -67,15 +67,15 @@ public class BungeeVersionProvider extends BaseVersionProvider { return clientProtocolVersion; } - public static int getLowestSupportedVersion() { + public static ProtocolVersion getLowestSupportedVersion() { List list; try { list = ReflectionUtil.getStatic(ProtocolConstants.class, "SUPPORTED_VERSION_IDS", List.class); - return list.get(0); + return ProtocolVersion.getProtocol(list.get(0)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } // Fallback - return ProxyServer.getInstance().getProtocolVersion(); + return ProtocolVersion.getProtocol(ProxyServer.getInstance().getProtocolVersion()); } } diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/service/ProtocolDetectorService.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/service/ProtocolDetectorService.java index cd381989f..9cb68df8f 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/service/ProtocolDetectorService.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/service/ProtocolDetectorService.java @@ -18,6 +18,7 @@ package com.viaversion.viaversion.bungee.service; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.bungee.platform.BungeeViaConfig; import com.viaversion.viaversion.bungee.providers.BungeeVersionProvider; import com.viaversion.viaversion.platform.AbstractProtocolDetectorService; @@ -38,7 +39,7 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi return; } - final int oldProtocolVersion = serverProtocolVersion(serverName); + final int oldProtocolVersion = serverProtocolVersion(serverName).getVersion(); if (oldProtocolVersion == serverPing.getVersion().getProtocol()) { // Same value as previously return; @@ -87,7 +88,7 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi } @Override - protected int lowestSupportedProtocolVersion() { + protected ProtocolVersion lowestSupportedProtocolVersion() { return BungeeVersionProvider.getLowestSupportedVersion(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/ViaAPIBase.java b/common/src/main/java/com/viaversion/viaversion/ViaAPIBase.java index 8ac94eb27..6745226bf 100644 --- a/common/src/main/java/com/viaversion/viaversion/ViaAPIBase.java +++ b/common/src/main/java/com/viaversion/viaversion/ViaAPIBase.java @@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.ViaAPI; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.legacy.LegacyViaAPI; import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion; import com.viaversion.viaversion.legacy.LegacyAPI; import io.netty.buffer.ByteBuf; @@ -40,9 +41,9 @@ public abstract class ViaAPIBase implements ViaAPI { } @Override - public int getPlayerVersion(UUID uuid) { + public ProtocolVersion getPlayerProtocolVersion(UUID uuid) { UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid); - return connection != null ? connection.getProtocolInfo().protocolVersion().getVersion() : -1; + return connection != null ? connection.getProtocolInfo().protocolVersion() : ProtocolVersion.unknown; } @Override @@ -71,15 +72,15 @@ public abstract class ViaAPIBase implements ViaAPI { } @Override - public SortedSet getSupportedVersions() { - SortedSet outputSet = new TreeSet<>(Via.getManager().getProtocolManager().getSupportedVersions()); + public SortedSet getSupportedProtocolVersions() { + SortedSet outputSet = new TreeSet<>(Via.getManager().getProtocolManager().getSupportedVersions()); BlockedProtocolVersions blockedVersions = Via.getPlatform().getConf().blockedProtocolVersions(); outputSet.removeIf(blockedVersions::contains); return outputSet; } @Override - public SortedSet getFullSupportedVersions() { + public SortedSet getFullSupportedProtocolVersions() { return Via.getManager().getProtocolManager().getSupportedVersions(); } diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index 144d8c0e3..f721c1b22 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -22,15 +22,16 @@ import com.viaversion.viaversion.api.configuration.ViaVersionConfig; import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import com.viaversion.viaversion.api.protocol.version.VersionType; import com.viaversion.viaversion.protocol.BlockedProtocolVersionsImpl; import com.viaversion.viaversion.util.Config; -import it.unimi.dsi.fastutil.ints.IntOpenHashSet; -import it.unimi.dsi.fastutil.ints.IntSet; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.IntPredicate; +import java.util.function.Predicate; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import it.unimi.dsi.fastutil.objects.ObjectSet; import org.checkerframework.checker.nullness.qual.Nullable; public abstract class AbstractViaConfig extends Config implements ViaVersionConfig { @@ -161,9 +162,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf private BlockedProtocolVersions loadBlockedProtocolVersions() { List blockProtocols = getListSafe("block-protocols", Integer.class, "Invalid blocked version protocol found in config: '%s'"); List blockVersions = getListSafe("block-versions", String.class, "Invalid blocked version found in config: '%s'"); - IntSet blockedProtocols = new IntOpenHashSet(blockProtocols); - int lowerBound = -1; - int upperBound = -1; + ObjectSet blockedProtocols = blockProtocols.stream().map(integer -> ProtocolVersion.getProtocol(VersionType.RELEASE, integer)).collect(ObjectOpenHashSet::of, ObjectSet::add, ObjectSet::addAll); + ProtocolVersion lowerBound = ProtocolVersion.unknown; + ProtocolVersion upperBound = ProtocolVersion.unknown; for (String s : blockVersions) { if (s.isEmpty()) { continue; @@ -178,15 +179,15 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf } if (c == '<') { - if (lowerBound != -1) { + if (lowerBound.isKnown()) { LOGGER.warning("Already set lower bound " + lowerBound + " overridden by " + protocolVersion.getName()); } - lowerBound = protocolVersion.getVersion(); + lowerBound = protocolVersion; } else { - if (upperBound != -1) { + if (upperBound.isKnown()) { LOGGER.warning("Already set upper bound " + upperBound + " overridden by " + protocolVersion.getName()); } - upperBound = protocolVersion.getVersion(); + upperBound = protocolVersion; } continue; } @@ -197,19 +198,18 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf } // Add single protocol version and check for duplication - if (!blockedProtocols.add(protocolVersion.getVersion())) { - LOGGER.warning("Duplicated blocked protocol version " + protocolVersion.getName() + "/" + protocolVersion.getVersion()); + if (!blockedProtocols.add(protocolVersion)) { + LOGGER.warning("Duplicated blocked protocol version " + protocolVersion); } } // Check for duplicated entries - if (lowerBound != -1 || upperBound != -1) { - final int finalLowerBound = lowerBound; - final int finalUpperBound = upperBound; - blockedProtocols.removeIf((IntPredicate) version -> { - if (finalLowerBound != -1 && version < finalLowerBound || finalUpperBound != -1 && version > finalUpperBound) { - ProtocolVersion protocolVersion = ProtocolVersion.getProtocol(version); - LOGGER.warning("Blocked protocol version " + protocolVersion.getName() + "/" + protocolVersion.getVersion() + " already covered by upper or lower bound"); + if (lowerBound.isKnown() || upperBound.isKnown()) { + final ProtocolVersion finalLowerBound = lowerBound; + final ProtocolVersion finalUpperBound = upperBound; + blockedProtocols.removeIf(version -> { + if (finalLowerBound.isKnown() && version.olderThan(finalLowerBound) || finalUpperBound.isKnown() && version.newerThan(finalUpperBound)) { + LOGGER.warning("Blocked protocol version " + version + " already covered by upper or lower bound"); return true; } return false; diff --git a/common/src/main/java/com/viaversion/viaversion/dump/VersionInfo.java b/common/src/main/java/com/viaversion/viaversion/dump/VersionInfo.java index fdd422331..5b50a0b65 100644 --- a/common/src/main/java/com/viaversion/viaversion/dump/VersionInfo.java +++ b/common/src/main/java/com/viaversion/viaversion/dump/VersionInfo.java @@ -17,20 +17,21 @@ */ package com.viaversion.viaversion.dump; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import java.util.Set; public class VersionInfo { private final String javaVersion; private final String operatingSystem; - private final int serverProtocol; - private final Set enabledProtocols; + private final ProtocolVersion serverProtocol; + private final Set enabledProtocols; private final String platformName; private final String platformVersion; private final String pluginVersion; private final String implementationVersion; private final Set subPlatforms; - public VersionInfo(String javaVersion, String operatingSystem, int serverProtocol, Set enabledProtocols, + public VersionInfo(String javaVersion, String operatingSystem, ProtocolVersion serverProtocol, Set enabledProtocols, String platformName, String platformVersion, String pluginVersion, String implementationVersion, Set subPlatforms) { this.javaVersion = javaVersion; this.operatingSystem = operatingSystem; @@ -51,11 +52,11 @@ public class VersionInfo { return operatingSystem; } - public int getServerProtocol() { + public ProtocolVersion getServerProtocol() { return serverProtocol; } - public Set getEnabledProtocols() { + public Set getEnabledProtocols() { return enabledProtocols; } diff --git a/common/src/main/java/com/viaversion/viaversion/platform/AbstractProtocolDetectorService.java b/common/src/main/java/com/viaversion/viaversion/platform/AbstractProtocolDetectorService.java index 17bc375a6..b1833ddff 100644 --- a/common/src/main/java/com/viaversion/viaversion/platform/AbstractProtocolDetectorService.java +++ b/common/src/main/java/com/viaversion/viaversion/platform/AbstractProtocolDetectorService.java @@ -18,6 +18,8 @@ package com.viaversion.viaversion.platform; import com.viaversion.viaversion.api.platform.ProtocolDetectorService; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import com.viaversion.viaversion.api.protocol.version.VersionType; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import java.util.Map; @@ -33,7 +35,7 @@ public abstract class AbstractProtocolDetectorService implements ProtocolDetecto } @Override - public int serverProtocolVersion(final String serverName) { + public ProtocolVersion serverProtocolVersion(final String serverName) { // Step 1. Check detected lock.readLock().lock(); final int detectedProtocol; @@ -43,20 +45,20 @@ public abstract class AbstractProtocolDetectorService implements ProtocolDetecto lock.readLock().unlock(); } if (detectedProtocol != -1) { - return detectedProtocol; + return ProtocolVersion.getProtocol(VersionType.RELEASE, detectedProtocol); } // Step 2. Check config (CME moment?) final Map servers = configuredServers(); final Integer protocol = servers.get(serverName); if (protocol != null) { - return protocol; + return ProtocolVersion.getProtocol(VersionType.RELEASE, protocol); } // Step 3. Use Default (CME moment intensifies?) final Integer defaultProtocol = servers.get("default"); if (defaultProtocol != null) { - return defaultProtocol; + return ProtocolVersion.getProtocol(VersionType.RELEASE, defaultProtocol); } // Step 4: Use the proxy's lowest supported... *cries* @@ -95,5 +97,5 @@ public abstract class AbstractProtocolDetectorService implements ProtocolDetecto protected abstract Map configuredServers(); - protected abstract int lowestSupportedProtocolVersion(); + protected abstract ProtocolVersion lowestSupportedProtocolVersion(); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/BlockedProtocolVersionsImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/BlockedProtocolVersionsImpl.java index 10555c1c8..0d1ffcb02 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/BlockedProtocolVersionsImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/BlockedProtocolVersionsImpl.java @@ -18,38 +18,39 @@ package com.viaversion.viaversion.protocol; import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions; -import it.unimi.dsi.fastutil.ints.IntSet; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import java.util.Set; public class BlockedProtocolVersionsImpl implements BlockedProtocolVersions { - private final IntSet singleBlockedVersions; - private final int blocksBelow; - private final int blocksAbove; + private final Set singleBlockedVersions; + private final ProtocolVersion blocksBelow; + private final ProtocolVersion blocksAbove; - public BlockedProtocolVersionsImpl(final IntSet singleBlockedVersions, final int blocksBelow, final int blocksAbove) { + public BlockedProtocolVersionsImpl(final Set singleBlockedVersions, final ProtocolVersion blocksBelow, final ProtocolVersion blocksAbove) { this.singleBlockedVersions = singleBlockedVersions; this.blocksBelow = blocksBelow; this.blocksAbove = blocksAbove; } @Override - public boolean contains(final int protocolVersion) { - return blocksBelow != -1 && protocolVersion < blocksBelow - || blocksAbove != -1 && protocolVersion > blocksAbove + public boolean contains(final ProtocolVersion protocolVersion) { + return blocksBelow.isKnown() && protocolVersion.olderThan(blocksBelow) + || blocksAbove.isKnown() && protocolVersion.newerThan(blocksAbove) || singleBlockedVersions.contains(protocolVersion); } @Override - public int blocksBelow() { + public ProtocolVersion blocksBelow() { return blocksBelow; } @Override - public int blocksAbove() { + public ProtocolVersion blocksAbove() { return blocksAbove; } @Override - public IntSet singleBlockedVersions() { + public Set singleBlockedVersions() { return singleBlockedVersions; } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index 98b787d3f..9763cf64b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -79,10 +79,6 @@ import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.Protocol1_9_3T import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import com.viaversion.viaversion.util.Pair; import io.netty.buffer.ByteBuf; -import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -104,6 +100,10 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Function; import java.util.logging.Level; +import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectSortedMap; import org.checkerframework.checker.nullness.qual.Nullable; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; @@ -111,11 +111,11 @@ public class ProtocolManagerImpl implements ProtocolManager { private static final Protocol BASE_PROTOCOL = new BaseProtocol(); // Input Version -> Output Version & Protocol (Allows fast lookup) - private final Int2ObjectMap> registryMap = new Int2ObjectOpenHashMap<>(32); + private final Object2ObjectMap> registryMap = new Object2ObjectOpenHashMap<>(32); private final Map, Protocol> protocols = new HashMap<>(64); private final Map> pathCache = new ConcurrentHashMap<>(); - private final Set supportedVersions = new HashSet<>(); - private final List, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList(); + private final Set supportedVersions = new HashSet<>(); + private final List, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList(); private final ReadWriteLock mappingLoaderLock = new ReentrantReadWriteLock(); private Map, CompletableFuture> mappingLoaderFutures = new HashMap<>(); @@ -134,12 +134,12 @@ public class ProtocolManagerImpl implements ProtocolManager { public void registerProtocols() { // Base Protocol - registerBaseProtocol(BASE_PROTOCOL, Range.lessThan(Integer.MIN_VALUE)); - registerBaseProtocol(new BaseProtocol1_7(), Range.lessThan(ProtocolVersion.v1_16.getVersion())); - registerBaseProtocol(new BaseProtocol1_16(), Range.atLeast(ProtocolVersion.v1_16.getVersion())); + BASE_PROTOCOL.initialize(); + registerBaseProtocol(new BaseProtocol1_7(), Range.closedOpen(ProtocolVersion.v1_7_1, ProtocolVersion.v1_16)); + registerBaseProtocol(new BaseProtocol1_16(), Range.atLeast(ProtocolVersion.v1_16)); registerProtocol(new Protocol1_9To1_8(), ProtocolVersion.v1_9, ProtocolVersion.v1_8); - registerProtocol(new Protocol1_9_1To1_9(), Arrays.asList(ProtocolVersion.v1_9_1.getVersion(), ProtocolVersion.v1_9_2.getVersion()), ProtocolVersion.v1_9.getVersion()); + registerProtocol(new Protocol1_9_1To1_9(), Arrays.asList(ProtocolVersion.v1_9_1, ProtocolVersion.v1_9_2), ProtocolVersion.v1_9); registerProtocol(new Protocol1_9_3To1_9_1_2(), ProtocolVersion.v1_9_3, ProtocolVersion.v1_9_2); registerProtocol(new Protocol1_10To1_9_3_4(), ProtocolVersion.v1_10, ProtocolVersion.v1_9_3); @@ -190,11 +190,11 @@ public class ProtocolManagerImpl implements ProtocolManager { @Override public void registerProtocol(Protocol protocol, ProtocolVersion clientVersion, ProtocolVersion serverVersion) { - registerProtocol(protocol, Collections.singletonList(clientVersion.getVersion()), serverVersion.getVersion()); + registerProtocol(protocol, Collections.singletonList(clientVersion), serverVersion); } @Override - public void registerProtocol(Protocol protocol, List supportedClientVersion, int serverVersion) { + public void registerProtocol(Protocol protocol, List supportedClientVersion, ProtocolVersion serverVersion) { // Register the protocol's handlers protocol.initialize(); @@ -205,11 +205,11 @@ public class ProtocolManagerImpl implements ProtocolManager { protocols.put(protocol.getClass(), protocol); - for (int clientVersion : supportedClientVersion) { + for (ProtocolVersion clientVersion : supportedClientVersion) { // Throw an error if supported client version = server version - Preconditions.checkArgument(clientVersion != serverVersion); + Preconditions.checkArgument(!clientVersion.equals(serverVersion)); - Int2ObjectMap protocolMap = registryMap.computeIfAbsent(clientVersion, s -> new Int2ObjectOpenHashMap<>(2)); + Object2ObjectMap protocolMap = registryMap.computeIfAbsent(clientVersion, s -> new Object2ObjectOpenHashMap<>(2)); protocolMap.put(serverVersion, protocol); } @@ -230,7 +230,7 @@ public class ProtocolManagerImpl implements ProtocolManager { } @Override - public void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { + public void registerBaseProtocol(Protocol baseProtocol, Range supportedProtocols) { Preconditions.checkArgument(baseProtocol.isBaseProtocol(), "Protocol is not a base protocol"); baseProtocol.initialize(); @@ -244,12 +244,12 @@ public class ProtocolManagerImpl implements ProtocolManager { public void refreshVersions() { supportedVersions.clear(); - supportedVersions.add(serverProtocolVersion.lowestSupportedVersion()); + supportedVersions.add(serverProtocolVersion.lowestSupportedProtocolVersion()); for (ProtocolVersion version : ProtocolVersion.getProtocols()) { - List protocolPath = getProtocolPath(version.getVersion(), serverProtocolVersion.lowestSupportedVersion()); + List protocolPath = getProtocolPath(version, serverProtocolVersion.lowestSupportedProtocolVersion()); if (protocolPath == null) continue; - supportedVersions.add(version.getVersion()); + supportedVersions.add(version); for (ProtocolPathEntry pathEntry : protocolPath) { supportedVersions.add(pathEntry.outputProtocolVersion()); } @@ -257,7 +257,7 @@ public class ProtocolManagerImpl implements ProtocolManager { } @Override - public @Nullable List getProtocolPath(int clientVersion, int serverVersion) { + public @Nullable List getProtocolPath(ProtocolVersion clientVersion, ProtocolVersion serverVersion) { if (clientVersion == serverVersion) return null; // Nothing to do! // Check cache @@ -268,14 +268,14 @@ public class ProtocolManagerImpl implements ProtocolManager { } // Calculate path - Int2ObjectSortedMap outputPath = getProtocolPath(new Int2ObjectLinkedOpenHashMap<>(), clientVersion, serverVersion); + Object2ObjectSortedMap outputPath = getProtocolPath(new Object2ObjectLinkedOpenHashMap<>(), clientVersion, serverVersion); if (outputPath == null) { return null; } List path = new ArrayList<>(outputPath.size()); - for (Int2ObjectMap.Entry entry : outputPath.int2ObjectEntrySet()) { - path.add(new ProtocolPathEntryImpl(entry.getIntKey(), entry.getValue())); + for (Map.Entry entry : outputPath.entrySet()) { + path.add(new ProtocolPathEntryImpl(entry.getKey(), entry.getValue())); } pathCache.put(protocolKey, path); return path; @@ -299,11 +299,11 @@ public class ProtocolManagerImpl implements ProtocolManager { * @param serverVersion desired output version * @return path that has been generated, null if failed */ - private @Nullable Int2ObjectSortedMap getProtocolPath(Int2ObjectSortedMap current, int clientVersion, int serverVersion) { + private @Nullable Object2ObjectSortedMap getProtocolPath(Object2ObjectSortedMap current, ProtocolVersion clientVersion, ProtocolVersion serverVersion) { if (current.size() > maxProtocolPathSize) return null; // Fail-safe, protocol too complicated. // First, check if there is any protocols for this - Int2ObjectMap toServerProtocolMap = registryMap.get(clientVersion); + Object2ObjectMap toServerProtocolMap = registryMap.get(clientVersion); if (toServerProtocolMap == null) { return null; // Not supported } @@ -316,19 +316,22 @@ public class ProtocolManagerImpl implements ProtocolManager { } // There might be a more advanced solution... So we'll see if any of the others can get us there - Int2ObjectSortedMap shortest = null; - for (Int2ObjectMap.Entry entry : toServerProtocolMap.int2ObjectEntrySet()) { + Object2ObjectSortedMap shortest = null; + for (Map.Entry entry : toServerProtocolMap.entrySet()) { // Ensure we don't go back to already contained versions - int translatedToVersion = entry.getIntKey(); + ProtocolVersion translatedToVersion = entry.getKey(); if (current.containsKey(translatedToVersion)) continue; // Check if the new version is farther away than the current client version - if (maxPathDeltaIncrease != -1 && Math.abs(serverVersion - translatedToVersion) - Math.abs(serverVersion - clientVersion) > maxPathDeltaIncrease) { - continue; + if (maxPathDeltaIncrease != -1 && translatedToVersion.getVersionType() == clientVersion.getVersionType()) { + final int delta = Math.abs(serverVersion.getVersion() - translatedToVersion.getVersion()) - Math.abs(serverVersion.getVersion() - clientVersion.getVersion()); + if (delta > maxPathDeltaIncrease) { + continue; + } } // Create a copy - Int2ObjectSortedMap newCurrent = new Int2ObjectLinkedOpenHashMap<>(current); + Object2ObjectSortedMap newCurrent = new Object2ObjectLinkedOpenHashMap<>(current); newCurrent.put(translatedToVersion, entry.getValue()); // Calculate the rest of the protocol starting from translatedToVersion and take the shortest @@ -347,14 +350,14 @@ public class ProtocolManagerImpl implements ProtocolManager { } @Override - public @Nullable Protocol getProtocol(int clientVersion, int serverVersion) { - Int2ObjectMap map = registryMap.get(clientVersion); + public @Nullable Protocol getProtocol(ProtocolVersion clientVersion, ProtocolVersion serverVersion) { + Object2ObjectMap map = registryMap.get(clientVersion); return map != null ? map.get(serverVersion) : null; } @Override - public Protocol getBaseProtocol(int serverVersion) { - for (Pair, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) { + public Protocol getBaseProtocol(ProtocolVersion serverVersion) { + for (Pair, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) { if (rangeProtocol.key().contains(serverVersion)) { return rangeProtocol.value(); } @@ -380,7 +383,7 @@ public class ProtocolManagerImpl implements ProtocolManager { @Override public boolean isWorkingPipe() { - for (Int2ObjectMap map : registryMap.values()) { + for (Object2ObjectMap map : registryMap.values()) { for (ProtocolVersion protocolVersion : serverProtocolVersion.supportedProtocolVersions()) { if (map.containsKey(protocolVersion.getVersion())) { return true; @@ -391,7 +394,7 @@ public class ProtocolManagerImpl implements ProtocolManager { } @Override - public SortedSet getSupportedVersions() { + public SortedSet getSupportedVersions() { return Collections.unmodifiableSortedSet(new TreeSet<>(supportedVersions)); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathEntryImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathEntryImpl.java index 9b573e404..f758c4787 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathEntryImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathEntryImpl.java @@ -19,18 +19,20 @@ package com.viaversion.viaversion.protocol; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import java.util.Objects; public class ProtocolPathEntryImpl implements ProtocolPathEntry { - private final int outputProtocolVersion; + private final ProtocolVersion outputProtocolVersion; private final Protocol protocol; - public ProtocolPathEntryImpl(int outputProtocolVersion, Protocol protocol) { + public ProtocolPathEntryImpl(ProtocolVersion outputProtocolVersion, Protocol protocol) { this.outputProtocolVersion = outputProtocolVersion; this.protocol = protocol; } @Override - public int outputProtocolVersion() { + public ProtocolVersion outputProtocolVersion() { return outputProtocolVersion; } @@ -50,9 +52,7 @@ public class ProtocolPathEntryImpl implements ProtocolPathEntry { @Override public int hashCode() { - int result = outputProtocolVersion; - result = 31 * result + protocol.hashCode(); - return result; + return Objects.hash(outputProtocolVersion, protocol); } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathKeyImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathKeyImpl.java index 6059d7ffd..9b586dc7a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathKeyImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPathKeyImpl.java @@ -18,23 +18,25 @@ package com.viaversion.viaversion.protocol; import com.viaversion.viaversion.api.protocol.ProtocolPathKey; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import java.util.Objects; public class ProtocolPathKeyImpl implements ProtocolPathKey { - private final int clientProtocolVersion; - private final int serverProtocolVersion; + private final ProtocolVersion clientProtocolVersion; + private final ProtocolVersion serverProtocolVersion; - public ProtocolPathKeyImpl(int clientProtocolVersion, int serverProtocolVersion) { + public ProtocolPathKeyImpl(ProtocolVersion clientProtocolVersion, ProtocolVersion serverProtocolVersion) { this.clientProtocolVersion = clientProtocolVersion; this.serverProtocolVersion = serverProtocolVersion; } @Override - public int clientProtocolVersion() { + public ProtocolVersion clientProtocolVersion() { return clientProtocolVersion; } @Override - public int serverProtocolVersion() { + public ProtocolVersion serverProtocolVersion() { return serverProtocolVersion; } @@ -49,8 +51,6 @@ public class ProtocolPathKeyImpl implements ProtocolPathKey { @Override public int hashCode() { - int result = clientProtocolVersion; - result = 31 * result + serverProtocolVersion; - return result; + return Objects.hash(clientProtocolVersion, serverProtocolVersion); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/VersionedPacketTransformerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/VersionedPacketTransformerImpl.java index e5e1521e7..81cbe7954 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/VersionedPacketTransformerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/VersionedPacketTransformerImpl.java @@ -37,7 +37,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class VersionedPacketTransformerImpl implements VersionedPacketTransformer { - private final int inputProtocolVersion; // TODO Use ProtocolVersion + private final ProtocolVersion inputProtocolVersion; private final Class clientboundPacketsClass; private final Class serverboundPacketsClass; @@ -45,7 +45,7 @@ public class VersionedPacketTransformerImpl path = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol.java index 7912cbdf5..d4d9c05b6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol.java @@ -72,8 +72,7 @@ public class BaseProtocol extends AbstractProtocol protocols = null; if (info.protocolVersion().newerThanOrEquals(closestServerProtocol) || Via.getPlatform().isOldClientsAllowed()) { protocols = Via.getManager().getProtocolManager() - .getProtocolPath(info.protocolVersion().getVersion(), closestServerProtocol.getVersion()); + .getProtocolPath(info.protocolVersion(), closestServerProtocol); } if (protocols != null) { - if (protocolVersion == closestServerProtocol.getVersion() || protocolVersion == 0) { // Fix ServerListPlus + if (protocolVersion.equalTo(closestServerProtocol) || protocolVersion.getVersion() == 0) { // Fix ServerListPlus version.addProperty("protocol", info.protocolVersion().getOriginalVersion()); } } else { @@ -112,7 +114,7 @@ public class BaseProtocol1_7 extends AbstractProtocol { - int protocol = wrapper.user().getProtocolInfo().protocolVersion().getVersion(); + ProtocolVersion protocol = wrapper.user().getProtocolInfo().protocolVersion(); if (Via.getConfig().blockedProtocolVersions().contains(protocol)) { if (!wrapper.user().getChannel().isOpen()) return; if (!wrapper.user().shouldApplyBlockProtocol()) return; diff --git a/common/src/main/java/com/viaversion/viaversion/util/DumpUtil.java b/common/src/main/java/com/viaversion/viaversion/util/DumpUtil.java index c3afc705a..a03ce2ad0 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/DumpUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/DumpUtil.java @@ -57,7 +57,7 @@ public final class DumpUtil { final VersionInfo version = new VersionInfo( System.getProperty("java.version"), System.getProperty("os.name"), - Via.getAPI().getServerVersion().lowestSupportedVersion(), + Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(), Via.getManager().getProtocolManager().getSupportedVersions(), Via.getPlatform().getPlatformName(), Via.getPlatform().getPlatformVersion(), diff --git a/common/src/test/java/com/viaversion/viaversion/common/dummy/TestPlatform.java b/common/src/test/java/com/viaversion/viaversion/common/dummy/TestPlatform.java index f5ba4e891..438c3e573 100644 --- a/common/src/test/java/com/viaversion/viaversion/common/dummy/TestPlatform.java +++ b/common/src/test/java/com/viaversion/viaversion/common/dummy/TestPlatform.java @@ -24,6 +24,7 @@ import com.viaversion.viaversion.api.command.ViaCommandSender; import com.viaversion.viaversion.api.configuration.ViaVersionConfig; import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.ViaPlatform; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import io.netty.buffer.ByteBuf; import java.io.File; import java.util.UUID; @@ -102,8 +103,8 @@ public final class TestPlatform implements ViaPlatform { public ViaAPI getApi() { return new ViaAPIBase() { @Override - public int getPlayerVersion(Object player) { - return 0; + public ProtocolVersion getPlayerProtocolVersion(Object player) { + return ProtocolVersion.unknown; } @Override diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaAPI.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaAPI.java index f540b5e30..83d2e62a8 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaAPI.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaAPI.java @@ -18,14 +18,15 @@ package com.viaversion.viaversion.sponge.platform; import com.viaversion.viaversion.ViaAPIBase; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import io.netty.buffer.ByteBuf; import org.spongepowered.api.entity.living.player.Player; public class SpongeViaAPI extends ViaAPIBase { @Override - public int getPlayerVersion(Player player) { - return getPlayerVersion(player.uniqueId()); + public ProtocolVersion getPlayerProtocolVersion(Player player) { + return getPlayerProtocolVersion(player.uniqueId()); } @Override diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityViaAPI.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityViaAPI.java index 77893a20d..38c157100 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityViaAPI.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityViaAPI.java @@ -19,13 +19,14 @@ package com.viaversion.viaversion.velocity.platform; import com.velocitypowered.api.proxy.Player; import com.viaversion.viaversion.ViaAPIBase; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import io.netty.buffer.ByteBuf; public class VelocityViaAPI extends ViaAPIBase { @Override - public int getPlayerVersion(Player player) { - return getPlayerVersion(player.getUniqueId()); + public ProtocolVersion getPlayerProtocolVersion(Player player) { + return getPlayerProtocolVersion(player.getUniqueId()); } @Override diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/providers/VelocityVersionProvider.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/providers/VelocityVersionProvider.java index c250d53f3..df272f163 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/providers/VelocityVersionProvider.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/providers/VelocityVersionProvider.java @@ -51,8 +51,7 @@ public class VelocityVersionProvider extends BaseVersionProvider { //TODO use newly added Velocity netty event ChannelHandler mcHandler = user.getChannel().pipeline().get("handler"); ServerConnection serverConnection = (ServerConnection) GET_ASSOCIATION.invoke(mcHandler); - final int protocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverConnection.getServerInfo().getName()); - return ProtocolVersion.getProtocol(protocolVersion); + return Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverConnection.getServerInfo().getName()); } private ProtocolVersion getFrontProtocol(UserConnection user) throws Exception { diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/service/ProtocolDetectorService.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/service/ProtocolDetectorService.java index 79e391018..612be037e 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/service/ProtocolDetectorService.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/service/ProtocolDetectorService.java @@ -55,8 +55,8 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi return; } - final int oldProtocolVersion = serverProtocolVersion(serverName); - if (oldProtocolVersion != -1 && oldProtocolVersion == serverPing.getVersion().getProtocol()) { + final ProtocolVersion oldProtocolVersion = serverProtocolVersion(serverName); + if (oldProtocolVersion.isKnown() && oldProtocolVersion.getVersion() == serverPing.getVersion().getProtocol()) { // Same value as previously return; } @@ -86,12 +86,12 @@ public final class ProtocolDetectorService extends AbstractProtocolDetectorServi } @Override - protected int lowestSupportedProtocolVersion() { + protected ProtocolVersion lowestSupportedProtocolVersion() { try { - return Via.getManager().getInjector().getServerProtocolVersion().getVersion(); + return Via.getManager().getInjector().getServerProtocolVersion(); } catch (final Exception e) { e.printStackTrace(); - return ProtocolVersion.v1_8.getVersion(); + return ProtocolVersion.v1_8; } } }