diff --git a/README.md b/README.md index 752e95e..8cfaa7a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ - get_available_api - get_player_list +- get_player_info ### Events diff --git a/build.gradle.kts b/build.gradle.kts index 6b098f0..37a9509 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -152,7 +152,7 @@ tasks { } register("devDebug") { - dependsOn(build) + dependsOn(ktlintFormat).dependsOn(build) doLast { copy { @@ -165,16 +165,12 @@ tasks { } configure { - version.set("0.47.1") + version.set("0.48.2") verbose.set(true) outputToConsole.set(true) - enableExperimentalRules.set(false) + enableExperimentalRules.set(true) disabledRules.set( setOf( - "final-newline", - "trailing-comma-on-call-site", - "trailing-comma-on-declaration-site", - "no-consecutive-blank-lines", "no-wildcard-imports", ), ) diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/api/internal/HandlerGetPlayerInfo.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/api/internal/HandlerGetPlayerInfo.kt new file mode 100644 index 0000000..7804e45 --- /dev/null +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/api/internal/HandlerGetPlayerInfo.kt @@ -0,0 +1,29 @@ +package fyi.fyw.mc.pluginnonebot.api.internal + +import fyi.fyw.mc.pluginnonebot.api.ApiHandler +import fyi.fyw.mc.pluginnonebot.models.NPlayer +import fyi.fyw.mc.pluginnonebot.models.api.BaseApiResult +import fyi.fyw.mc.pluginnonebot.models.api.ResultGetPlayerInfo +import org.bukkit.Bukkit +import java.util.* + +class HandlerGetPlayerInfo : ApiHandler { + override val id: String = "get_player_info" + private val uuidPattern: Regex = Regex("^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$") + override fun handle(params: Map): BaseApiResult { + val playerId = params.getOrDefault("player_id", "") as String + return if (uuidPattern.matches(playerId)) { + ResultGetPlayerInfo( + NPlayer.from( + Bukkit.getPlayer(UUID.fromString(playerId)) ?: throw Exception("Player not found online"), + ), + ) + } else { + ResultGetPlayerInfo( + NPlayer.from( + Bukkit.getPlayer(playerId) ?: throw Exception("Player not found online"), + ), + ) + } + } +} diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NEntity.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NEntity.kt index 670f3ce..8b483d5 100644 --- a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NEntity.kt +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NEntity.kt @@ -1,3 +1,74 @@ package fyi.fyw.mc.pluginnonebot.models -class NEntity \ No newline at end of file +import com.google.gson.annotations.SerializedName +import org.bukkit.entity.Entity +import org.bukkit.util.Vector + +open class NEntity( + val velocity: NVelocity, + val location: NLocation, + val height: Double, + val width: Double, + @SerializedName("on_ground") val onGround: Boolean, + @SerializedName("in_water") val inWater: Boolean, + @SerializedName("entity_id") val entityId: Int, + @SerializedName("fire_ticks") val fireTicks: Int, + @SerializedName("max_fire_ticks") val maxFireTicks: Int, + + val vehicle: NEntity?, + val passengers: List?, + val empty: Boolean, + @SerializedName("inside_vehicle") val insideVehicle: Boolean, + + @SerializedName("ticks_lived") val ticksLived: Int, + @SerializedName("entity_type") val entityType: String, + + @SerializedName("custom_name_visible") val customNameVisible: Boolean, + @SerializedName("custom_name") val customName: String?, + + val invulnerable: Boolean, + @SerializedName("scoreboard_tags") val scoreboardTags: Set, +) { + + companion object { + fun from(entity: Entity): NEntity { + return NEntity( + NVelocity.from(entity.velocity), + NLocation.from(entity.location), + entity.height, + entity.width, + entity.isOnGround, + entity.isInWater, + entity.entityId, + entity.fireTicks, + entity.maxFireTicks, + entity.vehicle?.let { NEntity.from(it) }, + entity.passengers.map { NEntity.from(it) }, + entity.isEmpty, + entity.isInsideVehicle, + entity.ticksLived, + entity.type.name, + entity.isCustomNameVisible, + entity.customName, + entity.isInvulnerable, + entity.scoreboardTags, + ) + } + } + + class NVelocity( + val x: Double, + val y: Double, + val z: Double, + ) { + companion object { + fun from(velocity: Vector): NVelocity { + return NVelocity( + velocity.x, + velocity.y, + velocity.z, + ) + } + } + } +} diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NPlayer.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NPlayer.kt new file mode 100644 index 0000000..7754c03 --- /dev/null +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NPlayer.kt @@ -0,0 +1,162 @@ +package fyi.fyw.mc.pluginnonebot.models + +import com.google.gson.annotations.SerializedName +import org.bukkit.OfflinePlayer +import org.bukkit.entity.Player + +class NPlayer( + @SerializedName("display_name") val displayName: String, + @SerializedName("player_list_name") val playerListName: String, + @SerializedName("player_list_header") val playerListHeader: String, + @SerializedName("player_list_footer") val playerListFooter: String, + + val address: String, + val ping: Int, + val locale: String, + + val sneaking: Boolean, + val sprinting: Boolean, + val flying: Boolean, + + @SerializedName("bed_spawn_location") val bedSpawnLocation: NLocation?, + + val exp: Float, + val level: Int, + @SerializedName("total_experience") val totalExperience: Int, + + velocity: NVelocity, + location: NLocation, + height: Double, + width: Double, + onGround: Boolean, + inWater: Boolean, + entityId: Int, + fireTicks: Int, + maxFireTicks: Int, + + vehicle: NEntity?, + passengers: List?, + empty: Boolean, + insideVehicle: Boolean, + + ticksLived: Int, + entityType: String, + + customNameVisible: Boolean, + customName: String?, + + invulnerable: Boolean, + scoreboardTags: Set, +) : NEntity( + velocity, + location, + height, + width, + onGround, + inWater, + entityId, + fireTicks, + maxFireTicks, + + vehicle, + passengers, + empty, + insideVehicle, + + ticksLived, + entityType, + + customNameVisible, + customName, + + invulnerable, + scoreboardTags, +) { + companion object { + fun from(player: OfflinePlayer): NPlayer { + return when (player) { + is Player -> NPlayer( + player.displayName, + player.playerListName, + player.playerListHeader ?: "", + player.playerListFooter ?: "", + + player.address.toString(), + player.ping, + player.locale, + + player.isSneaking, + player.isSprinting, + player.isFlying, + + player.bedSpawnLocation?.let { NLocation.from(it) }, + + player.exp, + player.level, + player.totalExperience, + + NVelocity.from(player.velocity), + NLocation.from(player.location), + player.height, + player.width, + player.isOnGround, + player.isInWater, + player.entityId, + player.fireTicks, + player.maxFireTicks, + player.vehicle?.let { NEntity.from(it) }, + player.passengers.map { NEntity.from(it) }, + player.isEmpty, + player.isInsideVehicle, + player.ticksLived, + player.type.name, + player.isCustomNameVisible, + player.customName, + player.isInvulnerable, + player.scoreboardTags, + ) + + else -> NPlayer( + player.name ?: "", + player.name ?: "", + "", + "", + + "", + 0, + "", + + false, + false, + false, + + null, + + 0f, + 0, + 0, + + NVelocity(0.0, 0.0, 0.0), + NLocation("Unknown World", 0.0, 0.0, 0.0, 0.0f, 0.0f), + 0.0, + 0.0, + false, + false, + 0, + 0, + 0, + null, + null, + true, + false, + 0, + "", + false, + null, + false, + emptySet(), + ) + } + } + } +} diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/api/ResultGetPlayerInfo.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/api/ResultGetPlayerInfo.kt new file mode 100644 index 0000000..3fe1c3d --- /dev/null +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/api/ResultGetPlayerInfo.kt @@ -0,0 +1,7 @@ +package fyi.fyw.mc.pluginnonebot.models.api + +import fyi.fyw.mc.pluginnonebot.models.NPlayer + +class ResultGetPlayerInfo( + val player: NPlayer, +) : BaseApiResult diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/event/BaseEventFrame.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/event/BaseEventFrame.kt index 93b5f40..c048460 100644 --- a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/event/BaseEventFrame.kt +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/event/BaseEventFrame.kt @@ -6,7 +6,7 @@ import java.util.* class BaseEventFrame( val id: String = UUID.randomUUID().toString(), // Version 4 UUID val time: Long = System.currentTimeMillis(), // Unix timestamp - val data: BaseEvent? = null, + val data: BaseEvent, val type: String, @SerializedName("detail_type") val detailType: String, @SerializedName("sub_type") val subType: String = "", diff --git a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/registry/ApiHandlerRegistry.kt b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/registry/ApiHandlerRegistry.kt index 720ce75..1b4e0af 100644 --- a/src/main/kotlin/fyi/fyw/mc/pluginnonebot/registry/ApiHandlerRegistry.kt +++ b/src/main/kotlin/fyi/fyw/mc/pluginnonebot/registry/ApiHandlerRegistry.kt @@ -3,6 +3,7 @@ package fyi.fyw.mc.pluginnonebot.registry import fyi.fyw.mc.pluginnonebot.PluginNonebot import fyi.fyw.mc.pluginnonebot.api.ApiHandler import fyi.fyw.mc.pluginnonebot.api.internal.HandlerGetAvailableApi +import fyi.fyw.mc.pluginnonebot.api.internal.HandlerGetPlayerInfo import fyi.fyw.mc.pluginnonebot.api.internal.HandlerGetPlayerList import fyi.fyw.mc.pluginnonebot.models.api.BaseApiResultFrame import org.java_websocket.WebSocket @@ -93,5 +94,6 @@ object ApiHandlerRegistry : Registry { fun init() { this.register(HandlerGetAvailableApi()) this.register(HandlerGetPlayerList()) + this.register(HandlerGetPlayerInfo()) } }