mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-09-28 22:57:29 +02:00
Fix checking bypass on initialize (#2082)
* Fix checking bypass on initialize * Add SessionManager#createSession back as deprecated
This commit is contained in:
parent
8b7729e0e8
commit
ead1e1b77c
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package com.sk89q.worldguard.session;
|
package com.sk89q.worldguard.session;
|
||||||
|
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.CacheLoader;
|
import com.google.common.cache.CacheLoader;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
@ -67,10 +68,9 @@ public abstract class AbstractSessionManager implements SessionManager {
|
|||||||
.expireAfterWrite(2, TimeUnit.SECONDS)
|
.expireAfterWrite(2, TimeUnit.SECONDS)
|
||||||
.build(CacheLoader.from(tuple -> BYPASS_PERMISSION_TEST.test(tuple.getWorld(), tuple.getPlayer())));
|
.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)
|
.expireAfterAccess(SESSION_LIFETIME, TimeUnit.MINUTES)
|
||||||
.build(CacheLoader.from(key ->
|
.build();
|
||||||
createSession(key.playerRef.get())));
|
|
||||||
|
|
||||||
private boolean hasCustom = false;
|
private boolean hasCustom = false;
|
||||||
private List<Handler.Factory<? extends Handler>> handlers = new LinkedList<>();
|
private List<Handler.Factory<? extends Handler>> handlers = new LinkedList<>();
|
||||||
@ -152,7 +152,7 @@ public boolean hasBypass(LocalPlayer player, World world) {
|
|||||||
@Override
|
@Override
|
||||||
public void resetState(LocalPlayer player) {
|
public void resetState(LocalPlayer player) {
|
||||||
checkNotNull(player, "player");
|
checkNotNull(player, "player");
|
||||||
@Nullable Session session = sessions.getIfPresent(new CacheKey(player));
|
@Nullable Session session = getIfPresentInternal(new CacheKey(player));
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.resetState(player);
|
session.resetState(player);
|
||||||
}
|
}
|
||||||
@ -161,22 +161,35 @@ public void resetState(LocalPlayer player) {
|
|||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public Session getIfPresent(LocalPlayer player) {
|
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
|
@Override
|
||||||
public Session get(LocalPlayer player) {
|
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
|
@Override
|
||||||
public Session createSession(LocalPlayer player) {
|
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) {
|
for (Handler.Factory<? extends Handler> factory : handlers) {
|
||||||
session.register(factory.create(session));
|
session.register(factory.create(session));
|
||||||
}
|
}
|
||||||
session.initialize(player);
|
session.initialize(player);
|
||||||
return session;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static final class CacheKey {
|
protected static final class CacheKey {
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
@ -52,6 +53,7 @@ public class Session {
|
|||||||
private Location lastValid;
|
private Location lastValid;
|
||||||
private Set<ProtectedRegion> lastRegionSet;
|
private Set<ProtectedRegion> lastRegionSet;
|
||||||
private final AtomicBoolean needRefresh = new AtomicBoolean(false);
|
private final AtomicBoolean needRefresh = new AtomicBoolean(false);
|
||||||
|
private boolean initialized;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new session.
|
* 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.
|
* Uninitialize the session.
|
||||||
*
|
*
|
||||||
|
@ -95,7 +95,9 @@ public interface SessionManager {
|
|||||||
*
|
*
|
||||||
* @param player The player
|
* @param player The player
|
||||||
* @return The new session
|
* @return The new session
|
||||||
|
* @deprecated Use {@link SessionManager#get} instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
Session createSession(LocalPlayer player);
|
Session createSession(LocalPlayer player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user