Skip to content

Commit

Permalink
Don't block tracker connection destroy on BattleArena reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Sep 22, 2024
1 parent ee49462 commit dfe0bef
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/battleplugins/tracker/BattleTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private CompletableFuture<Void> disable(boolean block) {
listeners.forEach(HandlerList::unregisterAll);
}

saveFutures.add(tracker.saveAll().thenRun(tracker::destroy));
saveFutures.add(tracker.saveAll().thenRun(() -> tracker.destroy(block)));
}

CompletableFuture<Void> future = CompletableFuture.allOf(saveFutures.toArray(CompletableFuture[]::new))
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/battleplugins/tracker/SqlTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class SqlTracker implements Tracker {
protected final BattleTracker battleTracker;
Expand Down Expand Up @@ -334,11 +335,21 @@ public void flush(boolean aggressive) {
}

@Override
public void destroy() {
try {
this.sqlSerializer.closeConnection(this.sqlSerializer.getConnection());
} catch (SQLException e) {
throw new RuntimeException("Failed to close connection!", e);
public void destroy(boolean block) {
Supplier<Void> destroyRunnable = () -> {
try {
this.sqlSerializer.closeConnection(this.sqlSerializer.getConnection());
} catch (SQLException e) {
throw new RuntimeException("Failed to close connection!", e);
}

return null;
};

if (block) {
destroyRunnable.get();
} else {
CompletableFuture.supplyAsync(destroyRunnable);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/battleplugins/tracker/Tracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,5 @@ default boolean tracksData(TrackedDataType type) {
/**
* Destroys the tracker.
*/
void destroy();
void destroy(boolean block);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void onBattleArenaReload(BattleArenaReloadEvent event) {
e.printStackTrace();
}

tracker.destroy();
tracker.destroy(false);
});

this.battleTracker.unregisterTracker(tracker);
Expand Down

0 comments on commit dfe0bef

Please sign in to comment.