mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 03:25:19 +01:00
Add modifyTrack method to TrackManager (#3823)
This commit is contained in:
parent
e6599a2662
commit
96008f6338
@ -96,6 +96,27 @@ public interface TrackManager {
|
||||
*/
|
||||
@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.
|
||||
*
|
||||
|
@ -38,6 +38,7 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
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 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);
|
||||
}
|
||||
|
||||
@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
|
||||
public @NonNull CompletableFuture<Void> loadAllTracks() {
|
||||
return this.plugin.getStorage().loadAllTracks();
|
||||
|
Loading…
Reference in New Issue
Block a user