mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 13:31:32 +00:00
Reform dependency structure.
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
plugins {
|
||||
id("gay.pizza.foundation.concrete-library")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly(project(":foundation-shared"))
|
||||
}
|
||||
|
@ -1,74 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
|
||||
import org.bukkit.advancement.Advancement
|
||||
import java.lang.reflect.Field
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
private fun Advancement.getInternalHandle(): Any =
|
||||
javaClass.getMethod("getHandle").invoke(this)
|
||||
|
||||
private fun Class<*>.getDeclaredFieldAccessible(name: String): Field {
|
||||
val field = getDeclaredField(name)
|
||||
if (!field.trySetAccessible()) {
|
||||
throw RuntimeException("Failed to set reflection permissions to accessible.")
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
private fun Advancement.getInternalAdvancementDisplay(handle: Any = getInternalHandle()): Any? =
|
||||
handle.javaClass.methods.firstOrNull {
|
||||
it.returnType.simpleName == "AdvancementDisplay" &&
|
||||
it.parameterCount == 0
|
||||
}?.invoke(handle) ?: handle.javaClass.getDeclaredFieldAccessible("c").get(handle)
|
||||
|
||||
private fun Advancement.displayTitleText(): String? {
|
||||
val handle = getInternalHandle()
|
||||
val advancementDisplay = getInternalAdvancementDisplay(handle) ?: return null
|
||||
try {
|
||||
val field = advancementDisplay.javaClass.getDeclaredField("a")
|
||||
field.trySetAccessible()
|
||||
val message = field.get(advancementDisplay)
|
||||
val title = message.javaClass.getMethod("getString").invoke(message)
|
||||
return title.toString()
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
|
||||
val titleComponentField = advancementDisplay.javaClass.declaredFields.firstOrNull {
|
||||
it.type.simpleName == "IChatBaseComponent"
|
||||
}
|
||||
|
||||
if (titleComponentField != null) {
|
||||
titleComponentField.trySetAccessible()
|
||||
val titleChatBaseComponent = titleComponentField.get(advancementDisplay)
|
||||
val title = titleChatBaseComponent.javaClass.getMethod("getText").invoke(titleChatBaseComponent).toString()
|
||||
if (title.isNotBlank()) {
|
||||
return title
|
||||
}
|
||||
|
||||
val chatSerializerClass = titleChatBaseComponent.javaClass.declaredClasses.firstOrNull {
|
||||
it.simpleName == "ChatSerializer"
|
||||
}
|
||||
|
||||
if (chatSerializerClass != null) {
|
||||
val componentJson = chatSerializerClass
|
||||
.getMethod("a", titleChatBaseComponent.javaClass)
|
||||
.invoke(null, titleChatBaseComponent).toString()
|
||||
val gson = GsonComponentSerializer.gson().deserialize(componentJson)
|
||||
return LegacyComponentSerializer.legacySection().serialize(gson)
|
||||
}
|
||||
}
|
||||
|
||||
val rawAdvancementName = key.key
|
||||
return rawAdvancementName.substring(rawAdvancementName.lastIndexOf("/") + 1)
|
||||
.lowercase().split("_")
|
||||
.joinToString(" ") { it.substring(0, 1).uppercase() + it.substring(1) }
|
||||
}
|
||||
|
||||
object AdvancementTitleCache {
|
||||
private val cache = ConcurrentHashMap<Advancement, String?>()
|
||||
|
||||
fun of(advancement: Advancement): String? =
|
||||
cache.computeIfAbsent(advancement) { it.displayTitleText() }
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
fun Player.chat(vararg messages: String): Unit = messages.forEach { message ->
|
||||
chat(message)
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
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)
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
import gay.pizza.foundation.shared.IFoundationCore
|
||||
import org.bukkit.Server
|
||||
|
||||
object FoundationCoreLoader {
|
||||
fun get(server: Server): IFoundationCore {
|
||||
return server.pluginManager.getPlugin("Foundation") as IFoundationCore?
|
||||
?: throw RuntimeException("Foundation Core is not loaded!")
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
object Platform {
|
||||
private val os: String? = System.getProperty("os.name")
|
||||
|
||||
fun isWindows(): Boolean = os != null && os.lowercase().startsWith("windows")
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
annotation class PluginMainClass
|
@ -1,28 +0,0 @@
|
||||
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 }
|
@ -1,6 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
enum class SortOrder {
|
||||
Ascending,
|
||||
Descending
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package gay.pizza.foundation.common
|
||||
|
||||
import org.bukkit.Location
|
||||
import org.bukkit.World
|
||||
import org.bukkit.entity.Entity
|
||||
import org.bukkit.entity.Player
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun <T: Entity> World.spawn(location: Location, clazz: KClass<T>): T = spawn(location, clazz.java)
|
||||
|
||||
fun <T: Entity> Player.spawn(clazz: KClass<T>): T = spawn(clazz.java)
|
||||
fun <T: Entity> Player.spawn(clazz: Class<T>): T = world.spawn(location, clazz)
|
Reference in New Issue
Block a user