Skip to content

Commit

Permalink
Fix crash in /gamemode command (PrismarineJS#437)
Browse files Browse the repository at this point in the history
* Fix crash in /gamemode command

* Change gamemode command to take a mode string

* Update usage

* Fix formatting

* Add support for numeric game modes

* Fix formatting

* Update error message
  • Loading branch information
lleyton authored Aug 9, 2020
1 parent af8ee41 commit e08a50e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/lib/plugins/players.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,47 @@ module.exports.server = function (serv, { version }) {
base: 'gamemode',
aliases: ['gm'],
info: 'to change game mode',
usage: '/gamemode <0-3> [player]',
usage: '/gamemode <mode> [player]',
op: true,
parse (str, ctx) {
var paramsSplit = str.split(' ')
if (paramsSplit[0] === '') {
return false
}
if (!paramsSplit[0].match(/^([0-3])$/) && paramsSplit[0].match(/^([0-9]+)$/)) {
throw new UserError(`The number you have entered (${paramsSplit[0]}) is too big, it must be at most 3`)
if (!paramsSplit[0].match(/^(survival|creative|adventure|spectator|[0-3])$/)) {
throw new UserError(`The gamemode you have entered (${paramsSplit[0]}) is not valid, it must be survival, creative, adventure, spectator, or a number from 0-3`)
}
if (!paramsSplit[1]) {
if (ctx.player) return paramsSplit[0].match(/^([0-3])$/)
if (ctx.player) return paramsSplit[0].match(/^(survival|creative|adventure|spectator|[0-3])$/)
else throw new UserError('Console cannot set gamemode itself')
}

return str.match(/^([0-3]) (\w+)$/) || false
return str.match(/^(survival|creative|adventure|spectator|[0-3]) (\w+)$/) || false
// return params || false
},
action (str, ctx) {
var gamemodes = {
0: 'Survival',
1: 'Creative',
2: 'Adventure',
3: 'Spectator'
survival: 0,
creative: 1,
adventure: 2,
spectator: 3
}
var mode = gamemodes[str[1]]
var gamemodesReverse = Object.assign({}, ...Object.entries(gamemodes).map(([k, v]) => ({ [v]: k })))
var gamemode = parseInt(str[1], 10) || gamemodes[str[1]]
var mode = parseInt(str[1], 10) ? gamemodesReverse[parseInt(str[1], 10)] : str[1]
var plyr = serv.getPlayer(str[2])
if (ctx.player) {
if (str[2]) {
if (plyr !== null) {
plyr.setGameMode(parseInt(str[1], 10))
plyr.setGameMode(gamemode)
return `Set ${str[2]}'s game mode to ${mode} Mode`
} else {
throw new UserError(`Player '${str[2]}' cannot be found`)
}
} else ctx.player.setGameMode(parseInt(str[1], 10))
} else ctx.player.setGameMode(gamemode)
} else {
if (plyr !== null) {
plyr.setGameMode(parseInt(str[1], 10))
plyr.setGameMode(gamemode)
return `Set ${str[2]}'s game mode to ${mode} Mode`
} else {
throw new UserError(`Player '${str[2]}' cannot be found`)
Expand Down
2 changes: 1 addition & 1 deletion tools/dumpCodec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ download('1.16.1', MC_SERVER_JAR, (err) => {
client.on('login', packet => {
fs.writeFile('dimensionCodec.json', JSON.stringify(packet.dimensionCodec), (err) => {
if (err) return console.error(err)
console.log("Dumped dimension codec successfully to dimensionCodec.json!")
console.log('Dumped dimension codec successfully to dimensionCodec.json!')
client.end()
wrap.stopServer(() => {
wrap.deleteServerData(() => {})
Expand Down

0 comments on commit e08a50e

Please sign in to comment.