Add input type checking to commands

This commit is contained in:
Cube
2017-06-15 23:39:47 +03:00
parent 4ca8978ff3
commit c082f407ad
5 changed files with 101 additions and 19 deletions
@@ -1,9 +1,17 @@
import com.google.common.primitives.Ints
import org.apollo.game.model.Animation
import org.apollo.game.model.entity.setting.PrivilegeLevel
on_command("animate", PrivilegeLevel.MODERATOR)
.then { player ->
if(valid_arg_length(arguments, 1, player, "Invalid syntax - ::animate [animation-id]")) {
player.playAnimation(Animation(arguments[0].toInt()))
val invalidSyntax = "Invalid syntax - ::animate [animation-id]"
if(valid_arg_length(arguments, 1, player, invalidSyntax)) {
val id = Ints.tryParse(arguments[0])
if (id == null) {
player.sendMessage(invalidSyntax)
return@then
}
player.playAnimation(Animation(id))
}
}
+18 -3
View File
@@ -1,14 +1,29 @@
import com.google.common.primitives.Ints
import org.apollo.cache.def.ItemDefinition
import org.apollo.game.model.entity.setting.PrivilegeLevel
on_command("item", PrivilegeLevel.ADMINISTRATOR)
.then { player ->
if (!valid_arg_length(arguments, 1..2, player, "Invalid syntax - ::item [id] [amount]")) {
val invalidSyntax = "Invalid syntax - ::item [id] [amount]"
if (!valid_arg_length(arguments, 1..2, player, invalidSyntax)) {
return@then
}
val id = arguments[0].toInt()
val amount = if (arguments.size == 2) arguments[1].toInt() else 1
val id = Ints.tryParse(arguments[0])
if (id == null) {
player.sendMessage(invalidSyntax)
return@then
}
var amount = 1
if (arguments.size == 2) {
val amt = Ints.tryParse(arguments[1])
if (amt == null) {
player.sendMessage(invalidSyntax)
return@then
}
amount = amt
}
if (id < 0 || id >= ItemDefinition.count()) {
player.sendMessage("The item id you specified is out of bounds!")
+25 -6
View File
@@ -1,3 +1,4 @@
import com.google.common.primitives.Ints
import org.apollo.cache.def.ItemDefinition
import org.apollo.cache.def.NpcDefinition
import org.apollo.cache.def.ObjectDefinition
@@ -5,11 +6,17 @@ import org.apollo.game.model.entity.setting.PrivilegeLevel
on_command("iteminfo", PrivilegeLevel.ADMINISTRATOR)
.then { player ->
if (!valid_arg_length(arguments, 1, player, "Invalid syntax - ::npcinfo [npc id]")) {
val invalidSyntax = "Invalid syntax - ::npcinfo [npc id]"
if (!valid_arg_length(arguments, 1, player, invalidSyntax)) {
return@then
}
val id = Ints.tryParse(arguments[0])
if (id == null) {
player.sendMessage(invalidSyntax)
return@then
}
val id = arguments[0].toInt()
val definition = ItemDefinition.lookup(id)
val members = if (definition.isMembersOnly) "members" else "not members"
@@ -20,11 +27,17 @@ on_command("iteminfo", PrivilegeLevel.ADMINISTRATOR)
on_command("npcinfo", PrivilegeLevel.ADMINISTRATOR)
.then { player ->
if (!valid_arg_length(arguments, 1, player, "Invalid syntax - ::npcinfo [npc id]")) {
val invalidSyntax = "Invalid syntax - ::npcinfo [npc id]"
if (!valid_arg_length(arguments, 1, player, invalidSyntax)) {
return@then
}
val id = Ints.tryParse(arguments[0])
if (id == null) {
player.sendMessage(invalidSyntax)
return@then
}
val id = arguments[0].toInt()
val definition = NpcDefinition.lookup(id)
val isCombative = if (definition.hasCombatLevel()) "has a combat level of ${definition.combatLevel}" else
"does not have a combat level"
@@ -35,11 +48,17 @@ on_command("npcinfo", PrivilegeLevel.ADMINISTRATOR)
on_command("objectinfo", PrivilegeLevel.ADMINISTRATOR)
.then { player ->
if (!valid_arg_length(arguments, 1, player, "Invalid syntax - ::objectinfo [object id]")) {
val invalidSyntax = "Invalid syntax - ::objectinfo [object id]"
if (!valid_arg_length(arguments, 1, player, invalidSyntax)) {
return@then
}
val id = Ints.tryParse(arguments[0])
if (id == null) {
player.sendMessage(invalidSyntax)
return@then
}
val id = arguments[0].toInt()
val definition = ObjectDefinition.lookup(id)
player.sendMessage("Object $id is called ${definition.name} and its description is " +
"\"${definition.description}\".")
+24 -4
View File
@@ -1,4 +1,6 @@
import com.google.common.primitives.Doubles
import com.google.common.primitives.Ints
import org.apollo.game.model.entity.Skill
import org.apollo.game.model.entity.SkillSet
import org.apollo.game.model.entity.setting.PrivilegeLevel
@@ -26,8 +28,17 @@ on_command("level", PrivilegeLevel.ADMINISTRATOR)
return@then
}
val skillId = arguments[0].toInt()
val level = arguments[1].toInt()
val skillId = Ints.tryParse(arguments[0])
if (skillId == null) {
player.sendMessage(invalidSyntax)
return@then
}
val level = Ints.tryParse(arguments[1])
if (level == null) {
player.sendMessage(invalidSyntax)
return@then
}
if (skillId !in 0..20 || level !in 1..99) {
player.sendMessage(invalidSyntax)
return@then
@@ -55,8 +66,17 @@ on_command("xp", PrivilegeLevel.ADMINISTRATOR)
return@then
}
val skillId = arguments[0].toInt()
val experience = arguments[1].toDouble()
val skillId = Ints.tryParse(arguments[0])
if (skillId == null) {
player.sendMessage(invalidSyntax)
return@then
}
val experience = Doubles.tryParse(arguments[1])
if (experience == null) {
player.sendMessage(invalidSyntax)
return@then
}
if (skillId !in 0..20 || experience <= 0) {
player.sendMessage("Invalid syntax - ::xp [skill-id] [experience]")
return@then
@@ -1,3 +1,4 @@
import com.google.common.primitives.Ints
import org.apollo.game.model.Position
import org.apollo.game.model.entity.setting.PrivilegeLevel
@@ -14,13 +15,32 @@ on_command("pos", PrivilegeLevel.MODERATOR)
*/
on_command("tele", PrivilegeLevel.ADMINISTRATOR)
.then { player ->
if (!valid_arg_length(arguments, 2..3, player, "Invalid syntax - ::tele [x] [y] [optional-z]")) {
val invalidSyntax = "Invalid syntax - ::tele [x] [y] [optional-z]"
if (!valid_arg_length(arguments, 2..3, player, invalidSyntax)) {
return@then
}
val x = arguments[0].toInt()
val y = arguments[1].toInt()
val z = if (arguments.size == 3) arguments[2].toInt() else player.position.height
val x = Ints.tryParse(arguments[0])
if (x == null) {
player.sendMessage(invalidSyntax)
return@then
}
val y = Ints.tryParse(arguments[1])
if (y == null) {
player.sendMessage(invalidSyntax)
return@then
}
var z = player.position.height
if (arguments.size == 3) {
val plane = Ints.tryParse(arguments[2])
if (plane == null) {
player.sendMessage(invalidSyntax)
return@then
}
z = plane
}
if (z in 0..4) {
player.teleport(Position(x, y, z))