mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Continue command refactor work
This commit is contained in:
31 files changed
+319
-391
No files matched your search
@@ -0,0 +1,129 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.SavedPosition;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class SavedPositionCommand<T extends SavedPosition> extends Command {
|
||||
|
||||
private final Class<T> positionType;
|
||||
private final List<String> arguments;
|
||||
|
||||
protected SavedPositionCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull Class<T> positionType,
|
||||
@NotNull List<String> arguments, @NotNull HuskHomes plugin) {
|
||||
super(name, aliases, "<name>" + ((arguments.size() > 0) ? " [" + String.join("|", arguments) + "]" : ""), plugin);
|
||||
this.positionType = positionType;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getOtherPermission() {
|
||||
return (positionType == Home.class ? super.getPermission() + ".other" : super.getPermission());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
final Optional<String> name = parseStringArg(args, 0);
|
||||
if (name.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
final Optional<?> position = (positionType == Home.class
|
||||
? resolveHome(executor, name.get())
|
||||
: resolveWarp(executor, name.get()));
|
||||
position.ifPresent(p -> execute(executor, (T) p, args));
|
||||
}
|
||||
|
||||
public abstract void execute(@NotNull CommandUser executor, @NotNull T position, @NotNull String[] arguments);
|
||||
|
||||
private Optional<Home> resolveHome(@NotNull CommandUser executor, @NotNull String homeName) {
|
||||
if (homeName.contains(".")) {
|
||||
final String[] splitHomeName = homeName.split("\\.");
|
||||
final Optional<Home> optionalHome = plugin.getDatabase().getUserDataByName(splitHomeName[0])
|
||||
.flatMap(owner -> plugin.getDatabase().getHome(owner.user(), splitHomeName[1]));
|
||||
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale(executor.hasPermission(getOtherPermission())
|
||||
? "error_home_invalid_other" : "error_public_home_invalid",
|
||||
splitHomeName[0], splitHomeName[1])
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Home home = optionalHome.get();
|
||||
if (executor instanceof OnlineUser user && !home.isPublic() && !user.equals(home.getOwner())
|
||||
&& !user.hasPermission(getOtherPermission())) {
|
||||
plugin.getLocales().getLocale("error_public_home_invalid",
|
||||
splitHomeName[0], splitHomeName[1])
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return optionalHome;
|
||||
} else if (executor instanceof OnlineUser owner) {
|
||||
final Optional<Home> optionalHome = plugin.getDatabase().getHome(owner, homeName);
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
return optionalHome;
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Warp> resolveWarp(@NotNull CommandUser executor, @NotNull String warpName) {
|
||||
final Optional<Warp> warp = plugin.getDatabase().getWarp(warpName);
|
||||
if (warp.isPresent() && executor instanceof OnlineUser user
|
||||
&& !warp.get().hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user)) {
|
||||
plugin.getLocales().getLocale("error_warp_invalid", warpName)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
return warp;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
return switch (args.length) {
|
||||
case 0, 1 -> {
|
||||
if (args.length == 1 && args[0].contains(".")) {
|
||||
if (executor.hasPermission(getOtherPermission())) {
|
||||
yield filter(reduceHomeList(plugin.getCache().getHomes()), args);
|
||||
}
|
||||
yield filter(reduceHomeList(plugin.getCache().getHomes()), args);
|
||||
}
|
||||
if (executor instanceof OnlineUser user) {
|
||||
yield filter(plugin.getCache().getHomes().get(user.getUsername()), args);
|
||||
}
|
||||
yield filter(reduceHomeList(plugin.getCache().getHomes()), args);
|
||||
}
|
||||
case 2 -> filter(arguments.stream().toList(), args);
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> reduceHomeList(@NotNull Map<String, List<String>> cachedMap) {
|
||||
final List<String> homeNames = new ArrayList<>();
|
||||
cachedMap.forEach((key, value) -> value.forEach(home -> homeNames.add(key + "." + home)));
|
||||
return homeNames;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user