Skip to content

Commit 0e8e07f

Browse files
committed
refactor: create a separate class for every db table
1 parent ce6bd1e commit 0e8e07f

File tree

16 files changed

+276
-269
lines changed

16 files changed

+276
-269
lines changed

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/CrackedCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ private void onCrackedSelf(CommandSender sender) {
6363
}
6464

6565
// todo: load async if
66-
StoredProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName());
66+
StoredProfile profile = plugin.getCore().getAuthStorage().loadProfile(sender.getName());
6767
if (profile.isPremium()) {
6868
plugin.getCore().sendLocaleMessage("remove-premium", sender);
6969

7070
profile.setPremium(false);
7171
profile.setId(null);
7272
plugin.getScheduler().runAsync(() -> {
73-
plugin.getCore().getStorage().save(profile);
73+
plugin.getCore().getAuthStorage().save(profile);
7474
plugin.getServer().getPluginManager().callEvent(
7575
new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER));
7676
});
@@ -89,7 +89,7 @@ private void onCrackedOther(CommandSender sender, Command command, String[] args
8989
}
9090

9191
//todo: load async
92-
StoredProfile profile = plugin.getCore().getStorage().loadProfile(args[0]);
92+
StoredProfile profile = plugin.getCore().getAuthStorage().loadProfile(args[0]);
9393
if (profile == null) {
9494
sender.sendMessage("Error occurred");
9595
return;
@@ -103,7 +103,7 @@ private void onCrackedOther(CommandSender sender, Command command, String[] args
103103

104104
profile.setPremium(false);
105105
plugin.getScheduler().runAsync(() -> {
106-
plugin.getCore().getStorage().save(profile);
106+
plugin.getCore().getAuthStorage().save(profile);
107107
plugin.getServer().getPluginManager().callEvent(
108108
new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER));
109109
});

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/PremiumCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ private void onPremiumSelf(CommandSender sender) {
7878

7979
plugin.getCore().getPendingConfirms().remove(id);
8080
//todo: load async
81-
StoredProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName());
81+
StoredProfile profile = plugin.getCore().getAuthStorage().loadProfile(sender.getName());
8282
if (profile.isPremium()) {
8383
plugin.getCore().sendLocaleMessage("already-exists", sender);
8484
} else {
8585
//todo: resolve uuid
8686
profile.setPremium(true);
8787
plugin.getScheduler().runAsync(() -> {
88-
plugin.getCore().getStorage().save(profile);
88+
plugin.getCore().getAuthStorage().save(profile);
8989
plugin.getServer().getPluginManager().callEvent(
9090
new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_SELF));
9191
});
@@ -104,7 +104,7 @@ private void onPremiumOther(CommandSender sender, Command command, String[] args
104104
}
105105

106106
//todo: load async
107-
StoredProfile profile = plugin.getCore().getStorage().loadProfile(args[0]);
107+
StoredProfile profile = plugin.getCore().getAuthStorage().loadProfile(args[0]);
108108
if (profile == null) {
109109
plugin.getCore().sendLocaleMessage("player-unknown", sender);
110110
return;
@@ -116,7 +116,7 @@ private void onPremiumOther(CommandSender sender, Command command, String[] args
116116
//todo: resolve uuid
117117
profile.setPremium(true);
118118
plugin.getScheduler().runAsync(() -> {
119-
plugin.getCore().getStorage().save(profile);
119+
plugin.getCore().getAuthStorage().save(profile);
120120
plugin.getServer().getPluginManager().callEvent(
121121
new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER));
122122
});

bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void onSuccessMessage(ProxiedPlayer forPlayer) {
133133

134134
if (!loginSession.isAlreadySaved()) {
135135
playerProfile.setPremium(true);
136-
plugin.getCore().getStorage().save(playerProfile);
136+
plugin.getCore().getAuthStorage().save(playerProfile);
137137
loginSession.setAlreadySaved(true);
138138
}
139139
}

bungee/src/main/java/com/github/games647/fastlogin/bungee/task/AsyncToggleMessage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void run() {
6363
}
6464

6565
private void turnOffPremium() {
66-
StoredProfile playerProfile = core.getStorage().loadProfile(targetPlayer);
66+
StoredProfile playerProfile = core.getAuthStorage().loadProfile(targetPlayer);
6767
//existing player is already cracked
6868
if (playerProfile.isSaved() && !playerProfile.isPremium()) {
6969
sendMessage("not-premium");
@@ -72,7 +72,7 @@ private void turnOffPremium() {
7272

7373
playerProfile.setPremium(false);
7474
playerProfile.setId(null);
75-
core.getStorage().save(playerProfile);
75+
core.getAuthStorage().save(playerProfile);
7676
PremiumToggleReason reason = (!isPlayerSender || !sender.getName().equalsIgnoreCase(playerProfile.getName()))
7777
? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF;
7878
core.getPlugin().getProxy().getPluginManager().callEvent(
@@ -81,14 +81,14 @@ private void turnOffPremium() {
8181
}
8282

8383
private void activatePremium() {
84-
StoredProfile playerProfile = core.getStorage().loadProfile(targetPlayer);
84+
StoredProfile playerProfile = core.getAuthStorage().loadProfile(targetPlayer);
8585
if (playerProfile.isPremium()) {
8686
sendMessage("already-exists");
8787
return;
8888
}
8989

9090
playerProfile.setPremium(true);
91-
core.getStorage().save(playerProfile);
91+
core.getAuthStorage().save(playerProfile);
9292
PremiumToggleReason reason = (!isPlayerSender || !sender.getName().equalsIgnoreCase(playerProfile.getName()))
9393
? PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF;
9494
core.getPlugin().getProxy().getPluginManager().callEvent(

core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.github.games647.fastlogin.core.hooks.PasswordGenerator;
3838
import com.github.games647.fastlogin.core.mojang.ProxyAgnosticMojangResolver;
3939
import com.github.games647.fastlogin.core.storage.MySQLStorage;
40+
import com.github.games647.fastlogin.core.storage.SQLAuthStorage;
4041
import com.github.games647.fastlogin.core.storage.SQLStorage;
4142
import com.github.games647.fastlogin.core.storage.SQLiteStorage;
4243
import com.google.common.base.Ticker;
@@ -202,8 +203,8 @@ public MojangResolver getResolver() {
202203
return resolver;
203204
}
204205

205-
public SQLStorage getStorage() {
206-
return storage;
206+
public SQLAuthStorage getAuthStorage() {
207+
return storage.getAuthStorage();
207208
}
208209

209210
public T getPlugin() {

core/src/main/java/com/github/games647/fastlogin/core/shared/FloodgateManagement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public void run() {
7676

7777
//this happens on Bukkit if it's connected to Bungee
7878
//if that's the case, players will be logged in via plugin messages
79-
if (core.getStorage() == null) {
79+
if (core.getAuthStorage() == null) {
8080
return;
8181
}
8282

83-
profile = core.getStorage().loadProfile(username);
83+
profile = core.getAuthStorage().loadProfile(username);
8484
if (!profile.isSaved()) {
8585
// linked players are stored as Java (= not Floodgate) players
8686
profile.setFloodgate(!isLinked);

core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.github.games647.fastlogin.core.StoredProfile;
2929
import com.github.games647.fastlogin.core.hooks.AuthPlugin;
3030
import com.github.games647.fastlogin.core.shared.event.FastLoginAutoLoginEvent;
31-
import com.github.games647.fastlogin.core.storage.SQLStorage;
31+
import com.github.games647.fastlogin.core.storage.SQLAuthStorage;
3232

3333
public abstract class ForceLoginManagement<P extends C, C, L extends LoginSession, T extends PlatformPlugin<C>>
3434
implements Runnable {
@@ -55,7 +55,7 @@ public void run() {
5555
return;
5656
}
5757

58-
SQLStorage storage = core.getStorage();
58+
SQLAuthStorage storage = core.getAuthStorage();
5959
StoredProfile playerProfile = session.getProfile();
6060
try {
6161
if (isOnlineMode()) {

core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void onLogin(String username, S source) {
6060
}
6161
}
6262

63-
StoredProfile profile = core.getStorage().loadProfile(username);
63+
StoredProfile profile = core.getAuthStorage().loadProfile(username);
6464

6565
//can't be a premium Java player, if it's not saved in the database
6666
if (profile == null) {
@@ -152,7 +152,7 @@ private boolean checkPremiumName(S source, String username, StoredProfile profil
152152
private boolean checkNameChange(S source, String username, Profile profile) {
153153
//user not exists in the db
154154
if (core.getConfig().get("nameChangeCheck", false)) {
155-
StoredProfile storedProfile = core.getStorage().loadProfile(profile.getId());
155+
StoredProfile storedProfile = core.getAuthStorage().loadProfile(profile.getId());
156156
if (storedProfile != null) {
157157
if (storedProfile.isFloodgate()) {
158158
core.getPlugin().getLog()

core/src/main/java/com/github/games647/fastlogin/core/storage/AuthStorage.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@ public interface AuthStorage {
3535
StoredProfile loadProfile(UUID uuid);
3636

3737
void save(StoredProfile playerProfile);
38-
39-
void close();
4038
}

core/src/main/java/com/github/games647/fastlogin/core/storage/MigrationManager.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.github.games647.fastlogin.core.storage;
2727

2828
import com.github.games647.fastlogin.core.shared.FastLoginCore;
29-
import com.zaxxer.hikari.HikariDataSource;
3029

3130
import java.sql.Connection;
3231
import java.sql.PreparedStatement;
@@ -52,20 +51,20 @@ public class MigrationManager {
5251

5352
protected final FastLoginCore<?, ?, ?> core;
5453

55-
protected final HikariDataSource dataSource;
54+
protected final SQLStorage storage;
5655

57-
protected MigrationManager(FastLoginCore<?, ?, ?> core, HikariDataSource dataSource) {
56+
protected MigrationManager(FastLoginCore<?, ?, ?> core, SQLStorage storage) {
5857
this.core = core;
59-
this.dataSource = dataSource;
58+
this.storage = storage;
6059
}
6160

6261
protected void createTables() throws SQLException {
63-
try (Connection con = dataSource.getConnection();
62+
try (Connection con = storage.getDataSource().getConnection();
6463
Statement createStmt = con.createStatement()) {
6564

6665
// if (dataSource.getDriverClassName().contains("sqlite")) {
6766
// throws: the return value of "fastlogin.hikari.HikariDataSource.getDriverClassName()" is null
68-
if (core.getStorage() instanceof SQLiteStorage) {
67+
if (storage instanceof SQLiteStorage) {
6968
createStmt.executeUpdate(CREATE_TABLE_STMT.replace("AUTO_INCREMENT", "AUTOINCREMENT"));
7069
} else {
7170
createStmt.executeUpdate(CREATE_TABLE_STMT);
@@ -75,7 +74,7 @@ protected void createTables() throws SQLException {
7574
}
7675

7776
protected int getTableVersion(String table) {
78-
try (Connection con = dataSource.getConnection();
77+
try (Connection con = storage.getDataSource().getConnection();
7978
PreparedStatement loadStmt = con.prepareStatement(LOAD_TABLE_VERSION)) {
8079
loadStmt.setString(1, table);
8180

@@ -102,7 +101,7 @@ protected void migrateTable(MigratableStorage table) {
102101
for (int i = initialVersion; i < table.getLatestTableVersion(); i++) {
103102
core.getPlugin().getLog().info("Starting database migration of table {} to version {}",
104103
table.getTableName(), i + 1);
105-
try (Connection con = dataSource.getConnection();
104+
try (Connection con = storage.getDataSource().getConnection();
106105
Statement migrateStmt = con.createStatement();
107106
PreparedStatement saveStmt = con.prepareStatement(INSERT_MIGRATION);
108107
) {

0 commit comments

Comments
 (0)