Skip to content

Commit 2bcf8fa

Browse files
committed
Improve playerdata import tab complete and folder checking
1 parent 680fa2b commit 2bcf8fa

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/main/java/org/mvplugins/multiverse/inventories/command/MVInvCommandCompletion.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.mvplugins.multiverse.inventories.command;
22

3+
import org.bukkit.Bukkit;
4+
import org.bukkit.generator.WorldInfo;
35
import org.jetbrains.annotations.NotNull;
46
import org.jvnet.hk2.annotations.Service;
57
import org.mvplugins.multiverse.core.command.MVCommandCompletions;
@@ -19,6 +21,7 @@
1921
import org.mvplugins.multiverse.inventories.profile.key.ProfileTypes;
2022
import org.mvplugins.multiverse.inventories.share.Sharables;
2123

24+
import java.io.File;
2225
import java.util.Arrays;
2326
import java.util.Collection;
2427
import java.util.Collections;
@@ -60,6 +63,7 @@ private MVInvCommandCompletion(
6063
commandCompletions.registerAsyncCompletion("shares", this::suggestShares);
6164
commandCompletions.registerAsyncCompletion("worldGroups", this::suggestWorldGroups);
6265
commandCompletions.registerAsyncCompletion("worldGroupWorlds", this::suggestWorldGroupWorlds);
66+
commandCompletions.registerAsyncCompletion("worldwithplayerdata", this::suggestWorldWithPlayerData);
6367
}
6468

6569
private Collection<String> suggestDataImporters(BukkitCommandCompletionContext context) {
@@ -176,4 +180,11 @@ private Collection<String> suggestWorldGroupWorlds(BukkitCommandCompletionContex
176180

177181
return addonToCommaSeperated(context.getInput(), worlds);
178182
}
183+
184+
private Collection<String> suggestWorldWithPlayerData(BukkitCommandCompletionContext context) {
185+
return Bukkit.getWorlds().stream()
186+
.filter(world -> new File(world.getWorldFolder(), "playerdata").isDirectory())
187+
.map(WorldInfo::getName)
188+
.toList();
189+
}
179190
}

src/main/java/org/mvplugins/multiverse/inventories/commands/PlayerDataImportCommand.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.mvplugins.multiverse.inventories.commands;
22

33
import com.google.common.io.Files;
4-
import org.bukkit.Bukkit;
4+
import org.bukkit.World;
55
import org.jvnet.hk2.annotations.Service;
66
import org.mvplugins.multiverse.core.command.MVCommandIssuer;
77
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
@@ -41,18 +41,24 @@ final class PlayerDataImportCommand extends InventoriesCommand {
4141
@Subcommand("playerdata import")
4242
@Syntax("<world>")
4343
@CommandPermission("multiverse.inventories.importplayerdata")
44-
@CommandCompletion("")
44+
@CommandCompletion("@worldwithplayerdata")
4545
@Description("Import player data from the world's playerdata folder.")
46-
void onCommand(MVCommandIssuer issuer, String world) {
47-
Path worldPath = Bukkit.getWorldContainer().toPath().resolve(world);
46+
void onCommand(MVCommandIssuer issuer, World world) {
47+
Path worldPath = world.getWorldFolder().toPath();
4848
File playerDataPath = worldPath.resolve("playerdata").toFile();
4949
if (!playerDataPath.isDirectory()) {
50-
issuer.sendMessage("World's playerdata folder does not exist: " + world);
50+
issuer.sendMessage("World's playerdata folder does not exist: " + world.getName());
5151
return;
5252
}
5353

5454
List<CompletableFuture<Void>> playerDataFutures = new ArrayList<>();
55-
for (File playerDataFile : playerDataPath.listFiles()) {
55+
File[] files = playerDataPath.listFiles();
56+
if (files == null) {
57+
issuer.sendMessage("No player data files found in the world's playerdata folder: " + world.getName());
58+
return;
59+
}
60+
61+
for (File playerDataFile : files) {
5662
if (!Files.getFileExtension(playerDataFile.getName()).equals("dat")) {
5763
continue;
5864
}
@@ -62,13 +68,13 @@ void onCommand(MVCommandIssuer issuer, String world) {
6268
.getGlobalProfile(GlobalProfileKey.of(playerUUID))
6369
.thenCompose(profileDataSource::updateGlobalProfile)
6470
.thenCompose(ignore -> profileDataSource.getPlayerProfile(
65-
ProfileKey.of(ContainerType.WORLD, world, ProfileTypes.getDefault(), playerUUID)))
71+
ProfileKey.of(ContainerType.WORLD, world.getName(), ProfileTypes.getDefault(), playerUUID)))
6672
.thenCompose(playerProfile -> {
6773
playerProfile.update(profileData.get());
6874
return profileDataSource.updatePlayerProfile(playerProfile);
6975
}));
7076
}
7177
CompletableFuture.allOf(playerDataFutures.toArray(new CompletableFuture[0]))
72-
.thenRun(() -> issuer.sendMessage("Successfully imported all player data from " + world + "."));
78+
.thenRun(() -> issuer.sendMessage("Successfully imported all player data from " + world.getName() + "."));
7379
}
7480
}

0 commit comments

Comments
 (0)