diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/floodgate/FloodgateHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/floodgate/FloodgateHook.java new file mode 100644 index 00000000..c29fa10a --- /dev/null +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/floodgate/FloodgateHook.java @@ -0,0 +1,81 @@ +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 conflict's 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(); + if (allowConflict.equals("false")) { + + // check for conflicting Premium Java name + Optional premiumUUID = Optional.empty(); + try { + premiumUUID = plugin.getCore().getResolver().findProfile(username); + } catch (IOException | RateLimitException e) { + e.printStackTrace(); + plugin.getLog().error( + "Could not check wether Floodgate Player {}'s name conflits a premium Java player's name.", + username); + } + + if (premiumUUID.isPresent()) { + plugin.getLog().info("Bedrock Player {}'s name conflits an existing Java Premium Player's name", + username); + try { + source.kick("Your name conflits an existing Java Premium Player's name"); + } catch (Exception e) { + e.printStackTrace(); + plugin.getLog().error("Could not kick Player {}", username); + } + } + } else { + plugin.getLog().info("Skipping name conflict checking for player {}", username); + } + } + + /** + * The FloodgateApi does not support querying players by name, so this function + * iterates over every online FloodgatePlayer and checks if the requested + * username can be found + * + * @param username the name of the player + * @return FloodgatePlayer if found, null otherwise + */ + public FloodgatePlayer getFloodgatePlayer(String username) { + if (Bukkit.getServer().getPluginManager().isPluginEnabled("floodgate")) { + for (FloodgatePlayer floodgatePlayer : FloodgateApi.getInstance().getPlayers()) { + if (floodgatePlayer.getUsername().equals(username)) { + return floodgatePlayer; + } + } + } + return null; + } + +} diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java index ee6ba69b..db375059 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java @@ -30,6 +30,7 @@ import com.comphenix.protocol.events.PacketEvent; import com.github.games647.fastlogin.bukkit.BukkitLoginSession; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.bukkit.event.BukkitFastLoginPreLoginEvent; +import com.github.games647.fastlogin.bukkit.hook.floodgate.FloodgateHook; import com.github.games647.fastlogin.core.StoredProfile; import com.github.games647.fastlogin.core.shared.JoinManagement; import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent; @@ -37,10 +38,8 @@ import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent; import java.security.PublicKey; import java.util.Random; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.geysermc.floodgate.api.FloodgateApi; import org.geysermc.floodgate.api.player.FloodgatePlayer; public class NameCheckTask extends JoinManagement @@ -55,8 +54,10 @@ public class NameCheckTask extends JoinManagement