node-brigadier-commands/lib/arguments/IntegerArgumentType.js
Chipmunk d302cf8f37 Implement argument types
Also fixes CommandDispatcher not throwing parse exceptions correctly, RequiredArgumentBuilder returning incorrect objects, and CommandContext.getArgument not returning the correct value
2023-09-01 18:41:31 -04:00

44 lines
1.2 KiB
JavaScript

const ArgumentType = require('./ArgumentType.js')
const { CommandSyntaxException } = require('../exceptions')
const MINIMUM_INT = -2147483648
const MAXIMUM_INT = 2147483647
const EXAMPLES = ['0', '123', '-123']
class IntegerArgumentType extends ArgumentType {
constructor (minimum, maximum) {
super()
this.minumum = +minimum
this.maximum = +maximum
}
static integer (min = MINIMUM_INT, max = MAXIMUM_INT) {
return new IntegerArgumentType(min, max)
}
parse (reader) {
const start = reader.cursor
const result = reader.readInt()
if (result < this.minumum) {
reader.cursor = start
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooLow.createWithContext(reader, result, this.minimum)
}
if (result > this.maximum) {
reader.cursor = start
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh.createWithContext(reader, result, this.maximum)
}
return result
}
toString () {
if (this.minumum === MINIMUM_INT && this.maximum === MAXIMUM_INT) return 'integer()'
if (this.maximum === MAXIMUM_INT) return `integer(${this.minimum})`
return `integer(${this.minimum}, ${this.maximum})`
}
getExamples () {
return EXAMPLES
}
}
module.exports = IntegerArgumentType