From 3616753d7fabcc70b595e552c680605825b8583e Mon Sep 17 00:00:00 2001 From: Cube Date: Sun, 4 Jun 2017 21:11:37 +0300 Subject: [PATCH] Convert npc spawn commands to Kotlin --- game/src/plugins/cmd/src/spawn-cmd.plugin.kts | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 game/src/plugins/cmd/src/spawn-cmd.plugin.kts diff --git a/game/src/plugins/cmd/src/spawn-cmd.plugin.kts b/game/src/plugins/cmd/src/spawn-cmd.plugin.kts new file mode 100644 index 00000000..7f124d89 --- /dev/null +++ b/game/src/plugins/cmd/src/spawn-cmd.plugin.kts @@ -0,0 +1,110 @@ +import com.google.common.primitives.Ints +import org.apollo.game.model.Position +import org.apollo.game.model.entity.Npc +import org.apollo.game.model.entity.setting.PrivilegeLevel + +/** + * An array of npcs that cannot be spawned. + */ +val blacklist: IntArray = intArrayOf() + +/** + * Spawns a non-blacklisted npc in the specified position, or the player's position if both 'x' and + * 'y' are not supplied. + */ +on_command("spawn", PrivilegeLevel.ADMINISTRATOR) + .then { player -> + val invalidSyntax = "Invalid syntax - ::spawn [npc id] [optional-x] [optional-y] [optional-z]" + if (arguments.size !in intArrayOf(1, 3, 4)) { + player.sendMessage(invalidSyntax) + return@then + } + + val id = Ints.tryParse(arguments[0]) + if (id == null) { + player.sendMessage(invalidSyntax) + return@then + } + + if (id in blacklist) { + player.sendMessage("Sorry, npc $id is blacklisted!") + return@then + } + + val position: Position? + if (arguments.size == 1) { + position = player.position + } else { + var height = player.position.height + if (arguments.size == 4) { + val h = Ints.tryParse(arguments[3]) + if (h == null) { + player.sendMessage(invalidSyntax) + return@then + } + height = h + } + position = Position(arguments[1].toInt(), arguments[2].toInt(), height) + } + + player.world.register(Npc(player.world, id, position)) + } + +/** + * Mass spawns npcs around the player. + */ +on_command("mass", PrivilegeLevel.ADMINISTRATOR) + .then { player -> + val invalidSyntax = "Invalid syntax - ::mass [npc id] [range (1-5)]" + if (arguments.size != 2) { + player.sendMessage(invalidSyntax) + return@then + } + + val id = Ints.tryParse(arguments[0]) + if (id == null) { + player.sendMessage(invalidSyntax) + return@then + } + + val range = Ints.tryParse(arguments[1]) + if (range == null) { + player.sendMessage(invalidSyntax) + return@then + } + + if (id < 0 || range !in 1..5) { + player.sendMessage(invalidSyntax) + return@then + } + + if (id in blacklist) { + player.sendMessage("Sorry, npc $id is blacklisted!") + return@then + } + + val centerPosition = player.position + + val minX = centerPosition.x - range + val minY = centerPosition.y - range + val maxX = centerPosition.x + range + val maxY = centerPosition.y + range + val z = centerPosition.height + + for (x in minX..maxX) { + for (y in minY..maxY) { + player.world.register(Npc(player.world, id, Position(x, y, z))) + } + } + + player.sendMessage("Mass spawning npcs with id $id.") + } + +/** + * Unregisters all npcs from the world npc repository. + */ +on_command("clearnpcs", PrivilegeLevel.ADMINISTRATOR) + .then { player -> + player.world.npcRepository.forEach { npc -> player.world.unregister(npc) } + player.sendMessage("Unregistered all npcs from the world.") + } \ No newline at end of file