Skip to content

Commit

Permalink
fix: get exact OnlineUser if name matches globally, close #700
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Oct 31, 2024
1 parent d55260f commit 050f8a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ public void sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String t
final long expiry = Instant.now().getEpochSecond()
+ plugin.getSettings().getGeneral().getTeleportRequestExpiryTime();
final TeleportRequest request = new TeleportRequest(requester, type, expiry);
final Optional<OnlineUser> localTarget = plugin.getOnlineUser(targetUser);

// Lookup the user locally first. If there's a username match globally, perform an exact local check
final Optional<OnlineUser> localTarget = plugin.isUserOnlineGlobally(targetUser)
? plugin.getOnlineUserExact(targetUser) : plugin.getOnlineUser(targetUser);
if (localTarget.isPresent()) {
if (localTarget.get().equals(requester)) {
throw new IllegalArgumentException("Cannot send a teleport request to yourself");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ default void setUserList(@NotNull String server, @NotNull List<User> players) {
getGlobalUserList().put(server, players);
}

default boolean isUserOnline(@NotNull User user) {
return getOnlineUserMap().containsKey(user.getUuid());
default boolean isUserOnlineGlobally(@NotNull String playerName) {
return getUserList().stream()
.map(u -> u.getName().toLowerCase(Locale.ENGLISH))
.anyMatch(u -> u.equals(playerName.toLowerCase(Locale.ENGLISH)));
}

default Optional<OnlineUser> getOnlineUser(@NotNull String playerName) {
Expand Down

0 comments on commit 050f8a2

Please sign in to comment.