Skip to content

Commit

Permalink
custom /near, getpos
Browse files Browse the repository at this point in the history
  • Loading branch information
Gutin1 committed Jul 20, 2024
1 parent 919eae8 commit aea62f3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package net.horizonsend.ion.server.command.misc

import co.aikar.commands.annotation.CommandAlias
import co.aikar.commands.annotation.CommandCompletion
import net.horizonsend.ion.common.database.cache.nations.RelationCache
import net.horizonsend.ion.common.database.schema.nations.NationRelation
import net.horizonsend.ion.common.extensions.information
import net.horizonsend.ion.common.utils.miscellaneous.roundToHundredth
import net.horizonsend.ion.common.utils.text.colors.HEColorScheme.Companion.HE_DARK_GRAY
import net.horizonsend.ion.common.utils.text.colors.HEColorScheme.Companion.HE_LIGHT_BLUE
import net.horizonsend.ion.common.utils.text.colors.HEColorScheme.Companion.HE_LIGHT_GRAY
import net.horizonsend.ion.common.utils.text.colors.HEColorScheme.Companion.HE_MEDIUM_GRAY
import net.horizonsend.ion.common.utils.text.join
import net.horizonsend.ion.common.utils.text.ofChildren
import net.horizonsend.ion.server.IonServer
import net.horizonsend.ion.server.command.SLCommand
import net.horizonsend.ion.server.features.cache.PlayerCache
import net.kyori.adventure.text.Component.newline
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

object LocatorCommands : SLCommand() {
@CommandAlias("getpos")
@CommandCompletion("@players")
@Suppress("Unused")
fun onGetPos(sender: CommandSender, name: String) = asyncCommand(sender) {
val target = Bukkit.getPlayer(name) ?: fail { "Player $name not found!" }

var relation: NationRelation.Level = NationRelation.Level.NONE
var distance = 0.0

// All other conditions the sender is console / a command block
if (sender is Player) {
val targetNation = PlayerCache[target].nationOid

PlayerCache[sender].nationOid?.let {
if (targetNation != null) relation = RelationCache[it, targetNation]
}

failIf(sender.world != target.world) {
"You need to be closer to ${target.name} to do that!"
}

distance = sender.location.distance(target.location)

failIf(distance > IonServer.configuration.getPosMaxRange) {
"You need to be closer to ${target.name} to do that!"
}
}

sender.sendMessage(ofChildren(
text(target.name, relation.color), text("'s position", HE_MEDIUM_GRAY), newline(),
text("World: ", HE_LIGHT_GRAY), text(target.world.name, HE_LIGHT_BLUE), newline(),
text("X: ", HE_LIGHT_GRAY), text(target.location.blockX, HE_LIGHT_BLUE), newline(),
text("Y: ", HE_LIGHT_GRAY), text(target.location.blockY, HE_LIGHT_BLUE), newline(),
text("Z: ", HE_LIGHT_GRAY), text(target.location.blockZ, HE_LIGHT_BLUE), newline(),
text("Yaw: ", HE_LIGHT_GRAY), text(target.location.yaw.toDouble().roundToHundredth(), HE_LIGHT_BLUE), newline(),
text("Pitch: ", HE_LIGHT_GRAY), text(target.location.pitch.toDouble().roundToHundredth(), HE_LIGHT_BLUE), newline(),
text("Distance: ", HE_LIGHT_GRAY), text(distance.roundToHundredth(), HE_LIGHT_BLUE)
))
}

@CommandAlias("near")
@Suppress("Unused")
fun onNear(sender: Player) {
val players = sender.location.getNearbyPlayers(IonServer.configuration.nearMaxRange) {
it.uniqueId != sender.uniqueId
}.toList()

if (players.isEmpty()) {
sender.information("There are no nearby players")

return
}

val senderNation = PlayerCache[sender].nationOid

val body = players.mapNotNull { player ->
val cached = PlayerCache.getIfOnline(player) ?: return@mapNotNull null
val nation = cached.nationOid

var nameColor = NamedTextColor.WHITE

if (senderNation != null && nation != null) {
nameColor = RelationCache[senderNation, nation].color
}

val distance = player.location.distance(sender.location)

ofChildren(text(player.name, nameColor), text(": ", HE_DARK_GRAY), text(distance.roundToHundredth(), HE_LIGHT_GRAY))
}.join(separator = newline())

sender.sendMessage(ofChildren(text("Nearby Players:", HE_MEDIUM_GRAY), newline(), body))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ data class ServerConfiguration(
val mobSpawns: Map<String, PlanetSpawnConfig> = mapOf(),
val dutyModeMonitorWebhook: String? = null,
val eventLoggerWebhook: String? = null,
val getPosMaxRange: Double = 600.0,
val nearMaxRange: Double = 1200.0,
) {
/**
* @param baseAsteroidDensity: Roughly a base level of the number of asteroids per chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import net.horizonsend.ion.server.command.misc.GToggleCommand
import net.horizonsend.ion.server.command.misc.GlobalGameRuleCommand
import net.horizonsend.ion.server.command.misc.IonBroadcastCommand
import net.horizonsend.ion.server.command.misc.ListCommand
import net.horizonsend.ion.server.command.misc.LocatorCommands
import net.horizonsend.ion.server.command.misc.MultiblockCommand
import net.horizonsend.ion.server.command.misc.PlayerInfoCommand
import net.horizonsend.ion.server.command.misc.RegenerateCommand
Expand Down Expand Up @@ -165,5 +166,6 @@ val commands: List<SLCommand> = listOf(
BlockCommand,
ShipFactoryCommand,
SettingsCommand,
FleetCommand
FleetCommand,
LocatorCommands,
)

0 comments on commit aea62f3

Please sign in to comment.