List commands, more refactoring work

This commit is contained in:
William278 committed 2023-03-17 17:00:44 +00:00
1 parent 9903503f03
commit f746d67339
23 files changed
+361 -450

No files matched your search

@@ -1,101 +1,37 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TimedTeleport;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.teleport.Teleportable;
import net.william278.huskhomes.user.CommandUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.Optional;
public class WarpCommand extends Command implements TabProvider, ConsoleExecutable {
public class WarpCommand extends SavedPositionCommand<Warp> {
protected WarpCommand(@NotNull HuskHomes implementor) {
super("warp", Permission.COMMAND_WARP, implementor);
protected WarpCommand(@NotNull HuskHomes plugin) {
super("warp", List.of(), Warp.class, List.of(), plugin);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getWarps()
.thenApply(warps -> warps.stream()
.filter(warp -> warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), onlineUser))
.collect(Collectors.toList()))
.thenAccept(warps -> {
if (warps.isEmpty()) {
plugin.getLocales().getLocale("error_no_warps_set")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
plugin.getSettings().getListItemsPerPage(), 1)
.ifPresent(onlineUser::sendMessage);
});
case 1 -> {
final String warpName = args[0];
plugin.getDatabase()
.getWarp(warpName)
.thenAccept(warpResult -> warpResult.ifPresentOrElse(warp -> {
// Handle permission restrictions
if (!warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), onlineUser)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
Teleport.builder(plugin, onlineUser)
.setTarget(warp)
.toTimedTeleport()
.thenAccept(TimedTeleport::execute);
},
() -> plugin.getLocales().getLocale("error_warp_invalid", warpName)
.ifPresent(onlineUser::sendMessage)));
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warp [name]")
.ifPresent(onlineUser::sendMessage);
}
}
@Override
@NotNull
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
return plugin.getCache().getWarps().stream()
.filter(s -> user == null || Warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user, s))
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
.sorted()
.collect(Collectors.toList());
}
@Override
public void onConsoleExecute(@NotNull String[] args) {
if (args.length != 2) {
plugin.log(Level.WARNING, "Invalid syntax. Usage: warp <player> <warp>");
return;
}
final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null);
if (playerToTeleport == null) {
plugin.log(Level.WARNING, "Player not found: " + args[0]);
return;
}
plugin.getDatabase().getWarp(args[1]).thenAccept(optionalWarp -> {
if (optionalWarp.isEmpty()) {
plugin.log(Level.WARNING, "Could not find warp '" + args[1] + "'");
public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) {
if (plugin.getSettings().isPermissionRestrictWarps()) {
if (!executor.hasPermission(warp.getPermission()) && !executor.hasPermission(Warp.getWildcardPermission())) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
}
final Warp warp = optionalWarp.get();
}
plugin.log(Level.INFO, "Teleporting " + playerToTeleport.getUsername() + " to " + warp.getMeta().getName());
Teleport.builder(plugin, playerToTeleport)
.setTarget(warp)
.toTimedTeleport()
.thenAccept(TimedTeleport::execute);
});
final Optional<Teleportable> optionalTeleporter = resolveTeleportable(executor, args);
if (optionalTeleporter.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
return;
}
this.teleport(executor, optionalTeleporter.get(), warp);
}
}