Skip to content

Commit 158cd0e

Browse files
committed
Load world shops asynchronously and filter database queries
1 parent d5dc258 commit 158cd0e

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,6 @@ target/
111111
.gradle/
112112
!gradle/wrapper/gradle-wrapper.jar
113113
/build-logic
114+
115+
# Generated Maven Shade dependency metadata
116+
dependency-reduced-pom.xml

‎quickshop-bukkit/src/main/java/com/ghostchu/quickshop/database/SimpleDatabaseHelperV2.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,10 @@ public void insertTransactionRecord(@Nullable UUID from, @Nullable UUID to, fina
533533
+ " INNER JOIN " + DataTables.SHOP_MAP.getName()
534534
+ " ON " + DataTables.SHOP_MAP.getName() + ".shop = " + DataTables.SHOPS.getName() + ".id"
535535
+ " LEFT JOIN " + DataTables.EXTERNAL_CACHE.getName()
536-
+ " ON " + DataTables.EXTERNAL_CACHE.getName() + ".shop = " + DataTables.SHOPS.getName() + ".id";
537-
try(final SQLQuery query = manager.createQuery().withPreparedSQL(SQL).execute()) {
536+
+ " ON " + DataTables.EXTERNAL_CACHE.getName() + ".shop = " + DataTables.SHOPS.getName() + ".id"
537+
+ (worldFilter == null ? "" : " WHERE " + DataTables.SHOP_MAP.getName() + ".world = ?");
538+
try(final SQLQuery query = manager.createQuery().withPreparedSQL(SQL)
539+
.setParams(worldFilter == null ? java.util.List.of() : java.util.List.of(worldFilter)).execute()) {
538540
final ResultSet rs = query.getResultSet();
539541
while(rs.next()) {
540542
final String world = rs.getString("world");

‎quickshop-bukkit/src/main/java/com/ghostchu/quickshop/listener/WorldListener.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.ghostchu.quickshop.QuickShop;
44
import com.ghostchu.quickshop.api.shop.Shop;
5+
import com.ghostchu.quickshop.util.Util;
56
import com.ghostchu.quickshop.api.shop.ShopChunk;
67
import com.ghostchu.simplereloadlib.ReloadResult;
78
import com.ghostchu.simplereloadlib.ReloadStatus;
@@ -34,7 +35,21 @@ public void onWorldLoad(final WorldLoadEvent e) {
3435
*/
3536
final World world = e.getWorld();
3637

37-
plugin.getShopLoader().loadShops(world.getName());
38+
restoreWorldReferences(world);
39+
// Database fetching and waiting for loader workers must not block the global tick thread.
40+
Util.asyncThreadRun(() -> {
41+
if(!plugin.getJavaPlugin().isEnabled() || org.bukkit.Bukkit.getWorld(world.getUID()) != world) {
42+
return;
43+
}
44+
try {
45+
plugin.getShopLoader().loadShops(world.getName());
46+
} catch(final Exception exception) {
47+
plugin.logger().error("Failed to load shops for world {}", world.getName(), exception);
48+
}
49+
});
50+
}
51+
52+
private void restoreWorldReferences(final World world) {
3853
// New world data
3954
final Map<ShopChunk, Map<Location, Shop>> inWorld = new ConcurrentHashMap<>(1);
4055
// Old world data

0 commit comments

Comments
 (0)