mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-30 10:56:55 -05:00
113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
/* eslint-disable max-len */
|
|
const {Midi} = require('@tonejs/midi');
|
|
const {convertMidi} = require('../util/midi_converter');
|
|
|
|
const soundNames = {
|
|
harp: 'minecraft:block.note_block.harp',
|
|
basedrum: 'minecraft:block.note_block.basedrum',
|
|
snare: 'minecraft:block.note_block.snare',
|
|
hat: 'minecraft:block.note_block.hat',
|
|
bass: 'minecraft:block.note_block.bass',
|
|
flute: 'minecraft:block.note_block.flute',
|
|
bell: 'minecraft:block.note_block.bell',
|
|
guitar: 'minecraft:block.note_block.guitar',
|
|
chime: 'minecraft:block.note_block.chime',
|
|
xylophone: 'minecraft:block.note_block.xylophone',
|
|
iron_xylophone: 'minecraft:block.note_block.iron_xylophone',
|
|
cow_bell: 'minecraft:block.note_block.cow_bell',
|
|
didgeridoo: 'minecraft:block.note_block.didgeridoo',
|
|
bit: 'minecraft:block.note_block.bit',
|
|
banjo: 'minecraft:block.note_block.banjo',
|
|
pling: 'minecraft:block.note_block.pling',
|
|
};
|
|
|
|
function inject(bot) {
|
|
bot.music = function() {};
|
|
bot.music.song = null;
|
|
bot.music.loop = false;
|
|
time = 0;
|
|
startTime = 0;
|
|
noteIndex = 0;
|
|
setInterval(() => {
|
|
try {
|
|
if (!bot.music.song) return;
|
|
time = Date.now() - startTime;
|
|
while (bot.music.song.notes[noteIndex]?.time <= time) {
|
|
const note = bot.music.song.notes[noteIndex];
|
|
const floatingPitch = 2 ** ((note.pitch - 12) / 12.0);
|
|
bot.core.run(`minecraft:execute as @a[tag=!nomusic] at @s run playsound ${soundNames[note.instrument]} record @s ~ ~ ~ ${note.volume} ${floatingPitch}`);
|
|
noteIndex++;
|
|
bot.core.run('minecraft:title @a[tag=!nomusic] actionbar ' + JSON.stringify(toComponent()));
|
|
if (noteIndex >= bot.music.song.notes.length) {
|
|
if (bot.music.loop) {
|
|
resetTime();
|
|
return;
|
|
}
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Finished playing '}, {text: bot.music.song.name, color: 'gold'}]));
|
|
bot.music.stop();
|
|
}
|
|
}
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
}, 20);
|
|
|
|
bot.music.load = function(buffer, fallbackName = '[unknown]') {
|
|
// TODO: NBS Support
|
|
const midi = new Midi(buffer);
|
|
const song = convertMidi(midi);
|
|
if (song.name === '') song.name = fallbackName;
|
|
return song;
|
|
};
|
|
|
|
bot.music.play = function(song) {
|
|
bot.music.song = song;
|
|
loop = song.loop;
|
|
resetTime();
|
|
};
|
|
|
|
bot.music.stop = function() {
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Stopped playing '}, {text: bot.music.song.name, color: 'gold'}]));
|
|
bot.music.song = null;
|
|
bot.music.loop = false;
|
|
resetTime();
|
|
};
|
|
|
|
function resetTime() {
|
|
time = 0;
|
|
startTime = Date.now();
|
|
noteIndex = 0;
|
|
}
|
|
|
|
function formatTime(time) {
|
|
const seconds = Math.floor(time / 1000);
|
|
|
|
return `${Math.floor(seconds / 60)}:${(seconds % 60).toString().padStart(2, '0')}`;
|
|
}
|
|
|
|
function toComponent() {
|
|
const component = [
|
|
'§8[§eChomeNS Bot§8] ',
|
|
{text: 'Now Playing', color: 'gold'},
|
|
' §8| ',
|
|
{text: bot.music.song.name, color: 'green'},
|
|
' §8| ',
|
|
{text: formatTime(time), color: 'gray'},
|
|
' §8/ ',
|
|
{text: formatTime(bot.music.song.length), color: 'gray'},
|
|
' §8| ',
|
|
{text: noteIndex, color: 'gray'},
|
|
'§8 / ',
|
|
{text: bot.music.song.notes.length, color: 'gray'},
|
|
];
|
|
|
|
if (bot.music.loop) {
|
|
component.push(' §8| ');
|
|
component.push('§aLooping');
|
|
}
|
|
|
|
return component;
|
|
}
|
|
}
|
|
|
|
module.exports = {inject};
|