Add modifyTrack method to TrackManager (#3823)

This commit is contained in:
Emilia Kond 2024-01-23 19:44:20 +02:00 committed by GitHub
parent e6599a2662
commit 96008f6338
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -96,6 +96,27 @@ public interface TrackManager {
*/ */
@NonNull CompletableFuture<Void> deleteTrack(@NonNull Track track); @NonNull CompletableFuture<Void> deleteTrack(@NonNull Track track);
/**
* Loads (or creates) a track from the plugin's storage provider, applies the given {@code action},
* then saves the track's data back to storage.
*
* <p>This method effectively calls {@link #createAndLoadTrack(String)}, followed by the
* {@code action}, then {@link #saveTrack(Track)}, and returns an encapsulation of the whole
* process as a {@link CompletableFuture}. </p>
*
* @param name the name of the track
* @param action the action to apply to the track
* @return a future to encapsulate the operation
* @since 5.5
*/
default @NonNull CompletableFuture<Void> modifyTrack(@NonNull String name, @NonNull Consumer<? super Track> action) {
/* This default method is overridden in the implementation, and is just here
to demonstrate what this method does in the API sources. */
return createAndLoadTrack(name)
.thenApplyAsync(track -> { action.accept(track); return track; })
.thenCompose(this::saveTrack);
}
/** /**
* Loads all tracks into memory. * Loads all tracks into memory.
* *

View File

@ -38,6 +38,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public class ApiTrackManager extends ApiAbstractManager<Track, net.luckperms.api.track.Track, TrackManager<?>> implements net.luckperms.api.track.TrackManager { public class ApiTrackManager extends ApiAbstractManager<Track, net.luckperms.api.track.Track, TrackManager<?>> implements net.luckperms.api.track.TrackManager {
public ApiTrackManager(LuckPermsPlugin plugin, TrackManager<?> handle) { public ApiTrackManager(LuckPermsPlugin plugin, TrackManager<?> handle) {
@ -74,6 +75,19 @@ public class ApiTrackManager extends ApiAbstractManager<Track, net.luckperms.api
return this.plugin.getStorage().deleteTrack(ApiTrack.cast(track), DeletionCause.API); return this.plugin.getStorage().deleteTrack(ApiTrack.cast(track), DeletionCause.API);
} }
@Override
public @NonNull CompletableFuture<Void> modifyTrack(@NonNull String name, @NonNull Consumer<? super net.luckperms.api.track.Track> action) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(action, "action");
return this.plugin.getStorage().createAndLoadTrack(name, CreationCause.API)
.thenApplyAsync(track -> {
action.accept(track.getApiProxy());
return track;
}, this.plugin.getBootstrap().getScheduler().async())
.thenCompose(track -> this.plugin.getStorage().saveTrack(track));
}
@Override @Override
public @NonNull CompletableFuture<Void> loadAllTracks() { public @NonNull CompletableFuture<Void> loadAllTracks() {
return this.plugin.getStorage().loadAllTracks(); return this.plugin.getStorage().loadAllTracks();