Skip to content

Commit f136dcc

Browse files
committed
Implement global profile clear as a BulkEditAction
1 parent 83d9675 commit f136dcc

5 files changed

Lines changed: 62 additions & 21 deletions

File tree

src/main/java/org/mvplugins/multiverse/inventories/commands/bulkedit/globalprofile/ClearCommand.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@
1616
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
1717
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
1818
import org.mvplugins.multiverse.inventories.commands.InventoriesCommand;
19+
import org.mvplugins.multiverse.inventories.commands.bulkedit.BulkEditCommand;
1920
import org.mvplugins.multiverse.inventories.profile.ProfileDataSource;
21+
import org.mvplugins.multiverse.inventories.profile.bulkedit.BulkEditAction;
22+
import org.mvplugins.multiverse.inventories.profile.bulkedit.BulkEditCreator;
2023
import org.mvplugins.multiverse.inventories.profile.key.GlobalProfileKey;
2124

2225
import java.util.Arrays;
2326
import java.util.concurrent.CompletableFuture;
2427

2528
@Service
26-
final class ClearCommand extends InventoriesCommand {
29+
final class ClearCommand extends BulkEditCommand {
2730

2831
private final CommandQueueManager commandQueueManager;
29-
private final ProfileDataSource profileDataSource;
3032
private final Flags flags;
3133

3234
@Inject
3335
ClearCommand(
36+
@NotNull BulkEditCreator bulkEditCreator,
3437
@NotNull CommandQueueManager commandQueueManager,
35-
@NotNull ProfileDataSource profileDataSource,
3638
@NotNull Flags flags
3739
) {
40+
super(bulkEditCreator);
3841
this.commandQueueManager = commandQueueManager;
39-
this.profileDataSource = profileDataSource;
4042
this.flags = flags;
4143
}
4244

4345
@Subcommand("bulkedit globalprofile clear")
4446
@CommandPermission("multiverse.inventories.bulkedit")
4547
@CommandCompletion("@mvinvplayernames @flags:groupName=" + Flags.NAME)
46-
@Syntax("<players> [--clear-all-playerprofiles]")
48+
@Syntax("<players> [--clear-all-player-profiles]")
4749
void onCommand(
4850
MVCommandIssuer issuer,
4951

@@ -55,19 +57,16 @@ void onCommand(
5557
) {
5658
ParsedCommandFlags parsedFlags = flags.parse(flagArray);
5759

58-
commandQueueManager.addToQueue(CommandQueuePayload.issuer(issuer)
59-
.prompt(Message.of("Are you sure you want to clear %d profiles?".formatted(globalProfileKeys.length)))
60-
.action(() -> doClear(issuer, globalProfileKeys, parsedFlags.hasFlag(flags.clearAllPlayerprofiles))));
61-
}
60+
BulkEditAction<?> bulkEditAction = bulkEditCreator.globalProfileClear(
61+
globalProfileKeys,
62+
parsedFlags.hasFlag(flags.clearAllPlayerProfiles)
63+
);
6264

63-
private void doClear(MVCommandIssuer issuer, GlobalProfileKey[] globalProfileKeys, boolean clearPlayerProfile) {
64-
//TODO: Check lastWorld and online
65-
CompletableFuture[] futures = Arrays.stream(globalProfileKeys)
66-
.map(globalProfileKey -> profileDataSource.deleteGlobalProfile(globalProfileKey, clearPlayerProfile))
67-
.toArray(CompletableFuture[]::new);
65+
outputActionSummary(issuer, bulkEditAction);
6866

69-
CompletableFuture.allOf(futures)
70-
.thenRun(() -> issuer.sendMessage("Successfully cleared %d profiles.".formatted(globalProfileKeys.length)));
67+
commandQueueManager.addToQueue(CommandQueuePayload.issuer(issuer)
68+
.prompt(Message.of("Are you sure you want to clear the selected global profiles?"))
69+
.action(() -> runBulkEditAction(issuer, bulkEditAction)));
7170
}
7271

7372
@Service
@@ -79,7 +78,7 @@ private Flags(@NotNull CommandFlagsManager flagsManager) {
7978
super(NAME, flagsManager);
8079
}
8180

82-
private final CommandFlag clearAllPlayerprofiles = flag(CommandFlag.builder("--clear-all-playerprofiles")
81+
private final CommandFlag clearAllPlayerProfiles = flag(CommandFlag.builder("--clear-all-player-profiles")
8382
.addAlias("-a")
8483
.build());
8584
}

src/main/java/org/mvplugins/multiverse/inventories/commands/bulkedit/playerprofile/DeleteCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ void onCommand(
6363
sharable
6464
);
6565

66-
issuer.sendMessage("Summary of affected profiles:");
67-
bulkEditAction.getActionSummary().forEach((key, value) ->
68-
issuer.sendMessage(" %s: %s".formatted(key, value.size() > 10 ? value.size() : StringFormatter.join(value, ", "))));
66+
outputActionSummary(issuer, bulkEditAction);
6967

7068
commandQueueManager.addToQueue(CommandQueuePayload.issuer(issuer)
7169
.prompt(Message.of("Are you sure you want to delete %s from the selected profiles?".formatted(sharable.getNames()[0])))

src/main/java/org/mvplugins/multiverse/inventories/profile/bulkedit/BulkEditAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.concurrent.CompletableFuture;
1616

1717
@ApiStatus.Experimental
18-
public sealed abstract class BulkEditAction<K extends GlobalProfileKey> permits PlayerFileAction, PlayerProfileAction {
18+
public sealed abstract class BulkEditAction<K extends GlobalProfileKey> permits GlobalProfileClearAction, PlayerFileAction, PlayerProfileAction {
1919

2020
protected final MultiverseInventories inventories;
2121
protected final ProfileDataSource profileDataSource;

src/main/java/org/mvplugins/multiverse/inventories/profile/bulkedit/BulkEditCreator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.jvnet.hk2.annotations.Service;
66
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
77
import org.mvplugins.multiverse.inventories.MultiverseInventories;
8+
import org.mvplugins.multiverse.inventories.profile.key.GlobalProfileKey;
89
import org.mvplugins.multiverse.inventories.share.Sharable;
910

1011
@Service
@@ -18,6 +19,10 @@ public final class BulkEditCreator {
1819
this.inventories = inventories;
1920
}
2021

22+
public BulkEditAction<?> globalProfileClear(GlobalProfileKey[] globalProfileKeys, boolean clearPlayerProfiles) {
23+
return new GlobalProfileClearAction(inventories, globalProfileKeys, clearPlayerProfiles);
24+
}
25+
2126
public BulkEditAction<?> playerProfileClear(PlayerProfilesPayload bulkProfilesPayload) {
2227
return new PlayerProfileClearAction(inventories, bulkProfilesPayload);
2328
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.mvplugins.multiverse.inventories.profile.bulkedit;
2+
3+
import org.bukkit.entity.Player;
4+
import org.mvplugins.multiverse.inventories.MultiverseInventories;
5+
import org.mvplugins.multiverse.inventories.handleshare.ReadOnlyShareHandler;
6+
import org.mvplugins.multiverse.inventories.profile.key.GlobalProfileKey;
7+
8+
import java.util.List;
9+
import java.util.concurrent.CompletableFuture;
10+
11+
final class GlobalProfileClearAction extends BulkEditAction<GlobalProfileKey> {
12+
13+
private final boolean clearPlayerProfile;
14+
15+
GlobalProfileClearAction(MultiverseInventories inventories, GlobalProfileKey[] globalProfileKeys, boolean clearPlayerProfiles) {
16+
super(inventories, globalProfileKeys);
17+
this.clearPlayerProfile = clearPlayerProfiles;
18+
}
19+
20+
@Override
21+
protected List<GlobalProfileKey> aggregateKeys() {
22+
return List.of(globalProfileKeys);
23+
}
24+
25+
@Override
26+
protected CompletableFuture<Void> performAction(GlobalProfileKey key) {
27+
return profileDataSource.deleteGlobalProfile(key, clearPlayerProfile);
28+
}
29+
30+
@Override
31+
protected boolean isOnlinePlayerAffected(GlobalProfileKey key, Player player) {
32+
return super.isOnlinePlayerAffected(key, player) && clearPlayerProfile;
33+
}
34+
35+
@Override
36+
protected void updateOnlinePlayerNow(Player player) {
37+
new ReadOnlyShareHandler(inventories, player).handleSharing();
38+
}
39+
}

0 commit comments

Comments
 (0)