mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +00:00
List commands, more refactoring work
This commit is contained in:
1 parent
9903503f03
commit
f746d67339
23 files changed
+361
-450
No files matched your search
@@ -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<Home> {
|
||||
|
||||
protected HomeCommand(@NotNull String name, @NotNull List<String> 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<Home> {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> 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<User> 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<Home> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
|
||||
@@ -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<T extends SavedPosition> extends Command {
|
||||
|
||||
protected final Map<UUID, PaginatedList> cachedLists;
|
||||
|
||||
protected ListCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull String usage, @NotNull HuskHomes plugin) {
|
||||
super(name, aliases, usage, plugin);
|
||||
this.cachedLists = new HashMap<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Teleportable> resolveTeleportable(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
return parseStringArg(args, 0).map(Teleportable::username)
|
||||
.or(() -> executor instanceof Teleportable ? Optional.of((Teleportable) executor) : Optional.empty());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Home> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Home> {
|
||||
|
||||
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<String> 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<User> 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<Home> 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<PaginatedList> generateList(@NotNull CommandUser executor, @NotNull User user, @NotNull List<Home> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
|
||||
@@ -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<Home> {
|
||||
|
||||
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<Home> 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<Home> homes = plugin.getDatabase().getPublicHomes().join();
|
||||
StringJoiner rowJoiner = new StringJoiner("\t");
|
||||
private Optional<PaginatedList> generateList(@NotNull CommandUser executor, @NotNull List<Home> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<T extends SavedPosition> 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<T extends SavedPosition> extends Comm
|
||||
|
||||
private Optional<Warp> resolveWarp(@NotNull CommandUser executor, @NotNull String warpName) {
|
||||
final Optional<Warp> warp = plugin.getDatabase().getWarp(warpName);
|
||||
if (warp.isPresent() && executor instanceof OnlineUser user
|
||||
&& !warp.get().hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user)) {
|
||||
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<T extends SavedPosition> 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<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
|
||||
@@ -40,5 +40,4 @@ public interface TabProvider {
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,101 +1,37 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.teleport.Teleportable;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Optional;
|
||||
|
||||
public class WarpCommand extends Command implements TabProvider, ConsoleExecutable {
|
||||
public class WarpCommand extends SavedPositionCommand<Warp> {
|
||||
|
||||
protected WarpCommand(@NotNull HuskHomes implementor) {
|
||||
super("warp", Permission.COMMAND_WARP, implementor);
|
||||
protected WarpCommand(@NotNull HuskHomes plugin) {
|
||||
super("warp", List.of(), Warp.class, List.of(), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getWarps()
|
||||
.thenApply(warps -> warps.stream()
|
||||
.filter(warp -> warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), onlineUser))
|
||||
.collect(Collectors.toList()))
|
||||
.thenAccept(warps -> {
|
||||
if (warps.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_no_warps_set")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
|
||||
plugin.getSettings().getListItemsPerPage(), 1)
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
});
|
||||
case 1 -> {
|
||||
final String warpName = args[0];
|
||||
plugin.getDatabase()
|
||||
.getWarp(warpName)
|
||||
.thenAccept(warpResult -> warpResult.ifPresentOrElse(warp -> {
|
||||
// Handle permission restrictions
|
||||
if (!warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), onlineUser)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
Teleport.builder(plugin, onlineUser)
|
||||
.setTarget(warp)
|
||||
.toTimedTeleport()
|
||||
.thenAccept(TimedTeleport::execute);
|
||||
},
|
||||
() -> plugin.getLocales().getLocale("error_warp_invalid", warpName)
|
||||
.ifPresent(onlineUser::sendMessage)));
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warp [name]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
return plugin.getCache().getWarps().stream()
|
||||
.filter(s -> user == null || Warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user, s))
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
if (args.length != 2) {
|
||||
plugin.log(Level.WARNING, "Invalid syntax. Usage: warp <player> <warp>");
|
||||
return;
|
||||
}
|
||||
final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null);
|
||||
if (playerToTeleport == null) {
|
||||
plugin.log(Level.WARNING, "Player not found: " + args[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getDatabase().getWarp(args[1]).thenAccept(optionalWarp -> {
|
||||
if (optionalWarp.isEmpty()) {
|
||||
plugin.log(Level.WARNING, "Could not find warp '" + args[1] + "'");
|
||||
public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) {
|
||||
if (plugin.getSettings().isPermissionRestrictWarps()) {
|
||||
if (!executor.hasPermission(warp.getPermission()) && !executor.hasPermission(Warp.getWildcardPermission())) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
final Warp warp = optionalWarp.get();
|
||||
}
|
||||
|
||||
plugin.log(Level.INFO, "Teleporting " + playerToTeleport.getUsername() + " to " + warp.getMeta().getName());
|
||||
Teleport.builder(plugin, playerToTeleport)
|
||||
.setTarget(warp)
|
||||
.toTimedTeleport()
|
||||
.thenAccept(TimedTeleport::execute);
|
||||
});
|
||||
final Optional<Teleportable> optionalTeleporter = resolveTeleportable(executor, args);
|
||||
if (optionalTeleporter.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
this.teleport(executor, optionalTeleporter.get(), warp);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Warp> {
|
||||
|
||||
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<Warp> 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<PaginatedList> generateList(@NotNull CommandUser executor, @NotNull List<Warp> 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<Warp> getItems(@NotNull CommandUser executor) {
|
||||
List<Warp> 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user