File tree Expand file tree Collapse file tree
src/main/java/io/github/_4drian3d/authmevelocity/lastserver Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33import com .google .inject .Inject ;
44import com .velocitypowered .api .event .EventManager ;
55import com .velocitypowered .api .event .EventTask ;
6- import com .velocitypowered .api .proxy .Player ;
76import com .velocitypowered .api .proxy .ProxyServer ;
87import io .github ._4drian3d .authmevelocity .api .velocity .event .PreSendOnLoginEvent ;
98import io .github ._4drian3d .authmevelocity .api .velocity .event .ServerResult ;
109import io .github ._4drian3d .authmevelocity .lastserver .LastServerAddon ;
1110import 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
1413public 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 ();
You can’t perform that action at this time.
0 commit comments