Move server ping cache storage to the AdaptedServer instance
This commit is contained in:
+25
@@ -36,4 +36,29 @@ public class VelocityLogger implements QueueLogger {
|
||||
public void severe(String message) {
|
||||
logger.error(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message, Throwable t) {
|
||||
logger.warn(message, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(String message, Throwable t) {
|
||||
logger.warn(message, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String message, Throwable t) {
|
||||
logger.info(message, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, Throwable t) {
|
||||
logger.error(message, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void severe(String message, Throwable t) {
|
||||
logger.error(message, t);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-7
@@ -17,6 +17,7 @@ import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||
import us.ajg0702.queue.api.util.QueueLogger;
|
||||
import us.ajg0702.queue.commands.commands.PlayerSender;
|
||||
import us.ajg0702.queue.common.utils.Debug;
|
||||
import us.ajg0702.queue.platforms.velocity.players.VelocityPlayer;
|
||||
import us.ajg0702.queue.platforms.velocity.server.VelocityServer;
|
||||
|
||||
@@ -134,19 +135,48 @@ public class VelocityMethods implements PlatformMethods {
|
||||
|
||||
@Override
|
||||
public AdaptedServer getServer(String name) {
|
||||
Optional<RegisteredServer> server = proxyServer.getServer(name);
|
||||
if(!server.isPresent()) return null;
|
||||
return new VelocityServer(server.get());
|
||||
List<? extends AdaptedServer> servers = getServers();
|
||||
for (AdaptedServer server : servers) {
|
||||
if(server.getName().equals(name)) return server;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
List<VelocityServer> serverList = new ArrayList<>();
|
||||
|
||||
|
||||
@Override
|
||||
public List<AdaptedServer> getServers() {
|
||||
List<AdaptedServer> result = new ArrayList<>();
|
||||
public List<? extends AdaptedServer> getServers() {
|
||||
|
||||
proxyServer.getAllServers().forEach(registeredServer -> result.add(new VelocityServer(registeredServer)));
|
||||
for (RegisteredServer registeredServer : proxyServer.getAllServers()) {
|
||||
boolean found = false;
|
||||
for(VelocityServer sv : new ArrayList<>(serverList)) {
|
||||
if(sv.getHandle().equals(registeredServer)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found) continue;
|
||||
|
||||
return result;
|
||||
Debug.info("Added "+registeredServer.getServerInfo().getName());
|
||||
serverList.add(new VelocityServer(registeredServer, plugin.getMain()));
|
||||
}
|
||||
|
||||
for(VelocityServer sv : new ArrayList<>(serverList)) {
|
||||
boolean found = false;
|
||||
for (RegisteredServer registeredServer : proxyServer.getAllServers()) {
|
||||
if(sv.getHandle().equals(registeredServer)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found) continue;
|
||||
|
||||
Debug.info("Removed "+sv.getName());
|
||||
serverList.remove(sv);
|
||||
}
|
||||
|
||||
return serverList;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+5
-1
@@ -144,7 +144,7 @@ public class VelocityQueue implements Implementation {
|
||||
Optional<Component> reasonOptional = e.getServerKickReason();
|
||||
main.getEventHandler().onServerKick(
|
||||
new VelocityPlayer(e.getPlayer()),
|
||||
new VelocityServer(e.getServer()),
|
||||
new VelocityServer(e.getServer(), main),
|
||||
reasonOptional.orElseGet(() -> Component.text("Proxy lost connection")),
|
||||
// According to Tux on discord, velocity doesnt give a reason when the proxy loses connection to the connected server
|
||||
e.kickedDuringServerConnect()
|
||||
@@ -165,4 +165,8 @@ public class VelocityQueue implements Implementation {
|
||||
new VelocityCommand(main, (BaseCommand) command)
|
||||
);
|
||||
}
|
||||
|
||||
public QueueMain getMain() {
|
||||
return main;
|
||||
}
|
||||
}
|
||||
|
||||
+83
-5
@@ -7,17 +7,31 @@ import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||
import us.ajg0702.queue.api.server.AdaptedServerInfo;
|
||||
import us.ajg0702.queue.api.server.AdaptedServerPing;
|
||||
import us.ajg0702.queue.api.util.QueueLogger;
|
||||
import us.ajg0702.queue.common.QueueMain;
|
||||
import us.ajg0702.queue.platforms.velocity.players.VelocityPlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class VelocityServer implements AdaptedServer {
|
||||
|
||||
private final RegisteredServer handle;
|
||||
public VelocityServer(RegisteredServer handle) {
|
||||
|
||||
private AdaptedServerPing lastPing = null;
|
||||
private AdaptedServerPing lastSuccessfullPing = null;
|
||||
private long lastOffline = 0;
|
||||
|
||||
private int offlineTime = 0;
|
||||
|
||||
private final QueueMain main;
|
||||
|
||||
public VelocityServer(RegisteredServer handle, QueueMain main) {
|
||||
this.handle = handle;
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,22 +45,51 @@ public class VelocityServer implements AdaptedServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<AdaptedServerPing> ping() {
|
||||
public CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logger) {
|
||||
CompletableFuture<AdaptedServerPing> future = new CompletableFuture<>();
|
||||
|
||||
long sent = System.currentTimeMillis();
|
||||
|
||||
CompletableFuture<ServerPing> serverPing = handle.ping();
|
||||
|
||||
if(debug) logger.info("[pinger] [" + getName() + "] sending ping");
|
||||
|
||||
serverPing.thenRunAsync(() -> {
|
||||
AdaptedServerPing aPing;
|
||||
VelocityServerPing ping;
|
||||
try {
|
||||
aPing = new VelocityServerPing(serverPing.get());
|
||||
ping = new VelocityServerPing(serverPing.get(), sent);
|
||||
} catch (Throwable e) {
|
||||
|
||||
long lastOnline = lastSuccessfullPing == null ? 0 : lastSuccessfullPing.getFetchedTime();
|
||||
offlineTime = (int) Math.min(sent - lastOnline, Integer.MAX_VALUE);
|
||||
|
||||
lastOffline = sent;
|
||||
|
||||
future.completeExceptionally(e);
|
||||
lastPing = null;
|
||||
if(debug) logger.info("[pinger] [" + getName() + "] offline:", e);
|
||||
return;
|
||||
}
|
||||
future.complete(aPing);
|
||||
|
||||
offlineTime = 0;
|
||||
lastSuccessfullPing = ping;
|
||||
|
||||
if(debug) logger.info(
|
||||
"[pinger] [" + getName() + "] online. motd: "+ping.getPlainDescription()+" " +
|
||||
" players: "+ping.getPlayerCount()+"/"+ping.getMaxPlayers()
|
||||
);
|
||||
|
||||
future.complete(ping);
|
||||
lastPing = ping;
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AdaptedServerPing> getLastPing() {
|
||||
return Optional.ofNullable(lastPing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAccess(AdaptedPlayer player) {
|
||||
return true;
|
||||
@@ -61,8 +104,43 @@ public class VelocityServer implements AdaptedServer {
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOfflineTime() {
|
||||
return offlineTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canJoinFull(AdaptedPlayer player) {
|
||||
if(player == null) return true;
|
||||
return
|
||||
player.hasPermission("ajqueue.joinfull") ||
|
||||
player.hasPermission("ajqueue.joinfullserver."+getName()) ||
|
||||
player.hasPermission("ajqueue.joinfullandbypassserver."+getName()) ||
|
||||
player.hasPermission("ajqueue.joinfullandbypass") ||
|
||||
(main.isPremium() && main.getLogic().getPermissionGetter().hasUniqueFullBypass(player, getName()))
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean justWentOnline() {
|
||||
return System.currentTimeMillis()-lastOffline <= (main.getConfig().getDouble("wait-time")) && isOnline();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegisteredServer getHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VelocityServer)) return false;
|
||||
VelocityServer that = (VelocityServer) o;
|
||||
return getHandle().equals(that.getHandle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getHandle());
|
||||
}
|
||||
}
|
||||
|
||||
+8
-1
@@ -8,8 +8,10 @@ import us.ajg0702.queue.api.server.AdaptedServerPing;
|
||||
public class VelocityServerPing implements AdaptedServerPing {
|
||||
|
||||
private final ServerPing handle;
|
||||
public VelocityServerPing(ServerPing handle) {
|
||||
private final long sent;
|
||||
public VelocityServerPing(ServerPing handle, long sent) {
|
||||
this.handle = handle;
|
||||
this.sent = sent;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,6 +41,11 @@ public class VelocityServerPing implements AdaptedServerPing {
|
||||
add++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFetchedTime() {
|
||||
return sent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerPing getHandle() {
|
||||
return handle;
|
||||
|
||||
Reference in New Issue
Block a user