-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve toggleconnectionmsgs persistency for legacy
- Loading branch information
Showing
7 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/SQLiteData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters