From 411148b560983a61be51f0ad44aff21f12f516bd Mon Sep 17 00:00:00 2001 From: Smart123s <28480228+Smart123s@users.noreply.github.com> Date: Sun, 13 Jun 2021 14:24:14 +0200 Subject: [PATCH] No longer reference 'Floodgate' in JoinManagement Referencing 'FloodgatePlayer' in JoinManagement.java and it's subclasses has cause ProtocolLib to fail to register an event when Floodgate was not installed. --- .../fastlogin/bukkit/FastLoginBukkit.java | 2 +- .../bukkit/hook/floodgate/FloodgateHook.java | 55 --------- .../listener/protocollib/NameCheckTask.java | 15 +-- .../protocollib/ProtocolLibListener.java | 6 +- .../ProtocolSupportListener.java | 11 -- .../fastlogin/bungee/FastLoginBungee.java | 6 + .../bungee/task/AsyncPremiumCheck.java | 8 -- .../fastlogin/core/hooks/FloodgateHook.java | 111 ++++++++++++++++++ .../fastlogin/core/shared/JoinManagement.java | 66 +---------- .../fastlogin/core/shared/PlatformPlugin.java | 2 + 10 files changed, 127 insertions(+), 155 deletions(-) delete mode 100644 bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/floodgate/FloodgateHook.java create mode 100644 core/src/main/java/com/github/games647/fastlogin/core/hooks/FloodgateHook.java diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java index 067bd9b6..8277b231 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java @@ -278,7 +278,7 @@ public class FastLoginBukkit extends JavaPlugin implements PlatformPlugin - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package com.github.games647.fastlogin.bukkit.hook.floodgate; - -import org.bukkit.Bukkit; -import org.geysermc.floodgate.api.FloodgateApi; -import org.geysermc.floodgate.api.player.FloodgatePlayer; - -public class FloodgateHook { - - public FloodgateHook() { } - - /** - * 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 e1e1bbe6..fcd47edf 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,7 +30,6 @@ 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; @@ -40,7 +39,6 @@ import java.util.Random; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.geysermc.floodgate.api.player.FloodgatePlayer; public class NameCheckTask extends JoinManagement implements Runnable { @@ -54,10 +52,9 @@ public class NameCheckTask extends JoinManagement + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.core.hooks; + +import java.io.IOException; +import java.util.Optional; + +import com.github.games647.craftapi.model.Profile; +import com.github.games647.craftapi.resolver.RateLimitException; +import com.github.games647.fastlogin.core.shared.FastLoginCore; +import com.github.games647.fastlogin.core.shared.LoginSource; + +import org.geysermc.floodgate.api.FloodgateApi; +import org.geysermc.floodgate.api.player.FloodgatePlayer; + +public class FloodgateHook

{ + + private final FastLoginCore core; + + public FloodgateHook(FastLoginCore core) { + this.core = core; + } + + /** + * Check if the player's name conflicts an existing Java player's name, and + * kick them if it does + * + * @param username the name of the player + * @param source an instance of LoginSource + */ + public void checkFloodgateNameConflict(String username, LoginSource source, FloodgatePlayer floodgatePlayer) { + String allowConflict = core.getConfig().get("allowFloodgateNameConflict").toString().toLowerCase(); + + // check if the Bedrock player is linked to a Java account + boolean isLinked = ((FloodgatePlayer) floodgatePlayer).getLinkedPlayer() != null; + + if (allowConflict.equals("false") + || allowConflict.equals("linked") && !isLinked) { + + // check for conflicting Premium Java name + Optional premiumUUID = Optional.empty(); + try { + premiumUUID = core.getResolver().findProfile(username); + } catch (IOException | RateLimitException e) { + core.getPlugin().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) { + core.getPlugin().getLog().error("Could not kick Player {}", username); + } + } + + if (premiumUUID.isPresent()) { + core.getPlugin().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) { + core.getPlugin().getLog().error("Could not kick Player {}", username); + } + } + } else { + core.getPlugin().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 (core.getPlugin().isPluginInstalled("floodgate")) { + for (FloodgatePlayer floodgatePlayer : FloodgateApi.getInstance().getPlayers()) { + if (floodgatePlayer.getUsername().equals(username)) { + return floodgatePlayer; + } + } + } + return null; + } + +} diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java index 2528ff7b..ec129a8d 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java @@ -29,9 +29,9 @@ import com.github.games647.craftapi.model.Profile; import com.github.games647.craftapi.resolver.RateLimitException; import com.github.games647.fastlogin.core.StoredProfile; import com.github.games647.fastlogin.core.hooks.AuthPlugin; +import com.github.games647.fastlogin.core.hooks.FloodgateHook; import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent; -import java.io.IOException; import java.util.Optional; import org.geysermc.floodgate.api.player.FloodgatePlayer; @@ -42,10 +42,12 @@ public abstract class JoinManagement

{ protected final FastLoginCore core; protected final AuthPlugin

authHook; + private final FloodgateHook floodgateHook; public JoinManagement(FastLoginCore core, AuthPlugin

authHook) { this.core = core; this.authHook = authHook; + this.floodgateHook = new FloodgateHook<>(core); } public void onLogin(String username, S source) { @@ -56,10 +58,10 @@ public abstract class JoinManagement

{ } //check if the player is connecting through Floodgate - Object floodgatePlayer = getFloodgatePlayer(username); + FloodgatePlayer floodgatePlayer = floodgateHook.getFloodgatePlayer(username); if (floodgatePlayer != null) { - checkFloodgateNameConflict(username, source, floodgatePlayer); + floodgateHook.checkFloodgateNameConflict(username, source, floodgatePlayer); return; } callFastLoginPreLoginEvent(username, source, profile); @@ -141,64 +143,6 @@ public abstract class JoinManagement

{ return false; } - /** - * 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 - */ - public void checkFloodgateNameConflict(String username, LoginSource source, Object floodgatePlayer) { - String allowConflict = core.getConfig().get("allowFloodgateNameConflict").toString().toLowerCase(); - - // check if the Bedrock player is linked to a Java account - boolean isLinked = ((FloodgatePlayer) floodgatePlayer).getLinkedPlayer() != null; - - if (allowConflict.equals("false") - || allowConflict.equals("linked") && !isLinked) { - - // check for conflicting Premium Java name - Optional premiumUUID = Optional.empty(); - try { - premiumUUID = core.getResolver().findProfile(username); - } catch (IOException | RateLimitException e) { - core.getPlugin().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) { - core.getPlugin().getLog().error("Could not kick Player {}", username); - } - } - - if (premiumUUID.isPresent()) { - core.getPlugin().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) { - core.getPlugin().getLog().error("Could not kick Player {}", username); - } - } - } else { - core.getPlugin().getLog().info("Skipping name conflict checking for player {}", username); - } - } - - /** - * Gets a FloodgatePlayer based on name or UUID Note: Don't change the return - * type from Object to FloodgatePlayer, unless you want ProtocolSupport to throw - * an error if Floodgate is not installed - * - * @param id UUID for BungeeCord, username for Bukkit - * @return an instance of FloodgatePlayer, if Floodgate is installed and a - * player is found
- * null if Floodgate is unavailable - */ - protected abstract Object getFloodgatePlayer(Object id); - public abstract FastLoginPreLoginEvent callFastLoginPreLoginEvent(String username, S source, StoredProfile profile); public abstract void requestPremiumLogin(S source, StoredProfile profile, String username, boolean registered); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java index 5761359e..11971e45 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java @@ -45,6 +45,8 @@ public interface PlatformPlugin { AsyncScheduler getScheduler(); + boolean isPluginInstalled(String name); + default void sendMultiLineMessage(C receiver, String message) { for (String line : message.split("%nl%")) { sendMessage(receiver, line);