Use Paper method to get server protocol version if possible

This commit is contained in:
KennyTV 2021-04-12 20:11:05 +02:00
parent 76e739e4f0
commit 7300a69817
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
6 changed files with 28 additions and 4 deletions

View File

@ -47,11 +47,11 @@ subprojects {
} }
repositories { repositories {
maven("https://repo.viaversion.com")
maven("https://papermc.io/repo/repository/maven-public/")
maven("https://oss.sonatype.org/content/repositories/snapshots/") maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots")
maven("https://nexus.velocitypowered.com/repository/velocity-artifacts-snapshots/") maven("https://nexus.velocitypowered.com/repository/velocity-artifacts-snapshots/")
maven("https://repo.spongepowered.org/repository/maven-public/") maven("https://repo.spongepowered.org/repository/maven-public/")
maven("https://repo.viaversion.com")
maven("https://libraries.minecraft.net") maven("https://libraries.minecraft.net")
maven("https://repo.maven.apache.org/maven2/") maven("https://repo.maven.apache.org/maven2/")
} }

View File

@ -15,7 +15,7 @@ object Versions {
const val checkerQual = "3.12.0" const val checkerQual = "3.12.0"
// Platforms // Platforms
const val spigot = "1.16.5-R0.1-SNAPSHOT" const val paper = "1.16.5-R0.1-SNAPSHOT"
const val legacyBukkit = "1.8.8-R0.1-SNAPSHOT" const val legacyBukkit = "1.8.8-R0.1-SNAPSHOT"
const val bungee = "1.16-R0.5-SNAPSHOT" const val bungee = "1.16-R0.5-SNAPSHOT"
const val sponge = "5.0.0" const val sponge = "5.0.0"

View File

@ -2,7 +2,7 @@ dependencies {
implementation(project(":viaversion-bukkit-legacy")) implementation(project(":viaversion-bukkit-legacy"))
implementation(project(":viaversion-common")) implementation(project(":viaversion-common"))
implementation("org.javassist", "javassist", Versions.javassist) implementation("org.javassist", "javassist", Versions.javassist)
compileOnly("org.spigotmc", "spigot-api", Versions.spigot) { compileOnly("com.destroystokyo.paper", "paper-api", Versions.paper) {
exclude("junit", "junit") exclude("junit", "junit")
exclude("com.google.code.gson", "gson") exclude("com.google.code.gson", "gson")
exclude("javax.persistence", "persistence-api") exclude("javax.persistence", "persistence-api")

View File

@ -23,6 +23,8 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import org.bukkit.Bukkit;
import org.bukkit.UnsafeValues;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
@ -39,10 +41,12 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
//TODO screams
public class BukkitViaInjector implements ViaInjector { public class BukkitViaInjector implements ViaInjector {
private final List<ChannelFuture> injectedFutures = new ArrayList<>(); private final List<ChannelFuture> injectedFutures = new ArrayList<>();
private final List<Pair<Field, Object>> injectedLists = new ArrayList<>(); private final List<Pair<Field, Object>> injectedLists = new ArrayList<>();
private final boolean modernPaper = hasServerProtocolMethod();
private boolean protocolLib; private boolean protocolLib;
@Override @Override
@ -186,6 +190,11 @@ public class BukkitViaInjector implements ViaInjector {
@Override @Override
public int getServerProtocolVersion() throws Exception { public int getServerProtocolVersion() throws Exception {
if (modernPaper) {
// *Trust me, it's safe*
return Bukkit.getUnsafe().getProtocolVersion();
}
try { try {
Class<?> serverClazz = NMSUtil.nms("MinecraftServer"); Class<?> serverClazz = NMSUtil.nms("MinecraftServer");
Object server = ReflectionUtil.invokeStatic(serverClazz, "getServer"); Object server = ReflectionUtil.invokeStatic(serverClazz, "getServer");
@ -367,4 +376,17 @@ public class BukkitViaInjector implements ViaInjector {
public void setProtocolLib(boolean protocolLib) { public void setProtocolLib(boolean protocolLib) {
this.protocolLib = protocolLib; this.protocolLib = protocolLib;
} }
public boolean isModernPaper() {
return modernPaper;
}
private static boolean hasServerProtocolMethod() {
try {
Class.forName("org.bukkit.UnsafeValues").getDeclaredMethod("getProtocolVersion");
return true;
} catch (ReflectiveOperationException e) {
return false;
}
}
} }

View File

@ -42,6 +42,7 @@ public class BungeeViaInjector implements ViaInjector {
field.setAccessible(true); field.setAccessible(true);
// Remove the final modifier (unless removed by a fork) // Remove the final modifier (unless removed by a fork)
//TODO Fix Java 16 compatibility
int modifiers = field.getModifiers(); int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers)) { if (Modifier.isFinal(modifiers)) {
try { try {

View File

@ -37,6 +37,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
//TODO screams
public class SpongeViaInjector implements ViaInjector { public class SpongeViaInjector implements ViaInjector {
private List<ChannelFuture> injectedFutures = new ArrayList<>(); private List<ChannelFuture> injectedFutures = new ArrayList<>();
private List<Pair<Field, Object>> injectedLists = new ArrayList<>(); private List<Pair<Field, Object>> injectedLists = new ArrayList<>();