From f746d673394fff042f4ab5b74109095a00310316 Mon Sep 17 00:00:00 2001 From: William278 Date: Fri, 17 Mar 2023 17:00:44 +0000 Subject: [PATCH] List commands, more refactoring work --- .../huskhomes/command/BukkitCommand.java | 2 +- .../event/BukkitEventDispatcher.java | 9 +- .../huskhomes/event/HomeListEvent.java | 7 +- .../huskhomes/event/WarpListEvent.java | 11 +- .../java/net/william278/huskhomes/Cache.java | 103 ---------------- .../net/william278/huskhomes/HuskHomes.java | 7 ++ .../huskhomes/command/HomeCommand.java | 17 +-- .../huskhomes/command/HomeListCommand.java | 71 ----------- .../huskhomes/command/HuskHomesCommand.java | 26 ++++- .../huskhomes/command/ListCommand.java | 22 ++++ .../william278/huskhomes/command/Node.java | 6 + .../huskhomes/command/PrivateHomeCommand.java | 25 ++++ .../command/PrivateHomeListCommand.java | 94 +++++++++++++++ .../huskhomes/command/PublicHomeCommand.java | 11 ++ .../command/PublicHomeListCommand.java | 106 +++++++---------- .../command/SavedPositionCommand.java | 22 +++- .../huskhomes/command/TabProvider.java | 1 - .../huskhomes/command/WarpCommand.java | 104 ++++------------- .../huskhomes/command/WarpListCommand.java | 110 +++++++++--------- .../huskhomes/event/EventDispatcher.java | 5 +- .../huskhomes/event/IHomeListEvent.java | 3 +- .../huskhomes/event/IWarpListEvent.java | 7 +- .../william278/huskhomes/position/Warp.java | 42 ++----- 23 files changed, 361 insertions(+), 450 deletions(-) delete mode 100644 common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java create mode 100644 common/src/main/java/net/william278/huskhomes/command/ListCommand.java create mode 100644 common/src/main/java/net/william278/huskhomes/command/PrivateHomeListCommand.java diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java index e6dc0e9b..af56b90a 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java @@ -65,7 +65,7 @@ public class BukkitCommand implements CommandExecutor, TabCompleter { public enum Type { HOME_COMMAND(new PrivateHomeCommand(BukkitHuskHomes.getInstance())), SET_HOME_COMMAND(new SetHomeCommand(BukkitHuskHomes.getInstance())), - HOME_LIST_COMMAND(new HomeListCommand(BukkitHuskHomes.getInstance())), + HOME_LIST_COMMAND(new PrivateHomeListCommand(BukkitHuskHomes.getInstance())), DEL_HOME_COMMAND(new DelHomeCommand(BukkitHuskHomes.getInstance())), EDIT_HOME_COMMAND(new EditHomeCommand(BukkitHuskHomes.getInstance())), PUBLIC_HOME_COMMAND(new PublicHomeCommand(BukkitHuskHomes.getInstance())), diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java index 90180f11..f8a28cb0 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.User; import net.william278.huskhomes.position.Home; @@ -53,13 +54,13 @@ public interface BukkitEventDispatcher extends EventDispatcher { } @Override - default IHomeListEvent getViewHomeListEvent(@NotNull List homes, @NotNull OnlineUser user, boolean publicHomeList) { - return new HomeListEvent(homes, user, publicHomeList); + default IHomeListEvent getViewHomeListEvent(@NotNull List homes, @NotNull CommandUser listViewer, boolean publicHomeList) { + return new HomeListEvent(homes, listViewer, publicHomeList); } @Override - default IWarpListEvent getViewWarpListEvent(@NotNull List warps, @NotNull OnlineUser user) { - return new WarpListEvent(warps, user); + default IWarpListEvent getViewWarpListEvent(@NotNull List warps, @NotNull CommandUser listViewer) { + return new WarpListEvent(warps, listViewer); } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/HomeListEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/HomeListEvent.java index dd2c4129..db6a05fa 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/HomeListEvent.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/HomeListEvent.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.position.Home; import org.bukkit.event.Cancellable; @@ -15,11 +16,11 @@ public class HomeListEvent extends Event implements IHomeListEvent, Cancellable @NotNull private final List homes; @NotNull - private final OnlineUser onlineUser; + private final CommandUser onlineUser; private final boolean isPublicHomeList; private boolean cancelled; - public HomeListEvent(@NotNull List homes, @NotNull OnlineUser onlineUser, boolean isPublicHomeList) { + public HomeListEvent(@NotNull List homes, @NotNull CommandUser onlineUser, boolean isPublicHomeList) { this.homes = homes; this.onlineUser = onlineUser; this.isPublicHomeList = isPublicHomeList; @@ -44,7 +45,7 @@ public class HomeListEvent extends Event implements IHomeListEvent, Cancellable @Override @NotNull - public OnlineUser getOnlineUser() { + public CommandUser getListViewer() { return onlineUser; } diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/WarpListEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/WarpListEvent.java index 2369b388..348db5b1 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/WarpListEvent.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/WarpListEvent.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.position.Warp; import org.bukkit.event.Cancellable; @@ -15,12 +16,12 @@ public class WarpListEvent extends Event implements IWarpListEvent, Cancellable @NotNull private final List warps; @NotNull - private final OnlineUser onlineUser; + private final CommandUser listViewer; private boolean cancelled; - public WarpListEvent(@NotNull List warps, @NotNull OnlineUser onlineUser) { + public WarpListEvent(@NotNull List warps, @NotNull CommandUser listViewer) { this.warps = warps; - this.onlineUser = onlineUser; + this.listViewer = listViewer; } @NotNull @@ -42,8 +43,8 @@ public class WarpListEvent extends Event implements IWarpListEvent, Cancellable @Override @NotNull - public OnlineUser getOnlineUser() { - return onlineUser; + public CommandUser getListViewer() { + return listViewer; } @Override diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index b62d8c1f..7e2c2ff8 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -90,109 +90,6 @@ public class Cache { return this.getCurrentlyOnWarmup().contains(userUuid); } - @NotNull - private ListOptions.Builder getBaseList(@NotNull Locales locales, int itemsPerPage) { - return new ListOptions.Builder().setFooterFormat(locales.getRawLocale("list_footer", - "%previous_page_button%", "%current_page%", - "%total_pages%", "%next_page_button%", "%page_jumpers%").orElse("")) - .setNextButtonFormat(locales.getRawLocale("list_next_page_button", - "%next_page_index%", "%command%").orElse("")) - .setPreviousButtonFormat(locales.getRawLocale("list_previous_page_button", - "%previous_page_index%", "%command%").orElse("")) - .setPageJumpersFormat(locales.getRawLocale("list_page_jumpers", - "%page_jump_buttons%").orElse("")) - .setPageJumperPageFormat(locales.getRawLocale("list_page_jumper_button", - "%target_page_index%", "%command%").orElse("")) - .setPageJumperCurrentPageFormat(locales.getRawLocale("list_page_jumper_current_page", - "%current_page%").orElse("")) - .setPageJumperPageSeparator(locales.getRawLocale("list_page_jumper_separator").orElse("")) - .setPageJumperGroupSeparator(locales.getRawLocale("list_page_jumper_group_separator").orElse("")) - .setItemSeparator(locales.getRawLocale("list_item_divider").orElse(" ")) - .setItemsPerPage(itemsPerPage) - .setEscapeItemsMineDown(false) - .setSpaceAfterHeader(false) - .setSpaceBeforeFooter(false); - } - - @NotNull - public Optional getHomeList(@NotNull CommandUser executor, @NotNull User homeOwner, @NotNull Locales locales, - @NotNull List homes, final int itemsPerPage, final int page) { - if (executor instanceof OnlineUser user && plugin.getEventDispatcher().getViewHomeListEvent(homes, user, false).join().isCancelled()) { - return Optional.empty(); - } - final String homeListArguments = !executor.equals(homeOwner) ? " " + homeOwner.getUsername() : ""; - final PaginatedList homeList = PaginatedList.of(homes.stream().map(home -> - locales.getRawLocale("home_list_item", - Locales.escapeText(home.getMeta().getName()), - Locales.escapeText(home.getOwner().getUsername() + "." + home.getMeta().getName()), - Locales.escapeText(locales.wrapText(home.getMeta().getDescription(), 40))) - .orElse(home.getMeta().getName())).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) - .setHeaderFormat(locales.getRawLocale("home_list_page_title", - homeOwner.getUsername(), "%first_item_on_page_index%", - "%last_item_on_page_index%", "%total_items%").orElse("")) - .setCommand("/huskhomes:homelist" + homeListArguments).build()); - this.getPrivateHomeLists().put(homeOwner.getUsername(), homeList); - return Optional.of(homeList.getNearestValidPage(page)); - } - - @NotNull - public Optional getPublicHomeList(@NotNull OnlineUser onlineUser, @NotNull Locales locales, - @NotNull List publicHomes, final int itemsPerPage, final int page) { - if (plugin.getEventDispatcher().getViewHomeListEvent(publicHomes, onlineUser, true).join().isCancelled()) { - return Optional.empty(); - } - final PaginatedList publicHomeList = PaginatedList.of(publicHomes.stream().map(home -> - locales.getRawLocale("public_home_list_item", - Locales.escapeText(home.getMeta().getName()), - Locales.escapeText(home.getOwner().getUsername() + "." + home.getMeta().getName()), - Locales.escapeText(home.getOwner().getUsername()), - Locales.escapeText(locales.wrapText(home.getMeta().getDescription(), 40))) - .orElse(home.getMeta().getName())).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) - .setHeaderFormat(locales.getRawLocale("public_home_list_page_title", - "%first_item_on_page_index%", "%last_item_on_page_index%", - "%total_items%").orElse("")) - .setCommand("/huskhomes:publichomelist").build()); - getPublicHomeLists().put(onlineUser.getUuid(), publicHomeList); - return Optional.of(publicHomeList.getNearestValidPage(page)); - } - - @NotNull - public Optional getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales, - @NotNull List warps, final int itemsPerPage, final int page) { - if (plugin.getEventDispatcher().getViewWarpListEvent(warps, onlineUser).join().isCancelled()) { - return Optional.empty(); - } - final PaginatedList warpList = PaginatedList.of(warps.stream() - .map(warp -> locales.getRawLocale("warp_list_item", - Locales.escapeText(warp.getMeta().getName()), - Locales.escapeText(locales.wrapText(warp.getMeta().getDescription(), 40))) - .orElse(warp.getMeta().getName())).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) - .setHeaderFormat(locales.getRawLocale("warp_list_page_title", - "%first_item_on_page_index%", "%last_item_on_page_index%", - "%total_items%").orElse("")) - .setCommand("/huskhomes:warplist").build()); - getWarpLists().put(onlineUser.getUuid(), warpList); - return Optional.of(warpList.getNearestValidPage(page)); - } - - @NotNull - public MineDown getCommandList(@NotNull CommandUser user, @NotNull Locales locales, - @NotNull List commands, final int itemsPerPage, final int page) { - return PaginatedList.of(commands.stream() - .filter(command -> user.hasPermission(command.getPermission())) - .map(command -> locales.getRawLocale("command_list_item", - Locales.escapeText(command.getName()), - Locales.escapeText(command.getDescription().length() > 50 - ? command.getDescription().substring(0, 49).trim() + "…" - : command.getDescription()), - Locales.escapeText(locales.wrapText(command.getDescription(), 40))) - .orElse(command.getName())) - .collect(Collectors.toList()), - getBaseList(locales, Math.min(itemsPerPage, 6)) - .setHeaderFormat(locales.getRawLocale("command_list_title").orElse("")) - .setItemSeparator("\n").setCommand("/huskhomes:huskhomes help").build()) - .getNearestValidPage(page); - } /** * Cached home names - maps a {@link UUID} to a list of their homes diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index d18628f8..81614aa6 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -324,6 +324,13 @@ public interface HuskHomes extends TaskRunner, EventDispatcher { @NotNull List getCommands(); + default Optional getCommand(@NotNull Class type) { + return getCommands().stream() + .filter(command -> command.getClass().isInstance(type)) + .findFirst() + .map(type::cast); + } + /** * Reloads the {@link Settings} and {@link Locales} from their respective config files * diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index 289480ae..124c4ccc 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -2,11 +2,9 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.position.Home; -import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.Teleportable; import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; -import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -15,7 +13,7 @@ import java.util.Optional; public abstract class HomeCommand extends SavedPositionCommand { protected HomeCommand(@NotNull String name, @NotNull List aliases, @NotNull HuskHomes plugin) { - super(name, aliases, Home.class, List.of(), plugin); + super(name, aliases, Home.class, List.of("player"), plugin); } @Override @@ -36,18 +34,7 @@ public abstract class HomeCommand extends SavedPositionCommand { return; } - final Teleportable teleporter = optionalTeleporter.get(); - if (!teleporter.equals(executor) && !executor.hasPermission(Permission.COMMAND_TP_OTHER.node)) { - plugin.getLocales().getLocale("error_no_permission") - .ifPresent(executor::sendMessage); - return; - } - - Teleport.builder(plugin) - .teleporter(teleporter) - .target(home) - .toTimedTeleport() - .execute(); + this.teleport(executor, optionalTeleporter.get(), home); } } diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java deleted file mode 100644 index a39edbef..00000000 --- a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.william278.huskhomes.command; - -import net.william278.huskhomes.HuskHomes; -import net.william278.huskhomes.position.Home; -import net.william278.huskhomes.user.CommandUser; -import net.william278.huskhomes.user.OnlineUser; -import net.william278.huskhomes.user.SavedUser; -import net.william278.huskhomes.user.User; -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.Optional; - -public class HomeListCommand extends Command { - - protected HomeListCommand(@NotNull HuskHomes plugin) { - super("homelist", List.of("homes"), "[player] [page]", plugin); - } - - @Override - public void execute(@NotNull CommandUser executor, @NotNull String[] args) { - final Optional homeOwner = args.length == 2 ? parseStringArg(args, 0) - : executor instanceof OnlineUser user ? Optional.of(user.getUsername()) : Optional.empty(); - final int pageNumber = parseIntArg(args, args.length > 0 ? args.length - 1 : 0).orElse(1); - if (homeOwner.isEmpty()) { - plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) - .ifPresent(executor::sendMessage); - return; - } - - showHomeList(executor, homeOwner.get(), pageNumber); - } - - private void showHomeList(@NotNull CommandUser executor, @NotNull String homeOwner, int pageNumber) { - if (plugin.getCache().getPrivateHomeLists().containsKey(homeOwner)) { - executor.sendMessage(plugin.getCache().getPrivateHomeLists().get(homeOwner).getNearestValidPage(pageNumber)); - return; - } - - final Optional targetUser = plugin.getDatabase().getUserDataByName(homeOwner).map(SavedUser::getUser); - if (targetUser.isEmpty()) { - plugin.getLocales().getLocale("error_player_not_found", homeOwner) - .ifPresent(executor::sendMessage); - return; - } - - final User user = targetUser.get(); - if (executor instanceof OnlineUser onlineUser && !user.getUuid().equals(onlineUser.getUuid())) { - if (!executor.hasPermission(getPermission("other"))) { - plugin.getLocales().getLocale("error_no_permission").ifPresent(executor::sendMessage); - return; - } - } - - final List homes = plugin.getDatabase().getHomes(user); - if (homes.isEmpty()) { - if (!executor.equals(user)) { - plugin.getLocales().getLocale("error_no_homes_set_other", user.getUsername()) - .ifPresent(executor::sendMessage); - } else { - plugin.getLocales().getLocale("error_no_homes_set").ifPresent(executor::sendMessage); - } - return; - } - plugin.getCache().getHomeList(executor, user, - plugin.getLocales(), homes, - plugin.getSettings().getListItemsPerPage(), pageNumber) - .ifPresent(executor::sendMessage); - } - -} diff --git a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java index e51a349f..5170f912 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java @@ -3,10 +3,13 @@ package net.william278.huskhomes.command; import de.themoep.minedown.adventure.MineDown; import net.william278.desertwell.AboutMenu; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.user.CommandUser; +import net.william278.paginedown.PaginatedList; import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.stream.Collectors; public class HuskHomesCommand extends Command { @@ -51,9 +54,8 @@ public class HuskHomesCommand extends Command { switch (action.toLowerCase()) { case "about" -> executor.sendMessage(aboutMenu.toMineDown()); - case "help" -> executor.sendMessage(plugin.getCache().getCommandList(executor, plugin.getLocales(), - plugin.getCommands(), plugin.getSettings().getListItemsPerPage(), - parseIntArg(args, 1).orElse(1))); + case "help" -> executor.sendMessage(getCommandList(executor) + .getNearestValidPage(parseIntArg(args, 0).orElse(1))); case "reload" -> { if (!plugin.reload()) { executor.sendMessage(new MineDown("[Error:](#ff3300) [Failed to reload the plugin. Check console for errors.](#ff7e5e)")); @@ -74,6 +76,24 @@ public class HuskHomesCommand extends Command { } } + @NotNull + private PaginatedList getCommandList(@NotNull CommandUser user) { + return PaginatedList.of(plugin.getCommands().stream() + .filter(command -> user.hasPermission(command.getPermission())) + .map(command -> plugin.getLocales().getRawLocale("command_list_item", + Locales.escapeText(command.getName()), + Locales.escapeText(command.getDescription().length() > 50 + ? command.getDescription().substring(0, 49).trim() + "…" + : command.getDescription()), + Locales.escapeText(plugin.getLocales().wrapText(command.getDescription(), 40))) + .orElse(command.getName())) + .collect(Collectors.toList()), + plugin.getLocales().getBaseList(Math.min(plugin.getSettings().getListItemsPerPage(), 6)) + .setHeaderFormat(plugin.getLocales().getRawLocale("command_list_title").orElse("")) + .setItemSeparator("\n").setCommand("/huskhomes:huskhomes help") + .build()); + } + @Override @NotNull public List suggest(@NotNull CommandUser user, @NotNull String[] args) { diff --git a/common/src/main/java/net/william278/huskhomes/command/ListCommand.java b/common/src/main/java/net/william278/huskhomes/command/ListCommand.java new file mode 100644 index 00000000..e1a7b90e --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/ListCommand.java @@ -0,0 +1,22 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.position.SavedPosition; +import net.william278.paginedown.PaginatedList; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public abstract class ListCommand extends Command { + + protected final Map cachedLists; + + protected ListCommand(@NotNull String name, @NotNull List aliases, @NotNull String usage, @NotNull HuskHomes plugin) { + super(name, aliases, usage, plugin); + this.cachedLists = new HashMap<>(); + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/command/Node.java b/common/src/main/java/net/william278/huskhomes/command/Node.java index fb6b9d1a..8904527f 100644 --- a/common/src/main/java/net/william278/huskhomes/command/Node.java +++ b/common/src/main/java/net/william278/huskhomes/command/Node.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.teleport.Teleportable; import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.ConsoleUser; import org.jetbrains.annotations.NotNull; @@ -114,4 +115,9 @@ public abstract class Node implements Executable { return Optional.empty(); } + protected Optional resolveTeleportable(@NotNull CommandUser executor, @NotNull String[] args) { + return parseStringArg(args, 0).map(Teleportable::username) + .or(() -> executor instanceof Teleportable ? Optional.of((Teleportable) executor) : Optional.empty()); + } + } \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/PrivateHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/PrivateHomeCommand.java index 8a41cdfc..231e32b6 100644 --- a/common/src/main/java/net/william278/huskhomes/command/PrivateHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/PrivateHomeCommand.java @@ -1,6 +1,9 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.position.Home; +import net.william278.huskhomes.user.CommandUser; +import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -11,4 +14,26 @@ public class PrivateHomeCommand extends HomeCommand { super("home", List.of(), plugin); } + @Override + public void execute(@NotNull CommandUser executor, @NotNull String[] args) { + if (args.length == 0) { + if (!(executor instanceof OnlineUser user)) { + plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) + .ifPresent(executor::sendMessage); + return; + } + + // If the user has a home, teleport them there, otherwise show them their home list + final List homes = plugin.getDatabase().getHomes(user); + if (homes.size() == 1) { + super.execute(executor, homes.get(0), args); + return; + } + plugin.getCommand(PrivateHomeListCommand.class) + .ifPresent(command -> command.showHomeList(executor, user.getUsername(), 1)); + return; + } + super.execute(executor, args); + } + } diff --git a/common/src/main/java/net/william278/huskhomes/command/PrivateHomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/PrivateHomeListCommand.java new file mode 100644 index 00000000..3e6e5b8f --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/PrivateHomeListCommand.java @@ -0,0 +1,94 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; +import net.william278.huskhomes.position.Home; +import net.william278.huskhomes.user.CommandUser; +import net.william278.huskhomes.user.OnlineUser; +import net.william278.huskhomes.user.SavedUser; +import net.william278.huskhomes.user.User; +import net.william278.paginedown.PaginatedList; +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.stream.Collectors; + +public class PrivateHomeListCommand extends ListCommand { + + protected PrivateHomeListCommand(@NotNull HuskHomes plugin) { + super("homelist", List.of("homes"), "[player] [page]", plugin); + } + + @Override + public void execute(@NotNull CommandUser executor, @NotNull String[] args) { + final Optional homeOwner = args.length == 2 ? parseStringArg(args, 0) + : executor instanceof OnlineUser user ? Optional.of(user.getUsername()) : Optional.empty(); + final int pageNumber = parseIntArg(args, args.length > 0 ? args.length - 1 : 0).orElse(1); + if (homeOwner.isEmpty()) { + plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) + .ifPresent(executor::sendMessage); + return; + } + + this.showHomeList(executor, homeOwner.get(), pageNumber); + } + + protected void showHomeList(@NotNull CommandUser executor, @NotNull String homeOwner, int pageNumber) { + final Optional targetUser = plugin.getDatabase().getUserDataByName(homeOwner).map(SavedUser::getUser); + if (targetUser.isEmpty()) { + plugin.getLocales().getLocale("error_player_not_found", homeOwner) + .ifPresent(executor::sendMessage); + return; + } + + final User user = targetUser.get(); + if (executor instanceof OnlineUser onlineUser && !user.getUuid().equals(onlineUser.getUuid())) { + if (!executor.hasPermission(getPermission("other"))) { + plugin.getLocales().getLocale("error_no_permission").ifPresent(executor::sendMessage); + return; + } + } + + if (cachedLists.containsKey(user.getUuid())) { + executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(pageNumber)); + return; + } + + final List homes = plugin.getDatabase().getHomes(user); + plugin.fireEvent(plugin.getViewHomeListEvent(homes, executor, false), + (event) -> this.generateList(executor, user, homes).ifPresent(homeList -> { + cachedLists.put(user.getUuid(), homeList); + executor.sendMessage(homeList.getNearestValidPage(pageNumber)); + })); + } + + private Optional generateList(@NotNull CommandUser executor, @NotNull User user, @NotNull List homes) { + if (homes.isEmpty()) { + if (!executor.equals(user)) { + plugin.getLocales().getLocale("error_no_homes_set_other", user.getUsername()) + .ifPresent(executor::sendMessage); + } else { + plugin.getLocales().getLocale("error_no_homes_set").ifPresent(executor::sendMessage); + } + return Optional.empty(); + } + + final String homeListArguments = !executor.equals(user) ? " " + user.getUsername() : ""; + final PaginatedList homeList = PaginatedList.of(homes.stream().map(home -> + plugin.getLocales() + .getRawLocale("home_list_item", + Locales.escapeText(home.getMeta().getName()), + home.getOwner().getUsername() + "." + Locales.escapeText(home.getMeta().getName()), + Locales.escapeText(plugin.getLocales().wrapText(home.getMeta().getDescription(), 40))) + .orElse(home.getMeta().getName())).sorted().collect(Collectors.toList()), + plugin.getLocales() + .getBaseList(plugin.getSettings().getListItemsPerPage()) + .setHeaderFormat(plugin.getLocales().getRawLocale("home_list_page_title", + user.getUsername(), "%first_item_on_page_index%", + "%last_item_on_page_index%", "%total_items%") + .orElse("")) + .setCommand("/huskhomes:homelist" + homeListArguments).build()); + return Optional.of(homeList); + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java index a188c406..129ab0ac 100644 --- a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java @@ -12,6 +12,17 @@ public class PublicHomeCommand extends HomeCommand { super("phome", List.of("publichome"), plugin); } + @Override + public void execute(@NotNull CommandUser executor, @NotNull String[] args) { + // Display the public home list if no arguments are provided + if (args.length == 0) { + plugin.getCommand(PublicHomeListCommand.class) + .ifPresent(command -> command.showPublicHomeList(executor, 1)); + return; + } + super.execute(executor, args); + } + @Override @NotNull public List suggest(@NotNull CommandUser executor, @NotNull String[] args) { diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java index 4f431a8d..7ac56735 100644 --- a/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java @@ -1,84 +1,68 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.position.Home; -import net.william278.huskhomes.util.Permission; +import net.william278.paginedown.PaginatedList; import org.jetbrains.annotations.NotNull; import java.util.List; -import java.util.StringJoiner; -import java.util.concurrent.CompletableFuture; -import java.util.logging.Level; +import java.util.Optional; +import java.util.stream.Collectors; -public class PublicHomeListCommand extends Command implements ConsoleExecutable { +public class PublicHomeListCommand extends ListCommand { - protected PublicHomeListCommand(@NotNull HuskHomes implementor) { - super("publichomelist", Permission.COMMAND_HOME, implementor, "phomelist", "phomes"); + + protected PublicHomeListCommand(@NotNull HuskHomes plugin) { + super("phomelist", List.of("phomes", "publichomelist"), "[page]", plugin); } @Override - public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { - switch (args.length) { - case 0 -> showPublicHomeList(onlineUser, 1); - case 1 -> { - try { - int pageNumber = Integer.parseInt(args[0]); - showPublicHomeList(onlineUser, pageNumber); - } catch (NumberFormatException e) { - plugin.getLocales().getLocale("error_invalid_syntax", "/publichomelist [page]") - .ifPresent(onlineUser::sendMessage); - } - } - default -> plugin.getLocales().getLocale("error_invalid_syntax", "/publichomelist [page]") - .ifPresent(onlineUser::sendMessage); - } + public void execute(@NotNull CommandUser executor, @NotNull String[] args) { + final int pageNumber = parseIntArg(args, 0).orElse(1); + this.showPublicHomeList(executor, pageNumber); } - /** - * Show a (cached) list of public homes - * - * @param onlineUser the user to display the homes to - * @param pageNumber page number to display - */ - private void showPublicHomeList(@NotNull OnlineUser onlineUser, int pageNumber) { - if (plugin.getCache().getPublicHomeLists().containsKey(onlineUser.getUuid())) { - onlineUser.sendMessage(plugin.getCache().getPublicHomeLists().get(onlineUser.getUuid()).getNearestValidPage(pageNumber)); + protected void showPublicHomeList(@NotNull CommandUser executor, int pageNumber) { + if (executor instanceof OnlineUser user && cachedLists.containsKey(user.getUuid())) { + executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(pageNumber)); return; } - plugin.getDatabase().getPublicHomes().thenAcceptAsync(publicHomes -> { - if (publicHomes.isEmpty()) { - plugin.getLocales().getLocale("error_no_public_homes_set").ifPresent(onlineUser::sendMessage); - return; - } - plugin.getCache().getPublicHomeList(onlineUser, - plugin.getLocales(), publicHomes, - plugin.getSettings().getListItemsPerPage(), pageNumber) - .ifPresent(onlineUser::sendMessage); - }); - + final List homes = plugin.getDatabase().getPublicHomes(); + plugin.fireEvent(plugin.getViewHomeListEvent(homes, executor, true), + (event) -> this.generateList(executor, homes).ifPresent(homeList -> { + if (executor instanceof OnlineUser onlineUser) { + cachedLists.put(onlineUser.getUuid(), homeList); + } + executor.sendMessage(homeList.getNearestValidPage(pageNumber)); + })); } - @Override - public void onConsoleExecute(@NotNull String[] args) { - CompletableFuture.runAsync(() -> { - final List homes = plugin.getDatabase().getPublicHomes().join(); - StringJoiner rowJoiner = new StringJoiner("\t"); + private Optional generateList(@NotNull CommandUser executor, @NotNull List publicHomes) { + if (publicHomes.isEmpty()) { + plugin.getLocales().getLocale("error_no_public_homes_set") + .ifPresent(executor::sendMessage); + return Optional.empty(); + } - plugin.log(Level.INFO, "List of " + homes.size() + " public homes:"); - for (int i = 0; i < homes.size(); i++) { - final String ownerUsername = homes.get(i).getOwner().getUsername(); - final String homeName = homes.get(i).getMeta().getName(); - final String home = ownerUsername + "." + homeName; - int spacingSize = (16 - ownerUsername.length()) + 17; - rowJoiner.add(home.length() < spacingSize ? home + " ".repeat(spacingSize - home.length()) : home); - if ((i + 1) % 3 == 0) { - plugin.log(Level.INFO, rowJoiner.toString()); - rowJoiner = new StringJoiner("\t"); - } - } - plugin.log(Level.INFO, rowJoiner.toString()); - }); + final PaginatedList homeList = PaginatedList.of(publicHomes.stream().map(home -> + plugin.getLocales() + .getRawLocale("public_home_list_item", + Locales.escapeText(home.getMeta().getName()), + home.getOwner().getUsername() + "." + Locales.escapeText(home.getMeta().getName()), + Locales.escapeText(home.getOwner().getUsername()), + Locales.escapeText(plugin.getLocales().wrapText(home.getMeta().getDescription(), 40))) + .orElse(home.getName())).sorted().collect(Collectors.toList()), + plugin.getLocales() + .getBaseList(plugin.getSettings().getListItemsPerPage()) + .setHeaderFormat(plugin.getLocales().getRawLocale("public_home_list_page_title", + "%first_item_on_page_index%", "%last_item_on_page_index%", "%total_items%") + .orElse("")) + .setCommand("/huskhomes:phomelist").build()); + return Optional.of(homeList); } + } diff --git a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java index bd2acd0c..ff0cad4a 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java @@ -4,6 +4,8 @@ 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.teleport.Teleport; +import net.william278.huskhomes.teleport.Teleportable; import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; @@ -64,7 +66,7 @@ public abstract class SavedPositionCommand extends Comm final Home home = optionalHome.get(); if (executor instanceof OnlineUser user && !home.isPublic() && !user.equals(home.getOwner()) - && !user.hasPermission(getOtherPermission())) { + && !user.hasPermission(getOtherPermission())) { plugin.getLocales().getLocale("error_public_home_invalid", splitHomeName[0], splitHomeName[1]) .ifPresent(executor::sendMessage); @@ -89,8 +91,8 @@ public abstract class SavedPositionCommand extends Comm private Optional resolveWarp(@NotNull CommandUser executor, @NotNull String warpName) { final Optional warp = plugin.getDatabase().getWarp(warpName); - if (warp.isPresent() && executor instanceof OnlineUser user - && !warp.get().hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user)) { + if (warp.isPresent() && executor instanceof OnlineUser user && (plugin.getSettings().isPermissionRestrictWarps() + && user.hasPermission(Warp.getWildcardPermission()) || user.hasPermission(Warp.getPermission(warpName)))) { plugin.getLocales().getLocale("error_warp_invalid", warpName) .ifPresent(executor::sendMessage); return Optional.empty(); @@ -98,6 +100,20 @@ public abstract class SavedPositionCommand extends Comm return warp; } + protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable teleporter, @NotNull T position) { + if (!teleporter.equals(executor) && !executor.hasPermission(getPermission("other"))) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(executor::sendMessage); + return; + } + + Teleport.builder(plugin) + .teleporter(teleporter) + .target(position) + .toTimedTeleport() + .execute(); + } + @Override @NotNull public List suggest(@NotNull CommandUser executor, @NotNull String[] args) { diff --git a/common/src/main/java/net/william278/huskhomes/command/TabProvider.java b/common/src/main/java/net/william278/huskhomes/command/TabProvider.java index 21a7f911..81547afc 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TabProvider.java +++ b/common/src/main/java/net/william278/huskhomes/command/TabProvider.java @@ -40,5 +40,4 @@ public interface TabProvider { .toList(); } - } \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index 0e99833a..57df6709 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -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 { - 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 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 "); - 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 optionalTeleporter = resolveTeleportable(executor, args); + if (optionalTeleporter.isEmpty()) { + plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) + .ifPresent(executor::sendMessage); + return; + } + + this.teleport(executor, optionalTeleporter.get(), warp); } - } diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java index c7708ef1..34b713c6 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java @@ -1,81 +1,75 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; +import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; -import net.william278.huskhomes.util.Permission; +import net.william278.paginedown.PaginatedList; import org.jetbrains.annotations.NotNull; -import java.util.StringJoiner; -import java.util.logging.Level; +import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; -public class WarpListCommand extends Command implements ConsoleExecutable { +public class WarpListCommand extends ListCommand { - protected WarpListCommand(@NotNull HuskHomes implementor) { - super("warplist", Permission.COMMAND_WARP, implementor, "warps"); + protected WarpListCommand(@NotNull HuskHomes plugin) { + super("homelist", List.of("homes"), "[player] [page]", plugin); } @Override - public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { - switch (args.length) { - case 0 -> showWarpList(onlineUser, 1); - case 1 -> { - try { - int pageNumber = Integer.parseInt(args[0]); - showWarpList(onlineUser, pageNumber); - } catch (NumberFormatException e) { - plugin.getLocales().getLocale("error_invalid_syntax", "/warplist [page]") - .ifPresent(onlineUser::sendMessage); - } - } - default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warplist [page]") - .ifPresent(onlineUser::sendMessage); - } + public void execute(@NotNull CommandUser executor, @NotNull String[] args) { + final int pageNumber = parseIntArg(args, 0).orElse(1); + this.showWarpList(executor, pageNumber); } - /** - * Show a (cached) list of server warps - * - * @param onlineUser the user to display warps to - * @param pageNumber page number to display - */ - private void showWarpList(@NotNull OnlineUser onlineUser, int pageNumber) { - if (plugin.getCache().getWarpLists().containsKey(onlineUser.getUuid())) { - onlineUser.sendMessage(plugin.getCache().getWarpLists().get(onlineUser.getUuid()).getNearestValidPage(pageNumber)); + protected void showWarpList(@NotNull CommandUser executor, int pageNumber) { + if (executor instanceof OnlineUser user && cachedLists.containsKey(user.getUuid())) { + executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(pageNumber)); return; } - // Dispatch the warp list event - 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; + final List warps = getItems(executor); + plugin.fireEvent(plugin.getViewWarpListEvent(warps, executor), + (event) -> this.generateList(executor, warps).ifPresent(homeList -> { + if (executor instanceof OnlineUser onlineUser) { + cachedLists.put(onlineUser.getUuid(), homeList); } - plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps, - plugin.getSettings().getListItemsPerPage(), pageNumber) - .ifPresent(onlineUser::sendMessage); - }); + executor.sendMessage(homeList.getNearestValidPage(pageNumber)); + })); } - @Override - public void onConsoleExecute(@NotNull String[] args) { - plugin.getDatabase().getWarps().thenAccept(warps -> { - plugin.log(Level.INFO, "List of " + warps.size() + " warps:"); + private Optional generateList(@NotNull CommandUser executor, @NotNull List warps) { + if (warps.isEmpty()) { + plugin.getLocales().getLocale("error_no_warps_set") + .ifPresent(executor::sendMessage); + return Optional.empty(); + } - StringJoiner rowJoiner = new StringJoiner("\t"); - for (int i = 0; i < warps.size(); i++) { - final String warp = warps.get(i).meta.name; - rowJoiner.add(warp.length() < 16 ? warp + " ".repeat(16 - warp.length()) : warp); - if ((i + 1) % 3 == 0) { - plugin.log(Level.INFO, rowJoiner.toString()); - rowJoiner = new StringJoiner("\t"); - } - } - plugin.log(Level.INFO, rowJoiner.toString()); - }); + final PaginatedList warpList = PaginatedList.of(warps.stream().map(warp -> + plugin.getLocales() + .getRawLocale("warp_list_item", + Locales.escapeText(warp.getName()), + Locales.escapeText(plugin.getLocales().wrapText(warp.getMeta().getDescription(), 40))) + .orElse(warp.getName())).sorted().collect(Collectors.toList()), + plugin.getLocales() + .getBaseList(plugin.getSettings().getListItemsPerPage()) + .setHeaderFormat(plugin.getLocales().getRawLocale("warp_list_page_title", + "%first_item_on_page_index%", "%last_item_on_page_index%", "%total_items%") + .orElse("")) + .setCommand("/huskhomes:warplist").build()); + return Optional.of(warpList); + } + + @NotNull + private List getItems(@NotNull CommandUser executor) { + List warps = plugin.getDatabase().getWarps(); + if (plugin.getSettings().isPermissionRestrictWarps() && !executor.hasPermission(Warp.getWildcardPermission())) { + warps = warps.stream() + .filter(warp -> executor.hasPermission(getPermission(warp.getPermission()))) + .collect(Collectors.toList()); + } + return warps; } } diff --git a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java index cc615fb4..d90a6e68 100644 --- a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java +++ b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.event; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.User; import net.william278.huskhomes.position.Home; @@ -54,9 +55,9 @@ public interface EventDispatcher { IWarpDeleteEvent getWarpDeleteEvent(@NotNull Warp warp); - IHomeListEvent getViewHomeListEvent(@NotNull List homes, @NotNull OnlineUser user, boolean publicHomeList); + IHomeListEvent getViewHomeListEvent(@NotNull List homes, @NotNull CommandUser listViewer, boolean publicHomeList); - IWarpListEvent getViewWarpListEvent(@NotNull List homes, @NotNull OnlineUser user); + IWarpListEvent getViewWarpListEvent(@NotNull List homes, @NotNull CommandUser listViewer); IDeleteAllHomesEvent getDeleteAllHomesEvent(@NotNull User user); diff --git a/common/src/main/java/net/william278/huskhomes/event/IHomeListEvent.java b/common/src/main/java/net/william278/huskhomes/event/IHomeListEvent.java index 48b4ce7f..a5c49af9 100644 --- a/common/src/main/java/net/william278/huskhomes/event/IHomeListEvent.java +++ b/common/src/main/java/net/william278/huskhomes/event/IHomeListEvent.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.position.Home; import org.jetbrains.annotations.NotNull; @@ -24,7 +25,7 @@ public interface IHomeListEvent extends Cancellable { * @return the player viewing the home list */ @NotNull - OnlineUser getOnlineUser(); + CommandUser getListViewer(); /** * Indicates if the player has requested to view a list of public homes diff --git a/common/src/main/java/net/william278/huskhomes/event/IWarpListEvent.java b/common/src/main/java/net/william278/huskhomes/event/IWarpListEvent.java index 7bb84ae5..6ef10c14 100644 --- a/common/src/main/java/net/william278/huskhomes/event/IWarpListEvent.java +++ b/common/src/main/java/net/william278/huskhomes/event/IWarpListEvent.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.position.Warp; import org.jetbrains.annotations.NotNull; @@ -19,11 +20,11 @@ public interface IWarpListEvent extends Cancellable { List getWarps(); /** - * Get the player viewing the warp list + * Get the user viewing the warp list * - * @return the player viewing the warp list + * @return the user viewing the warp list */ @NotNull - OnlineUser getOnlineUser(); + CommandUser getListViewer(); } diff --git a/common/src/main/java/net/william278/huskhomes/position/Warp.java b/common/src/main/java/net/william278/huskhomes/position/Warp.java index abdab544..7aeb2225 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Warp.java +++ b/common/src/main/java/net/william278/huskhomes/position/Warp.java @@ -1,8 +1,5 @@ package net.william278.huskhomes.position; -import net.william278.huskhomes.user.OnlineUser; -import net.william278.huskhomes.util.Permission; -import org.intellij.lang.annotations.Subst; import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -12,6 +9,8 @@ import java.util.UUID; */ public class Warp extends SavedPosition { + private static final String PERMISSION_PREFIX = "huskhomes.command.warp."; + public Warp(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server, @NotNull PositionMeta positionMeta, @NotNull UUID uuid) { @@ -28,40 +27,19 @@ public class Warp extends SavedPosition { super(position, meta); } - /** - * Get the permission node for a warp - * - * @param warp The warp name - * @return The permission node - */ @NotNull - @Subst("huskhomes.command.warp") - public static String getPermissionNode(@NotNull String warp) { - return Permission.COMMAND_WARP.node + "." + warp.toLowerCase(); + public static String getPermission(@NotNull String warpName) { + return PERMISSION_PREFIX + warpName.toLowerCase(); } - /** - * Check if a {@link OnlineUser} has permission to teleport to this warp - * - * @param restrictWarps Whether to restrict warps to permission nodes - * @param user The {@link OnlineUser} to check - * @return true if the user has permission to teleport to this warp - */ - public boolean hasPermission(boolean restrictWarps, @NotNull OnlineUser user) { - return hasPermission(restrictWarps, user, getMeta().getName()); + @NotNull + public String getPermission() { + return getPermission(getName()); } - /** - * Check if a {@link OnlineUser} has permission to teleport to this warp - * - * @param restrictWarps Whether to restrict warps to permission nodes - * @param user The {@link OnlineUser} to check - * @param warp The warp name to check - * @return true if the user has permission to teleport to this warp - */ - public static boolean hasPermission(boolean restrictWarps, @NotNull OnlineUser user, @NotNull String warp) { - return !restrictWarps || (user.hasPermission(Permission.COMMAND_SET_WARP.node) - || user.hasPermission(getPermissionNode(warp))); + @NotNull + public static String getWildcardPermission() { + return PERMISSION_PREFIX + "*"; } }