Move Floodgate name conflict check to Core

This commit is contained in:
Smart123s committed 2021-06-12 19:57:31 +02:00
1 parent 0e8ad6e318
commit ee2b3a37f8
7 files changed
+106 -83

No files matched your search

@@ -25,72 +25,13 @@
*/
package com.github.games647.fastlogin.bukkit.hook.floodgate;
import java.io.IOException;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import com.github.games647.craftapi.model.Profile;
import com.github.games647.craftapi.resolver.RateLimitException;
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
import com.github.games647.fastlogin.core.shared.LoginSource;
public class FloodgateHook {
private final FastLoginBukkit plugin;
public FloodgateHook(FastLoginBukkit plugin) {
this.plugin = plugin;
}
/**
* Check if the player's name conflicts an existing Java player's name, and
* kick them if it does
*
* @param core the FastLoginCore
* @param username the name of the player
* @param source an instance of LoginSource
* @param plugin the FastLoginBukkit plugin
*/
public void checkNameConflict(String username, LoginSource source, FloodgatePlayer floodgatePlayer) {
String allowConflict = plugin.getCore().getConfig().get("allowFloodgateNameConflict").toString().toLowerCase();
// check if the Bedrock player is linked to a Java account
boolean isLinked = floodgatePlayer.getLinkedPlayer() != null;
if (allowConflict.equals("false")
|| allowConflict.equals("linked") && !isLinked) {
// check for conflicting Premium Java name
Optional<Profile> premiumUUID = Optional.empty();
try {
premiumUUID = plugin.getCore().getResolver().findProfile(username);
} catch (IOException | RateLimitException e) {
plugin.getLog().error(
"Could not check wether Floodgate Player {}'s name conflicts a premium Java player's name.",
username);
try {
source.kick("Could not check if your name conflicts an existing Java Premium Player's name");
} catch (Exception e1) {
plugin.getLog().error("Could not kick Player {}", username);
}
}
if (premiumUUID.isPresent()) {
plugin.getLog().info("Bedrock Player {}'s name conflicts an existing Java Premium Player's name",
username);
try {
source.kick("Your name conflicts an existing Java Premium Player's name");
} catch (Exception e) {
plugin.getLog().error("Could not kick Player {}", username);
}
}
} else {
plugin.getLog().info("Skipping name conflict checking for player {}", username);
}
}
public FloodgateHook() { }
/**
* The FloodgateApi does not support querying players by name, so this function
@@ -72,17 +72,7 @@ public class NameCheckTask extends JoinManagement<Player, CommandSender, Protoco
@Override
public void run() {
try {
ProtocolLibLoginSource source = new ProtocolLibLoginSource(packetEvent, player, random, publicKey);
//check if the player is connecting through Floodgate
FloodgatePlayer floodgatePlayer = floodgateHook.getFloodgatePlayer(username);
if (floodgatePlayer != null) {
floodgateHook.checkNameConflict(username, source, floodgatePlayer);
} else {
//do Java login tasks
super.onLogin(username, source);
}
super.onLogin(username, new ProtocolLibLoginSource(packetEvent, player, random, publicKey));
} finally {
ProtocolLibrary.getProtocolManager().getAsynchronousManager().signalPacketTransmission(packetEvent);
}
@@ -127,4 +117,12 @@ public class NameCheckTask extends JoinManagement<Player, CommandSender, Protoco
plugin.putSession(player.getAddress(), loginSession);
}
@Override
protected FloodgatePlayer getFloodgatePlayer(Object id) {
if ((id instanceof String)) {
return floodgateHook.getFloodgatePlayer((String) id);
}
return null;
}
}
@@ -61,7 +61,7 @@ public class ProtocolLibListener extends PacketAdapter {
this.plugin = plugin;
this.rateLimiter = rateLimiter;
this.floodgateHook = new FloodgateHook(plugin);
this.floodgateHook = new FloodgateHook();
}
public static void register(FastLoginBukkit plugin, RateLimiter rateLimiter) {
@@ -60,7 +60,7 @@ public class ProtocolSupportListener extends JoinManagement<Player, CommandSende
this.plugin = plugin;
this.rateLimiter = rateLimiter;
this.floodgateHook = new FloodgateHook(plugin);
this.floodgateHook = new FloodgateHook();
}
@EventHandler
@@ -81,16 +81,7 @@ public class ProtocolSupportListener extends JoinManagement<Player, CommandSende
plugin.removeSession(address);
ProtocolLoginSource source = new ProtocolLoginSource(loginStartEvent);
//check if the player is connecting through Floodgate
FloodgatePlayer floodgatePlayer = floodgateHook.getFloodgatePlayer(username);
if (floodgatePlayer != null) {
floodgateHook.checkNameConflict(username, source, floodgatePlayer);
} else {
//do Java login tasks
super.onLogin(username, source);
}
super.onLogin(username, source);
}
@EventHandler
@@ -139,4 +130,12 @@ public class ProtocolSupportListener extends JoinManagement<Player, CommandSende
BukkitLoginSession loginSession = new BukkitLoginSession(username, profile);
plugin.putSession(source.getAddress(), loginSession);
}
@Override
protected FloodgatePlayer getFloodgatePlayer(Object id) {
if ((id instanceof String)) {
return floodgateHook.getFloodgatePlayer((String) id);
}
return null;
}
}