Move some code to common-plugin.

This commit is contained in:
2023-02-05 19:04:38 -08:00
parent ef6daa8467
commit 6803a456b3
10 changed files with 28 additions and 15 deletions

View File

@ -0,0 +1,7 @@
package gay.pizza.foundation.common
import org.bukkit.entity.Player
fun Player.chat(vararg messages: String): Unit = messages.forEach { message ->
chat(message)
}

View File

@ -0,0 +1,8 @@
package gay.pizza.foundation.common
fun <T, R : Comparable<R>> Collection<T>.sortedBy(order: SortOrder, selector: (T) -> R?): List<T> =
if (order == SortOrder.Ascending) {
sortedBy(selector)
} else {
sortedByDescending(selector)
}

View File

@ -0,0 +1,7 @@
package gay.pizza.foundation.common
object Platform {
private val os: String? = System.getProperty("os.name")
fun isWindows(): Boolean = os != null && os.lowercase().startsWith("windows")
}

View File

@ -0,0 +1,28 @@
package gay.pizza.foundation.common
import org.bukkit.Material
import org.bukkit.OfflinePlayer
import org.bukkit.Server
import org.bukkit.Statistic
import org.bukkit.entity.EntityType
val Server.allPlayers: List<OfflinePlayer>
get() = listOf(onlinePlayers, offlinePlayers.filter { !isPlayerOnline(it) }.toList()).flatten()
fun Server.isPlayerOnline(player: OfflinePlayer) =
onlinePlayers.any { onlinePlayer -> onlinePlayer.name == player.name }
fun Server.allPlayerStatisticsOf(
statistic: Statistic,
material: Material? = null,
entityType: EntityType? = null,
order: SortOrder = SortOrder.Ascending
) = allPlayers.map { player ->
player to if (material != null) {
player.getStatistic(statistic, material)
} else if (entityType != null) {
player.getStatistic(statistic, entityType)
} else {
player.getStatistic(statistic)
}
}.sortedBy(order) { it.second }

View File

@ -0,0 +1,6 @@
package gay.pizza.foundation.common
enum class SortOrder {
Ascending,
Descending
}