Skip to content

Commit

Permalink
Fix setting offline UUID with newer BungeeCord
Browse files Browse the repository at this point in the history
Fixes #1189
Fixes #1193
  • Loading branch information
games647 committed Apr 30, 2024
1 parent a65a5f3 commit 896e250
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,43 @@ public class ConnectListener implements Listener {
private static final String UUID_FIELD_NAME = "uniqueId";
protected static final MethodHandle UNIQUE_ID_SETTER;

private static final String REWRITE_ID_NAME = "rewriteId";
protected static final MethodHandle REWRITE_ID_SETTER;

static {
MethodHandle setHandle = null;
MethodHandle uniqueIdHandle = null;
MethodHandle rewriterHandle = null;
try {
Lookup lookup = MethodHandles.lookup();

// test for implementation class availability
Class.forName("net.md_5.bungee.connection.InitialHandler");

Field uuidField = InitialHandler.class.getDeclaredField(UUID_FIELD_NAME);
uuidField.setAccessible(true);
setHandle = lookup.unreflectSetter(uuidField);
uniqueIdHandle = getHandlerSetter(lookup, UUID_FIELD_NAME);
try {
rewriterHandle = getHandlerSetter(lookup, REWRITE_ID_NAME);
} catch (NoSuchFieldException noSuchFieldEx) {
Logger logger = LoggerFactory.getLogger(ConnectListener.class);
logger.error(
"Rewrite field not found. Setting only legacy BungeeCord field"
);
}
} catch (ReflectiveOperationException reflectiveOperationException) {
Logger logger = LoggerFactory.getLogger(ConnectListener.class);
logger.error(
"Cannot find Bungee initial handler; Disabling premium UUID and skin won't work.",
"Cannot find Bungee UUID field implementation; Disabling premium UUID and skin won't work.",
reflectiveOperationException
);
}

UNIQUE_ID_SETTER = setHandle;
UNIQUE_ID_SETTER = uniqueIdHandle;
REWRITE_ID_SETTER = rewriterHandle;
}

private static MethodHandle getHandlerSetter(Lookup lookup, String fieldName)
throws NoSuchFieldException, IllegalAccessException {
Field uuidField = InitialHandler.class.getDeclaredField(fieldName);
uuidField.setAccessible(true);
return lookup.unreflectSetter(uuidField);
}

private final FastLoginBungee plugin;
Expand Down Expand Up @@ -179,6 +197,12 @@ protected void setOfflineId(InitialHandler connection, String username) {
// So we have to do it with reflection
UNIQUE_ID_SETTER.invokeExact(connection, offlineUUID);

// if available set rewrite id to forward the UUID for newer BungeeCord versions since
// https://github.com/SpigotMC/BungeeCord/commit/1be25b6c74ec2be4b15adf8ca53a0497f01e2afe
if (REWRITE_ID_SETTER != null) {
REWRITE_ID_SETTER.invokeExact(connection, offlineUUID);
}

String format = "Overridden UUID from {} to {} (based of {}) on {}";
plugin.getLog().info(format, oldPremiumId, offlineUUID, username, connection);
} catch (Exception ex) {
Expand Down

0 comments on commit 896e250

Please sign in to comment.