Fix list and TAB completion issues; close #272, #273

This commit is contained in:
William committed 2022-11-27 20:17:23 +00:00
1 parent 1dc45b8286
commit b7cf0fa590
18 files changed
+111 -67

No files matched your search

@@ -163,6 +163,7 @@ public class Cache {
if (eventDispatcher.dispatchViewHomeListEvent(homes, onlineUser, false).join().isCancelled()) {
return Optional.empty();
}
final String homeListArguments = !onlineUser.equals(homeOwner) ? " " + homeOwner.username : "";
final PaginatedList homeList = PaginatedList.of(homes.stream().map(home ->
locales.getRawLocale("home_list_item",
Locales.escapeMineDown(home.meta.name),
@@ -172,7 +173,7 @@ public class Cache {
.setHeaderFormat(locales.getRawLocale("home_list_page_title",
homeOwner.username, "%first_item_on_page_index%",
"%last_item_on_page_index%", "%total_items%").orElse(""))
.setCommand("/huskhomes:homelist").build());
.setCommand("/huskhomes:homelist" + homeListArguments).build());
this.privateHomeLists.put(homeOwner.username, homeList);
return Optional.of(homeList.getNearestValidPage(page));
}
@@ -200,18 +201,15 @@ public class Cache {
@NotNull
public Optional<MineDown> getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
@NotNull List<Warp> warps, final boolean permissionRestrictWarps,
final int itemsPerPage, final int page) {
@NotNull List<Warp> warps, final int itemsPerPage, final int page) {
if (eventDispatcher.dispatchViewWarpListEvent(warps, onlineUser).join().isCancelled()) {
return Optional.empty();
}
final PaginatedList warpList = PaginatedList.of(warps.stream()
.filter(warp -> !permissionRestrictWarps || onlineUser.hasPermission(warp.getPermissionNode()))
.map(warp ->
locales.getRawLocale("warp_list_item",
Locales.escapeMineDown(warp.meta.name),
Locales.escapeMineDown(locales.formatDescription(warp.meta.description)))
.orElse(warp.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage)
.map(warp -> locales.getRawLocale("warp_list_item",
Locales.escapeMineDown(warp.meta.name),
Locales.escapeMineDown(locales.formatDescription(warp.meta.description)))
.orElse(warp.meta.name)).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(""))
@@ -3,6 +3,8 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.intellij.lang.annotations.Pattern;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
/**
@@ -26,6 +28,7 @@ public abstract class CommandBase {
* The permission node required to use this command
*/
@NotNull
@Pattern(Permission.PERMISSION_PATTERN)
public final String permission;
/**
@@ -34,8 +37,8 @@ public abstract class CommandBase {
@NotNull
protected final HuskHomes plugin;
protected CommandBase(@NotNull String command, @NotNull Permission permission, @NotNull HuskHomes implementor,
@NotNull String... aliases) {
protected CommandBase(@NotNull String command, @NotNull @Subst("huskhomes") Permission permission,
@NotNull HuskHomes implementor, @NotNull String... aliases) {
this.command = command;
this.permission = permission.node;
this.plugin = implementor;
@@ -69,7 +69,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
private void deletePlayerHome(@NotNull OnlineUser deleter, @NotNull User homeOwner, @NotNull String homeName,
final boolean delHomeAllConfirm) {
plugin.getSavedPositionManager().deleteHome(homeOwner, homeName).thenAccept(deleted -> {
if (deleter.uuid.equals(homeOwner.uuid)) {
if (deleter.equals(homeOwner)) {
if (deleted) {
plugin.getLocales().getLocale("home_deleted", homeName).ifPresent(deleter::sendMessage);
return;
@@ -85,7 +85,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
private void editHome(@NotNull Home home, @NotNull OnlineUser editor,
@Nullable String editOperation, @Nullable String editArgs) {
final AtomicBoolean showMenuFlag = new AtomicBoolean(false);
final boolean otherOwner = !editor.uuid.equals(home.owner.uuid);
final boolean otherOwner = !editor.equals(home.owner);
if (editOperation == null) {
getHomeEditorWindow(home, true, otherOwner,
@@ -119,7 +119,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(newHomeName, home.meta.description))
.thenAccept(renameResult -> (switch (renameResult.resultType()) {
case SUCCESS -> {
if (home.owner.uuid.equals(editor.uuid)) {
if (home.owner.equals(editor)) {
yield plugin.getLocales().getLocale("edit_home_update_name",
oldHomeName, newHomeName);
} else {
@@ -140,7 +140,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(home.meta.name, newDescription))
.thenAccept(descriptionUpdateResult -> (switch (descriptionUpdateResult.resultType()) {
case SUCCESS -> {
if (home.owner.uuid.equals(editor.uuid)) {
if (home.owner.equals(editor)) {
yield plugin.getLocales().getLocale("edit_home_update_description",
home.meta.name,
oldHomeDescription.isBlank() ? plugin.getLocales()
@@ -166,7 +166,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
}
case "relocate" ->
plugin.getSavedPositionManager().updateHomePosition(home, editor.getPosition()).thenRun(() -> {
if (home.owner.uuid.equals(editor.uuid)) {
if (home.owner.equals(editor)) {
editor.sendMessage(plugin.getLocales().getLocale("edit_home_update_location",
home.meta.name).orElse(new MineDown("")));
} else {
@@ -233,7 +233,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
// Execute the update
plugin.getSavedPositionManager().updateHomePrivacy(home, newIsPublic.get()).thenRun(() -> {
if (home.owner.uuid.equals(editor.uuid)) {
if (home.owner.equals(editor)) {
editor.sendMessage(plugin.getLocales().getLocale(
"edit_home_privacy_" + privacyKeyedString + "_success",
home.meta.name).orElse(new MineDown("")));
@@ -57,7 +57,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
}
private void teleportToNamedHome(@NotNull OnlineUser teleporter, @NotNull User owner, @NotNull String homeName) {
final boolean otherHome = !owner.uuid.equals(teleporter.uuid);
final boolean otherHome = !owner.equals(teleporter);
plugin.getDatabase()
.getHome(owner, homeName)
.thenAccept(homeResult -> homeResult.ifPresentOrElse(home -> {
@@ -66,7 +66,7 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
}
plugin.getDatabase().getHomes(userData.user()).thenAcceptAsync(homes -> {
if (homes.isEmpty()) {
if (!onlineUser.uuid.equals(userData.user().uuid)) {
if (!onlineUser.equals(userData.user())) {
plugin.getLocales().getLocale("error_no_homes_set_other",
userData.user().username).ifPresent(onlineUser::sendMessage);
} else {
@@ -69,7 +69,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
}
private void teleportToNamedHome(@NotNull OnlineUser teleporter, @NotNull User owner, @NotNull String homeName) {
final boolean otherHome = !owner.uuid.equals(teleporter.uuid);
final boolean otherHome = !owner.equals(teleporter);
plugin.getDatabase()
.getHome(owner, homeName)
.thenAccept(homeResult -> homeResult.ifPresentOrElse(home -> {
@@ -114,7 +114,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
.orElse(null)),
() -> matchedHome.set(plugin.getDatabase().getPublicHomes().join()
.stream()
.filter(home -> home.owner.uuid.equals(playerToTeleport.uuid) && home.meta.name.equalsIgnoreCase(args[1]))
.filter(home -> home.owner.equals(playerToTeleport) && home.meta.name.equalsIgnoreCase(args[1]))
.findFirst()
.orElse(null)));
@@ -54,7 +54,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
}
final OnlineUser userToTeleport = target;
final boolean isExecutorTeleporting = userToTeleport.uuid.equals(onlineUser.uuid);
final boolean isExecutorTeleporting = userToTeleport.equals(onlineUser);
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> {
// Check the user is not still on /rtp cooldown
if (userData.isEmpty()) {
@@ -6,7 +6,6 @@ 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 org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -23,30 +22,30 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getWarps().thenAcceptAsync(warps -> {
if (warps.isEmpty()) {
plugin.getLocales().getLocale("error_no_warps_set").ifPresent(onlineUser::sendMessage);
return;
}
plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
plugin.getSettings().permissionRestrictWarps,
plugin.getSettings().listItemsPerPage, 1)
.ifPresent(onlineUser::sendMessage);
});
case 0 -> plugin.getDatabase().getWarps()
.thenApply(warps -> warps.stream()
.filter(warp -> warp.hasPermission(plugin.getSettings().permissionRestrictWarps, 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().listItemsPerPage, 1)
.ifPresent(onlineUser::sendMessage);
});
case 1 -> {
final String warpName = args[0];
plugin.getDatabase()
.getWarp(warpName)
.thenAccept(warpResult -> warpResult.ifPresentOrElse(warp -> {
// Handle permission restrictions
if (plugin.getSettings().permissionRestrictWarps) {
@Subst("huskhomes.command.warp") final String warpPermission = Permission.COMMAND_WARP.node + "." + warp.meta.name;
if (!onlineUser.hasPermission(warpPermission) && !onlineUser.hasPermission(Permission.COMMAND_SET_WARP.node)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
if (!warp.hasPermission(plugin.getSettings().permissionRestrictWarps, onlineUser)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
Teleport.builder(plugin, onlineUser)
@@ -65,8 +64,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
return plugin.getCache().warps.stream()
.filter(s -> user == null || plugin.getSettings().permissionRestrictWarps && !user.hasPermission(
Permission.COMMAND_SET_WARP.node) && user.hasPermission(Warp.getPermissionNode(s)))
.filter(s -> user == null || Warp.hasPermission(plugin.getSettings().permissionRestrictWarps, user, s))
.filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : ""))
.sorted()
.collect(Collectors.toList());
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class WarpListCommand extends CommandBase implements ConsoleExecutable {
@@ -45,17 +46,19 @@ public class WarpListCommand extends CommandBase implements ConsoleExecutable {
}
// Dispatch the warp list event
plugin.getDatabase().getWarps().thenAcceptAsync(warps -> {
if (warps.isEmpty()) {
plugin.getLocales().getLocale("error_no_warps_set").ifPresent(onlineUser::sendMessage);
return;
}
plugin.getCache().getWarpList(onlineUser,
plugin.getLocales(), warps,
plugin.getSettings().permissionRestrictWarps,
plugin.getSettings().listItemsPerPage,
pageNumber).ifPresent(onlineUser::sendMessage);
});
plugin.getDatabase().getWarps()
.thenApply(warps -> warps.stream()
.filter(warp -> warp.hasPermission(plugin.getSettings().permissionRestrictWarps, 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().listItemsPerPage, pageNumber)
.ifPresent(onlineUser::sendMessage);
});
}
@Override
@@ -276,7 +276,7 @@ public abstract class Database {
.collect(Collectors.toList()));
} catch (HuskHomesException e) {
return getWarps().thenApplyAsync(warps -> warps.stream()
.filter(warp -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(warp.world.uuid)))
.filter(warp -> plugin.getWorlds().stream().anyMatch(world -> world.equals(warp.world)))
.collect(Collectors.toList()));
}
}
@@ -303,7 +303,7 @@ public abstract class Database {
.collect(Collectors.toList()));
} catch (HuskHomesException e) {
return plugin.getDatabase().getPublicHomes().thenApplyAsync(homes -> homes.stream()
.filter(home -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(home.world.uuid)))
.filter(home -> plugin.getWorlds().stream().anyMatch(world -> world.equals(home.world)))
.collect(Collectors.toList()));
}
}
@@ -134,7 +134,7 @@ public class EventListener {
// Update the cached player list using another online player if possible
plugin.getOnlinePlayers()
.stream()
.filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid))
.filter(onlinePlayer -> !onlinePlayer.equals(onlineUser))
.findAny()
.ifPresent(updater -> plugin.getCache().updatePlayerListCache(plugin, updater));
@@ -102,7 +102,7 @@ public class Message {
}
@Override
public boolean equals(Object other) {
public boolean equals(@NotNull Object other) {
if (other instanceof Message message) {
return message.uuid.equals(this.uuid);
}
@@ -26,4 +26,11 @@ public class User {
this.username = username;
}
@Override
public boolean equals(@NotNull Object obj) {
if (obj instanceof User user) {
return user.uuid.equals(uuid);
}
return super.equals(obj);
}
}
@@ -1,5 +1,6 @@
package net.william278.huskhomes.position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
@@ -16,13 +17,14 @@ public class Server {
this.name = (name != null ? name : "server");
}
@SuppressWarnings("unused")
public Server() {
}
@Override
public boolean equals(Object other) {
public boolean equals(@NotNull Object other) {
// If the name of this server matches another, the servers are the same.
if (other instanceof Server server) {
// If the name of this server matches another, the servers are the same.
return server.name.equalsIgnoreCase(this.name);
}
return super.equals(other);
@@ -1,5 +1,6 @@
package net.william278.huskhomes.position;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
@@ -27,16 +28,40 @@ public class Warp extends SavedPosition {
super(position, meta);
}
@NotNull
@Subst("huskhomes.command.warp")
public String getPermissionNode() {
return getPermissionNode(meta.name);
}
/**
* 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 + "." + warp;
}
/**
* 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, meta.name);
}
/**
* 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)));
}
}
@@ -38,6 +38,7 @@ public class World {
this.environment = environment;
}
@SuppressWarnings("unused")
public World() {
}
@@ -51,4 +52,11 @@ public class World {
CUSTOM
}
@Override
public boolean equals(@NotNull Object obj) {
if (obj instanceof World world) {
return world.uuid.equals(uuid);
}
return super.equals(obj);
}
}
@@ -130,7 +130,7 @@ public class RequestManager {
Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime);
final Optional<OnlineUser> localTarget = plugin.findOnlinePlayer(targetUser);
if (localTarget.isPresent()) {
if (localTarget.get().uuid.equals(requester.uuid)) {
if (localTarget.get().equals(requester)) {
return CompletableFuture.completedFuture(Optional.empty());
}
request.recipientName = localTarget.get().username;