mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +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
@@ -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<MineDown> getHomeList(@NotNull CommandUser executor, @NotNull User homeOwner, @NotNull Locales locales,
|
||||
@NotNull List<Home> 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<MineDown> getPublicHomeList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
|
||||
@NotNull List<Home> 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<MineDown> getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
|
||||
@NotNull List<Warp> 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<Command> 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
|
||||
|
||||
@@ -324,6 +324,13 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
@NotNull
|
||||
List<Command> getCommands();
|
||||
|
||||
default <T extends Command> Optional<T> getCommand(@NotNull Class<T> 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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Home> homes, @NotNull OnlineUser user, boolean publicHomeList);
|
||||
IHomeListEvent getViewHomeListEvent(@NotNull List<Home> homes, @NotNull CommandUser listViewer, boolean publicHomeList);
|
||||
|
||||
IWarpListEvent getViewWarpListEvent(@NotNull List<Warp> homes, @NotNull OnlineUser user);
|
||||
IWarpListEvent getViewWarpListEvent(@NotNull List<Warp> homes, @NotNull CommandUser listViewer);
|
||||
|
||||
IDeleteAllHomesEvent getDeleteAllHomesEvent(@NotNull User user);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Warp> 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();
|
||||
|
||||
}
|
||||
@@ -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 + "*";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user