Fix teleport request queue being reversed order (#4755)

Fix #4753
This commit is contained in:
Josh Roy 2022-02-06 13:18:40 -05:00 committed by GitHub
parent d23796dade
commit ddc258ed11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 16 deletions

View File

@ -261,12 +261,12 @@ public interface IUser {
* period are removed from queue and therefore not returned here. The maximum size of this * period are removed from queue and therefore not returned here. The maximum size of this
* queue is determined by {@link ISettings#getTpaMaxRequests()}. * 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 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 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. * @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. * @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. * Whether or not this {@link IUser} has any valid TPA requests in queue.

View File

@ -34,10 +34,11 @@ import org.bukkit.potion.PotionEffectType;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -349,11 +350,8 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
// Handle max queue size // Handle max queue size
teleportRequestQueue.remove(request.getName()); teleportRequestQueue.remove(request.getName());
if (teleportRequestQueue.size() >= ess.getSettings().getTpaMaxRequests()) { if (teleportRequestQueue.size() >= ess.getSettings().getTpaMaxRequests()) {
String lastKey = null; final List<String> keys = new ArrayList<>(teleportRequestQueue.keySet());
for (Map.Entry<String, TpaRequest> entry : teleportRequestQueue.entrySet()) { teleportRequestQueue.remove(keys.get(keys.size() - 1));
lastKey = entry.getKey();
}
teleportRequestQueue.remove(lastKey);
} }
// Add request to queue // Add request to queue
@ -402,22 +400,24 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
} }
@Override @Override
public TpaRequest getNextTpaRequest(boolean inform, boolean performExpirations, boolean excludeHere) { public TpaRequest getNextTpaRequest(boolean inform, boolean ignoreExpirations, boolean excludeHere) {
if (teleportRequestQueue.isEmpty()) { if (teleportRequestQueue.isEmpty()) {
return null; return null;
} }
final long timeout = ess.getSettings().getTpaAcceptCancellation(); final long timeout = ess.getSettings().getTpaAcceptCancellation();
final Iterator<Map.Entry<String, TpaRequest>> iterator = teleportRequestQueue.entrySet().iterator(); final List<String> keys = new ArrayList<>(teleportRequestQueue.keySet());
Collections.reverse(keys);
TpaRequest nextRequest = null; TpaRequest nextRequest = null;
while (iterator.hasNext()) { for (final String key : keys) {
final TpaRequest request = iterator.next().getValue(); final TpaRequest request = teleportRequestQueue.get(key);
if (timeout < 1 || (System.currentTimeMillis() - request.getTime()) <= TimeUnit.SECONDS.toMillis(timeout)) { if (timeout < 1 || (System.currentTimeMillis() - request.getTime()) <= TimeUnit.SECONDS.toMillis(timeout)) {
if (excludeHere && request.isHere()) { if (excludeHere && request.isHere()) {
continue; continue;
} }
if (performExpirations) { if (ignoreExpirations) {
return request; return request;
} else if (nextRequest == null) { } else if (nextRequest == null) {
nextRequest = request; nextRequest = request;
@ -426,7 +426,7 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
if (inform) { if (inform) {
sendMessage(tl("requestTimedOutFrom", ess.getUser(request.getRequesterUuid()).getDisplayName())); sendMessage(tl("requestTimedOutFrom", ess.getUser(request.getRequesterUuid()).getDisplayName()));
} }
iterator.remove(); teleportRequestQueue.remove(key);
} }
} }
return nextRequest; return nextRequest;