Start command refactor

This commit is contained in:
William278 committed 2023-03-05 19:10:19 +00:00
1 parent 39e0b25d46
commit 3e27f7c5b1
57 files changed
+728 -384

No files matched your search

@@ -2,27 +2,39 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TimedTeleport;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
public class BackCommand extends CommandBase {
import java.util.List;
import java.util.Optional;
protected BackCommand(@NotNull HuskHomes implementor) {
super("back", Permission.COMMAND_BACK, implementor);
public class BackCommand extends Command {
protected BackCommand(@NotNull HuskHomes plugin) {
super("back", List.of(), plugin);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
plugin.getDatabase().getLastPosition(onlineUser).thenAccept(lastPosition ->
lastPosition.ifPresentOrElse(position -> Teleport.builder(plugin, onlineUser)
.setTarget(position)
.setEconomyActions(EconomyHook.Action.BACK_COMMAND)
.toTimedTeleport()
.thenApply(TimedTeleport::execute),
() -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage)));
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
final OnlineUser player = (OnlineUser) executor;
plugin.runAsync(() -> {
final Optional<Position> lastPosition = plugin.getDatabase().getLastPosition(player);
if (lastPosition.isEmpty()) {
plugin.getLocales().getLocale("error_no_last_position")
.ifPresent(player::sendMessage);
return;
}
Teleport.builder(plugin)
.teleporter(player)
.target(lastPosition.get())
.economyActions(EconomyHook.Action.BACK_COMMAND)
.toTimedTeleport()
.execute();
});
}
}
@@ -0,0 +1,39 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
public abstract class ChildCommand extends Node {
protected final Command parent;
private final String usage;
protected ChildCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull Command parent,
@NotNull String usage, @NotNull HuskHomes plugin) {
super(name, aliases, plugin);
this.parent = parent;
this.usage = usage;
}
@Override
@NotNull
public String getPermission() {
return new StringJoiner(".")
.add(parent.getPermission())
.add(getName()).toString();
}
@NotNull
public String getUsage() {
return "/" + parent.getName() + " " + getName() + ((" " + usage).isBlank() ? "" : " " + usage);
}
@NotNull
public final Optional<String> getDescription() {
return plugin.getLocales().getRawLocale("command_" + parent.getName() + "_" + getName() + "_description");
}
}
@@ -0,0 +1,168 @@
package net.william278.huskhomes.command;
import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.ConsoleUser;
import net.william278.paginedown.PaginatedList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class Command extends Node implements TabProvider {
public List<ChildCommand> children;
private Executable defaultExecutor;
protected Command(@NotNull String name, @NotNull List<String> aliases, @NotNull HuskHomes plugin) {
super(name, aliases, plugin);
}
public void setDefaultExecutor(@NotNull Executable defaultExecutor) {
this.defaultExecutor = defaultExecutor;
}
@NotNull
protected Executable getDefaultExecutor() {
if (defaultExecutor == null) {
throw new IllegalStateException("Default executor not set");
}
return defaultExecutor;
}
public void setChildren(@NotNull List<ChildCommand> children) {
this.children = children;
}
@Override
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
if (!executor.hasPermission(getPermission())) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
}
if (args.length >= 1 && !children.isEmpty()) {
for (Node child : children) {
if (child.matchesInput(args[0])) {
if (!executor.hasPermission(child.getPermission())) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return;
}
if (executor instanceof ConsoleUser && !isConsoleExecutable()) {
plugin.getLocales().getLocale("error_command_in_game_only")
.ifPresent(executor::sendMessage);
return;
}
child.execute(executor, removeFirstArg(args));
return;
}
}
plugin.getLocales().getLocale("error_unknown_command",
"/" + getName() + " " + getHelpCommand().getName())
.ifPresent(executor::sendMessage);
return;
}
if (executor instanceof ConsoleUser && !isConsoleExecutable()) {
plugin.getLocales().getLocale("error_command_in_game_only")
.ifPresent(executor::sendMessage);
return;
}
this.defaultExecutor.execute(executor, args);
}
@Override
@NotNull
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
if (args.length <= 1) {
return TabProvider.getMatchingNames(args[0], user, children);
} else {
for (final Node child : children) {
if (child.matchesInput(args[0]) && child instanceof TabProvider provider) {
return provider.getSuggestions(user, removeFirstArg(args));
}
}
}
return List.of();
}
@NotNull
private String[] removeFirstArg(String[] args) {
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, args.length - 1);
return newArgs;
}
@NotNull
public final String getUsage() {
final String arguments = children.stream().map(Node::getName).collect(Collectors.joining("|"));
return "/" + getName() + " [" + plugin.getLocales().truncateText(arguments, 40) + "]";
}
@NotNull
public String getDescription() {
return plugin.getLocales().getRawLocale(getName() + "_command_description")
.map(description -> plugin.getLocales().truncateText(description, 40))
.orElse(getUsage());
}
@NotNull
public List<ChildCommand> getChildren() {
return children;
}
@NotNull
protected MineDown getChildCommandList(@NotNull CommandUser user, final int page) {
final Locales locales = plugin.getLocales();
return PaginatedList.of(getChildren().stream()
.filter(child -> child.canPerform(user))
.map(command -> locales.getRawLocale("command_list_item",
Locales.escapeText(command.getUsage()),
"/" + command.parent.getName() + " " + command.getName(),
command.getDescription().map(description ->
Locales.escapeText(description.length() > 50
? description.substring(0, 49).trim() + "…"
: description)).orElse(""),
command.getDescription().map(description ->
Locales.escapeText(locales.wrapText(description, 40))).orElse(""))
.orElse(command.getUsage()))
.toList(),
locales.getBaseList(plugin.getSettings().getListItemsPerPage())
.setHeaderFormat(locales.getRawLocale("child_command_list_title",
"/" + getName()).orElse(""))
.setItemSeparator("\n").setCommand("/huskhomes:" + getName() + " help")
.build())
.getNearestValidPage(page);
}
@NotNull
public Command.HelpCommand getHelpCommand() {
return new HelpCommand(this, plugin);
}
public static class HelpCommand extends ChildCommand implements TabProvider {
protected HelpCommand(@NotNull Command parent, @NotNull HuskHomes plugin) {
super("help", List.of(), parent, "[page]", plugin);
}
@Override
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
int page = parseIntArg(args, 0).orElse(1);
executor.sendMessage(parent.getChildCommandList(executor, page));
}
@Override
@Nullable
public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
return Stream.iterate(1, i -> i + 1)
.limit(9)
.map(String::valueOf)
.collect(Collectors.toList());
}
}
}
@@ -1,67 +0,0 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.intellij.lang.annotations.Pattern;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
/**
* Represents an abstract cross-platform representation for a plugin command
*/
public abstract class CommandBase {
/**
* The input string to match for this command
*/
@NotNull
public final String command;
/**
* Alias input strings for this command
*/
@NotNull
public final String[] aliases;
/**
* The permission node required to use this command
*/
@NotNull
@Pattern(Permission.PERMISSION_PATTERN)
public final String permission;
/**
* Instance of the implementing plugin
*/
@NotNull
protected final HuskHomes plugin;
protected CommandBase(@NotNull String command, @NotNull @Subst("huskhomes") Permission permission,
@NotNull HuskHomes implementor, @NotNull String... aliases) {
this.command = command;
this.permission = permission.node;
this.plugin = implementor;
this.aliases = aliases;
}
/**
* Fires when the command is executed
*
* @param onlineUser {@link OnlineUser} executing the command
* @param args Command arguments
*/
public abstract void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args);
/**
* Returns the localised description string of this command
*
* @return the command description
*/
@NotNull
public String getDescription() {
return plugin.getLocales().getRawLocale(command + "_command_description")
.orElse("A HuskHomes command");
}
}
@@ -1,17 +0,0 @@
package net.william278.huskhomes.command;
import org.jetbrains.annotations.NotNull;
/**
* Interface providing console execution of commands
*/
public interface ConsoleExecutable {
/**
* What to do when console executes a command
*
* @param args command argument strings
*/
void onConsoleExecute(@NotNull String[] args);
}
@@ -12,7 +12,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class DelHomeCommand extends CommandBase implements TabCompletable {
public class DelHomeCommand extends Command implements TabProvider {
protected DelHomeCommand(@NotNull HuskHomes implementor) {
super("delhome", Permission.COMMAND_DELETE_HOME, implementor);
@@ -120,7 +120,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
if (user == null) {
return Collections.emptyList();
}
@@ -10,7 +10,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class DelWarpCommand extends CommandBase implements TabCompletable {
public class DelWarpCommand extends Command implements TabProvider {
protected DelWarpCommand(@NotNull HuskHomes implementor) {
super("delwarp", Permission.COMMAND_DELETE_WARP, implementor);
@@ -72,7 +72,7 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length > 1 ? Collections.emptyList() : plugin.getCache().getWarps()
.stream()
.filter(s -> s.startsWith(args.length == 1 ? args[0] : ""))
@@ -5,7 +5,7 @@ import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class DisabledCommand extends CommandBase {
public class DisabledCommand extends Command {
public DisabledCommand(@NotNull HuskHomes implementor) {
super("", Permission.COMMAND_DISABLED_MESSAGE, implementor);
@@ -18,7 +18,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class EditHomeCommand extends CommandBase implements TabCompletable {
public class EditHomeCommand extends Command implements TabProvider {
private final String[] EDIT_HOME_COMPLETIONS = {"rename", "description", "relocate", "privacy"};
@@ -308,7 +308,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
plugin.getLocales().getLocale("edit_home_menu_description",
home.getMeta().getDescription().length() > 50
? home.getMeta().getDescription().substring(0, 49).trim() + "…" : home.getMeta().getDescription(),
plugin.getLocales().formatDescription(home.getMeta().getDescription()))
plugin.getLocales().wrapText(home.getMeta().getDescription()))
.ifPresent(this::add);
}
@@ -331,7 +331,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
formattedName)
.ifPresent(this::add);
}
final String escapedName = Locales.escapeMineDown(formattedName);
final String escapedName = Locales.escapeText(formattedName);
plugin.getLocales().getRawLocale("edit_home_menu_manage_buttons", escapedName,
showPrivacyToggleButton ? plugin.getLocales()
.getRawLocale("edit_home_menu_privacy_button_"
@@ -345,7 +345,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
if (user == null) {
return Collections.emptyList();
}
@@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class EditWarpCommand extends CommandBase implements TabCompletable {
public class EditWarpCommand extends Command implements TabProvider {
private final String[] EDIT_WARP_COMPLETIONS = {"rename", "description", "relocate"};
@@ -171,7 +171,7 @@ public class EditWarpCommand extends CommandBase implements TabCompletable {
plugin.getLocales().getLocale("edit_warp_menu_description",
warp.getMeta().getDescription().length() > 50
? warp.getMeta().getDescription().substring(0, 49).trim() + "…" : warp.getMeta().getDescription(),
plugin.getLocales().formatDescription(warp.getMeta().getDescription()))
plugin.getLocales().wrapText(warp.getMeta().getDescription()))
.ifPresent(this::add);
}
@@ -198,7 +198,7 @@ public class EditWarpCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return switch (args.length) {
case 0, 1 -> plugin.getCache().getWarps()
.stream()
@@ -0,0 +1,10 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.user.CommandUser;
import org.jetbrains.annotations.NotNull;
public interface Executable {
void execute(@NotNull CommandUser executor, @NotNull String[] args);
}
@@ -18,7 +18,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class HomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public class HomeCommand extends Command implements TabProvider, ConsoleExecutable {
protected HomeCommand(@NotNull HuskHomes implementor) {
super("home", Permission.COMMAND_HOME, implementor);
@@ -119,7 +119,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
if (user == null) {
return Collections.emptyList();
}
@@ -2,7 +2,7 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.UserData;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -13,7 +13,7 @@ import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class HomeListCommand extends CommandBase implements ConsoleExecutable {
public class HomeListCommand extends Command implements ConsoleExecutable {
protected HomeListCommand(@NotNull HuskHomes implementor) {
super("homelist", Permission.COMMAND_HOME, implementor, "homes");
@@ -90,7 +90,7 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
return;
}
CompletableFuture.runAsync(() -> {
final Optional<UserData> userData = plugin.getDatabase().getUserDataByName(args[0]).join();
final Optional<SavedUser> userData = plugin.getDatabase().getUserDataByName(args[0]).join();
if (userData.isEmpty()) {
plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]);
return;
@@ -14,7 +14,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, TabCompletable {
public class HuskHomesCommand extends Command implements ConsoleExecutable, TabProvider {
private final String[] SUB_COMMANDS = {"about", "help", "reload", "update"};
private final AboutMenu aboutMenu;
@@ -158,7 +158,7 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable,
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
if (args.length == 0 || args.length == 1) {
return Arrays.stream(SUB_COMMANDS)
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
@@ -0,0 +1,115 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.ConsoleUser;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
public abstract class Node implements Executable {
protected static final String PERMISSION_PREFIX = "huskhomes.command";
protected final HuskHomes plugin;
private final String name;
private final List<String> aliases;
private boolean consoleExecutable = false;
private boolean operatorCommand = false;
protected Node(@NotNull String name, @NotNull List<String> aliases, @NotNull HuskHomes plugin) {
if (name.isBlank()) {
throw new IllegalArgumentException("Command name cannot be blank");
}
this.name = name;
this.aliases = aliases;
this.plugin = plugin;
}
@NotNull
public String getName() {
return name;
}
@NotNull
public List<String> getAliases() {
return aliases;
}
public final boolean matchesInput(@NotNull String input) {
return input.equalsIgnoreCase(getName()) || getAliases().contains(input.toLowerCase());
}
@NotNull
public String getPermission() {
return new StringJoiner(".")
.add(PERMISSION_PREFIX)
.add(getName()).toString();
}
public boolean canPerform(@NotNull CommandUser user) {
if (user instanceof ConsoleUser) {
return isConsoleExecutable();
}
return user.hasPermission(getPermission());
}
public boolean isConsoleExecutable() {
return consoleExecutable;
}
public void setConsoleExecutable(boolean consoleExecutable) {
this.consoleExecutable = consoleExecutable;
}
public boolean isOperatorCommand() {
return operatorCommand;
}
public void setOperatorCommand(boolean operatorCommand) {
this.operatorCommand = operatorCommand;
}
protected Optional<Integer> parseIntArg(@NotNull String[] args, int index) {
try {
if (args.length > index) {
return Optional.of(Integer.parseInt(args[index]));
}
} catch (NumberFormatException e) {
return Optional.empty();
}
return Optional.empty();
}
protected Optional<Double> parseDoubleArg(@NotNull String[] args, int index) {
try {
if (args.length > index) {
return Optional.of(Double.parseDouble(args[index]));
}
} catch (NumberFormatException e) {
return Optional.empty();
}
return Optional.empty();
}
protected Optional<String> parseStringArg(@NotNull String[] args, int index) {
if (args.length > index) {
return Optional.of(args[index]);
}
return Optional.empty();
}
protected Optional<String> parseGreedyString(@NotNull String[] args, int startIndex) {
if (args.length > startIndex) {
final StringJoiner sentence = new StringJoiner(" ");
for (int i = startIndex; i < args.length; i++) {
sentence.add(args[i]);
}
return Optional.of(sentence.toString().trim());
}
return Optional.empty();
}
}
@@ -19,7 +19,7 @@ import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class PublicHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public class PublicHomeCommand extends Command implements TabProvider, ConsoleExecutable {
protected PublicHomeCommand(@NotNull HuskHomes implementor) {
super("publichome", Permission.COMMAND_PUBLIC_HOME, implementor, "phome");
@@ -133,7 +133,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
// Return every public home name as username.home_name from the cache
final List<String> publicHomes = new ArrayList<>();
plugin.getCache().getPublicHomes().forEach((ownerName, homeNames) ->
@@ -11,7 +11,7 @@ import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class PublicHomeListCommand extends CommandBase implements ConsoleExecutable {
public class PublicHomeListCommand extends Command implements ConsoleExecutable {
protected PublicHomeListCommand(@NotNull HuskHomes implementor) {
super("publichomelist", Permission.COMMAND_HOME, implementor, "phomelist", "phomes");
@@ -3,7 +3,7 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.UserData;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.util.Permission;
@@ -16,7 +16,7 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class RtpCommand extends CommandBase implements ConsoleExecutable {
public class RtpCommand extends Command implements ConsoleExecutable {
protected RtpCommand(@NotNull HuskHomes implementor) {
super("rtp", Permission.COMMAND_RTP, implementor);
@@ -89,7 +89,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
.thenAccept(result -> {
if (isExecutorTeleporting &&
result.successful() && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
plugin.getDatabase().updateUserData(new UserData(onlineUser,
plugin.getDatabase().updateUserData(new SavedUser(onlineUser,
userData.get().homeSlots(), userData.get().ignoringTeleports(),
Instant.now().plus(plugin.getSettings().getRtpCooldownLength(), ChronoUnit.MINUTES)));
}
@@ -3,7 +3,7 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.UserData;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.util.Permission;
@@ -13,7 +13,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class SetHomeCommand extends CommandBase {
public class SetHomeCommand extends Command {
protected SetHomeCommand(@NotNull HuskHomes implementor) {
super("sethome", Permission.COMMAND_SET_HOME, implementor);
@@ -66,7 +66,7 @@ public class SetHomeCommand extends CommandBase {
plugin.getDatabase().getUserData(onlineUser.getUuid()).thenAccept(fetchedData -> {
// Check against economy if needed
final AtomicBoolean newSlotNeeded = new AtomicBoolean(false);
final AtomicReference<UserData> userDataToUpdate = new AtomicReference<>(null);
final AtomicReference<SavedUser> userDataToUpdate = new AtomicReference<>(null);
if (plugin.getSettings().doEconomy()) {
final int freeHomes = onlineUser.getFreeHomes(plugin.getSettings().getFreeHomeSlots(),
plugin.getSettings().doStackPermissionLimits());
@@ -79,7 +79,7 @@ public class SetHomeCommand extends CommandBase {
if (!plugin.validateEconomyCheck(onlineUser, action)) {
return;
}
userDataToUpdate.set(new UserData(onlineUser, (currentHomes.size() + 1) - freeHomes,
userDataToUpdate.set(new SavedUser(onlineUser, (currentHomes.size() + 1) - freeHomes,
fetchedData.get().ignoringTeleports(), fetchedData.get().rtpCooldown()));
} else {
if (currentHomes.size() == freeHomes) {
@@ -7,7 +7,7 @@ import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class SetSpawnCommand extends CommandBase {
public class SetSpawnCommand extends Command {
protected SetSpawnCommand(@NotNull HuskHomes implementor) {
super("setspawn", Permission.COMMAND_SET_SPAWN, implementor);
@@ -6,7 +6,7 @@ import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class SetWarpCommand extends CommandBase {
public class SetWarpCommand extends Command {
protected SetWarpCommand(@NotNull HuskHomes implementor) {
super("setwarp", Permission.COMMAND_SET_WARP, implementor);
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public class SpawnCommand extends CommandBase {
public class SpawnCommand extends Command {
protected SpawnCommand(@NotNull HuskHomes implementor) {
super("spawn", Permission.COMMAND_SPAWN, implementor);
@@ -1,23 +0,0 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* Interface providing tab completions for a command
*/
public interface TabCompletable {
/**
* What should be returned when the player attempts to TAB-complete the command
*
* @param args Current command arguments
* @return List of String arguments to offer TAB suggestions
*/
@NotNull
List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user);
}
@@ -0,0 +1,44 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.ConsoleUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public interface TabProvider {
@Nullable
List<String> suggest(@NotNull CommandUser user, @NotNull String[] args);
@NotNull
default List<String> getSuggestions(@NotNull CommandUser user, @NotNull String[] args) {
List<String> suggestions = suggest(user, args);
if (suggestions == null) {
suggestions = List.of();
}
return filter(suggestions, args);
}
@NotNull
default List<String> filter(@NotNull List<String> suggestions, @NotNull String[] args) {
return suggestions.stream()
.filter(suggestion -> args.length == 0 || suggestion.toLowerCase()
.startsWith(args[args.length - 1].toLowerCase().trim()))
.toList();
}
@NotNull
static List<String> getMatchingNames(@Nullable String argument, @NotNull CommandUser user,
@NotNull List<? extends Node> providers) {
return providers.stream()
.filter(command -> !(user instanceof ConsoleUser) || command.isConsoleExecutable())
.map(Node::getName)
.filter(commandName -> argument == null || argument.isBlank() || commandName.toLowerCase()
.startsWith(argument.toLowerCase().trim()))
.toList();
}
}
@@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
public class TpAllCommand extends CommandBase {
public class TpAllCommand extends Command {
protected TpAllCommand(@NotNull HuskHomes implementor) {
super("tpall", Permission.COMMAND_TPALL, implementor);
@@ -14,7 +14,7 @@ import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public class TpCommand extends Command implements TabProvider, ConsoleExecutable {
protected TpCommand(@NotNull HuskHomes implementor) {
super("tp", Permission.COMMAND_TP, implementor, "tpo");
@@ -203,7 +203,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
final boolean serveCoordinateCompletions = user != null && user.hasPermission(Permission.COMMAND_TP_TO_COORDINATES.node);
switch (args.length) {
case 0, 1 -> {
@@ -12,7 +12,7 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class TpHereCommand extends CommandBase implements TabCompletable {
public class TpHereCommand extends Command implements TabProvider {
protected TpHereCommand(@NotNull HuskHomes implementor) {
super("tphere", Permission.COMMAND_TPHERE, implementor, "tpohere");
@@ -52,7 +52,7 @@ public class TpHereCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length <= 1 ? plugin.getCache().getPlayers().stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
@@ -3,11 +3,11 @@ package net.william278.huskhomes.command;
import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.UserData;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class TpIgnoreCommand extends CommandBase {
public class TpIgnoreCommand extends Command {
protected TpIgnoreCommand(@NotNull HuskHomes implementor) {
super("tpignore", Permission.COMMAND_TPIGNORE, implementor);
@@ -28,7 +28,7 @@ public class TpIgnoreCommand extends CommandBase {
// Update value on the database and send a message | todo: Clean this up
plugin.getDatabase().getUserData(onlineUser.getUuid())
.thenAcceptAsync(userData -> userData.ifPresent(data -> plugin.getDatabase()
.updateUserData(new UserData(onlineUser, data.homeSlots(), isIgnoringRequests, data.rtpCooldown()))
.updateUserData(new SavedUser(onlineUser, data.homeSlots(), isIgnoringRequests, data.rtpCooldown()))
.thenRun(() -> plugin.getLocales().getRawLocale("tpignore_toggle_" + (isIgnoringRequests ? "on" : "off"),
plugin.getLocales().getRawLocale("tpignore_toggle_button").orElse(""))
.ifPresent(locale -> onlineUser.sendMessage(new MineDown(locale))))));
@@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpOfflineCommand extends CommandBase implements TabCompletable {
public class TpOfflineCommand extends Command implements TabProvider {
protected TpOfflineCommand(@NotNull HuskHomes implementor) {
super("tpoffline", Permission.COMMAND_TPOFFLINE, implementor);
@@ -53,7 +53,7 @@ public class TpOfflineCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length <= 1 ? plugin.getCache().getPlayers().stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
* A command used for responding to tp requests - can either be a /tpaccept or /tpdecline command, controlled by the
* acceptRequestCommand flag
*/
public class TpRespondCommand extends CommandBase implements TabCompletable {
public class TpRespondCommand extends Command implements TabProvider {
private final boolean acceptRequestCommand;
@@ -41,7 +41,7 @@ public class TpRespondCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length <= 1 ? plugin.getCache().getPlayers().stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
@@ -11,7 +11,7 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
public class TpaAllCommand extends CommandBase {
public class TpaAllCommand extends Command {
protected TpaAllCommand(@NotNull HuskHomes implementor) {
super("tpaall", Permission.COMMAND_TPA_ALL, implementor);
@@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpaCommand extends CommandBase implements TabCompletable {
public class TpaCommand extends Command implements TabProvider {
protected TpaCommand(@NotNull HuskHomes implementor) {
super("tpa", Permission.COMMAND_TPA, implementor);
@@ -51,7 +51,7 @@ public class TpaCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length <= 1 ? plugin.getCache().getPlayers().stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
@@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpaHereCommand extends CommandBase implements TabCompletable {
public class TpaHereCommand extends Command implements TabProvider {
protected TpaHereCommand(@NotNull HuskHomes implementor) {
super("tpahere", Permission.COMMAND_TPAHERE, implementor);
@@ -51,7 +51,7 @@ public class TpaHereCommand extends CommandBase implements TabCompletable {
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
return args.length <= 1 ? plugin.getCache().getPlayers().stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
@@ -13,7 +13,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class WarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public class WarpCommand extends Command implements TabProvider, ConsoleExecutable {
protected WarpCommand(@NotNull HuskHomes implementor) {
super("warp", Permission.COMMAND_WARP, implementor);
@@ -62,7 +62,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
public @NotNull List<String> suggest(@NotNull String[] args, @Nullable OnlineUser user) {
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() : ""))
@@ -9,7 +9,7 @@ import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class WarpListCommand extends CommandBase implements ConsoleExecutable {
public class WarpListCommand extends Command implements ConsoleExecutable {
protected WarpListCommand(@NotNull HuskHomes implementor) {
super("warplist", Permission.COMMAND_WARP, implementor, "warps");