Skip to content

Commit ddc258e

Browse files
authored
Fix teleport request queue being reversed order (#4755)
Fix #4753
1 parent d23796d commit ddc258e

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

Essentials/src/main/java/com/earth2me/essentials/IUser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ public interface IUser {
261261
* period are removed from queue and therefore not returned here. The maximum size of this
262262
* queue is determined by {@link ISettings#getTpaMaxRequests()}.
263263
*
264-
* @param inform true if the underlying {@link IUser} should be informed if a request expires during iteration.
265-
* @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.
266-
* @param excludeHere true if /tphere requests should be ignored in fetching the next tpa request.
264+
* @param inform true if the underlying {@link IUser} should be informed if a request expires during iteration.
265+
* @param ignoreExpirations true if this method should not process expirations for the entire queue and stop execution on the first unexpired request.
266+
* @param excludeHere true if /tphere requests should be ignored in fetching the next tpa request.
267267
* @return A {@link TpaRequest} corresponding to the next available request or null if no valid request is present.
268268
*/
269-
@Nullable TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, boolean excludeHere);
269+
@Nullable TpaRequest getNextTpaRequest(boolean inform, boolean ignoreExpirations, boolean excludeHere);
270270

271271
/**
272272
* Whether or not this {@link IUser} has any valid TPA requests in queue.

Essentials/src/main/java/com/earth2me/essentials/User.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import org.checkerframework.checker.nullness.qual.Nullable;
3535

3636
import java.math.BigDecimal;
37+
import java.util.ArrayList;
3738
import java.util.Calendar;
3839
import java.util.Collection;
40+
import java.util.Collections;
3941
import java.util.GregorianCalendar;
40-
import java.util.Iterator;
4142
import java.util.LinkedHashMap;
4243
import java.util.List;
4344
import java.util.Locale;
@@ -349,11 +350,8 @@ public void requestTeleport(final User player, final boolean here) {
349350
// Handle max queue size
350351
teleportRequestQueue.remove(request.getName());
351352
if (teleportRequestQueue.size() >= ess.getSettings().getTpaMaxRequests()) {
352-
String lastKey = null;
353-
for (Map.Entry<String, TpaRequest> entry : teleportRequestQueue.entrySet()) {
354-
lastKey = entry.getKey();
355-
}
356-
teleportRequestQueue.remove(lastKey);
353+
final List<String> keys = new ArrayList<>(teleportRequestQueue.keySet());
354+
teleportRequestQueue.remove(keys.get(keys.size() - 1));
357355
}
358356

359357
// Add request to queue
@@ -402,22 +400,24 @@ public TpaRequest removeTpaRequest(String playerUsername) {
402400
}
403401

404402
@Override
405-
public TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, boolean excludeHere) {
403+
public TpaRequest getNextTpaRequest(boolean inform, boolean ignoreExpirations, boolean excludeHere) {
406404
if (teleportRequestQueue.isEmpty()) {
407405
return null;
408406
}
409407

410408
final long timeout = ess.getSettings().getTpaAcceptCancellation();
411-
final Iterator<Map.Entry<String, TpaRequest>> iterator = teleportRequestQueue.entrySet().iterator();
409+
final List<String> keys = new ArrayList<>(teleportRequestQueue.keySet());
410+
Collections.reverse(keys);
411+
412412
TpaRequest nextRequest = null;
413-
while (iterator.hasNext()) {
414-
final TpaRequest request = iterator.next().getValue();
413+
for (final String key : keys) {
414+
final TpaRequest request = teleportRequestQueue.get(key);
415415
if (timeout < 1 || (System.currentTimeMillis() - request.getTime()) <= TimeUnit.SECONDS.toMillis(timeout)) {
416416
if (excludeHere && request.isHere()) {
417417
continue;
418418
}
419419

420-
if (performExpirations) {
420+
if (ignoreExpirations) {
421421
return request;
422422
} else if (nextRequest == null) {
423423
nextRequest = request;
@@ -426,7 +426,7 @@ public TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations,
426426
if (inform) {
427427
sendMessage(tl("requestTimedOutFrom", ess.getUser(request.getRequesterUuid()).getDisplayName()));
428428
}
429-
iterator.remove();
429+
teleportRequestQueue.remove(key);
430430
}
431431
}
432432
return nextRequest;

0 commit comments

Comments
 (0)