Skip to content

Commit

Permalink
Update to support team name for all new placeholders
Browse files Browse the repository at this point in the history
This update not only adds the `displayname` parameter but also adds support for passing a team name to any placeholder.

Placeholders are now also auto generated and will show all teams if using papi parse
  • Loading branch information
Groovin-Dev committed Apr 25, 2024
1 parent 1d46afd commit 984ffa4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ Adds `6` new PlaceholderAPI expansions:
- %team_prefix%
- %team_suffix%
- %team_count%
- %team_count_\<team\>%
- %team_displayname%
- %team_name%

All placeholders accept an optional team name parameter,
for example `%team_color_test%` will return the color of the team named `test`.

The team name parameter takes precedence over the player's team.

# How to build

1. Clone the repository
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official

group=dev.groovin
version=1.0.2
version=2.0.0
plugin_main=dev.groovin.teamsexpansion.Plugin
66 changes: 42 additions & 24 deletions src/main/kotlin/dev/groovin/teamsexpansion/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,56 @@ package dev.groovin.teamsexpansion
import me.clip.placeholderapi.expansion.PlaceholderExpansion
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.OfflinePlayer
import org.bukkit.scoreboard.Scoreboard
import org.bukkit.scoreboard.Team

// I added this because ChatColor is deprecated but this code still works
const val COLOR_CHAR = '\u00A7'

class TeamsExpansion : PlaceholderExpansion() {
override fun canRegister(): Boolean = true
override fun getName(): String = "TeamsExpansion"
override fun getIdentifier(): String = "team"
override fun getAuthor(): String = "Groovin-Dev"
override fun getVersion(): String = javaClass.getResource("/version.txt")?.readText() ?: "Unknown"
override fun getPlaceholders(): List<String> = listOf("color", "prefix", "suffix", "name", "count", "count_<team>").map { "%${identifier}_$it%" }

override fun onPlaceholderRequest(player: Player?, params: String): String {
val teamName = params.substringAfter("_")
val team = if (params.startsWith("count_")) {
scoreboard.getTeam(teamName)
} else {
player?.let { scoreboard.getEntryTeam(it.name) } ?: scoreboard.getTeam(teamName)
override fun getPlaceholders(): List<String> = generatePlaceholders()

override fun onRequest(player: OfflinePlayer?, params: String): String {
val parts = params.split("_", limit = 2)
val placeholder = parts.getOrNull(0).orEmpty()
val teamName = parts.getOrNull(1).orEmpty()

val team = getTeam(player, teamName)

return when (placeholder) {
"color" -> team?.getTeamColor().orEmpty()
"prefix" -> team?.getTeamPrefix().orEmpty()
"suffix" -> team?.getTeamSuffix().orEmpty()
"count" -> team?.getTeamCount().orEmpty()
"name" -> team?.name.orEmpty()
"displayname" -> team?.getDisplayName.orEmpty()
else -> ""
}
}

if (team == null) {
return ""
private fun generatePlaceholders(): List<String> {
val placeholderTypes = listOf("color", "prefix", "suffix", "count", "name", "displayname")
val teams = scoreboard.teams.map { it.name }

val placeholders = mutableListOf<String>()
for (placeholderType in placeholderTypes) {
placeholders.add("%${identifier}_$placeholderType%")
for (team in teams) {
placeholders.add("%${identifier}_${placeholderType}_$team%")
}
}
return placeholders
}

private fun getTeam(player: OfflinePlayer?, teamName: String): Team? {
return when {
params.startsWith("color") -> team.getTeamColor()
params.startsWith("prefix") -> team.getTeamPrefix()
params.startsWith("suffix") -> team.getTeamSuffix()
params == "count" -> team.getTeamCount()
params.startsWith("count_") -> team.getTeamCount()
params == "name" -> team.name
else -> ""
teamName.isNotEmpty() -> scoreboard.getTeam(teamName)
player != null -> player.name?.let { scoreboard.getEntryTeam(it) }
else -> null
}
}

Expand All @@ -46,10 +61,13 @@ class TeamsExpansion : PlaceholderExpansion() {
}
}

private fun Team.getTeamColor(): String = runCatching { color().toString() }.getOrDefault("")
private fun Team.getTeamColor(): String? = runCatching { color().toString() }.getOrNull()

private fun Team.getTeamPrefix(): String = prefix().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) }

private fun Team.getTeamPrefix(): String = LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(prefix())
private fun Team.getTeamSuffix(): String = suffix().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) }

private fun Team.getTeamSuffix(): String = LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(suffix())
private fun Team.getTeamCount(): String = entries.size.toString()

private fun Team.getTeamCount(): String = entries.size.toString()
private val Team.getDisplayName: String
get() = displayName().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) }

0 comments on commit 984ffa4

Please sign in to comment.