Skip to content

Commit

Permalink
feat(velocity): remove vanished players from server ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Syrent committed Jun 18, 2024
1 parent 045441d commit 3b2b1fa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ description=A modular vanish system for Minecraft servers
website=https://sayandev.org
author=Syrent

version=1.1.1-SNAPSHOT
version=1.1.1
stickynoteVersion=1.1.9
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ open class SayanVanishAPI<U: User>(val type: KClass<out User>) {
return database.getUser(uniqueId, useCache, type)?.isVanished == true
}

fun getVanishedUsers(): Collection<U> {
return getUsers(User::isVanished)
fun getVanishedUsers(useCache: Boolean = true): Collection<U> {
return getUsers(useCache).filter { it.isVanished }
}

fun getOnlineUsers(): Collection<U> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sayandev.sayanvanish.velocity

import com.google.inject.Inject
import com.velocitypowered.api.event.PostOrder
import com.velocitypowered.api.event.Subscribe
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent
import com.velocitypowered.api.plugin.annotation.DataDirectory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sayandev.sayanvanish.velocity.feature.features

import com.velocitypowered.api.event.Subscribe
import com.velocitypowered.api.event.proxy.ProxyPingEvent
import com.velocitypowered.api.proxy.server.ServerPing
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.velocity.api.SayanVanishVelocityAPI
import org.sayandev.sayanvanish.velocity.feature.ListenedFeature
import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.ConfigSerializable
import kotlin.collections.count
import kotlin.collections.filter
import kotlin.collections.map
import kotlin.collections.toTypedArray
import kotlin.jvm.optionals.getOrNull

@RegisteredFeature
@ConfigSerializable
class FeatureOnlinePlayers : ListenedFeature("online_players") {

@Subscribe
fun onProxyPing(event: ProxyPingEvent) {
if (!isActive()) return
val pingPlayers = event.ping.players.getOrNull() ?: return
val onlineVanishedPlayers = SayanVanishVelocityAPI.getInstance().getVanishedUsers(false).filter { it.isOnline }
val nonVanishedPlayersCount = pingPlayers.online - onlineVanishedPlayers.count()
val nonVanishedPlayersSample = pingPlayers.sample.filter { !onlineVanishedPlayers.map { it.username }.contains(it.name) }
event.ping = event.ping
.asBuilder()
.onlinePlayers(nonVanishedPlayersCount)
.samplePlayers(*nonVanishedPlayersSample.map { ServerPing.SamplePlayer(it.name, it.id) }.toTypedArray())
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sayandev.sayanvanish.velocity.feature.hook
package org.sayandev.sayanvanish.velocity.feature.features.hook

import ir.syrent.enhancedvelocity.api.VanishHook
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sayandev.sayanvanish.velocity.feature.hook
package org.sayandev.sayanvanish.velocity.feature.features.hook

import com.velocitypowered.api.event.Subscribe
import net.william278.velocitab.api.VelocitabAPI
Expand Down

0 comments on commit 3b2b1fa

Please sign in to comment.