Skip to content

Commit

Permalink
skip mc prefix, make setblock with name
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Oct 13, 2023
1 parent cb540ce commit 8df2121
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/lib/plugins/blocks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { skipMcPrefix } = require('../utils')

const Vec3 = require('vec3').Vec3

module.exports.player = function (player, serv) {
Expand Down Expand Up @@ -60,7 +62,7 @@ module.exports.server = function (serv, { version }) {
op: true,
tab: ['blockX', 'blockY', 'blockZ', 'block', 'number'],
parse (str) {
const results = str.match(/^(~|~?-?[0-9]+) (~|~?-?[0-9]+) (~|~?-?[0-9]+) ([0-9]{1,3})(?: ([0-9]{1,3}))?/)
const results = str.match(/^(~|~?-?[0-9]+) (~|~?-?[0-9]+) (~|~?-?[0-9]+) ([\w_:0-9]+)(?: ([0-9]{1,3}))?/)
if (!results) return false
return results
},
Expand All @@ -69,6 +71,7 @@ module.exports.server = function (serv, { version }) {
if (ctx.player) res = res.map((val, i) => serv.posFromString(val, ctx.player.position[['x', 'y', 'z'][i]]))
else res = res.map((val, i) => serv.posFromString(val, new Vec3(0, 128, 0)[['x', 'y', 'z'][i]]))

res[1] = isNaN(+res[1]) ? mcData.blocksByName[skipMcPrefix(res[1])]?.id : +res[1]
const id = parseInt(params[4], 10)
const data = parseInt(params[5] || 0, 10)
const stateId = serv.supportFeature('theFlattening') ? (blocks[id].minStateId + data) : (id << 4 | data)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/plugins/effects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { pascalCase } = require('change-case')
const UserError = require('../user_error')
const { skipMcPrefix } = require('../utils')

module.exports.entity = function (entity, serv) {
entity.effects = {}
Expand Down Expand Up @@ -73,8 +74,8 @@ module.exports.server = function (serv, options) {
let effId = parseInt(params[2])
if (isNaN(effId)) {
const mcData = require('minecraft-data')(options.version)
const effectNamePascal = pascalCase(params[2])
const effect = mcData.effectsByName[effectNamePascal];
const effectNamePascal = pascalCase(skipMcPrefix(params[2]))
const effect = mcData.effectsByName[effectNamePascal]
if (!effect) throw new UserError(`Unknown effect ${params[2]}}`)
effId = effect.id
}
Expand Down
8 changes: 5 additions & 3 deletions src/lib/plugins/players.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { skipMcPrefix } = require('../utils')

const UserError = require('flying-squid').UserError

module.exports.server = function (serv, { version }) {
Expand Down Expand Up @@ -111,12 +113,12 @@ module.exports.server = function (serv, { version }) {
if (args[2] && !args[2].match(/\d/)) throw new UserError('Count must be numerical')
return {
players,
item: args[1],
item: skipMcPrefix(args[1]),
count: args[2] ? args[2] : 1
}
},
action ({ players, item, count }) {
const itemData = isNaN(+item) ? mcData.itemsByName[item.replace(/minecraft:/, '')] : mcData.items[+item]
const itemData = isNaN(+item) ? mcData.itemsByName[skipMcPrefix(item)] : mcData.items[+item]

if (!itemData) throw new UserError(`Unknown item '${item}'`)
const newItem = new Item(itemData.id, count)
Expand Down Expand Up @@ -149,7 +151,7 @@ module.exports.server = function (serv, { version }) {
parse (args, ctx) {
args = args.split(' ')
if (args[0] === '') return false
const enchantment = mcData.enchantmentsByName[args[1]]
const enchantment = mcData.enchantmentsByName[skipMcPrefix(args[1])]
if (!enchantment) throw new UserError('No such enchantment')
if (args[2] && (parseInt(args[2]) > enchantment.maxLevel || parseInt(args[2]) < 1)) throw new UserError(`Level ${args[2]} is not supported by that enchantment`)
const players = serv.getPlayers(args[0], ctx.player)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/plugins/sound.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { skipMcPrefix } = require('../utils')

const Vec3 = require('vec3').Vec3

module.exports.server = function (serv) {
Expand Down Expand Up @@ -48,7 +50,7 @@ module.exports.server = function (serv) {
const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?/)
if (!results) return false
return {
sound_name: results[1],
sound_name: skipMcPrefix(results[1]),
volume: results[2] ? parseFloat(results[2]) : 1.0,
pitch: results[3] ? parseFloat(results[3]) : 1.0
}
Expand All @@ -69,7 +71,7 @@ module.exports.server = function (serv) {
const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?/)
if (!results) return false
return {
sound_name: results[1],
sound_name: skipMcPrefix(results[1]),
volume: results[2] ? parseFloat(results[2]) : 1.0,
pitch: results[3] ? parseFloat(results[3]) : 1.0
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/plugins/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const requireIndex = require('../requireindex')
const plugins = requireIndex(path.join(__dirname, '..', 'plugins'))
const UserError = require('flying-squid').UserError
const UUID = require('uuid-1345')
const { skipMcPrefix } = require('../utils')
const Vec3 = require('vec3').Vec3

module.exports.server = function (serv, options) {
Expand Down Expand Up @@ -116,7 +117,7 @@ module.exports.server = function (serv, options) {
op: true,
action (name, ctx) {
if (Object.keys(serv.entities).length > options['max-entities']) { throw new UserError('Too many mobs !') }
const entity = entitiesByName[name]
const entity = entitiesByName[skipMcPrefix(name)]
if (!entity) {
return 'No entity named ' + name
}
Expand Down Expand Up @@ -146,7 +147,7 @@ module.exports.server = function (serv, options) {
},
action ({ number, name }, ctx) {
if (Object.keys(serv.entities).length > options['max-entities'] - number) { throw new UserError('Too many mobs !') }
const entity = entitiesByName[name]
const entity = entitiesByName[skipMcPrefix(name)]
if (!entity) {
return 'No entity named ' + name
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/tabComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.player = function (player, serv, options) {

use: function (id, otherData = null, existingContent = '') {
if (id === undefined || !this.types[id]) return
const matches = this.types[id](otherData) || this.types.player();
const matches = this.types[id](otherData) || this.types.player()
sendTabComplete(matches, existingContent)
},
add: function (id, cb) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.skipMcPrefix = (name) => typeof name === 'string' ? name.replace(/^minecraft:/, '') : name

0 comments on commit 8df2121

Please sign in to comment.