Skip to content

Commit fa6c0c9

Browse files
committed
feat: Add OnlineServerCache
1 parent a7136b6 commit fa6c0c9

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github._4drian3d.authmevelocity.lastserver.database;
2+
3+
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import com.github.benmanes.caffeine.cache.LoadingCache;
5+
import com.google.inject.Inject;
6+
import com.google.inject.Singleton;
7+
import com.velocitypowered.api.proxy.ProxyServer;
8+
import com.velocitypowered.api.proxy.server.RegisteredServer;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
@Singleton
13+
public class OnlineServerCache {
14+
private final LoadingCache<String, Boolean> cache;
15+
16+
@Inject
17+
public OnlineServerCache(final ProxyServer proxyServer) {
18+
this.cache = Caffeine.newBuilder()
19+
.expireAfterWrite(3, TimeUnit.MINUTES)
20+
.build(server -> proxyServer.getServer(server)
21+
.filter(registeredServer -> registeredServer.ping()
22+
.handle((ping, throwable) -> ping != null)
23+
.join())
24+
.isPresent());
25+
}
26+
27+
public boolean isOnline(final String server) {
28+
return this.cache.get(server);
29+
}
30+
31+
public boolean isOnline(final RegisteredServer server) {
32+
return this.isOnline(server.getServerInfo().getName());
33+
}
34+
}

src/main/java/io/github/_4drian3d/authmevelocity/lastserver/listener/PreSendOnLoginListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import com.google.inject.Inject;
44
import com.velocitypowered.api.event.EventManager;
55
import com.velocitypowered.api.event.EventTask;
6-
import com.velocitypowered.api.proxy.Player;
76
import com.velocitypowered.api.proxy.ProxyServer;
87
import io.github._4drian3d.authmevelocity.api.velocity.event.PreSendOnLoginEvent;
98
import io.github._4drian3d.authmevelocity.api.velocity.event.ServerResult;
109
import io.github._4drian3d.authmevelocity.lastserver.LastServerAddon;
1110
import io.github._4drian3d.authmevelocity.lastserver.database.Database;
12-
import org.checkerframework.checker.nullness.qual.Nullable;
11+
import io.github._4drian3d.authmevelocity.lastserver.database.OnlineServerCache;
1312

1413
public final class PreSendOnLoginListener implements LastLoginListener<PreSendOnLoginEvent> {
1514
@Inject
@@ -20,6 +19,8 @@ public final class PreSendOnLoginListener implements LastLoginListener<PreSendOn
2019
private LastServerAddon plugin;
2120
@Inject
2221
private Database database;
22+
@Inject
23+
private OnlineServerCache serverCache;
2324

2425
@Override
2526
public EventTask executeAsync(final PreSendOnLoginEvent event) {
@@ -28,6 +29,7 @@ public EventTask executeAsync(final PreSendOnLoginEvent event) {
2829
final String newServer = this.database.lastServerOf(player);
2930
if (newServer != null) {
3031
this.proxyServer.getServer(newServer)
32+
.filter(serverCache::isOnline)
3133
.ifPresent(server -> event.setResult(ServerResult.allowed(server)));
3234
}
3335
continuation.resume();

0 commit comments

Comments
 (0)