Allocate WorldPlayerTuple only when using cache

This commit is contained in:
TheMolkaPL 2020-06-27 04:03:49 +02:00 committed by wizjany
parent a97db0ede9
commit 1652c9bfc0

View File

@ -48,7 +48,7 @@
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.function.BiPredicate;
import java.util.logging.Level;
import static com.google.common.base.Preconditions.checkNotNull;
@ -58,14 +58,14 @@ public abstract class AbstractSessionManager implements SessionManager {
public static final int RUN_DELAY = 20;
public static final long SESSION_LIFETIME = 10;
private static final Predicate<WorldPlayerTuple> BYPASS_PERMISSION_TEST = tuple -> {
return tuple.getPlayer().hasPermission("worldguard.region.bypass." + tuple.getWorld().getName());
private static final BiPredicate<World, LocalPlayer> BYPASS_PERMISSION_TEST = (world, player) -> {
return player.hasPermission("worldguard.region.bypass." + world.getName());
};
private final LoadingCache<WorldPlayerTuple, Boolean> bypassCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(2, TimeUnit.SECONDS)
.build(CacheLoader.from(BYPASS_PERMISSION_TEST::test));
.build(CacheLoader.from(tuple -> BYPASS_PERMISSION_TEST.test(tuple.getWorld(), tuple.getPlayer())));
private final LoadingCache<CacheKey, Session> sessions = CacheBuilder.newBuilder()
.expireAfterAccess(SESSION_LIFETIME, TimeUnit.MINUTES)
@ -142,9 +142,11 @@ public boolean hasBypass(LocalPlayer player, World world) {
return false;
}
WorldPlayerTuple tuple = new WorldPlayerTuple(world, player);
boolean disablePermissionCache = WorldGuard.getInstance().getPlatform().getGlobalStateManager().disablePermissionCache;
return disablePermissionCache ? BYPASS_PERMISSION_TEST.test(tuple) : bypassCache.getUnchecked(tuple);
if (WorldGuard.getInstance().getPlatform().getGlobalStateManager().disablePermissionCache) {
return BYPASS_PERMISSION_TEST.test(world, player);
}
return bypassCache.getUnchecked(new WorldPlayerTuple(world, player));
}
@Override