forked from PrismarineJS/flying-squid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
192 lines (180 loc) · 6.91 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { skipMcPrefix } = require('../utils')
const Vec3 = require('vec3').Vec3
module.exports.server = function (serv, { version }) {
const { registry } = serv
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])
: 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()
if (serv.supportFeature('removedNamedSoundEffectPacket')) { // 1.19.3 removes named_sound_effect
player._client.write('sound_effect', {
soundId: 0,
soundEvent: {
resource: sound,
range: undefined
},
soundCategory,
x: pos.x,
y: pos.y,
z: pos.z,
volume,
pitch: Math.round(pitch * 63),
seed: 0
})
} else {
// only packet still in fixed position in all versions
player._client.write('named_sound_effect', {
soundName: sound,
soundCategory,
x: pos.x,
y: pos.y,
z: pos.z,
volume,
pitch: Math.round(pitch * 63),
seed: 0
})
}
})
}
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),
seed: 0
})
})
}
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)), {
count: 1,
size: new Vec3(0, 0, 0)
})
}
serv.playSound('note.' + instrument, world, position, { pitch: serv.getNote(pitch) })
}
serv.getNote = note => 0.5 * Math.pow(Math.pow(2, 1 / 12), note)
serv.commands.add({
base: 'playsoundforall',
info: 'to play sound for everyone',
usage: '/playsoundforall <sound_name> [volume] [pitch] [useID]',
onlyPlayer: true,
op: true,
parse (str) {
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,
useId: results[4] ? results[4] === 'true' : false
}
},
action (action, ctx) {
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 <sound_name> [volume] [pitch] [useID]',
onlyPlayer: true,
op: true,
parse (str) {
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,
useId: results[4] ? results[4] === 'true' : false
}
},
action (action, ctx) {
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 })
}
}
})
}
module.exports.player = function (player, serv) {
player.playSound = (sound, opt = {}) => {
opt.whitelist = player
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)
if (id !== 25) return
cancel(false)
if (!player.world.blockEntityData[reference.toString()]) player.world.blockEntityData[reference.toString()] = {}
const data = player.world.blockEntityData[reference.toString()]
if (typeof data.note === 'undefined') data.note = -1
data.note++
data.note %= 25
serv.playNoteBlock(data.note, player.world, reference)
})
player.on('dig_cancel', async ({ position }, cancel) => {
const id = await player.world.getBlockType(position)
if (id !== 25) return
cancel(false)
if (!player.world.blockEntityData[position.toString()]) player.world.blockEntityData[position.toString()] = {}
const data = player.world.blockEntityData[position.toString()]
if (typeof data.note === 'undefined') data.note = 0
serv.playNoteBlock(data.note, player.world, position)
})
}
module.exports.entity = function (entity, serv) {
entity.playSoundAtSelf = (sound, opt = {}) => {
serv.playSound(sound, entity.world, entity.position, opt)
}
}