From 04a8fe7f30146c55161b2043e1a12b298000b6a3 Mon Sep 17 00:00:00 2001 From: William Gaylord Date: Sun, 17 Mar 2024 10:04:03 -0500 Subject: [PATCH 1/2] Implement an option to send the sound as an ID instead of as a string. (#652) --- src/lib/plugins/sound.js | 75 ++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/src/lib/plugins/sound.js b/src/lib/plugins/sound.js index 999cac3a..6e93c27a 100644 --- a/src/lib/plugins/sound.js +++ b/src/lib/plugins/sound.js @@ -2,7 +2,8 @@ const { skipMcPrefix } = require('../utils') const Vec3 = require('vec3').Vec3 -module.exports.server = function (serv) { +module.exports.server = function (serv, { version }) { + const registry = require('prismarine-registry')(version) serv.playSound = (sound, world, position, { whitelist, blacklist = [], radius = 32, volume = 1.0, pitch = 1.0, soundCategory = 0 } = {}) => { const players = (typeof whitelist !== 'undefined' ? (typeof whitelist instanceof Array ? whitelist : [whitelist]) @@ -28,6 +29,31 @@ module.exports.server = function (serv) { }) } + serv.playSoundId = (soundId, world, position, { whitelist, blacklist = [], radius = 32, volume = 1.0, pitch = 1.0, soundCategory = 0 } = {}) => { + const players = (typeof whitelist !== 'undefined' + ? (typeof whitelist instanceof Array ? whitelist : [whitelist]) + : serv.getNearby({ + world, + position, + radius + })) + players.filter(player => blacklist.indexOf(player) === -1) + .forEach(player => { + const iniPos = position ? position.scaled(1 / 32) : player.position.scaled(1 / 32) + const pos = iniPos.scaled(8).floored() + // only packet still in fixed position in all versions + player._client.write('sound_effect', { + soundId, + soundCategory, + x: pos.x, + y: pos.y, + z: pos.z, + volume, + pitch: Math.round(pitch * 63) + }) + }) + } + serv.playNoteBlock = (pitch, world, position, { instrument = 'harp', particle = true } = {}) => { if (particle) { serv.emitParticle(23, world, position.clone().add(new Vec3(0.5, 1.5, 0.5)), { @@ -43,42 +69,64 @@ module.exports.server = function (serv) { serv.commands.add({ base: 'playsoundforall', info: 'to play sound for everyone', - usage: '/playsoundforall [volume] [pitch]', + usage: '/playsoundforall [volume] [pitch] [useID]', onlyPlayer: true, op: true, parse (str) { - const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?/) + const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?(?: (true|false))?/) if (!results) return false return { sound_name: skipMcPrefix(results[1]), volume: results[2] ? parseFloat(results[2]) : 1.0, - pitch: results[3] ? parseFloat(results[3]) : 1.0 + pitch: results[3] ? parseFloat(results[3]) : 1.0, + useId: results[4] ? results[4] === 'true' : false } }, action (action, ctx) { - ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ')') - serv.playSound(action.sound_name, ctx.player.world, ctx.player.position, { volume: action.volume, pitch: action.pitch }) + if (action.useId) { + const id = registry?.soundsByName[action.sound_name]?.id + if (id) { + ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ') using ID') + serv.playSoundId(id, ctx.player.world, ctx.player.position, { volume: action.volume, pitch: action.pitch }) + } else { + ctx.player.chat('"' + action.sound_name + '" could not be played by ID') + } + } else { + ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ')') + serv.playSound(action.sound_name, ctx.player.world, ctx.player.position, { volume: action.volume, pitch: action.pitch }) + } } }) serv.commands.add({ base: 'playsound', info: 'to play sound for yourself', - usage: '/playsound [volume] [pitch]', + usage: '/playsound [volume] [pitch] [useID]', onlyPlayer: true, op: true, parse (str) { - const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?/) + const results = str.match(/([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?(?: (true|false))?/) if (!results) return false return { sound_name: skipMcPrefix(results[1]), volume: results[2] ? parseFloat(results[2]) : 1.0, - pitch: results[3] ? parseFloat(results[3]) : 1.0 + pitch: results[3] ? parseFloat(results[3]) : 1.0, + useId: results[4] ? results[4] === 'true' : false } }, action (action, ctx) { - ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ')') - ctx.player.playSound(action.sound_name, { volume: action.volume, pitch: action.pitch }) + if (action.useId) { + const id = registry?.soundsByName[action.sound_name]?.id + if (id) { + ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ') using ID') + ctx.player.playSoundId(id, { volume: action.volume, pitch: action.pitch }) + } else { + ctx.player.chat('"' + action.sound_name + '" could not be played by ID') + } + } else { + ctx.player.chat('Playing "' + action.sound_name + '" (volume: ' + action.volume + ', pitch: ' + action.pitch + ')') + ctx.player.playSound(action.sound_name, { volume: action.volume, pitch: action.pitch }) + } } }) } @@ -89,6 +137,11 @@ module.exports.player = function (player, serv) { serv.playSound(sound, player.world, null, opt) } + player.playSoundId = (sound, opt = {}) => { + opt.whitelist = player + serv.playSoundId(sound, player.world, null, opt) + } + player.on('placeBlock_cancel', async ({ reference }, cancel) => { if (player.crouching) return const id = await player.world.getBlockType(reference) From 8f29e3a5604b25d8151b85920ecd2b406ede75e0 Mon Sep 17 00:00:00 2001 From: tfcprivt <141457761+privt00@users.noreply.github.com> Date: Fri, 19 Apr 2024 23:29:55 +0200 Subject: [PATCH 2/2] fixed gramma (#654) * fixed gramma * fixed gramma --- src/lib/plugins/errorHandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/plugins/errorHandler.js b/src/lib/plugins/errorHandler.js index 6acb33e9..4efd8509 100644 --- a/src/lib/plugins/errorHandler.js +++ b/src/lib/plugins/errorHandler.js @@ -1,7 +1,7 @@ module.exports.player = async function (player, serv) { function unhandledRejection (promise) { serv.warn('-------------------------------') - serv.warn('Please report this flying-squid! This is bug (mabye)') + serv.warn('Please report this error to flying-squid! This is can be bug') serv.warn('Unhandled rejection warning!') serv.warn('Error: ' + promise) serv.warn('Report this error here: https://github.com/PrismarineJS/flying-squid/issues') @@ -11,7 +11,7 @@ module.exports.player = async function (player, serv) { function uncaughtException (err) { serv.err('-------------------------------') - serv.err('Please report this flying-squid! This is bug (mabye)') + serv.err('Please report this error to flying-squid! This is can be bug') serv.err('Something went wrong!') serv.err('Error: ' + err.stack) serv.err('Report this error here: https://github.com/PrismarineJS/flying-squid/issues')