Skip to content

Commit

Permalink
Fix trackers not being created on BattleArena reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Aug 25, 2024
1 parent 1d39883 commit 86cbdd4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
16 changes: 15 additions & 1 deletion src/main/java/org/battleplugins/tracker/BattleTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,24 @@ public void registerCalculator(RatingCalculator calculator) {
this.ratingCalculators.put(calculator.getName(), calculator);
}

void registerTracker(Tracker tracker) {
/**
* Registers a {@link Tracker}.
*
* @param tracker the tracker to register
*/
public void registerTracker(Tracker tracker) {
this.trackers.put(tracker.getName(), tracker);
}

/**
* Unregisters a {@link Tracker}.
*
* @param tracker the tracker to unregister
*/
public void unregisterTracker(Tracker tracker) {
this.trackers.remove(tracker.getName());
}

/**
* Registers a {@link Listener} to a {@link Tracker}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected TrackerSqlSerializer createSerializer() {
StatType.MAX_STREAK, StatType.MAX_RANKING, StatType.RATING,
StatType.MAX_RATING, StatType.MAX_KD_RATIO
);

return new TrackerSqlSerializer(
this,
generalStats,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.battleplugins.tracker.feature.battlearena;

import com.google.common.base.Supplier;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.competition.JoinResult;
import org.battleplugins.arena.competition.LiveCompetition;
import org.battleplugins.arena.competition.phase.CompetitionPhaseType;
import org.battleplugins.arena.event.BattleArenaReloadEvent;
import org.battleplugins.arena.event.BattleArenaReloadedEvent;
import org.battleplugins.arena.event.arena.ArenaCreateExecutorEvent;
import org.battleplugins.arena.event.arena.ArenaDrawEvent;
import org.battleplugins.arena.event.arena.ArenaInitializeEvent;
Expand All @@ -14,10 +17,13 @@
import org.battleplugins.arena.event.player.ArenaPreJoinEvent;
import org.battleplugins.arena.options.types.BooleanArenaOption;
import org.battleplugins.tracker.BattleTracker;
import org.battleplugins.tracker.SqlTracker;
import org.battleplugins.tracker.TrackedDataType;
import org.battleplugins.tracker.Tracker;
import org.battleplugins.tracker.stat.Record;
import org.battleplugins.tracker.stat.StatType;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -34,10 +40,54 @@ public class BattleArenaListener implements Listener {
private final BattleTracker battleTracker;
private final Map<Arena, Consumer<Tracker>> pendingExecutors = new HashMap<>();

private boolean reloaded;

public BattleArenaListener(BattleTracker battleTracker) {
this.battleTracker = battleTracker;
}

@EventHandler
public void onBattleArenaReload(BattleArenaReloadEvent event) {
this.reloaded = true;

for (Arena arena : event.getBattleArena().getArenas()) {
Tracker tracker = this.battleTracker.getTracker(arena.getName().toLowerCase(Locale.ROOT));
if (tracker == null) {
continue;
}

tracker.saveAll().whenComplete((aVoid, e) -> {
if (e != null) {
this.battleTracker.error("Failed to save tracker data for Arena: {}.", arena.getName());
e.printStackTrace();
}

tracker.destroy();
});

this.battleTracker.unregisterTracker(tracker);
}
}

@EventHandler
public void onBattleArenaReloaded(BattleArenaReloadedEvent event) {
for (Arena arena : event.getBattleArena().getArenas()) {
Tracker tracker = this.battleTracker.getTracker(arena.getName().toLowerCase(Locale.ROOT));
if (tracker == null) {
continue;
}

// Ensure records are created for online players
for (Player player : Bukkit.getOnlinePlayers()) {
tracker.getOrCreateRecord((OfflinePlayer) player).whenComplete((record, e) -> {
if (tracker instanceof SqlTracker sqlTracker) {
sqlTracker.getRecords().lock(player.getUniqueId());
}
});
}
}
}

@EventHandler
public void onArenaInitialize(ArenaInitializeEvent event) {
Arena arena = event.getArena();
Expand All @@ -48,25 +98,33 @@ public void onArenaInitialize(ArenaInitializeEvent event) {
}

Consumer<Tracker> pendingExecutor = this.pendingExecutors.remove(arena);
Supplier<Tracker> supplier = () -> {
Tracker tracker = new ArenaTracker(
this.battleTracker,
arena.getName(),
this.battleTracker.getCalculator("elo"),
Set.of(TrackedDataType.PVP),
List.of()
);

if (pendingExecutor != null) {
pendingExecutor.accept(tracker);
}

return tracker;
};

this.battleTracker.registerTracker(
event.getArena().getName().toLowerCase(Locale.ROOT),
() -> {
Tracker tracker = new ArenaTracker(
this.battleTracker,
arena.getName(),
this.battleTracker.getCalculator("elo"),
Set.of(TrackedDataType.PVP),
List.of()
);

if (pendingExecutor != null) {
pendingExecutor.accept(tracker);
}

return tracker;
}
supplier
);

// This is kind of silly, but in the event that the plugin is reloaded,
// we want to ensure our tracker instance gets updated.
if (this.reloaded) {
this.battleTracker.registerTracker(supplier.get());
}

this.battleTracker.info("Enabled tracking for arena: {}.", arena.getName());
}

Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/battleplugins/tracker/sql/SqlSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,13 @@ protected ResultSetConnection executeQuery(Connection con, boolean displayErrors

protected void executeUpdate(boolean async, String strRawStmt, Object... varArgs) {
if (async) {
new Thread(() -> {
CompletableFuture.runAsync(() -> {
try {
this.executeUpdate(strRawStmt, varArgs);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
});
} else {
try {
this.executeUpdate(strRawStmt, varArgs);
Expand Down Expand Up @@ -433,17 +433,11 @@ protected int executeUpdate(String strRawStmt, Object... varArgs) {
}

protected CompletableFuture<Void> executeBatch(boolean async, String updateStatement, List<List<Object>> batch) {
CompletableFuture<Void> future = new CompletableFuture<>();
CompletableFuture<Void> future;
if (async) {
new Thread(() -> {
try {
this.executeBatch(updateStatement, batch);
future.complete(null);
} catch (Exception e) {
future.completeExceptionally(e);
}
}).start();
future = CompletableFuture.runAsync(() -> this.executeBatch(updateStatement, batch));
} else {
future = new CompletableFuture<>();
try {
this.executeBatch(updateStatement, batch);
future.complete(null);
Expand Down

0 comments on commit 86cbdd4

Please sign in to comment.