mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Add delhome and delwarp
This commit is contained in:
1 parent
12f1823662
commit
970941b21b
6 files changed
+170
-7
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.Player;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DelHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||
|
||||
public DelHomeCommand(@NotNull HuskHomes implementor) {
|
||||
super("delhome", Permission.COMMAND_DELETE_HOME, implementor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull Player player, @NotNull String[] args) {
|
||||
final User user = new User(player);
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getHomes(user).thenAccept(homes -> {
|
||||
if (homes.size() == 1) {
|
||||
homes.stream().findFirst().ifPresent(home -> plugin.getSavedPositionManager().deleteHome(user, home.meta.name));
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/delhome <name>")
|
||||
.ifPresent(player::sendMessage);
|
||||
}
|
||||
});
|
||||
case 1 -> {
|
||||
final String homeName = args[0];
|
||||
if (RegexUtil.OWNER_NAME_PATTERN.matcher(homeName).matches()) {
|
||||
final String ownerName = homeName.split("\\.")[0];
|
||||
final String ownersHomeName = homeName.split("\\.")[1];
|
||||
plugin.getDatabase().getUserByName(ownerName).thenAccept(optionalUserData -> optionalUserData
|
||||
.ifPresentOrElse(userData -> {
|
||||
if (!userData.uuid.equals(player.getUuid())) {
|
||||
if (!player.hasPermission(Permission.COMMAND_DELETE_HOME_OTHER.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(player::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
plugin.getSavedPositionManager().deleteHome(userData, ownersHomeName);
|
||||
},
|
||||
() -> plugin.getLocales().getLocale("error_home_invalid_other", ownerName, homeName)
|
||||
.ifPresent(player::sendMessage)));
|
||||
} else {
|
||||
plugin.getSavedPositionManager().deleteHome(user, homeName);
|
||||
}
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/delhome <name>")
|
||||
.ifPresent(player::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull Player player, @NotNull String[] args) {
|
||||
return plugin.getCache().homes.get(player.getUuid()).stream()
|
||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.Player;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DelWarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||
|
||||
public DelWarpCommand(@NotNull HuskHomes implementor) {
|
||||
super("delwarp", Permission.COMMAND_DELETE_WARP, implementor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull Player player, @NotNull String[] args) {
|
||||
if (args.length == 1) {
|
||||
plugin.getSavedPositionManager().deleteWarp(args[0]);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/delwarp <name>")
|
||||
.ifPresent(player::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull Player player, @NotNull String[] args) {
|
||||
return plugin.getCache().warps.stream()
|
||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -51,15 +51,15 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
//todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull Player player, @NotNull String[] args) {
|
||||
return plugin.getCache().homes.get(player.getUuid()).stream()
|
||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
//todo
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,30 @@ public class SavedPositionManager {
|
||||
}).join()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to delete a {@link User}'s home by given name
|
||||
*
|
||||
* @param homeOwner the owner of the home
|
||||
* @param homeName the name of the home
|
||||
* @return a future supplying a boolean; {@code true} if the home was deleted, otherwise {@code false} if the home name was invalid.
|
||||
*/
|
||||
public CompletableFuture<Boolean> deleteHome(@NotNull User homeOwner, @NotNull String homeName) {
|
||||
return CompletableFuture.supplyAsync(() -> database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
|
||||
if (optionalHome.isPresent()) {
|
||||
final Home home = optionalHome.get();
|
||||
return database.deleteHome(home.uuid).thenApply(ignored -> {
|
||||
cache.homes.computeIfPresent(home.owner.uuid, (ownerUUID, homeNames) -> {
|
||||
homeNames.remove(home.meta.name);
|
||||
return homeNames;
|
||||
});
|
||||
cache.positionLists.clear();
|
||||
return true;
|
||||
}).join();
|
||||
}
|
||||
return false;
|
||||
}).join());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to create a server {@link Warp} at the specified {@link Position} from supplied
|
||||
* {@link PositionMeta} data and add it to the database
|
||||
@@ -80,6 +104,26 @@ public class SavedPositionManager {
|
||||
}).join()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to delete a warp by given name
|
||||
*
|
||||
* @param warpName the name of the warp
|
||||
* @return a future supplying a boolean; {@code true} if the warp was deleted, otherwise {@code false} if the warp name was invalid.
|
||||
*/
|
||||
public CompletableFuture<Boolean> deleteWarp(@NotNull String warpName) {
|
||||
return CompletableFuture.supplyAsync(() -> database.getWarp(warpName).thenApply(optionalWarp -> {
|
||||
if (optionalWarp.isPresent()) {
|
||||
final Warp warp = optionalWarp.get();
|
||||
return database.deleteWarp(warp.uuid).thenApply(ignored -> {
|
||||
cache.warps.remove(warp.meta.name);
|
||||
cache.positionLists.clear();
|
||||
return true;
|
||||
}).join();
|
||||
}
|
||||
return false;
|
||||
}).join());
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates {@link PositionMeta} to ensure it can be added to the database
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user