Add delhome and delwarp

This commit is contained in:
William committed 2022-06-12 00:39:26 +01:00
1 parent 12f1823662
commit 970941b21b
6 files changed
+170 -7

No files matched your search

@@ -177,8 +177,8 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes {
// Register commands - todo add all here
getLogger().log(Level.INFO, "Registering commands...");
final CommandBase[] commands = new CommandBase[]{
new HomeCommand(this), new SetHomeCommand(this), new HomeListCommand(this),
new WarpCommand(this), new SetWarpCommand(this), new WarpListCommand(this),
new HomeCommand(this), new SetHomeCommand(this), new HomeListCommand(this), new DelHomeCommand(this),
new WarpCommand(this), new SetWarpCommand(this), new WarpListCommand(this), new DelWarpCommand(this),
new BackCommand(this)};
for (CommandBase commandBase : commands) {
final PluginCommand pluginCommand = getCommand(commandBase.command);
+10
View File
@@ -11,5 +11,15 @@ commands:
usage: /sethome <name>
homelist:
usage: /homelist [page]
delhome:
usage: /delhome <name>
warp:
usage: /warp <name>
setwarp:
usage: /setwarp <name>
warplist:
usage: /warplist [page]
delwarp:
usage: /delwarp <name>
back:
usage: /back
@@ -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
*