From dc3d297d1d2927ddb56b4bf5d8dc3e2117959ca9 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:51:30 -0500 Subject: [PATCH 1/2] Fix teleport request queue being reversed order --- .../java/com/earth2me/essentials/IUser.java | 8 +++--- .../java/com/earth2me/essentials/User.java | 25 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/IUser.java b/Essentials/src/main/java/com/earth2me/essentials/IUser.java index 797df13e439..b8784add50a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/IUser.java +++ b/Essentials/src/main/java/com/earth2me/essentials/IUser.java @@ -261,12 +261,12 @@ public interface IUser { * period are removed from queue and therefore not returned here. The maximum size of this * queue is determined by {@link ISettings#getTpaMaxRequests()}. * - * @param inform true if the underlying {@link IUser} should be informed if a request expires during iteration. - * @param performExpirations true if this method should not spend time validating time for all items in the queue and just return the first item in the queue. - * @param excludeHere true if /tphere requests should be ignored in fetching the next tpa request. + * @param inform true if the underlying {@link IUser} should be informed if a request expires during iteration. + * @param ignoreExpirations true if this method should not process expirations for the entire queue and stop execution on the first unexpired request. + * @param excludeHere true if /tphere requests should be ignored in fetching the next tpa request. * @return A {@link TpaRequest} corresponding to the next available request or null if no valid request is present. */ - @Nullable TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, boolean excludeHere); + @Nullable TpaRequest getNextTpaRequest(boolean inform, boolean ignoreExpirations, boolean excludeHere); /** * Whether or not this {@link IUser} has any valid TPA requests in queue. diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index 039747ad133..af69cb57ece 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -34,10 +34,11 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; import java.util.GregorianCalendar; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -349,11 +350,9 @@ public void requestTeleport(final User player, final boolean here) { // Handle max queue size teleportRequestQueue.remove(request.getName()); if (teleportRequestQueue.size() >= ess.getSettings().getTpaMaxRequests()) { - String lastKey = null; - for (Map.Entry entry : teleportRequestQueue.entrySet()) { - lastKey = entry.getKey(); - } - teleportRequestQueue.remove(lastKey); + final List keys = new ArrayList<>(teleportRequestQueue.keySet()); + Collections.reverse(keys); + teleportRequestQueue.remove(keys.get(0)); } // Add request to queue @@ -402,22 +401,24 @@ public TpaRequest removeTpaRequest(String playerUsername) { } @Override - public TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, boolean excludeHere) { + public TpaRequest getNextTpaRequest(boolean inform, boolean ignoreExpirations, boolean excludeHere) { if (teleportRequestQueue.isEmpty()) { return null; } final long timeout = ess.getSettings().getTpaAcceptCancellation(); - final Iterator> iterator = teleportRequestQueue.entrySet().iterator(); + final List keys = new ArrayList<>(teleportRequestQueue.keySet()); + Collections.reverse(keys); + TpaRequest nextRequest = null; - while (iterator.hasNext()) { - final TpaRequest request = iterator.next().getValue(); + for (final String key : keys) { + final TpaRequest request = teleportRequestQueue.get(key); if (timeout < 1 || (System.currentTimeMillis() - request.getTime()) <= TimeUnit.SECONDS.toMillis(timeout)) { if (excludeHere && request.isHere()) { continue; } - if (performExpirations) { + if (ignoreExpirations) { return request; } else if (nextRequest == null) { nextRequest = request; @@ -426,7 +427,7 @@ public TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, if (inform) { sendMessage(tl("requestTimedOutFrom", ess.getUser(request.getRequesterUuid()).getDisplayName())); } - iterator.remove(); + teleportRequestQueue.remove(key); } } return nextRequest; From 0aaea0adfde4f75c3764c4f18cabbc9f47c7dd36 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Sun, 6 Feb 2022 13:12:29 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- Essentials/src/main/java/com/earth2me/essentials/User.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index af69cb57ece..af075539b5f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -351,8 +351,7 @@ public void requestTeleport(final User player, final boolean here) { teleportRequestQueue.remove(request.getName()); if (teleportRequestQueue.size() >= ess.getSettings().getTpaMaxRequests()) { final List keys = new ArrayList<>(teleportRequestQueue.keySet()); - Collections.reverse(keys); - teleportRequestQueue.remove(keys.get(0)); + teleportRequestQueue.remove(keys.get(keys.size() - 1)); } // Add request to queue