package example;
import com.marioded.reverse.api.RecordSession;
import com.marioded.reverse.api.RecordingBounds;
import com.marioded.reverse.api.ReplayText;
import com.marioded.reverse.api.catalog.ReplayCatalog;
import com.marioded.reverse.api.recorder.ReverseRecorder;
import com.marioded.reverse.api.recorder.service.StopResult;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import java.util.logging.Level;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
/** Example match hooks; invoke start/finish from your own main-thread match lifecycle. */
public final class RecordingIntegration extends JavaPlugin {
private ReverseRecorder recorder;
@Override
public void onEnable() {
recorder = ReverseRecorder.get().orElseThrow(
() -> new IllegalStateException("Reverse recorder is not available"));
}
public UUID startMatch(World world, UUID ownerId, Set<UUID> participants) {
ReplayCatalog.validateMetadata("Arena final", "Best of three");
RecordSession session = recorder.getRecorderService().startRecording(
world, null, List.of(ownerId), new ReplayText("Arena final", "Best of three"),
new RecordingBounds(-64, 0, -64, 64, 255, 64), Set.copyOf(participants));
return session.getUniqueId(); // Store this in your match object.
}
public CompletionStage<StopResult> finishMatch(UUID sessionId) {
return reportSave(sessionId, recorder.getRecorderService().stopSession(sessionId));
}
/** Main thread: export the last ten seconds while the source keeps recording. */
public CompletionStage<StopResult> exportRecentClip(UUID sessionId) {
return reportSave(sessionId, recorder.getRecorderService().saveRecentClip(
sessionId, Duration.ofSeconds(10), new ReplayText("Recent round", "Last ten seconds")));
}
/** Main thread: export inclusive offsets; null to means the current tick. */
public CompletionStage<StopResult> exportClip(UUID sessionId, Duration from, Duration to) {
return reportSave(sessionId, recorder.getRecorderService().saveClip(
sessionId, from, to, new ReplayText("Round clip", "Selected interval")));
}
private CompletionStage<StopResult> reportSave(
UUID sessionId, CompletionStage<StopResult> saving) {
saving.whenComplete((result, failure) -> {
if (failure != null) {
getLogger().log(Level.SEVERE, "Could not save recording " + sessionId, failure);
return;
}
if (result.replayId() == null) {
getLogger().warning("Source session was not recording: " + sessionId);
return;
}
if (result.degraded()) {
getLogger().log(Level.WARNING, "Events saved without usable terrain: " + result.replayId(),
result.terrainFailure());
return;
}
// Publish the UUID to your match database or application here.
// Schedule onto the main thread before touching Bukkit player/world state.
getLogger().info("Replay ready: " + result.replayId());
});
return saving;
}
}