Support ViaBackwards 1.13 with Velocity modern forwarding

This commit is contained in:
creeper123123321 2019-02-15 13:51:54 -02:00
parent 7859f5ebd6
commit 6a2e1a8114
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
2 changed files with 39 additions and 10 deletions

View File

@ -4,11 +4,25 @@ import io.netty.channel.ChannelInitializer;
import us.myles.ViaVersion.VelocityPlugin; import us.myles.ViaVersion.VelocityPlugin;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaInjector;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.ReflectionUtil;
import us.myles.ViaVersion.velocity.handlers.VelocityChannelInitializer; import us.myles.ViaVersion.velocity.handlers.VelocityChannelInitializer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class VelocityViaInjector implements ViaInjector { public class VelocityViaInjector implements ViaInjector {
public static Method getPlayerInfoForwardingMode;
static {
try {
getPlayerInfoForwardingMode = Class.forName("com.velocitypowered.proxy.config.VelocityConfiguration").getMethod("getPlayerInfoForwardingMode");
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override @Override
public void inject() throws Exception { public void inject() throws Exception {
Object connectionManager = ReflectionUtil.get(VelocityPlugin.PROXY, "cm", Object.class); Object connectionManager = ReflectionUtil.get(VelocityPlugin.PROXY, "cm", Object.class);
@ -30,6 +44,12 @@ public class VelocityViaInjector implements ViaInjector {
} }
public static int getLowestSupportedProtocolVersion() { public static int getLowestSupportedProtocolVersion() {
try {
if (getPlayerInfoForwardingMode != null
&& ((Enum<?>) getPlayerInfoForwardingMode.invoke(VelocityPlugin.PROXY.getConfiguration()))
.name().equals("MODERN")) return ProtocolVersion.v1_13.getId();
} catch (IllegalAccessException | InvocationTargetException ignored) {
}
return com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION.getProtocol(); return com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION.getProtocol();
} }

View File

@ -1,6 +1,6 @@
package us.myles.ViaVersion.velocity.providers; package us.myles.ViaVersion.velocity.providers;
import com.google.common.collect.Lists; import us.myles.ViaVersion.VelocityPlugin;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.ProtocolVersion;
@ -8,32 +8,41 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.base.VersionProvider;
import us.myles.ViaVersion.velocity.platform.VelocityViaInjector; import us.myles.ViaVersion.velocity.platform.VelocityViaInjector;
import java.util.*; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.IntStream;
public class VelocityVersionProvider extends VersionProvider { public class VelocityVersionProvider extends VersionProvider {
private static final List<Integer> VELOCITY_PROTOCOLS = com.velocitypowered.api.network.ProtocolVersion.SUPPORTED_VERSIONS.stream()
.map(com.velocitypowered.api.network.ProtocolVersion::getProtocol)
.collect(Collectors.toList());
@Override @Override
public int getServerProtocol(UserConnection user) throws Exception { public int getServerProtocol(UserConnection user) throws Exception {
int playerVersion = user.get(ProtocolInfo.class).getProtocolVersion(); int playerVersion = user.get(ProtocolInfo.class).getProtocolVersion();
IntStream versions = com.velocitypowered.api.network.ProtocolVersion.SUPPORTED_VERSIONS.stream()
.mapToInt(com.velocitypowered.api.network.ProtocolVersion::getProtocol);
// Modern forwarding mode needs 1.13 Login plugin message
if (VelocityViaInjector.getPlayerInfoForwardingMode != null
&& ((Enum<?>) VelocityViaInjector.getPlayerInfoForwardingMode.invoke(VelocityPlugin.PROXY.getConfiguration()))
.name().equals("MODERN")) {
versions = versions.filter(ver -> ver >= ProtocolVersion.v1_13.getId());
}
int[] compatibleProtocols = versions.toArray();
// Bungee supports it // Bungee supports it
if (Collections.binarySearch(VELOCITY_PROTOCOLS, playerVersion) >= 0) if (Arrays.binarySearch(compatibleProtocols, playerVersion) >= 0)
return playerVersion; return playerVersion;
// Older than bungee supports, get the lowest version // Older than bungee supports, get the lowest version
if (playerVersion < VELOCITY_PROTOCOLS.get(0)) { if (playerVersion < compatibleProtocols[0]) {
return VelocityViaInjector.getLowestSupportedProtocolVersion(); return compatibleProtocols[0];
} }
// Loop through all protocols to get the closest protocol id that bungee supports (and that viaversion does too) // Loop through all protocols to get the closest protocol id that bungee supports (and that viaversion does too)
// TODO: This needs a better fix, i.e checking ProtocolRegistry to see if it would work. // TODO: This needs a better fix, i.e checking ProtocolRegistry to see if it would work.
// This is more of a workaround for snapshot support by bungee. // This is more of a workaround for snapshot support by bungee.
for (Integer protocol : Lists.reverse(VELOCITY_PROTOCOLS)) { for (int i = compatibleProtocols.length - 1; i >= 0; i--) {
int protocol = compatibleProtocols[i];
if (playerVersion > protocol && ProtocolVersion.isRegistered(protocol)) if (playerVersion > protocol && ProtocolVersion.isRegistered(protocol))
return protocol; return protocol;
} }