diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index 81e8bdca..863dfec7 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -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 getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales, - @NotNull List warps, final boolean permissionRestrictWarps, - final int itemsPerPage, final int page) { + @NotNull List 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("")) diff --git a/common/src/main/java/net/william278/huskhomes/command/CommandBase.java b/common/src/main/java/net/william278/huskhomes/command/CommandBase.java index 88a55c87..1a58c996 100644 --- a/common/src/main/java/net/william278/huskhomes/command/CommandBase.java +++ b/common/src/main/java/net/william278/huskhomes/command/CommandBase.java @@ -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; diff --git a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java index 8bac4805..187f3ea3 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java @@ -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; diff --git a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java index 1a199306..5c2016ac 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -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(""))); diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index baddc6c6..1bd085da 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -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 -> { diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java index a629a274..be9d1ba4 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java @@ -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 { diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java index b5634598..f976394a 100644 --- a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java @@ -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))); diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java index f6076ce5..b9b01ed5 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -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()) { diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index 2f650987..49d3f364 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -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 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()); diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java index 82b2e642..66c3ca88 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java @@ -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 diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index b3dfd209..6ff5612b 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -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())); } } diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index 93c75070..a93cdc2f 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -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)); diff --git a/common/src/main/java/net/william278/huskhomes/messenger/Message.java b/common/src/main/java/net/william278/huskhomes/messenger/Message.java index 3346af37..b3191dce 100644 --- a/common/src/main/java/net/william278/huskhomes/messenger/Message.java +++ b/common/src/main/java/net/william278/huskhomes/messenger/Message.java @@ -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); } diff --git a/common/src/main/java/net/william278/huskhomes/player/User.java b/common/src/main/java/net/william278/huskhomes/player/User.java index e8112cd5..dcafcf0e 100644 --- a/common/src/main/java/net/william278/huskhomes/player/User.java +++ b/common/src/main/java/net/william278/huskhomes/player/User.java @@ -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); + } } diff --git a/common/src/main/java/net/william278/huskhomes/position/Server.java b/common/src/main/java/net/william278/huskhomes/position/Server.java index fe2d141b..9b606d82 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Server.java +++ b/common/src/main/java/net/william278/huskhomes/position/Server.java @@ -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); diff --git a/common/src/main/java/net/william278/huskhomes/position/Warp.java b/common/src/main/java/net/william278/huskhomes/position/Warp.java index 9cc75500..48da4326 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Warp.java +++ b/common/src/main/java/net/william278/huskhomes/position/Warp.java @@ -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))); + } + } diff --git a/common/src/main/java/net/william278/huskhomes/position/World.java b/common/src/main/java/net/william278/huskhomes/position/World.java index b061be78..86c30db2 100644 --- a/common/src/main/java/net/william278/huskhomes/position/World.java +++ b/common/src/main/java/net/william278/huskhomes/position/World.java @@ -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); + } } diff --git a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java index 4ad86237..6e2e106e 100644 --- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java +++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java @@ -130,7 +130,7 @@ public class RequestManager { Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime); final Optional 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;