Skip to content

Commit

Permalink
improve toggleconnectionmsgs persistency for legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 20, 2024
1 parent 966f6d4 commit ee816d7
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 32 deletions.
2 changes: 2 additions & 0 deletions AnarchyExploitFixesLegacy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,11 +32,11 @@ public class AnarchyExploitFixes extends JavaPlugin {
private static AnarchyExploitFixes instance;
private static HashMap<String, LanguageCache> 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<UUID> CONNECT_MSG_TOGGLE = new HashSet<>();

@Override
public void onLoad() {
Expand All @@ -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(" ");
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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<Void> 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();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit ee816d7

Please sign in to comment.