From ee816d7bca8b7f757b4791581c9287a8b8dc5087 Mon Sep 17 00:00:00 2001 From: xGinko Date: Wed, 20 Mar 2024 12:32:42 +0100 Subject: [PATCH] improve toggleconnectionmsgs persistency for legacy --- AnarchyExploitFixesLegacy/build.gradle.kts | 2 + .../AnarchyExploitFixes.java | 7 +- .../commands/ToggleConnectionMsgsCmd.java | 22 ++----- .../config/SQLiteData.java | 66 +++++++++++++++++++ .../modules/misc/FirstJoinMessages.java | 22 ++++--- .../modules/misc/JoinLeaveMessages.java | 20 +++--- gradle/libs.versions.toml | 1 + 7 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/SQLiteData.java diff --git a/AnarchyExploitFixesLegacy/build.gradle.kts b/AnarchyExploitFixesLegacy/build.gradle.kts index 561c21510..f902ca5e4 100755 --- a/AnarchyExploitFixesLegacy/build.gradle.kts +++ b/AnarchyExploitFixesLegacy/build.gradle.kts @@ -6,6 +6,7 @@ plugins { dependencies { compileOnly(libs.paper12) implementation("com.github.cryptomorin:XSeries:9.9.0") // XSeries for cross-version support + implementation("com.zaxxer:HikariCP:5.1.0") implementation(libs.caffeineJ8) } @@ -29,6 +30,7 @@ tasks.shadowJar { "com/cryptomorin/xseries/NoteBlockMusic*", "com/cryptomorin/xseries/SkullCacheListener*" ) + relocate("com.zaxxer", "me.moomoo.anarchyexploitfixes.libs.zaxxer") relocate("org.apache.commons.math3", "me.moomoo.anarchyexploitfixes.libs.fastmath") relocate("com.github.benmanes.caffeine", "me.moomoo.anarchyexploitfixes.libs.caffeine") relocate("io.papermc.lib", "me.moomoo.anarchyexploitfixes.libs.paperlib") diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java index 7881151f9..e997604df 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java @@ -6,6 +6,7 @@ import me.moomoo.anarchyexploitfixes.commands.AEFCommand; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.config.LanguageCache; +import me.moomoo.anarchyexploitfixes.config.SQLiteData; import me.moomoo.anarchyexploitfixes.listener.AEFListener; import me.moomoo.anarchyexploitfixes.modules.AEFModule; import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; @@ -31,11 +32,11 @@ public class AnarchyExploitFixes extends JavaPlugin { private static AnarchyExploitFixes instance; private static HashMap languageCacheMap; private static Config config; + private static SQLiteData data; private static TPSCache tpsCache; private static CachingPermTool cachingPermTool; private static Logger prefixedLogger, unPrefixedLogger; private static Metrics metrics; - public static final Set CONNECT_MSG_TOGGLE = new HashSet<>(); @Override public void onLoad() { @@ -51,6 +52,7 @@ public void onEnable() { unPrefixedLogger = getServer().getLogger(); cachingPermTool = CachingPermTool.enable(this); metrics = new Metrics(this, 8700); + data = new SQLiteData(); // Fancy enable prefixedLogger.info(" "); @@ -130,6 +132,9 @@ public static AnarchyExploitFixes getInstance() { public static Config getConfiguration() { return config; } + public static SQLiteData getData() { + return data; + } public static TPSCache getTPSCache() { return tpsCache; } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/ToggleConnectionMsgsCmd.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/ToggleConnectionMsgsCmd.java index b698094ab..95ed49d1b 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/ToggleConnectionMsgsCmd.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/ToggleConnectionMsgsCmd.java @@ -25,22 +25,14 @@ public void onConMsgToggle(final CommandSender sender) { final Player player = (Player) sender; - if (AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(player.getUniqueId())) { - AnarchyExploitFixes.CONNECT_MSG_TOGGLE.remove(player.getUniqueId()); - - if (AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault) { - sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_enabledConnectionMsgs); + AnarchyExploitFixes.getData().getJoinLeaveEnabled(player.getUniqueId()).thenAccept(enabled -> { + if (enabled) { + AnarchyExploitFixes.getData().setJoinLeaveEnabled(player.getUniqueId(), false) + .thenAccept(v -> sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_disabledConnectionMsgs)); } else { - sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_disabledConnectionMsgs); + AnarchyExploitFixes.getData().setJoinLeaveEnabled(player.getUniqueId(), true) + .thenAccept(v -> sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_enabledConnectionMsgs)); } - } else { - AnarchyExploitFixes.CONNECT_MSG_TOGGLE.add(player.getUniqueId()); - - if (AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault) { - sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_disabledConnectionMsgs); - } else { - sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_enabledConnectionMsgs); - } - } + }); } } \ No newline at end of file diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/SQLiteData.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/SQLiteData.java new file mode 100644 index 000000000..cec1403b7 --- /dev/null +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/SQLiteData.java @@ -0,0 +1,66 @@ +package me.moomoo.anarchyexploitfixes.config; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; + +import javax.sql.DataSource; +import java.sql.*; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class SQLiteData { + + private final DataSource dataSource; + private final String loadPlayer, savePlayerConMsgSetting; + + public SQLiteData() { + HikariConfig config = new HikariConfig(); + config.setJdbcUrl("jdbc:sqlite:plugins/AnarchyExploitFixes/data.db"); + config.setMaximumPoolSize(10); + this.dataSource = new HikariDataSource(config); + + createTables(); + + loadPlayer = "SELECT * FROM `players` WHERE player_uuid = ?;"; + savePlayerConMsgSetting = "INSERT INTO `players` (player_uuid, show_connect_msgs) VALUES (?, ?) ON CONFLICT(player_uuid) DO UPDATE SET show_connect_msgs = ?;"; + } + + private void createTables() { + try (Statement statement = dataSource.getConnection().createStatement()) { + statement.execute("CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) NOT NULL PRIMARY KEY, `show_connect_msgs` boolean);"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public CompletableFuture getJoinLeaveEnabled(UUID uuid) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(loadPlayer)) { + statement.setString(1, uuid.toString()); + ResultSet result = statement.executeQuery(); + if (result.next()) { + return result.getBoolean("show_connect_msgs"); + } else { + return AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault; + } + } catch (SQLException e) { + e.printStackTrace(); + return AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault; + } + }); + } + + public CompletableFuture setJoinLeaveEnabled(UUID uuid, boolean enable) { + return CompletableFuture.runAsync(() -> { + try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(savePlayerConMsgSetting)) { + statement.setString(1, uuid.toString()); + statement.setBoolean(2, enable); + statement.setBoolean(3, enable); + statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } +} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java index 7e40e4519..3d48514d4 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java @@ -4,13 +4,16 @@ import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; import me.moomoo.anarchyexploitfixes.modules.AEFModule; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; public class FirstJoinMessages implements AEFModule, Listener { @@ -25,7 +28,8 @@ public FirstJoinMessages() { this.config = AnarchyExploitFixes.getConfiguration(); config.addComment("misc.join-leave-messages.first-join-messages.enable", "Configure message in lang folder."); // prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense - this.totalPlayers = new AtomicInteger(plugin.getServer().getOfflinePlayers().length); + this.totalPlayers = new AtomicInteger(Arrays.stream(plugin.getServer().getOfflinePlayers()) + .filter(OfflinePlayer::hasPlayedBefore).collect(Collectors.toSet()).size()); this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true); } @@ -58,14 +62,16 @@ private void onPlayerJoin(PlayerJoinEvent event) { final int joiningPlayersNumber = totalPlayers.incrementAndGet(); for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { - if (config.connectionMsgsAreOnByDefault != AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())) { - for (String line : AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_firstJoinMessage) { - onlinePlayer.sendMessage(line - .replace("%player%", joiningPlayer.getName()) - .replace("%players_num%", AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).ordinalFormat - .format(joiningPlayersNumber, "%digits-ordinal"))); + AnarchyExploitFixes.getData().getJoinLeaveEnabled(onlinePlayer.getUniqueId()).thenAccept(enabled -> { + if (enabled) { + for (String line : AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_firstJoinMessage) { + onlinePlayer.sendMessage(line + .replace("%player%", joiningPlayer.getName()) + .replace("%players_num%", AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).ordinalFormat + .format(joiningPlayersNumber, "%digits-ordinal"))); + } } - } + }); } if (logFirstJoin) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/JoinLeaveMessages.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/JoinLeaveMessages.java index f37010a59..da36e922a 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/JoinLeaveMessages.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/JoinLeaveMessages.java @@ -56,10 +56,12 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) { if (firstJoinEnabled && !joiningPlayer.hasPlayedBefore()) return; for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { - if (connectionMsgsOnByDefault != AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())) { - onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_joinMessage - .replace("%player%", joiningPlayer.getName())); - } + AnarchyExploitFixes.getData().getJoinLeaveEnabled(onlinePlayer.getUniqueId()).thenAccept(enabled -> { + if (enabled) { + onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_joinMessage + .replace("%player%", joiningPlayer.getName())); + } + }); } if (showInConsole) AnarchyExploitFixes.getUnprefixedLogger().info(AnarchyExploitFixes.getLang(joiningPlayer.getLocale()).misc_joinMessage @@ -74,10 +76,12 @@ private void onPlayerLeaveEvent(PlayerQuitEvent event) { for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { if (onlinePlayer.getUniqueId().equals(leavingPlayer.getUniqueId())) continue; - if (connectionMsgsOnByDefault != AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())) { - onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_leaveMessage - .replace("%player%", leavingPlayer.getName())); - } + AnarchyExploitFixes.getData().getJoinLeaveEnabled(onlinePlayer.getUniqueId()).thenAccept(enabled -> { + if (enabled) { + onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_leaveMessage + .replace("%player%", leavingPlayer.getName())); + } + }); } if (showInConsole) AnarchyExploitFixes.getUnprefixedLogger().info(AnarchyExploitFixes.getLang(leavingPlayer.getLocale()).misc_leaveMessage diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 47f9ba335..544542ef4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,7 @@ configmaster = "com.github.thatsmusic99:ConfigurationMaster-API:v2.0.0-rc.1" bstats = "org.bstats:bstats-bukkit:3.0.2" caffeineJ17 = "com.github.ben-manes.caffeine:caffeine:3.1.8" caffeineJ8 = "com.github.ben-manes.caffeine:caffeine:2.9.3" +hikaricp = "com.zaxxer:HikariCP:5.1.0" [plugins] runpaper = { id = "xyz.jpenilla.run-paper", version = "2.2.2" }