Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
Expand All @@ -43,12 +41,11 @@
import com.sk89q.worldguard.session.handler.WeatherLockFlag;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.time.Duration;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.logging.Level;

Expand All @@ -59,21 +56,19 @@ public abstract class AbstractSessionManager implements SessionManager {
public static final int RUN_DELAY = 20;
public static final long SESSION_LIFETIME = 10;

private static final BiPredicate<World, LocalPlayer> BYPASS_PERMISSION_TEST = (world, player) -> {
return player.hasPermission("worldguard.region.bypass." + world.getName());
};
private static final BiPredicate<World, LocalPlayer> BYPASS_PERMISSION_TEST = (world, player) -> player.hasPermission("worldguard.region.bypass." + world.getName());

private final LoadingCache<WorldPlayerTuple, Boolean> bypassCache = CacheBuilder.newBuilder()
private final Cache<BypassCacheKey, Boolean> bypassCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(2, TimeUnit.SECONDS)
.build(CacheLoader.from(tuple -> BYPASS_PERMISSION_TEST.test(tuple.getWorld(), tuple.getPlayer())));
.expireAfterWrite(Duration.ofSeconds(2))
.build();

private final Cache<CacheKey, Session> sessions = CacheBuilder.newBuilder()
.expireAfterAccess(SESSION_LIFETIME, TimeUnit.MINUTES)
private final Cache<UUID, Session> sessions = CacheBuilder.newBuilder()
.expireAfterAccess(Duration.ofMinutes(SESSION_LIFETIME))
.build();

private boolean hasCustom = false;
private List<Handler.Factory<? extends Handler>> handlers = new LinkedList<>();
private final List<Handler.Factory<? extends Handler>> handlers = new LinkedList<>();

private static final List<Handler.Factory<? extends Handler>> defaultHandlers = new LinkedList<>();

Expand Down Expand Up @@ -146,13 +141,14 @@ public boolean hasBypass(LocalPlayer player, World world) {
return BYPASS_PERMISSION_TEST.test(world, player);
}

return bypassCache.getUnchecked(new WorldPlayerTuple(world, player));
var bypassCacheKey = new BypassCacheKey(world.getName(), player.getUniqueId());
return bypassCache.asMap().computeIfAbsent(bypassCacheKey, (_) -> BYPASS_PERMISSION_TEST.test(world, player));
}

@Override
public void resetState(LocalPlayer player) {
checkNotNull(player, "player");
@Nullable Session session = getIfPresentInternal(new CacheKey(player));
@Nullable Session session = getIfPresentInternal(player);
if (session != null) {
session.resetState(player);
}
Expand All @@ -161,21 +157,21 @@ public void resetState(LocalPlayer player) {
@Override
@Nullable
public Session getIfPresent(LocalPlayer player) {
return getIfPresentInternal(new CacheKey(player));
return getIfPresentInternal(player);
}

private Session getIfPresentInternal(CacheKey cacheKey) {
@Nullable Session session = sessions.getIfPresent(cacheKey);
private Session getIfPresentInternal(LocalPlayer player) {
@Nullable Session session = sessions.getIfPresent(player.getUniqueId());
if (session != null) {
session.ensureInitialized(cacheKey.playerRef.get(), this::initializeSession);
session.ensureInitialized(player, this::initializeSession);
return session;
}
return null;
}

@Override
public Session get(LocalPlayer player) {
Session session = sessions.asMap().computeIfAbsent(new CacheKey(player), (k) -> new Session(this));
Session session = sessions.asMap().computeIfAbsent(player.getUniqueId(), (_) -> new Session(this));
session.ensureInitialized(player, this::initializeSession);
return session;
}
Expand All @@ -192,27 +188,6 @@ private void initializeSession(Session session, LocalPlayer player) {
session.initialize(player);
}

protected static final class CacheKey {
final WeakReference<LocalPlayer> playerRef;
final UUID uuid;

CacheKey(LocalPlayer player) {
playerRef = new WeakReference<>(player);
uuid = player.getUniqueId();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
return uuid.equals(cacheKey.uuid);

}

@Override
public int hashCode() {
return uuid.hashCode();
}
record BypassCacheKey(String worldName, UUID playerUUID) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public void uninitialize(LocalPlayer player) {
for (Handler handler : handlers.values()) {
handler.uninitialize(player, location, set);
}

lastValid = null;
lastRegionSet = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.LocalPlayer;

@Deprecated(forRemoval = true)
public class WorldPlayerTuple {

private final World world;
Expand Down
Loading