From 22d2c177692519ee076d760a09d10c8fed580635 Mon Sep 17 00:00:00 2001 From: Hidey Boi Date: Tue, 26 Sep 2023 19:42:48 -0500 Subject: [PATCH 1/5] Jank Fix for #444 --- .../floodgate/util/WhitelistUtils.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index 6747c28c..04457e68 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -25,10 +25,17 @@ package org.geysermc.floodgate.util; +import static org.bukkit.Bukkit.getServer; + import com.mojang.authlib.GameProfile; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.geysermc.floodgate.FloodgatePlatform; +import org.geysermc.floodgate.SpigotPlugin; +import org.geysermc.floodgate.api.FloodgateApi; + + @SuppressWarnings("ConstantConditions") public final class WhitelistUtils { @@ -45,12 +52,19 @@ public static boolean addPlayer(UUID uuid, String username) { OfflinePlayer player = ReflectionUtils.newInstance( ClassNames.CRAFT_OFFLINE_PLAYER_CONSTRUCTOR, - Bukkit.getServer(), profile + getServer(), profile ); if (player.isWhitelisted()) { return false; } - player.setWhitelisted(true); + + Bukkit.getScheduler().runTask(getServer().getPluginManager().getPlugin("floodgate"), + new Runnable() { + @Override + public void run() { + player.setWhitelisted(true); + } + }); return true; } @@ -67,12 +81,20 @@ public static boolean removePlayer(UUID uuid, String username) { OfflinePlayer player = ReflectionUtils.newInstance( ClassNames.CRAFT_OFFLINE_PLAYER_CONSTRUCTOR, - Bukkit.getServer(), profile + getServer(), profile ); if (!player.isWhitelisted()) { return false; } - player.setWhitelisted(false); + + Bukkit.getScheduler().runTask(getServer().getPluginManager().getPlugin("floodgate"), + new Runnable() { + @Override + public void run() { + player.setWhitelisted(false); + } + }); + return true; } } From 9f7a5a1a53e28a2c910aa5afb3c8d07ff420dad2 Mon Sep 17 00:00:00 2001 From: Hidey Boi Date: Fri, 29 Sep 2023 17:41:35 -0500 Subject: [PATCH 2/5] Move Scheduler to WhitelistUtils Also stopped using the Bukkit scheduler and am now scheduling via SpigotVersionSpecificMethods.maybeSchedule --- .../floodgate/util/SpigotCommandUtil.java | 17 +++++++-- .../floodgate/util/WhitelistUtils.java | 36 +++++++------------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java index 4950309d..378e60a2 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.UUID; +import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -122,11 +123,23 @@ public void kickPlayer(Object player, String message) { @Override public boolean whitelistPlayer(UUID uuid, String username) { - return WhitelistUtils.addPlayer(uuid, username); + return WhitelistUtils.addPlayer(uuid, username, this); } @Override public boolean removePlayerFromWhitelist(UUID uuid, String username) { - return WhitelistUtils.removePlayer(uuid, username); + return WhitelistUtils.removePlayer(uuid, username, this); + } + + public void setWhitelist(Object offlinePlayer, Boolean isWhitelisted) { + Runnable runnable = new Runnable() { + @Override + public void run() { + OfflinePlayer player = (OfflinePlayer)offlinePlayer; + player.setWhitelisted(isWhitelisted); + } + }; + + versionSpecificMethods.maybeSchedule(runnable); } } diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index 04457e68..cbf41ff0 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -29,11 +29,7 @@ import com.mojang.authlib.GameProfile; import java.util.UUID; -import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.geysermc.floodgate.FloodgatePlatform; -import org.geysermc.floodgate.SpigotPlugin; -import org.geysermc.floodgate.api.FloodgateApi; @@ -43,11 +39,12 @@ public final class WhitelistUtils { /** * Whitelist the given Bedrock player. * - * @param uuid the UUID of the Bedrock player to be whitelisted - * @param username the username of the Bedrock player to be whitelisted + * @param uuid the UUID of the Bedrock player to be whitelisted + * @param username the username of the Bedrock player to be whitelisted + * @param spigotCommandUtil * @return true if the player has been whitelisted, false if the player is already whitelisted */ - public static boolean addPlayer(UUID uuid, String username) { + public static boolean addPlayer(UUID uuid, String username, SpigotCommandUtil spigotCommandUtil) { GameProfile profile = new GameProfile(uuid, username); OfflinePlayer player = ReflectionUtils.newInstance( @@ -58,25 +55,22 @@ public static boolean addPlayer(UUID uuid, String username) { return false; } - Bukkit.getScheduler().runTask(getServer().getPluginManager().getPlugin("floodgate"), - new Runnable() { - @Override - public void run() { - player.setWhitelisted(true); - } - }); + spigotCommandUtil.setWhitelist(player, true); + return true; } /** * Removes the given Bedrock player from the whitelist. * - * @param uuid the UUID of the Bedrock player to be removed - * @param username the username of the Bedrock player to be removed + * @param uuid the UUID of the Bedrock player to be removed + * @param username the username of the Bedrock player to be removed + * @param spigotCommandUtil * @return true if the player has been removed from the whitelist, false if the player wasn't * whitelisted */ - public static boolean removePlayer(UUID uuid, String username) { + public static boolean removePlayer(UUID uuid, String username, + SpigotCommandUtil spigotCommandUtil) { GameProfile profile = new GameProfile(uuid, username); OfflinePlayer player = ReflectionUtils.newInstance( @@ -87,13 +81,7 @@ public static boolean removePlayer(UUID uuid, String username) { return false; } - Bukkit.getScheduler().runTask(getServer().getPluginManager().getPlugin("floodgate"), - new Runnable() { - @Override - public void run() { - player.setWhitelisted(false); - } - }); + spigotCommandUtil.setWhitelist(player, false); return true; } From 1baac77c6496c2a1864c24de1c11f72d941a002e Mon Sep 17 00:00:00 2001 From: Hidey Boi Date: Fri, 29 Sep 2023 19:03:41 -0500 Subject: [PATCH 3/5] Move setWhitelist to WhitelistUtils Includes requested formatting changes. --- .../floodgate/util/SpigotCommandUtil.java | 16 +------ .../floodgate/util/WhitelistUtils.java | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java index 378e60a2..d6028abe 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/SpigotCommandUtil.java @@ -123,23 +123,11 @@ public void kickPlayer(Object player, String message) { @Override public boolean whitelistPlayer(UUID uuid, String username) { - return WhitelistUtils.addPlayer(uuid, username, this); + return WhitelistUtils.addPlayer(uuid, username, versionSpecificMethods); } @Override public boolean removePlayerFromWhitelist(UUID uuid, String username) { - return WhitelistUtils.removePlayer(uuid, username, this); - } - - public void setWhitelist(Object offlinePlayer, Boolean isWhitelisted) { - Runnable runnable = new Runnable() { - @Override - public void run() { - OfflinePlayer player = (OfflinePlayer)offlinePlayer; - player.setWhitelisted(isWhitelisted); - } - }; - - versionSpecificMethods.maybeSchedule(runnable); + return WhitelistUtils.removePlayer(uuid, username, versionSpecificMethods); } } diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index cbf41ff0..ba6d5d31 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -25,64 +25,67 @@ package org.geysermc.floodgate.util; -import static org.bukkit.Bukkit.getServer; - +import org.bukkit.Bukkit; import com.mojang.authlib.GameProfile; import java.util.UUID; import org.bukkit.OfflinePlayer; - - @SuppressWarnings("ConstantConditions") public final class WhitelistUtils { /** * Whitelist the given Bedrock player. * - * @param uuid the UUID of the Bedrock player to be whitelisted - * @param username the username of the Bedrock player to be whitelisted - * @param spigotCommandUtil - * @return true if the player has been whitelisted, false if the player is already whitelisted + * @param uuid the UUID of the Bedrock player to be removed + * @param username the username of the Bedrock player to be removed + * @param versionSpecificMethods a reference to the SpigotVersionSpecificMethods used in SpigotCommandUtil + * @return true if the player has been removed from the whitelist, false if the player wasn't */ - public static boolean addPlayer(UUID uuid, String username, SpigotCommandUtil spigotCommandUtil) { + public static boolean addPlayer(UUID uuid, String username, SpigotVersionSpecificMethods versionSpecificMethods) { GameProfile profile = new GameProfile(uuid, username); OfflinePlayer player = ReflectionUtils.newInstance( ClassNames.CRAFT_OFFLINE_PLAYER_CONSTRUCTOR, - getServer(), profile + Bukkit.getServer(), profile ); if (player.isWhitelisted()) { return false; } - - spigotCommandUtil.setWhitelist(player, true); - + setWhitelist(player, true, versionSpecificMethods); return true; } /** * Removes the given Bedrock player from the whitelist. * - * @param uuid the UUID of the Bedrock player to be removed - * @param username the username of the Bedrock player to be removed - * @param spigotCommandUtil + * @param uuid the UUID of the Bedrock player to be removed + * @param username the username of the Bedrock player to be removed + * @param versionSpecificMethods a reference to the SpigotVersionSpecificMethods used in SpigotCommandUtil * @return true if the player has been removed from the whitelist, false if the player wasn't * whitelisted */ - public static boolean removePlayer(UUID uuid, String username, - SpigotCommandUtil spigotCommandUtil) { + public static boolean removePlayer(UUID uuid, String username, SpigotVersionSpecificMethods versionSpecificMethods) { GameProfile profile = new GameProfile(uuid, username); OfflinePlayer player = ReflectionUtils.newInstance( ClassNames.CRAFT_OFFLINE_PLAYER_CONSTRUCTOR, - getServer(), profile + Bukkit.getServer(), profile ); if (!player.isWhitelisted()) { return false; } + setWhitelist(player, false, versionSpecificMethods); + return true; + } - spigotCommandUtil.setWhitelist(player, false); + static void setWhitelist(OfflinePlayer player, boolean whitelist, SpigotVersionSpecificMethods versionSpecificMethods) { + Runnable runnable = new Runnable() { + @Override + public void run() { + player.setWhitelisted(whitelist); + } + }; - return true; + versionSpecificMethods.maybeSchedule(runnable); } } From 1a925325dd19d66b2a505b55d6127b15edd85f44 Mon Sep 17 00:00:00 2001 From: Hidey Boi Date: Tue, 10 Oct 2023 12:51:29 -0500 Subject: [PATCH 4/5] switch from runnables to using an lambda expression --- .../org/geysermc/floodgate/util/WhitelistUtils.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index ba6d5d31..3edca550 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -79,13 +79,8 @@ public static boolean removePlayer(UUID uuid, String username, SpigotVersionSpec } static void setWhitelist(OfflinePlayer player, boolean whitelist, SpigotVersionSpecificMethods versionSpecificMethods) { - Runnable runnable = new Runnable() { - @Override - public void run() { - player.setWhitelisted(whitelist); - } - }; - - versionSpecificMethods.maybeSchedule(runnable); + versionSpecificMethods.maybeSchedule(() -> { + player.setWhitelisted(whitelist); + }); } } From a850f2836652ce58c31db3b031f3125c5e7bbbed Mon Sep 17 00:00:00 2001 From: Hidey Boi Date: Tue, 10 Oct 2023 12:55:30 -0500 Subject: [PATCH 5/5] Made lambda a single line --- .../java/org/geysermc/floodgate/util/WhitelistUtils.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java index 3edca550..d5779565 100644 --- a/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java +++ b/spigot/src/main/java/org/geysermc/floodgate/util/WhitelistUtils.java @@ -25,9 +25,9 @@ package org.geysermc.floodgate.util; -import org.bukkit.Bukkit; import com.mojang.authlib.GameProfile; import java.util.UUID; +import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @SuppressWarnings("ConstantConditions") @@ -79,8 +79,6 @@ public static boolean removePlayer(UUID uuid, String username, SpigotVersionSpec } static void setWhitelist(OfflinePlayer player, boolean whitelist, SpigotVersionSpecificMethods versionSpecificMethods) { - versionSpecificMethods.maybeSchedule(() -> { - player.setWhitelisted(whitelist); - }); + versionSpecificMethods.maybeSchedule(() -> player.setWhitelisted(whitelist)); } }