Improve other user private home list parsing

This commit is contained in:
William committed 2023-03-25 18:07:56 +00:00
1 parent d4b855e659
commit 322694bff9
2 files changed
+17 -11

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ commands:
sethome: sethome:
usage: /sethome <name> usage: /sethome <name>
homelist: homelist:
usage: /homelist [page] usage: /homelist [player] [page]
aliases: [homes] aliases: [homes]
delhome: delhome:
usage: /delhome <name> usage: /delhome <name>
@@ -3,10 +3,7 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.*;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.User;
import net.william278.paginedown.PaginatedList; import net.william278.paginedown.PaginatedList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -38,13 +35,22 @@ public class PrivateHomeListCommand extends ListCommand {
protected void showHomeList(@NotNull CommandUser executor, @NotNull String homeOwner, int pageNumber) { protected void showHomeList(@NotNull CommandUser executor, @NotNull String homeOwner, int pageNumber) {
final Optional<User> targetUser = plugin.getDatabase().getUserDataByName(homeOwner).map(SavedUser::getUser); final Optional<User> targetUser = plugin.getDatabase().getUserDataByName(homeOwner).map(SavedUser::getUser);
final User user;
final int page;
if (targetUser.isEmpty()) { if (targetUser.isEmpty()) {
plugin.getLocales().getLocale("error_player_not_found", homeOwner) final Optional<Integer> pageNumberArg = parseIntArg(new String[]{homeOwner}, 0);
.ifPresent(executor::sendMessage); if (pageNumberArg.isEmpty() || !(executor instanceof OnlineUser onlineUser)) {
return; plugin.getLocales().getLocale("error_player_not_found", homeOwner)
.ifPresent(executor::sendMessage);
return;
}
page = pageNumberArg.get();
user = onlineUser;
} else {
user = targetUser.get();
page = pageNumber;
} }
final User user = targetUser.get();
if (executor instanceof OnlineUser onlineUser && !user.getUuid().equals(onlineUser.getUuid()) if (executor instanceof OnlineUser onlineUser && !user.getUuid().equals(onlineUser.getUuid())
&& !executor.hasPermission(getPermission("other"))) { && !executor.hasPermission(getPermission("other"))) {
plugin.getLocales().getLocale("error_no_permission") plugin.getLocales().getLocale("error_no_permission")
@@ -53,7 +59,7 @@ public class PrivateHomeListCommand extends ListCommand {
} }
if (cachedLists.containsKey(user.getUuid())) { if (cachedLists.containsKey(user.getUuid())) {
executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(pageNumber)); executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(page));
return; return;
} }
@@ -61,7 +67,7 @@ public class PrivateHomeListCommand extends ListCommand {
plugin.fireEvent(plugin.getViewHomeListEvent(homes, executor, false), plugin.fireEvent(plugin.getViewHomeListEvent(homes, executor, false),
(event) -> this.generateList(executor, user, event.getHomes()).ifPresent(homeList -> { (event) -> this.generateList(executor, user, event.getHomes()).ifPresent(homeList -> {
cachedLists.put(user.getUuid(), homeList); cachedLists.put(user.getUuid(), homeList);
executor.sendMessage(homeList.getNearestValidPage(pageNumber)); executor.sendMessage(homeList.getNearestValidPage(page));
})); }));
} }