Skip to content

Commit 1f3cd5f

Browse files
committed
Share Floodgate name conflict check between Protocol Plugins
Added a shared class for Floodgate name conflict checking that can be used by both ProtocolLib and ProtocolSupport Rebased on Sat May 22 11:42:08 2021 +0200 Added access modifier to "FastLoginBukkit plugin;" in FloodgateHook.java Rebased on Sat May 22 11:42:08 2021 +0200 Initialize FloogateHook in ProtocolLib's class
1 parent 119b9cb commit 1f3cd5f

File tree

3 files changed

+102
-22
lines changed

3 files changed

+102
-22
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.github.games647.fastlogin.bukkit.hook.floodgate;
2+
3+
import java.io.IOException;
4+
import java.util.Optional;
5+
6+
import org.bukkit.Bukkit;
7+
import org.geysermc.floodgate.api.FloodgateApi;
8+
import org.geysermc.floodgate.api.player.FloodgatePlayer;
9+
10+
import com.github.games647.craftapi.model.Profile;
11+
import com.github.games647.craftapi.resolver.RateLimitException;
12+
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
13+
import com.github.games647.fastlogin.core.shared.LoginSource;
14+
15+
public class FloodgateHook {
16+
17+
private final FastLoginBukkit plugin;
18+
19+
public FloodgateHook(FastLoginBukkit plugin) {
20+
this.plugin = plugin;
21+
}
22+
23+
/**
24+
* Check if the player's name conflict's an existing Java player's name, and
25+
* kick them if it does
26+
*
27+
* @param core the FastLoginCore
28+
* @param username the name of the player
29+
* @param source an instance of LoginSource
30+
* @param plugin the FastLoginBukkit plugin
31+
*/
32+
public void checkNameConflict(String username, LoginSource source, FloodgatePlayer floodgatePlayer) {
33+
String allowConflict = plugin.getCore().getConfig().get("allowFloodgateNameConflict").toString().toLowerCase();
34+
if (allowConflict.equals("false")) {
35+
36+
// check for conflicting Premium Java name
37+
Optional<Profile> premiumUUID = Optional.empty();
38+
try {
39+
premiumUUID = plugin.getCore().getResolver().findProfile(username);
40+
} catch (IOException | RateLimitException e) {
41+
e.printStackTrace();
42+
plugin.getLog().error(
43+
"Could not check wether Floodgate Player {}'s name conflits a premium Java player's name.",
44+
username);
45+
}
46+
47+
if (premiumUUID.isPresent()) {
48+
plugin.getLog().info("Bedrock Player {}'s name conflits an existing Java Premium Player's name",
49+
username);
50+
try {
51+
source.kick("Your name conflits an existing Java Premium Player's name");
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
plugin.getLog().error("Could not kick Player {}", username);
55+
}
56+
}
57+
} else {
58+
plugin.getLog().info("Skipping name conflict checking for player {}", username);
59+
}
60+
}
61+
62+
/**
63+
* The FloodgateApi does not support querying players by name, so this function
64+
* iterates over every online FloodgatePlayer and checks if the requested
65+
* username can be found
66+
*
67+
* @param username the name of the player
68+
* @return FloodgatePlayer if found, null otherwise
69+
*/
70+
public FloodgatePlayer getFloodgatePlayer(String username) {
71+
if (Bukkit.getServer().getPluginManager().isPluginEnabled("floodgate")) {
72+
for (FloodgatePlayer floodgatePlayer : FloodgateApi.getInstance().getPlayers()) {
73+
if (floodgatePlayer.getUsername().equals(username)) {
74+
return floodgatePlayer;
75+
}
76+
}
77+
}
78+
return null;
79+
}
80+
81+
}

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
import com.github.games647.fastlogin.bukkit.BukkitLoginSession;
3131
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
3232
import com.github.games647.fastlogin.bukkit.event.BukkitFastLoginPreLoginEvent;
33+
import com.github.games647.fastlogin.bukkit.hook.floodgate.FloodgateHook;
3334
import com.github.games647.fastlogin.core.StoredProfile;
3435
import com.github.games647.fastlogin.core.shared.JoinManagement;
3536
import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent;
3637

3738
import java.security.PublicKey;
3839
import java.util.Random;
3940

40-
import org.bukkit.Bukkit;
4141
import org.bukkit.command.CommandSender;
4242
import org.bukkit.entity.Player;
43-
import org.geysermc.floodgate.api.FloodgateApi;
4443
import org.geysermc.floodgate.api.player.FloodgatePlayer;
4544

4645
public class NameCheckTask extends JoinManagement<Player, CommandSender, ProtocolLibLoginSource>
@@ -55,8 +54,10 @@ public class NameCheckTask extends JoinManagement<Player, CommandSender, Protoco
5554
private final Player player;
5655
private final String username;
5756

57+
private final FloodgateHook floodgateHook;
58+
5859
public NameCheckTask(FastLoginBukkit plugin, PacketEvent packetEvent, Random random,
59-
Player player, String username, PublicKey publicKey) {
60+
Player player, String username, PublicKey publicKey, FloodgateHook floodgateHook) {
6061
super(plugin.getCore(), plugin.getCore().getAuthPluginHook());
6162

6263
this.plugin = plugin;
@@ -65,18 +66,23 @@ public NameCheckTask(FastLoginBukkit plugin, PacketEvent packetEvent, Random ran
6566
this.random = random;
6667
this.player = player;
6768
this.username = username;
69+
this.floodgateHook = floodgateHook;
6870
}
6971

7072
@Override
7173
public void run() {
7274
try {
73-
// check if the player is connecting through Geyser
74-
if (!plugin.getCore().getConfig().get("allowFloodgateNameConflict").toString().equalsIgnoreCase("false")
75-
&& getFloodgatePlayer(username) != null) {
76-
plugin.getLog().info("Skipping name conflict checking for player {}", username);
77-
return;
75+
ProtocolLibLoginSource source = new ProtocolLibLoginSource(packetEvent, player, random, publicKey);
76+
77+
//check if the player is connecting through Floodgate
78+
FloodgatePlayer floodgatePlayer = floodgateHook.getFloodgatePlayer(username);
79+
80+
if (floodgatePlayer != null) {
81+
floodgateHook.checkNameConflict(username, source, floodgatePlayer);
82+
} else {
83+
//do Java login tasks
84+
super.onLogin(username, source);
7885
}
79-
super.onLogin(username, new ProtocolLibLoginSource(packetEvent, player, random, publicKey));
8086
} finally {
8187
ProtocolLibrary.getProtocolManager().getAsynchronousManager().signalPacketTransmission(packetEvent);
8288
}
@@ -120,16 +126,5 @@ public void startCrackedSession(ProtocolLibLoginSource source, StoredProfile pro
120126
BukkitLoginSession loginSession = new BukkitLoginSession(username, profile);
121127
plugin.putSession(player.getAddress(), loginSession);
122128
}
123-
124-
private static FloodgatePlayer getFloodgatePlayer(String username) {
125-
if (Bukkit.getServer().getPluginManager().isPluginEnabled("floodgate")) {
126-
// the Floodgate API requires UUID, which is inaccessible at NameCheckTask.java
127-
for (FloodgatePlayer floodgatePlayer : FloodgateApi.getInstance().getPlayers()) {
128-
if (floodgatePlayer.getUsername().equals(username)) {
129-
return floodgatePlayer;
130-
}
131-
}
132-
}
133-
return null;
134-
}
129+
135130
}

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.comphenix.protocol.events.PacketContainer;
3232
import com.comphenix.protocol.events.PacketEvent;
3333
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
34+
import com.github.games647.fastlogin.bukkit.hook.floodgate.FloodgateHook;
3435
import com.github.games647.fastlogin.core.RateLimiter;
3536

3637
import java.security.KeyPair;
@@ -49,6 +50,7 @@ public class ProtocolLibListener extends PacketAdapter {
4950
private final SecureRandom random = new SecureRandom();
5051
private final KeyPair keyPair = EncryptionUtil.generateKeyPair();
5152
private final RateLimiter rateLimiter;
53+
private final FloodgateHook floodgateHook;
5254

5355
public ProtocolLibListener(FastLoginBukkit plugin, RateLimiter rateLimiter) {
5456
//run async in order to not block the server, because we are making api calls to Mojang
@@ -59,6 +61,7 @@ public ProtocolLibListener(FastLoginBukkit plugin, RateLimiter rateLimiter) {
5961

6062
this.plugin = plugin;
6163
this.rateLimiter = rateLimiter;
64+
this.floodgateHook = new FloodgateHook(plugin);
6265
}
6366

6467
public static void register(FastLoginBukkit plugin, RateLimiter rateLimiter) {
@@ -113,7 +116,8 @@ private void onLogin(PacketEvent packetEvent, Player player) {
113116
plugin.getLog().trace("GameProfile {} with {} connecting", sessionKey, username);
114117

115118
packetEvent.getAsyncMarker().incrementProcessingDelay();
116-
Runnable nameCheckTask = new NameCheckTask(plugin, packetEvent, random, player, username, keyPair.getPublic());
119+
Runnable nameCheckTask = new NameCheckTask(plugin, packetEvent, random, player, username, keyPair.getPublic(),
120+
floodgateHook);
117121
plugin.getScheduler().runAsync(nameCheckTask);
118122
}
119123
}

0 commit comments

Comments
 (0)