Skip to content

Commit

Permalink
Merge pull request #22
Browse files Browse the repository at this point in the history
Add vanish placeholders
  • Loading branch information
Syrent authored Nov 5, 2022
2 parents 389fd65 + ab7b04b commit 7c24cbf
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=3.4.0
version=3.5.0
plugin-name=VelocityVanish
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class VelocityVanishSpigot : RUoMPlugin() {
lateinit var vanishManager: VanishManager
private set

val proxyPlayers = mutableMapOf<String, List<String>>()
val vanishedNames = mutableSetOf<String>()

override fun onEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class BukkitBridgeManager(
plugin.vanishedNames.clear()
plugin.vanishedNames.addAll(vanishedPlayers.map { it.asString })
}
"Players" -> {
val servers = messageJson["servers"].asJsonObject
plugin.proxyPlayers.clear()
for (server in servers.keySet()) {
plugin.proxyPlayers[server] = servers[server].asJsonArray.map { it.asString }.toList()
}
}
else -> {
Ruom.warn("Unsupported plugin message received from internal channel: $type")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ir.syrent.velocityvanish.spigot.hook

import ir.syrent.velocityvanish.spigot.VelocityVanishSpigot
import ir.syrent.velocityvanish.spigot.storage.Settings

object DependencyManager {
Expand Down Expand Up @@ -28,7 +29,7 @@ object DependencyManager {
this.register()
proCosmeticsHook = this
}
PlaceholderAPIHook("PlaceholderAPI").apply {
PlaceholderAPIHook(VelocityVanishSpigot.instance, "PlaceholderAPI").apply {
this.register()
placeholderAPIHook = this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
package ir.syrent.velocityvanish.spigot.hook

class PlaceholderAPIHook constructor(name: String) : Dependency(name) {
import ir.syrent.velocityvanish.spigot.VelocityVanishSpigot
import ir.syrent.velocityvanish.spigot.ruom.Ruom
import ir.syrent.velocityvanish.spigot.storage.Settings
import me.clip.placeholderapi.expansion.PlaceholderExpansion
import org.bukkit.OfflinePlayer

class PlaceholderAPIHook constructor(plugin: VelocityVanishSpigot, name: String) : Dependency(name) {

init {
if (exists) {
VanishExpansion(plugin).register()
}
}

override fun features(): List<String> {
return mutableListOf(
"Access to all placeholders in all plugin messages."
"Access to all placeholders in all plugin messages.",
"Add plugin placeholders like %velocityvanish_online_total% to PlaceholderAPI."
)
}

class VanishExpansion(
private val plugin: VelocityVanishSpigot
) : PlaceholderExpansion() {
override fun getIdentifier(): String {
return Ruom.getPlugin().description.name.lowercase()
}

override fun getAuthor(): String {
return Ruom.getPlugin().description.authors.joinToString(", ")
}

override fun getVersion(): String {
return Ruom.getPlugin().description.version
}

override fun persist(): Boolean {
return true
}

override fun canRegister(): Boolean {
return true
}

override fun onRequest(player: OfflinePlayer, params: String): String? {
if (params.equals("vanished", true)) {
return if (plugin.vanishedNames.contains(player.name)) "true" else "false"
}

if (params.equals("count", true)) {
return plugin.vanishedNames.size.toString()
}

if (params.startsWith("online_")) {
val type = params.substring(7)

return if (type.equals("here", true)) {
Ruom.getOnlinePlayers().filter { !plugin.vanishedNames.contains(it.name) }.size.toString()
} else if (type.equals("total", true)) {
val players = mutableListOf<String>()
for (serverPlayers in plugin.proxyPlayers.values.filter { it.isNotEmpty() }) {
players.addAll(serverPlayers)
}
players.filter { !plugin.vanishedNames.contains(it) }.size.toString()
} else {
if (!Settings.velocitySupport) {
warning("Plugin trying to get online players from other servers but Velocity support is disabled.")
Ruom.getOnlinePlayers().filter { !plugin.vanishedNames.contains(it.name) }.size.toString()
} else {
(plugin.proxyPlayers[type.lowercase()]?.filter { !plugin.vanishedNames.contains(it) }?.size ?: 0).toString()
}
}
}

return null
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ class VelocityVanish @Inject constructor(
VRuom.runAsync({
for (registeredServer in getServer().allServers) {
if (registeredServer.playersConnected.isNotEmpty()) {
for (player in registeredServer.playersConnected) {
bridgeManager.sendVanishedPlayers(registeredServer)
}
bridgeManager.sendVanishedPlayers(registeredServer)
}
}
bridgeManager.sendProxyPlayers()
}, 0, TimeUnit.SECONDS, 1, TimeUnit.SECONDS)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ class VelocityBridgeManager(
private val bridge: VelocityBridge
) {

fun sendVanishedPlayers(name: String) {
val messageJson = JsonObject()
messageJson.addProperty("type", "Vanish")
messageJson.addProperty("name", name)

sendPluginMessage(messageJson)
}

fun sendVanishedPlayers(server: RegisteredServer) {
val messageJson = JsonObject()
messageJson.addProperty("type", "Vanish")
Expand All @@ -30,6 +22,19 @@ class VelocityBridgeManager(
sendPluginMessage(messageJson, server)
}

fun sendProxyPlayers() {
val messageJson = JsonObject()
messageJson.addProperty("type", "Players")

val jsonObject = JsonObject()
for (registeredServer in VRuom.getServer().allServers) {
jsonObject.add(registeredServer.serverInfo.name.lowercase(), GsonUtils.get().toJsonTree(registeredServer.playersConnected.map { it.username }).asJsonArray)
messageJson.add("servers", jsonObject)

sendPluginMessage(messageJson)
}
}

private fun sendPluginMessage(messageJson: JsonObject) {
val byteArrayInputStream = ByteStreams.newDataOutput()
byteArrayInputStream.writeUTF(GsonUtils.get().toJson(messageJson))
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/velocity-plugin.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":"velocityvanish","name":"VelocityVanish","version":"3.4.0","description":"Vanish plugin for velocity servers","url":"syrent.ir","authors":["Syrent"],"dependencies":[],"main":"ir.syrent.velocityvanish.velocity.VelocityVanish"}
{"id":"velocityvanish","name":"VelocityVanish","version":"3.5.0","description":"Vanish plugin for velocity servers","url":"syrent.ir","authors":["Syrent"],"dependencies":[],"main":"ir.syrent.velocityvanish.velocity.VelocityVanish"}

0 comments on commit 7c24cbf

Please sign in to comment.