Fix checking bypass on initialize (#2082)

* Fix checking bypass on initialize

* Add SessionManager#createSession back as deprecated
This commit is contained in:
Joni Aromaa 2024-05-24 15:48:08 +01:00 committed by GitHub
parent 8b7729e0e8
commit ead1e1b77c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 8 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.session;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
@ -67,10 +68,9 @@ public abstract class AbstractSessionManager implements SessionManager {
.expireAfterWrite(2, TimeUnit.SECONDS)
.build(CacheLoader.from(tuple -> BYPASS_PERMISSION_TEST.test(tuple.getWorld(), tuple.getPlayer())));
private final LoadingCache<CacheKey, Session> sessions = CacheBuilder.newBuilder()
private final Cache<CacheKey, Session> sessions = CacheBuilder.newBuilder()
.expireAfterAccess(SESSION_LIFETIME, TimeUnit.MINUTES)
.build(CacheLoader.from(key ->
createSession(key.playerRef.get())));
.build();
private boolean hasCustom = false;
private List<Handler.Factory<? extends Handler>> handlers = new LinkedList<>();
@ -152,7 +152,7 @@ public boolean hasBypass(LocalPlayer player, World world) {
@Override
public void resetState(LocalPlayer player) {
checkNotNull(player, "player");
@Nullable Session session = sessions.getIfPresent(new CacheKey(player));
@Nullable Session session = getIfPresentInternal(new CacheKey(player));
if (session != null) {
session.resetState(player);
}
@ -161,22 +161,35 @@ public void resetState(LocalPlayer player) {
@Override
@Nullable
public Session getIfPresent(LocalPlayer player) {
return sessions.getIfPresent(new CacheKey(player));
return getIfPresentInternal(new CacheKey(player));
}
private Session getIfPresentInternal(CacheKey cacheKey) {
@Nullable Session session = sessions.getIfPresent(cacheKey);
if (session != null) {
session.ensureInitialized(cacheKey.playerRef.get(), this::initializeSession);
return session;
}
return null;
}
@Override
public Session get(LocalPlayer player) {
return sessions.getUnchecked(new CacheKey(player));
Session session = sessions.asMap().computeIfAbsent(new CacheKey(player), (k) -> new Session(this));
session.ensureInitialized(player, this::initializeSession);
return session;
}
@Override
public Session createSession(LocalPlayer player) {
Session session = new Session(this);
return get(player);
}
private void initializeSession(Session session, LocalPlayer player) {
for (Handler.Factory<? extends Handler> factory : handlers) {
session.register(factory.create(session));
}
session.initialize(player);
return session;
}
protected static final class CacheKey {

View File

@ -38,6 +38,7 @@
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import static com.google.common.base.Preconditions.checkNotNull;
@ -52,6 +53,7 @@ public class Session {
private Location lastValid;
private Set<ProtectedRegion> lastRegionSet;
private final AtomicBoolean needRefresh = new AtomicBoolean(false);
private boolean initialized;
/**
* Create a new session.
@ -120,6 +122,14 @@ public void initialize(LocalPlayer player) {
}
}
synchronized void ensureInitialized(LocalPlayer player, BiConsumer<Session, LocalPlayer> initializer) {
if (initialized) {
return;
}
initialized = true;
initializer.accept(this, player);
}
/**
* Uninitialize the session.
*

View File

@ -95,7 +95,9 @@ public interface SessionManager {
*
* @param player The player
* @return The new session
* @deprecated Use {@link SessionManager#get} instead
*/
@Deprecated
Session createSession(LocalPlayer player);
/**