Skip to content

Commit 7f48849

Browse files
author
games647
committed
Minor code styling
* Use logging instead of raw exception printing * Extract method * Shorter var names * More precise generics if possible * Don't restore accessible state could be in conflict with other plugins if we don't restore the exact value
1 parent 9a40cf0 commit 7f48849

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.github.games647.fastlogin.velocity.task.ForceLoginTask;
3939
import com.google.common.cache.Cache;
4040
import com.google.common.collect.ListMultimap;
41+
import com.velocitypowered.api.event.EventManager;
4142
import com.velocitypowered.api.event.EventTask;
4243
import com.velocitypowered.api.event.Subscribe;
4344
import com.velocitypowered.api.event.connection.DisconnectEvent;
@@ -51,7 +52,6 @@
5152
import com.velocitypowered.api.proxy.server.RegisteredServer;
5253
import com.velocitypowered.api.util.GameProfile;
5354
import com.velocitypowered.api.util.GameProfile.Property;
54-
5555
import net.kyori.adventure.text.TextComponent;
5656
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
5757
import org.geysermc.floodgate.api.player.FloodgatePlayer;
@@ -87,10 +87,9 @@ public EventTask onPreLogin(PreLoginEvent preLoginEvent) {
8787
InetSocketAddress address = connection.getRemoteAddress();
8888
plugin.getLog().info("Incoming login request for {} from {}", username, address);
8989

90-
9190
// FloodgateVelocity only sets the correct username in GetProfileRequestEvent, but we need it here too.
9291
if (plugin.getFloodgateService() != null) {
93-
String floodgateUsername = getFloodgateUsername(preLoginEvent, connection);
92+
String floodgateUsername = getFloodgateUsername(connection);
9493
if (floodgateUsername != null) {
9594
plugin.getLog().info("Found player's Floodgate: {}", floodgateUsername);
9695
username = floodgateUsername;
@@ -197,65 +196,71 @@ public void onDisconnect(DisconnectEvent disconnectEvent) {
197196

198197
/**
199198
* Get the Floodgate username from the Floodgate plugin's playerCache using lots of reflections
200-
* @param preLoginEvent
199+
*
201200
* @param connection
202201
* @return the Floodgate username or null if not found
203202
*/
204-
private String getFloodgateUsername(PreLoginEvent preLoginEvent, InboundConnection connection) {
203+
private String getFloodgateUsername(InboundConnection connection) {
205204
try {
206-
// Get Velocity's event manager
207-
Object eventManager = plugin.getServer().getEventManager();
208-
Field handlerField = eventManager.getClass().getDeclaredField("handlersByType");
209-
handlerField.setAccessible(true);
210-
@SuppressWarnings("rawtypes")
211-
ListMultimap handlersByType = (ListMultimap) handlerField.get(eventManager);
212-
handlerField.setAccessible(false);
213-
214-
// Get all registered PreLoginEvent handlers
215-
@SuppressWarnings({ "rawtypes", "unchecked" })
216-
List preLoginEventHandlres = handlersByType.get(preLoginEvent.getClass());
217-
Field pluginField = preLoginEventHandlres.get(0).getClass().getDeclaredField("plugin");
218-
pluginField.setAccessible(true);
219-
Object floodgateEventHandlerRegistration = null;
220-
221-
// Find the Floodgate plugin's PreLoginEvent handler
222-
for (Object handler : preLoginEventHandlres) {
223-
PluginContainer eventHandlerPlugin = (PluginContainer) pluginField.get(handler);
224-
String eventHandlerPluginName = eventHandlerPlugin.getInstance().get().getClass().getName();
225-
if (eventHandlerPluginName.equals(FLOODGATE_PLUGIN_NAME)) {
226-
floodgateEventHandlerRegistration = handler;
227-
break;
228-
}
229-
}
230-
pluginField.setAccessible(false);
231-
if (floodgateEventHandlerRegistration == null) {
205+
// get floodgate's event handler
206+
Object floodgateEventHandler = getFloodgateHandler();
207+
if (floodgateEventHandler == null) {
232208
return null;
233209
}
234210

235-
// Extract the EventHandler instance from Velocity's internal registration handler storage
236-
Field eventHandlerField = floodgateEventHandlerRegistration.getClass().getDeclaredField("instance");
237-
eventHandlerField.setAccessible(true);
238-
Object floodgateEventHandler = eventHandlerField.get(floodgateEventHandlerRegistration);
239-
eventHandlerField.setAccessible(false);
240-
241211
// Get the Floodgate playerCache field
242212
Field playerCacheField = floodgateEventHandler.getClass().getDeclaredField("playerCache");
243213
playerCacheField.setAccessible(true);
244214
@SuppressWarnings("unchecked")
245215
Cache<InboundConnection, FloodgatePlayer> playerCache =
246-
(Cache<InboundConnection, FloodgatePlayer>) playerCacheField.get(floodgateEventHandler);
247-
playerCacheField.setAccessible(false);
216+
(Cache<InboundConnection, FloodgatePlayer>) playerCacheField.get(floodgateEventHandler);
248217

249218
// Find the FloodgatePlayer instance in playerCache
250219
FloodgatePlayer floodgatePlayer = playerCache.getIfPresent(connection);
251220
if (floodgatePlayer == null) {
252221
return null;
253222
}
254-
return floodgatePlayer.getCorrectUsername();
255223

256-
} catch (Exception e) {
257-
e.printStackTrace();
224+
return floodgatePlayer.getCorrectUsername();
225+
} catch (Exception ex) {
226+
plugin.getLog().error("Failed to fetch current floodgate username", ex);
258227
}
228+
259229
return null;
260230
}
231+
232+
private Object getFloodgateHandler()
233+
throws NoSuchFieldException, IllegalAccessException {
234+
// Get Velocity's event manager
235+
EventManager eventManager = plugin.getServer().getEventManager();
236+
Field handlerField = eventManager.getClass().getDeclaredField("handlersByType");
237+
handlerField.setAccessible(true);
238+
@SuppressWarnings("unchecked")
239+
ListMultimap<Class<?>, ?> handlersByType = (ListMultimap<Class<?>, ?>) handlerField.get(eventManager);
240+
241+
// Get all registered PreLoginEvent handlers
242+
List<?> loginEventRegistrations = handlersByType.get(PreLoginEvent.class);
243+
Field pluginField = loginEventRegistrations.get(0).getClass().getDeclaredField("plugin");
244+
pluginField.setAccessible(true);
245+
246+
// Find the Floodgate plugin's PreLoginEvent handler registration (Velocity implementation)
247+
Object floodgateRegistration = null;
248+
for (Object handler : loginEventRegistrations) {
249+
PluginContainer eventHandlerPlugin = (PluginContainer) pluginField.get(handler);
250+
String eventHandlerPluginName = eventHandlerPlugin.getInstance().get().getClass().getName();
251+
if (eventHandlerPluginName.equals(FLOODGATE_PLUGIN_NAME)) {
252+
floodgateRegistration = handler;
253+
break;
254+
}
255+
}
256+
257+
if (floodgateRegistration == null) {
258+
return null;
259+
}
260+
261+
// Extract the EventHandler instance (floodgate impl) from Velocity's internal registration handler storage
262+
Field eventHandlerField = floodgateRegistration.getClass().getDeclaredField("instance");
263+
eventHandlerField.setAccessible(true);
264+
return eventHandlerField.get(floodgateRegistration);
265+
}
261266
}

0 commit comments

Comments
 (0)