diff --git a/build.gradle b/build.gradle index f7ccf12..fef5a1c 100644 --- a/build.gradle +++ b/build.gradle @@ -15,16 +15,20 @@ repositories { name = 'papermc-repo' url = 'https://repo.papermc.io/repository/maven-public/' } + maven { + url = 'https://repo.loapu.dev/releases' + } } def velocityApiVersion = '3.2.0-SNAPSHOT' dependencies { + compileOnly "dev.loapu.vanishbridge:vanishbridge-api:1.0" compileOnly "com.velocitypowered:velocity-api:$velocityApiVersion" annotationProcessor "com.velocitypowered:velocity-api:$velocityApiVersion" } -def targetJavaVersion = 11 +def targetJavaVersion = 17 java { def javaVersion = JavaVersion.toVersion(targetJavaVersion) sourceCompatibility = javaVersion diff --git a/src/main/java/voruti/velocityplayerlistquery/VelocityPlayerListQuery.java b/src/main/java/voruti/velocityplayerlistquery/VelocityPlayerListQuery.java index 5eafb40..7846563 100644 --- a/src/main/java/voruti/velocityplayerlistquery/VelocityPlayerListQuery.java +++ b/src/main/java/voruti/velocityplayerlistquery/VelocityPlayerListQuery.java @@ -5,12 +5,14 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyPingEvent; import com.velocitypowered.api.event.proxy.ProxyReloadEvent; +import com.velocitypowered.api.plugin.Dependency; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.proxy.server.ServerPing; import lombok.AccessLevel; import lombok.NonNull; import lombok.experimental.FieldDefaults; import org.slf4j.Logger; +import voruti.velocityplayerlistquery.hook.VanishBridgeHook; import voruti.velocityplayerlistquery.model.exception.InvalidServerPingException; import voruti.velocityplayerlistquery.service.ConfigService; import voruti.velocityplayerlistquery.service.serverpingprocessor.ServerPingProcessor; @@ -25,7 +27,10 @@ version = BuildConstants.VERSION, description = "A Velocity plugin that shows current players in the server list.", url = "https://github.com/voruti/VelocityPlayerListQuery", - authors = {"voruti"} + authors = {"voruti"}, + dependencies = { + @Dependency(id = "vanishbridge", optional = true) + } ) @FieldDefaults(level = AccessLevel.PRIVATE) public class VelocityPlayerListQuery { @@ -38,12 +43,17 @@ public class VelocityPlayerListQuery { @Inject ServerPingProcessorRegistry serverPingProcessorRegistry; + + @Inject + VanishBridgeHook vanishBridgeHook; @Subscribe public void onProxyInitialization(ProxyInitializeEvent ignored) { this.configService.reloadConfig(); - + + if (this.vanishBridgeHook.hooked()) this.logger.info("VanishBridge found, enabling vanish support"); + this.logger.info("Enabled"); } diff --git a/src/main/java/voruti/velocityplayerlistquery/hook/VanishBridgeHook.java b/src/main/java/voruti/velocityplayerlistquery/hook/VanishBridgeHook.java new file mode 100644 index 0000000..f51ae3c --- /dev/null +++ b/src/main/java/voruti/velocityplayerlistquery/hook/VanishBridgeHook.java @@ -0,0 +1,41 @@ +package voruti.velocityplayerlistquery.hook; + +import com.google.inject.Inject; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import dev.loapu.vanishbridge.api.model.VanishBridgePlayer; +import lombok.AccessLevel; +import lombok.experimental.FieldDefaults; +import dev.loapu.vanishbridge.api.VanishBridgeProvider; + +import javax.inject.Singleton; +import java.util.ArrayList; +import java.util.Collection; + +@Singleton +@FieldDefaults(level = AccessLevel.PRIVATE) +public class VanishBridgeHook { + @Inject + ProxyServer server; + + public boolean hooked() { + return server.getPluginManager().isLoaded("vanishbridge"); + } + + public Collection unvanishedPlayers() { + var allPlayers = server.getAllPlayers(); + Collection unvanishedPlayers = new ArrayList<>(); + var vanishedPlayers = VanishBridgeProvider.get().vanishedPlayers(); + allPlayers.forEach(player -> { + if (vanishedPlayers.stream().noneMatch(vanishedPlayer -> vanishedPlayer.uuid().equals(player.getUniqueId()))) { + unvanishedPlayers.add(player); + } + }); + + return unvanishedPlayers; + } + + public int unvanishedPlayerCount() { + return unvanishedPlayers().size(); + } +} diff --git a/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/OnlinePlayerCountServerPingProcessor.java b/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/OnlinePlayerCountServerPingProcessor.java index baf6653..16ed101 100644 --- a/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/OnlinePlayerCountServerPingProcessor.java +++ b/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/OnlinePlayerCountServerPingProcessor.java @@ -4,6 +4,7 @@ import com.velocitypowered.api.proxy.server.ServerPing; import lombok.NonNull; import lombok.experimental.FieldDefaults; +import voruti.velocityplayerlistquery.hook.VanishBridgeHook; import voruti.velocityplayerlistquery.service.ConfigService; import javax.inject.Inject; @@ -18,6 +19,9 @@ public class OnlinePlayerCountServerPingProcessor extends ServerPingProcessor { @Inject ProxyServer proxyServer; + + @Inject + VanishBridgeHook vanishBridgeHook; @Override @@ -29,6 +33,6 @@ public boolean isEnabled() { public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) { super.applyToServerPing(serverPing); - serverPing.onlinePlayers(this.proxyServer.getPlayerCount()); + serverPing.onlinePlayers(this.vanishBridgeHook.hooked() ? this.vanishBridgeHook.unvanishedPlayerCount() : this.proxyServer.getPlayerCount()); } } diff --git a/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/PlayerListServerPingProcessor.java b/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/PlayerListServerPingProcessor.java index 951edfa..3078fb7 100644 --- a/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/PlayerListServerPingProcessor.java +++ b/src/main/java/voruti/velocityplayerlistquery/service/serverpingprocessor/PlayerListServerPingProcessor.java @@ -5,6 +5,7 @@ import com.velocitypowered.api.proxy.server.ServerPing; import lombok.NonNull; import lombok.experimental.FieldDefaults; +import voruti.velocityplayerlistquery.hook.VanishBridgeHook; import voruti.velocityplayerlistquery.model.Config; import voruti.velocityplayerlistquery.model.Config.PlayerListMode; import voruti.velocityplayerlistquery.service.ConfigService; @@ -33,6 +34,9 @@ public class PlayerListServerPingProcessor extends ServerPingProcessor { @Inject ServerListEntryBuilderService serverListEntryBuilderService; + + @Inject + VanishBridgeHook vanishBridgeHook; @Override @@ -51,7 +55,7 @@ public void applyToServerPing(@NonNull final ServerPing.Builder serverPing) { super.applyToServerPing(serverPing); // collect players: - final Collection players = this.proxyServer.getAllPlayers(); + final Collection players = this.vanishBridgeHook.hooked() ? this.vanishBridgeHook.unvanishedPlayers() : this.proxyServer.getAllPlayers(); final Stream playerStream = players.stream() // format players: .map(player -> new ServerPing.SamplePlayer(