import { world, ItemStack } from "@minecraft/server"

//Destroy Jukebox
world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_destroy_ruby_jukebox_hopelessness', {
        onPlayerDestroy: e => {
            const { player, block } = e;
            player.runCommandAsync(`event entity @e[type=old_nether:record_hopelessness,r=8] instant_despawn`)
            player.runCommandAsync(`music stop`)
            block.dimension.spawnItem(new ItemStack('old_nether:record_hopelessness', 1), block.location);
            block.dimension.spawnItem(new ItemStack('old_nether:ruby_jukebox', 1), block.location);
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_destroy_ruby_jukebox_pigman_invasion', {
        onPlayerDestroy: e => {
            const { player, block } = e;
            player.runCommandAsync(`event entity @e[type=old_nether:record_pigman_invasion,r=8] instant_despawn`)
            player.runCommandAsync(`music stop`)
            block.dimension.spawnItem(new ItemStack('old_nether:record_pigman_invasion', 1), block.location);
            block.dimension.spawnItem(new ItemStack('old_nether:ruby_jukebox', 1), block.location);
        }
    });
});

//Stop Music
world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:stop_pigman_invasion', {
        onPlayerInteract: e => {
            const { player, block } = e;
            player.runCommandAsync(`event entity @e[type=old_nether:record_pigman_invasion,r=8] instant_despawn`)
            player.runCommandAsync(`music stop`)
            block.dimension.spawnItem(new ItemStack('old_nether:record_pigman_invasion', 1), block.location);
            block.setType('old_nether:ruby_jukebox');
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:stop_hopelessness', {
        onPlayerInteract: e => {
            const { player, block } = e;
            player.runCommandAsync(`event entity @e[type=old_nether:record_hopelessness,r=8] instant_despawn`)
            player.runCommandAsync(`music stop`)
            block.dimension.spawnItem(new ItemStack('old_nether:record_hopelessness', 1), block.location);
            block.setType('old_nether:ruby_jukebox');
        }
    });
});