Skip to content

Commit

Permalink
Spigot 1.20.2
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <[email protected]>
  • Loading branch information
Kas-tle committed Sep 23, 2023
1 parent 492be77 commit 894aa22
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 16 deletions.
31 changes: 31 additions & 0 deletions bungee/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import org.gradle.plugins.ide.eclipse.model.AccessRule
import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry

plugins {
// suppress unsafe access errors for eclipse
id("eclipse")
}

var bungeeCommit = "master-SNAPSHOT"
var gsonVersion = "2.8.0"
var guavaVersion = "21.0"
Expand All @@ -19,3 +27,26 @@ relocate("org.yaml")
provided("com.github.SpigotMC.BungeeCord", "bungeecord-proxy", bungeeCommit)
provided("com.google.code.gson", "gson", gsonVersion)
provided("com.google.guava", "guava", guavaVersion)

// found how to do this here https://github.com/JFormDesigner/markdown-writer-fx/blob/main/build.gradle.kts
eclipse {
classpath {
file {
whenMerged.add( object: Action<org.gradle.plugins.ide.eclipse.model.Classpath> {
override fun execute( classpath: org.gradle.plugins.ide.eclipse.model.Classpath ) {
val jre = classpath.entries.find {
it is AbstractClasspathEntry &&
it.path.contains("org.eclipse.jdt.launching.JRE_CONTAINER")
} as AbstractClasspathEntry

// make sun.misc accessible in Eclipse project
// (when refreshing Gradle project in buildship)
jre.accessRules.add(AccessRule("accessible", "sun/misc/**"))

// remove trailing slash from jre path
if (jre.path.endsWith("/")) jre.path = jre.path.substring(0, jre.path.length - 1)
}
} )
}
}
}
14 changes: 13 additions & 1 deletion database/mongo/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
plugins {
// allow resolution of compileOnlyApi dependencies in Eclipse
id("eclipse")
}

val mongoClientVersion = "4.4.1"

dependencies {
Expand All @@ -8,4 +13,11 @@ dependencies {
description = "The Floodgate database extension for MongoDB"

relocate("com.mongodb")
relocate("org.bson")
relocate("org.bson")

eclipse {
classpath {
configurations.compileOnlyApi.get().setCanBeResolved(true)
plusConfigurations.add( configurations.compileOnlyApi.get() )
}
}
12 changes: 12 additions & 0 deletions database/mysql/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
plugins {
// allow resolution of compileOnlyApi dependencies in Eclipse
id("eclipse")
}

dependencies {
provided(projects.core)

Expand All @@ -15,3 +20,10 @@ description = "The Floodgate database extension for MySQL"
relocate("com.mysql")
relocate("com.zaxxer.hikari")
relocate("org.slf4j")

eclipse {
classpath {
configurations.compileOnlyApi.get().setCanBeResolved(true)
plusConfigurations.add( configurations.compileOnlyApi.get() )
}
}
12 changes: 12 additions & 0 deletions database/sqlite/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
plugins {
// allow resolution of compileOnlyApi dependencies in Eclipse
id("eclipse")
}

val sqliteJdbcVersion = "3.36.0.3"

dependencies {
Expand All @@ -6,3 +11,10 @@ dependencies {
}

description = "The Floodgate database extension for SQLite"

eclipse {
classpath {
configurations.compileOnlyApi.get().setCanBeResolved(true)
plusConfigurations.add( configurations.compileOnlyApi.get() )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.mojang.authlib.GameProfile;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.floodgate.config.FloodgateConfig;
Expand Down Expand Up @@ -58,8 +59,30 @@ protected void setNewIp(Channel channel, InetSocketAddress newIp) {

@Override
protected Object setHostname(Object handshakePacket, String hostname) {
setValue(handshakePacket, ClassNames.HANDSHAKE_HOST, hostname);
return handshakePacket;
if (ClassNames.IS_PRE_1_20_2) {
// 1.20.1 and below
setValue(handshakePacket, ClassNames.HANDSHAKE_HOST, hostname);

return handshakePacket;
} else {
// 1.20.2 and above
try {
Object[] components = new Object[]{
ClassNames.HANDSHAKE_PORT.get(handshakePacket),
hostname,
ClassNames.HANDSHAKE_PROTOCOL.get(handshakePacket),
ClassNames.HANDSHAKE_INTENTION.get(handshakePacket)
};

System.out.println(components + " ");

return ClassNames.HANDSHAKE_PACKET_CONSTRUCTOR.newInstance(components);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();

return handshakePacket;
}
}
}

@Override
Expand Down Expand Up @@ -104,7 +127,7 @@ protected boolean shouldCallFireRead(Object queuedPacket) {

@Override
public boolean channelRead(Object packet) throws Exception {
if (ClassNames.HANDSHAKE_PACKET.isInstance(packet)) {
if (ClassNames.HANDSHAKE_PACKET.isInstance(packet)) {
// ProtocolSupport would break if we added this during the creation of this handler
ctx.pipeline().addAfter("splitter", "floodgate_packet_blocker", blocker);

Expand Down Expand Up @@ -154,21 +177,30 @@ private boolean checkAndHandleLogin(Object packet) throws Exception {
);
setValue(packetListener, ClassNames.LOGIN_PROFILE, gameProfile);

// we have to fake the offline player (login) cycle
// just like on Spigot:
// we have to fake the offline player (login) cycle
// just like on Spigot:

Object loginHandler =
ClassNames.LOGIN_HANDLER_CONSTRUCTOR.newInstance(packetListener);

if (ClassNames.IS_PRE_1_20_2) {
// 1.20.1 and below

// LoginListener#initUUID
// new LoginHandler().fireEvents();

// and the tick of LoginListener will do the rest

ClassNames.INIT_UUID.invoke(packetListener);

Object loginHandler =
ClassNames.LOGIN_HANDLER_CONSTRUCTOR.newInstance(packetListener);
ClassNames.FIRE_LOGIN_EVENTS.invoke(loginHandler);
} else {
// 1.20.2 and above we directly register the profile

ClassNames.FIRE_LOGIN_EVENTS_GAME_PROFILE.invoke(loginHandler, gameProfile);
}

ctx.pipeline().remove(this);

return true;
}
return false;
Expand Down
75 changes: 68 additions & 7 deletions spigot/src/main/java/org/geysermc/floodgate/util/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import static org.geysermc.floodgate.util.ReflectionUtils.getBooleanValue;
import static org.geysermc.floodgate.util.ReflectionUtils.getClassOrFallback;
import static org.geysermc.floodgate.util.ReflectionUtils.getClassSilently;
import static org.geysermc.floodgate.util.ReflectionUtils.getConstructor;
import static org.geysermc.floodgate.util.ReflectionUtils.getField;
import static org.geysermc.floodgate.util.ReflectionUtils.getFieldOfType;
import static org.geysermc.floodgate.util.ReflectionUtils.getMethod;
import static org.geysermc.floodgate.util.ReflectionUtils.getValue;
import static org.geysermc.floodgate.util.ReflectionUtils.invoke;
import static org.geysermc.floodgate.util.ReflectionUtils.makeAccessible;

import com.google.common.base.Preconditions;
import com.mojang.authlib.GameProfile;
Expand All @@ -58,27 +60,35 @@ public class ClassNames {
public static final Class<?> LOGIN_START_PACKET;
public static final Class<?> LOGIN_LISTENER;
public static final Class<?> LOGIN_HANDLER;
@Nullable public static final Class<?> CLIENT_INTENT;

public static final Constructor<OfflinePlayer> CRAFT_OFFLINE_PLAYER_CONSTRUCTOR;
public static final Constructor<?> LOGIN_HANDLER_CONSTRUCTOR;
@Nullable public static final Constructor<?> HANDSHAKE_PACKET_CONSTRUCTOR;

public static final Field SOCKET_ADDRESS;
public static final Field HANDSHAKE_HOST;
public static final Field LOGIN_PROFILE;
public static final Field PACKET_LISTENER;

@Nullable public static final Field HANDSHAKE_PORT;
@Nullable public static final Field HANDSHAKE_PROTOCOL;
@Nullable public static final Field HANDSHAKE_INTENTION;

@Nullable public static final Field PAPER_DISABLE_USERNAME_VALIDATION;
@Nullable public static final BooleanSupplier PAPER_VELOCITY_SUPPORT;

public static final Method GET_PROFILE_METHOD;
public static final Method LOGIN_DISCONNECT;
public static final Method NETWORK_EXCEPTION_CAUGHT;
public static final Method INIT_UUID;
public static final Method FIRE_LOGIN_EVENTS;
@Nullable public static final Method INIT_UUID;
@Nullable public static final Method FIRE_LOGIN_EVENTS;
@Nullable public static final Method FIRE_LOGIN_EVENTS_GAME_PROFILE;

public static final Field BUNGEE;

public static final boolean IS_FOLIA;
public static final boolean IS_PRE_1_20_2;

static {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
Expand Down Expand Up @@ -153,14 +163,24 @@ public class ClassNames {
);

// there are multiple no-arg void methods
// Pre 1.20.2 uses initUUID so if it's null, we're on 1.20.2 or later
INIT_UUID = getMethod(LOGIN_LISTENER, "initUUID");
checkNotNull(INIT_UUID, "initUUID from LoginListener");
IS_PRE_1_20_2 = INIT_UUID != null;

Class<?> packetListenerClass = getClassOrFallback(
if (IS_PRE_1_20_2) {
Class<?> packetListenerClass = getClassOrFallback(
"net.minecraft.network.PacketListener",
nmsPackage + "PacketListener"
);
PACKET_LISTENER = getFieldOfType(networkManager, packetListenerClass);
);

PACKET_LISTENER = getFieldOfType(networkManager, packetListenerClass);
} else {
// We get the field by name on 1.20.2+ as there are now multiple fields of this type in network manager

// PacketListener packetListener of NetworkManager
PACKET_LISTENER = getField(networkManager, "q");
makeAccessible(PACKET_LISTENER);
}
checkNotNull(PACKET_LISTENER, "Packet listener");

LOGIN_HANDLER = getClassOrFallback(
Expand All @@ -173,8 +193,10 @@ public class ClassNames {
checkNotNull(LOGIN_HANDLER_CONSTRUCTOR, "LoginHandler constructor");

FIRE_LOGIN_EVENTS = getMethod(LOGIN_HANDLER, "fireEvents");
checkNotNull(FIRE_LOGIN_EVENTS, "fireEvents from LoginHandler");

// LoginHandler().fireEvents(GameProfile)
FIRE_LOGIN_EVENTS_GAME_PROFILE = getMethod(LOGIN_HANDLER, "fireEvents", GameProfile.class);
checkNotNull(FIRE_LOGIN_EVENTS, FIRE_LOGIN_EVENTS_GAME_PROFILE, "fireEvents from LoginHandler", "fireEvents(GameProfile) from LoginHandler");

PAPER_DISABLE_USERNAME_VALIDATION = getField(LOGIN_LISTENER,
"iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation");
Expand Down Expand Up @@ -229,9 +251,48 @@ public class ClassNames {
IS_FOLIA = ReflectionUtils.getClassSilently(
"io.papermc.paper.threadedregions.RegionizedServer"
) != null;

if (!IS_PRE_1_20_2) {
// PacketHandshakingInSetProtocol is now a record
// This means its fields are now private and final
// We therefore must use reflection to obtain the constructor
CLIENT_INTENT = getClassOrFallback(
"net.minecraft.network.protocol.handshake.ClientIntent",
nmsPackage + "ClientIntent"
);
checkNotNull(CLIENT_INTENT, "Client intent enum");

HANDSHAKE_PACKET_CONSTRUCTOR = getConstructor(HANDSHAKE_PACKET, false, int.class, String.class, int.class, CLIENT_INTENT);
checkNotNull(HANDSHAKE_PACKET_CONSTRUCTOR, "Handshake packet constructor");

HANDSHAKE_PORT = getField(HANDSHAKE_PACKET, "a");
checkNotNull(HANDSHAKE_PORT, "Handshake port");
makeAccessible(HANDSHAKE_PORT);

HANDSHAKE_PROTOCOL = getField(HANDSHAKE_PACKET, "c");
checkNotNull(HANDSHAKE_PROTOCOL, "Handshake protocol");
makeAccessible(HANDSHAKE_PROTOCOL);

HANDSHAKE_INTENTION = getFieldOfType(HANDSHAKE_PACKET, CLIENT_INTENT);
checkNotNull(HANDSHAKE_INTENTION, "Handshake intention");
makeAccessible(HANDSHAKE_INTENTION);
} else {
CLIENT_INTENT = null;
HANDSHAKE_PACKET_CONSTRUCTOR = null;
HANDSHAKE_PORT = null;
HANDSHAKE_PROTOCOL = null;
HANDSHAKE_INTENTION = null;
}
}

private static <T> T checkNotNull(@CheckForNull T toCheck, @CheckForNull String objectName) {
return Preconditions.checkNotNull(toCheck, objectName + " cannot be null");
}

// Ensure one of two is not null
private static <T> T checkNotNull(@CheckForNull T toCheck, @CheckForNull T toCheck2,
@CheckForNull String objectName, @CheckForNull String objectName2) {
return Preconditions.checkNotNull(toCheck != null ? toCheck : toCheck2,
objectName2 + " cannot be null if " + objectName + " is null");
}
}

0 comments on commit 894aa22

Please sign in to comment.