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

@@ -3,13 +3,16 @@ 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.util.Permission;
import net.william278.huskhomes.util.RegexUtil;
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 DelHomeCommand extends CommandBase implements TabCompletable {
@@ -20,8 +23,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) { //todo delhome all
switch (args.length) {
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
if (homes.size() == 1) {
homes.stream().findFirst().ifPresent(home -> deletePlayerHome(onlineUser, onlineUser, home.meta.name));
@@ -65,19 +67,51 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
if (deleter.uuid.equals(homeOwner.uuid)) {
if (deleted) {
plugin.getLocales().getLocale("home_deleted", homeName).ifPresent(deleter::sendMessage);
} else {
plugin.getLocales().getLocale("error_home_invalid", homeName).ifPresent(deleter::sendMessage);
return;
}
if (homeName.equals("all")) {
deleteAllHomes(deleter, homeOwner);
return;
}
plugin.getLocales().getLocale("error_home_invalid", homeName).ifPresent(deleter::sendMessage);
} else {
if (deleted) {
plugin.getLocales().getLocale("home_deleted_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
} else {
plugin.getLocales().getLocale("error_home_invalid_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
return;
}
if (homeName.equals("all")) {
deleteAllHomes(deleter, homeOwner);
return;
}
plugin.getLocales().getLocale("error_home_invalid_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
}
});
}
/**
* Delete all of a player's homes
*
* @param deleter the player who is deleting the homes
* @param homeOwner the player who owns the homes
*/
private void deleteAllHomes(@NotNull OnlineUser deleter, @NotNull User homeOwner) {
plugin.getDatabase().getHomes(homeOwner).thenAccept(homes -> {
if (homes.isEmpty()) {
plugin.getLocales().getLocale("error_no_warps_set")
.ifPresent(deleter::sendMessage);
return;
}
final List<CompletableFuture<Boolean>> homeDeletionFuture = new ArrayList<>();
for (final Home toDelete : homes) {
homeDeletionFuture.add(plugin.getSavedPositionManager().deleteHome(homeOwner, toDelete.meta.name));
}
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRun(() ->
plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(homeDeletionFuture.size()))
.ifPresent(deleter::sendMessage)).join();
});
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
if (user == null) {
@@ -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()