Add /delhome and /delwarp all

This commit is contained in:
William committed 2022-08-30 18:41:51 +01:00
1 parent 54e4b5ddef
commit 51dcad3843
2 files changed
+76 -9

No files matched your search

@@ -2,12 +2,17 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class DelWarpCommand extends CommandBase implements TabCompletable {
@@ -24,10 +29,15 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
if (deleted) {
plugin.getLocales().getLocale("warp_deleted", warpName)
.ifPresent(onlineUser::sendMessage);
} else {
plugin.getLocales().getLocale("error_warp_invalid", warpName)
.ifPresent(onlineUser::sendMessage);
return;
}
if (warpName.equals("all")) {
deleteAllWarps(onlineUser);
return;
}
plugin.getLocales().getLocale("error_warp_invalid", warpName)
.ifPresent(onlineUser::sendMessage);
});
} else {
plugin.getLocales().getLocale("error_invalid_syntax", "/delwarp <name>")
@@ -35,6 +45,29 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
}
}
/**
* Delete all the server warps
*
* @param deleter the player who is deleting the warps
*/
private void deleteAllWarps(@NotNull OnlineUser deleter) {
plugin.getDatabase().getWarps().thenAccept(warps -> {
if (warps.isEmpty()) {
plugin.getLocales().getLocale("error_no_warps_set")
.ifPresent(deleter::sendMessage);
return;
}
final List<CompletableFuture<Boolean>> homeDeletionFuture = new ArrayList<>();
for (final Warp toDelete : warps) {
homeDeletionFuture.add(plugin.getSavedPositionManager().deleteWarp(toDelete.meta.name));
}
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRun(() ->
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(homeDeletionFuture.size()))
.ifPresent(deleter::sendMessage)).join();
});
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length > 1 ? Collections.emptyList() : plugin.getCache().warps.stream()