From 181a233f31995ce63cba4124964d5b4a85073955 Mon Sep 17 00:00:00 2001 From: Skullian <133133667+Skullians@users.noreply.github.com> Date: Sat, 28 Dec 2024 14:56:02 +0000 Subject: [PATCH] Chore: Use virtual thread executor for db tasks --- .../common/database/AbstractTableManager.java | 17 +++++++ .../common/database/DatabaseManager.java | 27 ++++++----- .../impl/CurrencyDatabaseManager.java | 28 ++++++----- .../impl/DefencesDatabaseManager.java | 18 ++++---- .../impl/NotificationDatabaseManager.java | 16 +++---- .../database/impl/PlayerDatabaseManager.java | 22 ++++----- .../impl/PlayerIslandsDatabaseManager.java | 44 +++++++++--------- .../FactionAuditLogDatabaseManager.java | 14 +++--- .../impl/faction/FactionElectionManager.java | 16 ++++--- .../FactionInvitesDatabaseManager.java | 20 ++++---- .../faction/FactionIslandDatabaseManager.java | 24 +++++----- .../impl/faction/FactionsDatabaseManager.java | 46 +++++++++---------- 12 files changed, 157 insertions(+), 135 deletions(-) create mode 100644 common/src/main/java/net/skullian/skyfactions/common/database/AbstractTableManager.java diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/AbstractTableManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/AbstractTableManager.java new file mode 100644 index 00000000..d98104fb --- /dev/null +++ b/common/src/main/java/net/skullian/skyfactions/common/database/AbstractTableManager.java @@ -0,0 +1,17 @@ +package net.skullian.skyfactions.common.database; + +import org.jooq.DSLContext; + +import java.util.concurrent.Executor; + +public abstract class AbstractTableManager { + + protected final DSLContext ctx; + protected final Executor executor; + + public AbstractTableManager(DSLContext ctx, Executor executor) { + this.ctx = ctx; + this.executor = executor; + } + +} diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/DatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/DatabaseManager.java index 83f84d10..818423b3 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/DatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/DatabaseManager.java @@ -24,6 +24,8 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class DatabaseManager { @@ -46,7 +48,10 @@ public class DatabaseManager { @Getter private FactionsDatabaseManager factionsManager; @Getter private FactionElectionManager electionManager; + private Executor databaseExecutor; + public void initialise(String type) { + this.databaseExecutor = Executors.newVirtualThreadPerTaskExecutor(); createDataSource(SkyApi.getInstance().getFileAPI().getDatabasePath(), type); } @@ -160,17 +165,17 @@ private void setup() { if (result.success) { SLogger.info("Database migrations complete: ({} Migrations completed in {}ms)", result.getSuccessfulMigrations().size(), result.getTotalMigrationTime()); - this.currencyManager = new CurrencyDatabaseManager(this.ctx); - this.defencesManager = new DefencesDatabaseManager(this.ctx); - this.notificationManager = new NotificationDatabaseManager(this.ctx); - this.playerIslandManager = new PlayerIslandsDatabaseManager(this.ctx); - this.playerManager = new PlayerDatabaseManager(this.ctx); - - this.factionAuditLogManager = new FactionAuditLogDatabaseManager(this.ctx); - this.factionInvitesManager = new FactionInvitesDatabaseManager(this.ctx); - this.factionIslandManager = new FactionIslandDatabaseManager(this.ctx); - this.factionsManager = new FactionsDatabaseManager(this.ctx); - this.electionManager = new FactionElectionManager(this.ctx); + this.currencyManager = new CurrencyDatabaseManager(this.ctx, this.databaseExecutor); + this.defencesManager = new DefencesDatabaseManager(this.ctx, this.databaseExecutor); + this.notificationManager = new NotificationDatabaseManager(this.ctx, this.databaseExecutor); + this.playerIslandManager = new PlayerIslandsDatabaseManager(this.ctx, this.databaseExecutor); + this.playerManager = new PlayerDatabaseManager(this.ctx, this.databaseExecutor); + + this.factionAuditLogManager = new FactionAuditLogDatabaseManager(this.ctx, this.databaseExecutor); + this.factionInvitesManager = new FactionInvitesDatabaseManager(this.ctx, this.databaseExecutor); + this.factionIslandManager = new FactionIslandDatabaseManager(this.ctx, this.databaseExecutor); + this.factionsManager = new FactionsDatabaseManager(this.ctx, this.databaseExecutor); + this.electionManager = new FactionElectionManager(this.ctx, this.databaseExecutor); } else { ErrorUtil.handleDatabaseSetupError(new Exception("Failed to complete migrations - " + result.getFailedMigrations().size() + " Migrations failed: " + result.getException())); result.getFailedMigrations().forEach(migration -> SLogger.info("Migration failed: {}", migration.description)); diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/CurrencyDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/CurrencyDatabaseManager.java index e311fb29..80d23f2a 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/CurrencyDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/CurrencyDatabaseManager.java @@ -1,38 +1,36 @@ package net.skullian.skyfactions.common.database.impl; import net.skullian.skyfactions.common.api.SkyApi; +import net.skullian.skyfactions.common.database.AbstractTableManager; import org.jooq.DSLContext; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.Islands.ISLANDS; import static net.skullian.skyfactions.common.database.tables.FactionIslands.FACTION_ISLANDS; -public class CurrencyDatabaseManager { +public class CurrencyDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public CurrencyDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public CurrencyDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } // ------------------ GEMS ------------------ // public CompletableFuture getGems(UUID playerUUID) { - - return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.GEMS) .from(ISLANDS) .where(ISLANDS.UUID.eq(playerUUID.toString())) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), executor); } public CompletableFuture getGems(String factionName) { return CompletableFuture.supplyAsync(() -> ctx.select(FACTION_ISLANDS.GEMS) .from(FACTION_ISLANDS) .where(FACTION_ISLANDS.FACTIONNAME.eq(factionName)) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), executor); } public CompletableFuture modifyGems(UUID playerUUID, int amount, boolean subtract) { @@ -44,7 +42,7 @@ public CompletableFuture modifyGems(UUID playerUUID, int amount, boolean s .set(ISLANDS.GEMS, (subtract ? current - amount : current + amount)) .where(ISLANDS.UUID.eq(playerUUID.toString())) .execute(); - }); + }, executor); } public CompletableFuture modifyGems(String factionName, int amount, boolean subtract) { @@ -56,7 +54,7 @@ public CompletableFuture modifyGems(String factionName, int amount, boolea .set(FACTION_ISLANDS.GEMS, (subtract ? current - amount : current + amount)) .where(FACTION_ISLANDS.FACTIONNAME.eq(factionName)) .execute(); - }); + }, executor); } // ------------------ RUNES ------------------ // @@ -66,14 +64,14 @@ public CompletableFuture getRunes(UUID playerUUID) { return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.RUNES) .from(ISLANDS) .where(ISLANDS.UUID.eq(playerUUID.toString())) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), executor); } public CompletableFuture getRunes(String factionName) { return CompletableFuture.supplyAsync(() -> ctx.select(FACTION_ISLANDS.RUNES) .from(FACTION_ISLANDS) .where(FACTION_ISLANDS.FACTIONNAME.eq(factionName)) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), executor); } public CompletableFuture modifyRunes(UUID playerUUID, int amount, boolean subtract) { @@ -85,7 +83,7 @@ public CompletableFuture modifyRunes(UUID playerUUID, int amount, boolean .set(ISLANDS.RUNES, (subtract ? current - amount : current + amount)) .where(ISLANDS.UUID.eq(playerUUID.toString())) .execute(); - }); + }, executor); } public CompletableFuture modifyRunes(String factionName, int amount, boolean subtract) { @@ -97,7 +95,7 @@ public CompletableFuture modifyRunes(String factionName, int amount, boole .set(FACTION_ISLANDS.RUNES, (subtract ? current - amount : current + amount)) .where(FACTION_ISLANDS.FACTIONNAME.eq(factionName)) .execute(); - }); + }, executor); } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/DefencesDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/DefencesDatabaseManager.java index af1b191e..89c36871 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/DefencesDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/DefencesDatabaseManager.java @@ -1,6 +1,7 @@ package net.skullian.skyfactions.common.database.impl; import net.skullian.skyfactions.common.config.types.Settings; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.tables.records.DefenceLocationsRecord; import net.skullian.skyfactions.common.util.SkyLocation; import org.jooq.Condition; @@ -11,15 +12,14 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.DefenceLocations.DEFENCE_LOCATIONS; -public class DefencesDatabaseManager { +public class DefencesDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public DefencesDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public DefencesDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } public CompletableFuture> getDefenceLocations(Condition condition, String type) { @@ -41,7 +41,7 @@ public CompletableFuture> getDefenceLocations(Condition condit } return locations; - }); + }, executor); } public CompletableFuture registerDefenceLocations(List locations, String owner, boolean faction) { @@ -54,7 +54,7 @@ public CompletableFuture registerDefenceLocations(List locati .execute(); } }); - }); + }, executor); } public CompletableFuture removeDefenceLocations(List locations, String owner, boolean isFaction) { @@ -67,7 +67,7 @@ public CompletableFuture removeDefenceLocations(List location .execute(); } }); - }); + }, executor); } public CompletableFuture removeAllDefences(String owner, boolean isFaction) { @@ -75,7 +75,7 @@ public CompletableFuture removeAllDefences(String owner, boolean isFaction ctx.deleteFrom(DEFENCE_LOCATIONS) .where((isFaction ? DEFENCE_LOCATIONS.FACTIONNAME.eq(owner) : DEFENCE_LOCATIONS.UUID.eq(owner)), (isFaction ? DEFENCE_LOCATIONS.TYPE.eq("faction") : DEFENCE_LOCATIONS.TYPE.eq("player"))) .execute(); - }); + }, executor); } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/NotificationDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/NotificationDatabaseManager.java index 890aefcd..f54ae4ef 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/NotificationDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/NotificationDatabaseManager.java @@ -1,5 +1,6 @@ package net.skullian.skyfactions.common.database.impl; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.tables.records.NotificationsRecord; import net.skullian.skyfactions.common.notification.NotificationData; import net.skullian.skyfactions.common.user.SkyUser; @@ -12,15 +13,14 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.Notifications.NOTIFICATIONS; -public class NotificationDatabaseManager { +public class NotificationDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public NotificationDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public NotificationDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } public CompletableFuture createNotifications(List notifications) { @@ -34,7 +34,7 @@ public CompletableFuture createNotifications(List notifi .execute(); } }); - }); + }, executor); } public CompletableFuture removeNotifications(List notifications) { @@ -47,7 +47,7 @@ public CompletableFuture removeNotifications(List notifi .execute(); } }); - }); + }, executor); } public CompletableFuture> getNotifications(SkyUser player) { @@ -68,6 +68,6 @@ public CompletableFuture> getNotifications(SkyUser player } return data; - }); + }, executor); } } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerDatabaseManager.java index 4c47255d..fcb2c11d 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerDatabaseManager.java @@ -1,21 +1,21 @@ package net.skullian.skyfactions.common.database.impl; import net.skullian.skyfactions.common.api.SkyApi; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.struct.PlayerData; import net.skullian.skyfactions.common.database.tables.records.PlayerDataRecord; import org.jooq.DSLContext; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.PlayerData.PLAYER_DATA; -public class PlayerDatabaseManager { +public class PlayerDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public PlayerDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public PlayerDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } public CompletableFuture registerPlayer(UUID uuid, boolean shouldRegister) { @@ -25,7 +25,7 @@ public CompletableFuture registerPlayer(UUID uuid, boolean shouldRegister) .columns(PLAYER_DATA.UUID, PLAYER_DATA.DISCORD_ID, PLAYER_DATA.LAST_RAID, PLAYER_DATA.LOCALE) .values(uuid.toString(), "none", (long) 0, SkyApi.getInstance().getPlayerAPI().getLocale(uuid)) .execute(); - }); + }, executor); } public CompletableFuture getPlayerData(UUID uuid) { @@ -40,7 +40,7 @@ public CompletableFuture getPlayerData(UUID uuid) { result.getLastRaid(), result.getLocale() ) : null; - }); + }, executor); } public CompletableFuture registerDiscordLink(UUID playerUUID, String discordID) { @@ -51,7 +51,7 @@ public CompletableFuture registerDiscordLink(UUID playerUUID, String disco .set(PLAYER_DATA.DISCORD_ID, discordID) .where(PLAYER_DATA.UUID.eq(playerUUID.toString())) .execute(); - }); + }, executor); } public CompletableFuture updateLastRaid(UUID playerUUID, long time) { @@ -61,14 +61,14 @@ public CompletableFuture updateLastRaid(UUID playerUUID, long time) { .set(PLAYER_DATA.LAST_RAID, time) .where(PLAYER_DATA.UUID.eq(playerUUID.toString())) .execute(); - }); + }, executor); } public CompletableFuture getPlayerLocale(UUID uuid) { return CompletableFuture.supplyAsync(() -> ctx.select(PLAYER_DATA.LOCALE) .from(PLAYER_DATA) .where(PLAYER_DATA.UUID.eq(uuid.toString())) - .fetchOneInto(String.class)); + .fetchOneInto(String.class), executor); } public CompletableFuture setPlayerLocale(UUID uuid, String locale) { @@ -78,7 +78,7 @@ public CompletableFuture setPlayerLocale(UUID uuid, String locale) { .set(PLAYER_DATA.LOCALE, locale) .where(PLAYER_DATA.UUID.eq(uuid.toString())) .execute(); - }); + }, executor); } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerIslandsDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerIslandsDatabaseManager.java index c660cf74..c99cb562 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerIslandsDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/PlayerIslandsDatabaseManager.java @@ -2,6 +2,7 @@ import net.skullian.skyfactions.common.api.SkyApi; import net.skullian.skyfactions.common.config.types.Settings; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.struct.IslandRaidData; import net.skullian.skyfactions.common.database.tables.records.IslandsRecord; import net.skullian.skyfactions.common.database.tables.records.TrustedPlayersRecord; @@ -18,18 +19,17 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.Islands.ISLANDS; import static net.skullian.skyfactions.common.database.tables.TrustedPlayers.TRUSTED_PLAYERS; -public class PlayerIslandsDatabaseManager { - - private final DSLContext ctx; +public class PlayerIslandsDatabaseManager extends AbstractTableManager { public int cachedPlayerIslandID; - public PlayerIslandsDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public PlayerIslandsDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); setIslandCachedNextID(); } @@ -40,7 +40,7 @@ public CompletableFuture hasIsland(UUID playerUUID) { .fetch(); return !result.isEmpty(); - }); + }, executor); } public CompletableFuture createIsland(UUID playerUUID, IslandModificationAction action) { @@ -50,14 +50,14 @@ public CompletableFuture createIsland(UUID playerUUID, IslandModificationA .columns(ISLANDS.ID, ISLANDS.UUID, ISLANDS.LEVEL, ISLANDS.GEMS, ISLANDS.RUNES, ISLANDS.DEFENCECOUNT, ISLANDS.LAST_RAIDED, ISLANDS.LAST_RAIDER, ISLANDS.CREATED) .values(action.getId(), playerUUID.toString(), 0, 0, 0, 0, System.currentTimeMillis() + Settings.RAIDING_PLAYER_IMMUNITY.getLong(), "N/A", System.currentTimeMillis()) .execute(); - }); + }, executor); } public CompletableFuture getOwnerOfIsland(SkyIsland island) { return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.UUID) .from(ISLANDS) .where(ISLANDS.ID.eq(island.getId())) - .fetchOneInto(UUID.class)); + .fetchOneInto(UUID.class), executor); } public CompletableFuture getPlayerIsland(UUID playerUUID) { @@ -67,25 +67,25 @@ public CompletableFuture getPlayerIsland(UUID playerUUID) { .fetchOne(); return result != null ? new PlayerIsland(result.getId()) : null; - }); + }, executor); } - public CompletableFuture setIslandCachedNextID() { - return CompletableFuture.runAsync(() -> { + public void setIslandCachedNextID() { + CompletableFuture.runAsync(() -> { Integer result = ctx.select(DSL.max(ISLANDS.ID)) .from(ISLANDS) .fetchOneInto(Integer.class); if (result == null) this.cachedPlayerIslandID = 1; - else this.cachedPlayerIslandID = (result + 1); - }); + else this.cachedPlayerIslandID = (result + 1); + }, executor); } public CompletableFuture getIslandLevel(SkyIsland island) { return CompletableFuture.supplyAsync(() -> ctx.select(ISLANDS.LEVEL) .from(ISLANDS) .where(ISLANDS.ID.eq(island.getId())) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), executor); } public CompletableFuture setIslandCooldown(SkyIsland island, long time) { @@ -94,7 +94,7 @@ public CompletableFuture setIslandCooldown(SkyIsland island, long time) { .set(ISLANDS.LAST_RAIDED, time) .where(ISLANDS.ID.eq(island.getId())) .execute(); - }); + }, executor); } public CompletableFuture removeIsland(SkyUser player) { @@ -103,7 +103,7 @@ public CompletableFuture removeIsland(SkyUser player) { ctx.deleteFrom(ISLANDS) .where(ISLANDS.UUID.eq(player.getUniqueId().toString())) .execute(); - }); + }, executor); } public CompletableFuture> getRaidableIslands(SkyUser player) { @@ -122,13 +122,13 @@ public CompletableFuture> getRaidableIslands(SkyUser player } return islands; - }); + }, executor); } // ------------------ TRUSTING ------------------ // public CompletableFuture isPlayerTrusted(UUID playerUUID, int id) { - return CompletableFuture.supplyAsync(() -> ctx.fetchExists(TRUSTED_PLAYERS, TRUSTED_PLAYERS.UUID.eq(playerUUID.toString()), TRUSTED_PLAYERS.ISLAND_ID.eq(id))); + return CompletableFuture.supplyAsync(() -> ctx.fetchExists(TRUSTED_PLAYERS, TRUSTED_PLAYERS.UUID.eq(playerUUID.toString()), TRUSTED_PLAYERS.ISLAND_ID.eq(id)), executor); } public CompletableFuture trustPlayer(UUID playerUUID, int islandID) { @@ -137,7 +137,7 @@ public CompletableFuture trustPlayer(UUID playerUUID, int islandID) { .columns(TRUSTED_PLAYERS.ISLAND_ID, TRUSTED_PLAYERS.UUID) .values(islandID, playerUUID.toString()) .execute(); - }); + }, executor); } public CompletableFuture removePlayerTrust(UUID playerUUID, int islandID) { @@ -145,7 +145,7 @@ public CompletableFuture removePlayerTrust(UUID playerUUID, int islandID) ctx.deleteFrom(TRUSTED_PLAYERS) .where(TRUSTED_PLAYERS.ISLAND_ID.eq(islandID), TRUSTED_PLAYERS.UUID.eq(playerUUID.toString())) .execute(); - }); + }, executor); } public CompletableFuture removeAllTrustedPlayers(int islandID) { @@ -153,7 +153,7 @@ public CompletableFuture removeAllTrustedPlayers(int islandID) { ctx.deleteFrom(TRUSTED_PLAYERS) .where(TRUSTED_PLAYERS.ISLAND_ID.eq(islandID)) .execute(); - }); + }, executor); } public CompletableFuture> getTrustedPlayers(int islandID) { @@ -169,6 +169,6 @@ public CompletableFuture> getTrustedPlayers(int islandID) { } return players; - }); + }, executor); } } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionAuditLogDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionAuditLogDatabaseManager.java index c76fa342..9dfe38e8 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionAuditLogDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionAuditLogDatabaseManager.java @@ -1,6 +1,7 @@ package net.skullian.skyfactions.common.database.impl.faction; import net.skullian.skyfactions.common.api.SkyApi; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.struct.AuditLogData; import net.skullian.skyfactions.common.database.tables.records.AuditLogsRecord; import net.skullian.skyfactions.common.util.text.TextUtility; @@ -13,15 +14,14 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.AuditLogs.AUDIT_LOGS; -public class FactionAuditLogDatabaseManager { +public class FactionAuditLogDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public FactionAuditLogDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public FactionAuditLogDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } public CompletableFuture createAuditLogs(List auditLogs) { @@ -35,7 +35,7 @@ public CompletableFuture createAuditLogs(List auditLogs) { .execute(); } }); - }); + }, executor); } public CompletableFuture> getAuditLogs(String factionName) { @@ -57,6 +57,6 @@ public CompletableFuture> getAuditLogs(String factionName) { } return data; - }); + }, executor); } } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionElectionManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionElectionManager.java index 55371f00..c08c2a41 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionElectionManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionElectionManager.java @@ -1,27 +1,29 @@ package net.skullian.skyfactions.common.database.impl.faction; +import net.skullian.skyfactions.common.database.AbstractTableManager; import org.jooq.DSLContext; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.FactionElections.FACTION_ELECTIONS; -public class FactionElectionManager { - private final DSLContext ctx; +public class FactionElectionManager extends AbstractTableManager { - public FactionElectionManager(DSLContext ctx) { - this.ctx = ctx; + public FactionElectionManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } - public CompletableFuture isElectionRunning(String factionName) { - return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_ELECTIONS, FACTION_ELECTIONS.FACTIONNAME.eq(factionName))); + return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_ELECTIONS, FACTION_ELECTIONS.FACTIONNAME.eq(factionName)), executor); } public CompletableFuture getElectionID(String factionName) { return CompletableFuture.supplyAsync(() -> ctx.select(FACTION_ELECTIONS.ID) .from(FACTION_ELECTIONS) .where(FACTION_ELECTIONS.FACTIONNAME.eq(factionName)) - .fetchOneInto(Integer.class)); + .fetchOneInto(Integer.class), + + executor); } } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionInvitesDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionInvitesDatabaseManager.java index 0c7df1c2..bd0027ce 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionInvitesDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionInvitesDatabaseManager.java @@ -1,6 +1,7 @@ package net.skullian.skyfactions.common.database.impl.faction; import net.skullian.skyfactions.common.api.SkyApi; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.struct.InviteData; import net.skullian.skyfactions.common.database.tables.records.FactionInvitesRecord; import net.skullian.skyfactions.common.faction.JoinRequestData; @@ -12,15 +13,14 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.FactionInvites.FACTION_INVITES; -public class FactionInvitesDatabaseManager { +public class FactionInvitesDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public FactionInvitesDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public FactionInvitesDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } // ------------------ INVITES ------------------ // @@ -36,7 +36,7 @@ public CompletableFuture createFactionInvites(List invites) { .execute(); } }); - }); + }, executor); } public CompletableFuture> getAllInvites(String factionName) { @@ -58,7 +58,7 @@ public CompletableFuture> getAllInvites(String factionName) { } return data; - }); + }, executor); } public CompletableFuture> getInvitesOfPlayer(UUID playerUUID) { @@ -80,7 +80,7 @@ public CompletableFuture> getInvitesOfPlayer(UUID playerUUID) { } return data; - }); + }, executor); } public CompletableFuture getPlayerJoinRequest(UUID playerUUID) { @@ -94,7 +94,7 @@ public CompletableFuture getPlayerJoinRequest(UUID playerUUID) false, result.getTimestamp() ) : null; - }); + }, executor); } public CompletableFuture removeInvites(List invites) { @@ -106,6 +106,6 @@ public CompletableFuture removeInvites(List invites) { .execute(); } }); - }); + }, executor); } } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionIslandDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionIslandDatabaseManager.java index d445d3c2..e192de27 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionIslandDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionIslandDatabaseManager.java @@ -1,6 +1,7 @@ package net.skullian.skyfactions.common.database.impl.faction; import net.skullian.skyfactions.common.config.types.Settings; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.tables.records.FactionIslandsRecord; import net.skullian.skyfactions.common.island.IslandModificationAction; import net.skullian.skyfactions.common.island.impl.FactionIsland; @@ -8,29 +9,28 @@ import org.jooq.impl.DSL; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.FactionIslands.FACTION_ISLANDS; -public class FactionIslandDatabaseManager { - - private final DSLContext ctx; +public class FactionIslandDatabaseManager extends AbstractTableManager { public int cachedFactionIslandID; - public FactionIslandDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public FactionIslandDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); setFactionIslandCachedNextID(); } - public CompletableFuture setFactionIslandCachedNextID() { - return CompletableFuture.runAsync(() -> { + public void setFactionIslandCachedNextID() { + CompletableFuture.runAsync(() -> { Integer result = ctx.select(DSL.max(FACTION_ISLANDS.ID)) .from(FACTION_ISLANDS) .fetchOneInto(Integer.class); if (result == null) this.cachedFactionIslandID = 1; - else this.cachedFactionIslandID = (result + 1); - }); + else this.cachedFactionIslandID = (result + 1); + }, executor); } public CompletableFuture createFactionIsland(String factionName, IslandModificationAction action) { @@ -40,7 +40,7 @@ public CompletableFuture createFactionIsland(String factionName, IslandMod .columns(FACTION_ISLANDS.ID, FACTION_ISLANDS.FACTIONNAME, FACTION_ISLANDS.RUNES, FACTION_ISLANDS.DEFENCECOUNT, FACTION_ISLANDS.GEMS, FACTION_ISLANDS.LAST_RAIDED, FACTION_ISLANDS.LAST_RAIDER) .values(action.getId(), factionName, 0, 0, 0, System.currentTimeMillis() + Settings.RAIDING_FACTION_IMMUNITY.getLong(), "N/A") .execute(); - }); + }, executor); } public CompletableFuture getFactionIsland(String factionName) { @@ -50,14 +50,14 @@ public CompletableFuture getFactionIsland(String factionName) { .fetchOne(); return result != null ? new FactionIsland(result.getId()) : null; - }); + }, executor); } public CompletableFuture updateFactionLastRaid(String factionName, long newTime) { return CompletableFuture.runAsync(() -> ctx.update(FACTION_ISLANDS) .set(FACTION_ISLANDS.LAST_RAIDED, newTime) .where(FACTION_ISLANDS.FACTIONNAME.eq(factionName)) - .execute()); + .execute(), executor); } diff --git a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionsDatabaseManager.java b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionsDatabaseManager.java index 40487c4d..311239a3 100644 --- a/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionsDatabaseManager.java +++ b/common/src/main/java/net/skullian/skyfactions/common/database/impl/faction/FactionsDatabaseManager.java @@ -1,6 +1,7 @@ package net.skullian.skyfactions.common.database.impl.faction; import net.skullian.skyfactions.common.api.SkyApi; +import net.skullian.skyfactions.common.database.AbstractTableManager; import net.skullian.skyfactions.common.database.tables.records.FactionBansRecord; import net.skullian.skyfactions.common.database.tables.records.FactionIslandsRecord; import net.skullian.skyfactions.common.database.tables.records.FactionMembersRecord; @@ -17,6 +18,7 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import static net.skullian.skyfactions.common.database.tables.DefenceLocations.DEFENCE_LOCATIONS; import static net.skullian.skyfactions.common.database.tables.FactionBans.FACTION_BANS; @@ -28,12 +30,10 @@ import static net.skullian.skyfactions.common.database.tables.FactionElections.FACTION_ELECTIONS; import static net.skullian.skyfactions.common.database.tables.ElectionVotes.ELECTION_VOTES; -public class FactionsDatabaseManager { +public class FactionsDatabaseManager extends AbstractTableManager { - private final DSLContext ctx; - - public FactionsDatabaseManager(DSLContext ctx) { - this.ctx = ctx; + public FactionsDatabaseManager(DSLContext ctx, Executor executor) { + super(ctx, executor); } public CompletableFuture registerFaction(SkyUser factionOwner, String factionName) { @@ -47,7 +47,7 @@ public CompletableFuture registerFaction(SkyUser factionOwner, String fact .columns(FACTION_MEMBERS.FACTIONNAME, FACTION_MEMBERS.UUID, FACTION_MEMBERS.RANK) .values(factionName, factionOwner.getUniqueId().toString(), "owner") .execute(); - }); + }, executor); } public CompletableFuture getFaction(String factionName) { @@ -77,7 +77,7 @@ public CompletableFuture getFaction(String factionName) { SkyApi.getInstance().getDatabaseManager().getFactionInvitesManager().getAllInvites(factionName).join(), SkyApi.getInstance().getDatabaseManager().getFactionAuditLogManager().getAuditLogs(factionName).join() ) : null; - }); + }, executor); } public CompletableFuture getFaction(UUID playerUUID) { @@ -87,7 +87,7 @@ public CompletableFuture getFaction(UUID playerUUID) { .fetchOne(); return result != null ? getFaction(result.getFactionname()).join() : null; - }); + }, executor); } public CompletableFuture getFactionByIslandID(int id) { @@ -97,7 +97,7 @@ public CompletableFuture getFactionByIslandID(int id) { .fetchOne(); return result != null ? getFaction(result.getFactionname()).join() : null; - }); + }, executor); } public CompletableFuture updateFactionName(String oldName, String newName) { @@ -143,14 +143,14 @@ public CompletableFuture updateFactionName(String oldName, String newName) .where(FACTION_ELECTIONS.FACTIONNAME.eq(oldName)) .execute(); }); - }); + }, executor); } public CompletableFuture getFactionMOTD(String factionName) { return CompletableFuture.supplyAsync(() -> ctx.select(FACTIONS.MOTD) .from(FACTIONS) .where(FACTIONS.NAME.eq(factionName)) - .fetchOneInto(String.class)); + .fetchOneInto(String.class), executor); } public CompletableFuture updateFactionMOTD(String factionName, String newMOTD) { @@ -159,7 +159,7 @@ public CompletableFuture updateFactionMOTD(String factionName, String newM .set(FACTIONS.MOTD, newMOTD) .where(FACTIONS.NAME.eq(factionName)) .execute(); - }); + }, executor); } public CompletableFuture updateFactionLocale(String factionName, String newLocale) { @@ -204,7 +204,7 @@ public CompletableFuture removeFaction(String factionName) { .where(ELECTION_VOTES.ELECTION.eq(id)) .execute(); }); - }); + }, executor); } @@ -220,7 +220,7 @@ public CompletableFuture addFactionMembers(List players, String f .execute(); } }); - }); + }, executor); } public CompletableFuture updateFactionMemberRanks(String factionName, Map ranks) { @@ -233,11 +233,11 @@ public CompletableFuture updateFactionMemberRanks(String factionName, Map< .execute(); } }); - }); + }, executor); } public CompletableFuture isInFaction(UUID playerUUID) { - return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_MEMBERS, FACTION_MEMBERS.UUID.eq(playerUUID.toString()))); + return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_MEMBERS, FACTION_MEMBERS.UUID.eq(playerUUID.toString())), executor); } public CompletableFuture getFactionOwner(String factionName) { @@ -247,7 +247,7 @@ public CompletableFuture getFactionOwner(String factionName) { .fetchOne(); return result != null ? SkyApi.getInstance().getUserManager().getUser(UUID.fromString(result.getUuid())) : null; - }); + }, executor); } public CompletableFuture> getFactionMembersByRank(String factionName, RankType rank) { @@ -263,7 +263,7 @@ public CompletableFuture> getFactionMembersByRank(String factionNa } return players; - }); + }, executor); } // ------------------ ADMINISTRATION ------------------ // @@ -277,7 +277,7 @@ public CompletableFuture removeMembers(List players, String facti .execute(); } }); - }); + }, executor); } public CompletableFuture banMembers(List players, String factionName) { @@ -292,7 +292,7 @@ public CompletableFuture banMembers(List players, String factionN .execute(); } }); - }); + }, executor); } public CompletableFuture unbanMembers(List players, String factionName) { @@ -304,7 +304,7 @@ public CompletableFuture unbanMembers(List players, String factio .execute(); } }); - }); + }, executor); } public CompletableFuture> getBannedPlayers(String factionName) { @@ -320,11 +320,11 @@ public CompletableFuture> getBannedPlayers(String factionName) { } return players; - }); + }, executor); } public CompletableFuture isPlayerBanned(SkyUser player, String factionName) { - return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_BANS, FACTION_BANS.FACTIONNAME.eq(factionName), FACTION_BANS.UUID.eq(player.getUniqueId().toString()))); + return CompletableFuture.supplyAsync(() -> ctx.fetchExists(FACTION_BANS, FACTION_BANS.FACTIONNAME.eq(factionName), FACTION_BANS.UUID.eq(player.getUniqueId().toString())), executor); } }