From 9c1944c969886d375f10a85ffa954e8b3c4b19ba Mon Sep 17 00:00:00 2001 From: KeepBotting Date: Fri, 29 Mar 2019 18:36:07 -0400 Subject: [PATCH] Let ::pos return other players positions (#422) Rework ::pos to allow a user to return another player's position --- game/plugin/cmd/src/teleport-cmd.plugin.kts | 24 +++++++++++++++--- game/plugin/cmd/test/TeleportCommandTests.kt | 26 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/game/plugin/cmd/src/teleport-cmd.plugin.kts b/game/plugin/cmd/src/teleport-cmd.plugin.kts index 1aedd5b9..2ff9ad5d 100644 --- a/game/plugin/cmd/src/teleport-cmd.plugin.kts +++ b/game/plugin/cmd/src/teleport-cmd.plugin.kts @@ -1,20 +1,36 @@ import com.google.common.primitives.Ints import org.apollo.game.model.Position +import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.plugin.api.Position.component1 import org.apollo.game.plugin.api.Position.component2 import org.apollo.game.plugin.api.Position.component3 /** - * Sends the player's position. + * Sends a player's position. */ on_command("pos", PrivilegeLevel.MODERATOR) .then { player -> - val (x, y, z) = player.position - val region = player.position.regionCoordinates + val target: Player + val name: String - player.sendMessage("You are at: ($x, $y, $z) in region (${region.x}, ${region.y}).") + if (arguments.size >= 1) { + name = arguments.joinToString(" ") + if (player.world.isPlayerOnline(name)) { + target = player.world.getPlayer(name) + } else { + player.sendMessage("$name is offline.") + return@then + } + } else { + target = player + } + + val (x, y, z) = target.position + val region = target.position.regionCoordinates + + player.sendMessage("${target.username} is located at ($x, $y, $z) in region (${region.x}, ${region.y}).") } /** diff --git a/game/plugin/cmd/test/TeleportCommandTests.kt b/game/plugin/cmd/test/TeleportCommandTests.kt index 32da197a..2fffdf34 100644 --- a/game/plugin/cmd/test/TeleportCommandTests.kt +++ b/game/plugin/cmd/test/TeleportCommandTests.kt @@ -6,6 +6,7 @@ import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.plugin.testing.assertions.contains import org.apollo.game.plugin.testing.junit.ApolloTestingExtension +import org.apollo.game.plugin.testing.junit.api.annotations.Name import org.apollo.game.plugin.testing.junit.api.annotations.TestMock import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test @@ -22,6 +23,10 @@ class TeleportCommandTests { @TestMock lateinit var player: Player + @TestMock + @Name("player_two") + lateinit var player_two: Player + @Test fun `Teleport to given coordinates`() { player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR @@ -50,6 +55,27 @@ class TeleportCommandTests { } } + @Test + fun `Shows another players current position information`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + player_two.position = Position(1, 2, 3) + world.commandDispatcher.dispatch(player, Command("pos", arrayOf("player_two"))) + + verify { + player.sendMessage(contains("1, 2, 3")) + } + } + + @Test + fun `Shows no position information for a nonexistent player`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("pos", arrayOf("player999"))) + + verify { + player.sendMessage(contains("offline")) + } + } + @ParameterizedTest(name = "::tele {0}") @ValueSource(strings = [ "1 2 ",