Skip to content

Commit 76a2169

Browse files
Properly implement position cache
1 parent 014a523 commit 76a2169

2 files changed

Lines changed: 11 additions & 20 deletions

File tree

src/main/java/net/pistonmaster/pistonqueue/shared/QueueListenerShared.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import net.pistonmaster.pistonqueue.shared.events.PQServerConnectedEvent;
2929
import net.pistonmaster.pistonqueue.shared.events.PQServerPreConnectEvent;
3030
import net.pistonmaster.pistonqueue.shared.utils.BanType;
31-
import net.pistonmaster.pistonqueue.shared.utils.Pair;
3231

3332
import java.time.Duration;
3433
import java.time.Instant;
@@ -278,9 +277,10 @@ protected void connectPlayer(QueueType type) {
278277

279278
indexPositionTime();
280279

281-
List<Pair<Integer, Instant>> cache = type.getPositionCache().get(entry.getKey());
280+
Map<Integer, Instant> cache = type.getPositionCache().get(entry.getKey());
282281
if (cache != null) {
283-
cache.forEach(pair -> type.getDurationToPosition().put(pair.getLeft(), Duration.between(pair.getRight(), Instant.now())));
282+
cache.forEach((position, instant) ->
283+
type.getDurationToPosition().put(position, Duration.between(instant, Instant.now())));
284284
}
285285

286286
player.get().connect(entry.getValue());
@@ -309,24 +309,15 @@ protected void indexPositionTime() {
309309
for (QueueType type : QueueType.values()) {
310310
int position = 0;
311311

312-
for (Map.Entry<UUID, String> entry : new LinkedHashMap<>(type.getQueueMap()).entrySet()) {
313-
Optional<PlayerWrapper> player = plugin.getPlayer(entry.getKey());
314-
if (!player.isPresent()) {
315-
continue;
316-
}
317-
312+
for (UUID uuid : new LinkedHashMap<>(type.getQueueMap()).keySet()) {
318313
position++;
319-
320-
if (type.getPositionCache().containsKey(player.get().getUniqueId())) {
321-
List<Pair<Integer, Instant>> list = type.getPositionCache().get(player.get().getUniqueId());
322-
int finalPosition = position;
323-
if (list.stream().map(Pair::getLeft).noneMatch(integer -> integer == finalPosition)) {
324-
list.add(new Pair<>(position, Instant.now()));
325-
}
314+
Map<Integer, Instant> list = type.getPositionCache().get(uuid);
315+
if (list == null) {
316+
type.getPositionCache().put(uuid, new HashMap<>(Collections.singletonMap(position, Instant.now())));
326317
} else {
327-
List<Pair<Integer, Instant>> list = new ArrayList<>();
328-
list.add(new Pair<>(position, Instant.now()));
329-
type.getPositionCache().put(player.get().getUniqueId(), list);
318+
if (!list.containsKey(position)) {
319+
list.put(position, Instant.now());
320+
}
330321
}
331322
}
332323
}

src/main/java/net/pistonmaster/pistonqueue/shared/QueueType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum QueueType {
4141
private final Map<Integer, Duration> durationToPosition = Collections.synchronizedMap(new LinkedHashMap<>());
4242

4343
@Getter
44-
private final Map<UUID, List<Pair<Integer, Instant>>> positionCache = new ConcurrentHashMap<>();
44+
private final Map<UUID, Map<Integer, Instant>> positionCache = new ConcurrentHashMap<>();
4545

4646
@Getter
4747
private final AtomicInteger playersWithTypeInMain = new AtomicInteger();

0 commit comments

Comments
 (0)