import { world } from '@minecraft/server';

world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:play_music', {
        onPlayerInteract(e) {
            const { block, player } = e;
            const equipment = player.getComponent('equippable');
            const selectedItem = equipment.getEquipment('Mainhand');
            if (selectedItem?.typeId === 'old_nether:record_hopelessness') {
                block.setType('old_nether:ruby_jukebox_hopelessness');
                player.runCommandAsync("music play record.hopelessness");
                player.runCommandAsync("title @a [r=16] actionbar §dNow playing: IsraOrbit - hopelessness");
                player.runCommandAsync("clear @p old_nether:record_hopelessness 0 1");
                block.dimension.spawnEntity(('old_nether:r_h'), block.location);
            }
            if (selectedItem?.typeId === 'old_nether:record_pigman_invasion') {
                block.setType('old_nether:ruby_jukebox_pigman_invasion');
                player.runCommandAsync("music play record.pigman_invasion");
                player.runCommandAsync("title @a [r=16] actionbar §dNow playing: IsraOrbit - pigman invasion");
                player.runCommandAsync("clear @p old_nether:record_pigman_invasion 0 1");
                block.dimension.spawnEntity(('old_nether:r_pi'), block.location);
            }
        }
    })
})