3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Fix Velocity platform breaking changes

Dieser Commit ist enthalten in:
creeper123123321 2018-11-26 15:58:17 -02:00
Ursprung 73dccbaf24
Commit ad3465f0d3
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 0AC57D54786721D1
4 geänderte Dateien mit 27 neuen und 50 gelöschten Zeilen

Datei anzeigen

@ -2,20 +2,18 @@ package us.myles.ViaVersion.velocity.handlers;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import lombok.NonNull; import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline; import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@RequiredArgsConstructor @AllArgsConstructor
public class VelocityChannelInitializer extends ChannelInitializer { public class VelocityChannelInitializer extends ChannelInitializer {
@NonNull
private ChannelInitializer original; private ChannelInitializer original;
private Method initChannel; private static Method initChannel;
{ static {
try { try {
initChannel = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); initChannel = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
initChannel.setAccessible(true); initChannel.setAccessible(true);

Datei anzeigen

@ -4,6 +4,7 @@ import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.player.ServerPreConnectEvent; import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.network.ProtocolVersion;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.boss.BossBar; import us.myles.ViaVersion.api.boss.BossBar;
@ -28,8 +29,10 @@ public class VelocityServerHandler {
static { static {
try { try {
setProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection").getDeclaredMethod("setProtocolVersion", int.class); setProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection")
setNextProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection").getDeclaredMethod("setNextProtocolVersion", int.class); .getDeclaredMethod("setProtocolVersion", ProtocolVersion.class);
setNextProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection")
.getDeclaredMethod("setNextProtocolVersion", ProtocolVersion.class);
} catch (NoSuchMethodException | ClassNotFoundException e) { } catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -49,7 +52,9 @@ public class VelocityServerHandler {
// Check if ViaVersion can support that version // Check if ViaVersion can support that version
Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection"); Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection");
setNextProtocolVersion.invoke(connection, protocols == null ? user.get(ProtocolInfo.class).getProtocolVersion() : protocolId); setNextProtocolVersion.invoke(connection, ProtocolVersion.getProtocolVersion(protocols == null
? user.get(ProtocolInfo.class).getProtocolVersion()
: protocolId));
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e1) { } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e1) {
e1.printStackTrace(); e1.printStackTrace();
@ -128,8 +133,8 @@ public class VelocityServerHandler {
} }
Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection"); Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection");
int version = (int) ReflectionUtil.invoke(connection,"getNextProtocolVersion"); ProtocolVersion version = (ProtocolVersion) ReflectionUtil.invoke(connection,"getNextProtocolVersion");
setProtocolVersion.invoke(ReflectionUtil.invoke(e.getPlayer(), "getConnection"), version); setProtocolVersion.invoke(connection, version);
} }
} }
user.getVelocityLock().writeLock().unlock(); user.getVelocityLock().writeLock().unlock();

Datei anzeigen

@ -29,8 +29,8 @@ public class VelocityViaInjector implements ViaInjector {
return getLowestSupportedProtocolVersion(); return getLowestSupportedProtocolVersion();
} }
public static int getLowestSupportedProtocolVersion() throws Exception { public static int getLowestSupportedProtocolVersion() {
return ReflectionUtil.getStatic(Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants"), "MINIMUM_GENERIC_VERSION", int.class); return com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION.getProtocol();
} }
@Override @Override

Datei anzeigen

@ -6,42 +6,30 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; 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.util.ReflectionUtil; import us.myles.ViaVersion.velocity.platform.VelocityViaInjector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class VelocityVersionProvider extends VersionProvider { public class VelocityVersionProvider extends VersionProvider {
private static Class<?> ref ;
static {
try {
ref = Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants");
} catch (Exception e) {
Via.getPlatform().getLogger().severe("Could not detect the ProtocolConstants class");
e.printStackTrace();
}
}
@Override @Override
public int getServerProtocol(UserConnection user) throws Exception { public int getServerProtocol(UserConnection user) throws Exception {
if (ref == null)
return super.getServerProtocol(user);
// TODO Have one constant list forever until restart? (Might limit plugins if they change this) // TODO Have one constant list forever until restart? (Might limit plugins if they change this)
Object list = ReflectionUtil.getStatic(ref, "SUPPORTED_VERSIONS", Object.class); List<Integer> sorted = new ArrayList<>(com.velocitypowered.api.network.ProtocolVersion.ID_TO_PROTOCOL_CONSTANT.keySet());
List<Integer> sorted = new ArrayList<Integer>((List) ReflectionUtil.invoke(list, "asList")); sorted.remove(Integer.valueOf(-1)); // Unknown/legacy
Collections.sort(sorted); Collections.sort(sorted);
ProtocolInfo info = user.get(ProtocolInfo.class); int playerVersion = user.get(ProtocolInfo.class).getProtocolVersion();
// Bungee supports it // Bungee supports it
if (sorted.contains(info.getProtocolVersion())) if (sorted.contains(playerVersion))
return info.getProtocolVersion(); return playerVersion;
// Older than bungee supports, get the lowest version // Older than bungee supports, get the lowest version
if (info.getProtocolVersion() < sorted.get(0)) { if (playerVersion < sorted.get(0)) {
return getLowestSupportedVersion(); return VelocityViaInjector.getLowestSupportedProtocolVersion();
} }
// 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)
@ -49,25 +37,11 @@ public class VelocityVersionProvider extends VersionProvider {
// 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(sorted)) { for (Integer protocol : Lists.reverse(sorted)) {
if (info.getProtocolVersion() > protocol && ProtocolVersion.isRegistered(protocol)) if (playerVersion > protocol && ProtocolVersion.isRegistered(protocol))
return protocol; return protocol;
} }
Via.getPlatform().getLogger().severe("Panic, no protocol id found for " + info.getProtocolVersion()); Via.getPlatform().getLogger().severe("Panic, no protocol id found for " + playerVersion);
return info.getProtocolVersion(); return playerVersion;
}
public static int getLowestSupportedVersion() {
List<Integer> list;
try {
return ReflectionUtil.getStatic(
Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants"),
"MINIMUM_GENERIC_VERSION",
int.class);
} catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
// Fallback
return -1;
} }
} }