Skip to content

Commit

Permalink
feat(api): ✨ add get_player_info
Browse files Browse the repository at this point in the history
  • Loading branch information
FYWinds committed May 7, 2023
1 parent 4661d76 commit 413dfd9
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

- get_available_api
- get_player_list
- get_player_info

### Events

Expand Down
10 changes: 3 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ tasks {
}

register("devDebug") {
dependsOn(build)
dependsOn(ktlintFormat).dependsOn(build)

doLast {
copy {
Expand All @@ -165,16 +165,12 @@ tasks {
}

configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
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",
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any>): 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"),
),
)
}
}
}
73 changes: 72 additions & 1 deletion src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NEntity.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
package fyi.fyw.mc.pluginnonebot.models

class NEntity
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<NEntity>?,
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<String>,
) {

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,
)
}
}
}
}
162 changes: 162 additions & 0 deletions src/main/kotlin/fyi/fyw/mc/pluginnonebot/models/NPlayer.kt
Original file line number Diff line number Diff line change
@@ -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<NEntity>?,
empty: Boolean,
insideVehicle: Boolean,

ticksLived: Int,
entityType: String,

customNameVisible: Boolean,
customName: String?,

invulnerable: Boolean,
scoreboardTags: Set<String>,
) : 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(),
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fyi.fyw.mc.pluginnonebot.models.api

import fyi.fyw.mc.pluginnonebot.models.NPlayer

class ResultGetPlayerInfo(
val player: NPlayer,
) : BaseApiResult
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,5 +94,6 @@ object ApiHandlerRegistry : Registry<ApiHandler> {
fun init() {
this.register(HandlerGetAvailableApi())
this.register(HandlerGetPlayerList())
this.register(HandlerGetPlayerInfo())
}
}

0 comments on commit 413dfd9

Please sign in to comment.