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

world.beforeEvents.worldInitialize.subscribe(e => {
   e.itemComponentRegistry.registerCustomComponent("gs_ce:horn", Horn);
})

const Horn = {
	onCompleteUse({source}) {
		const player = source;
		const noOfBats = Math.floor(Math.random() * 4) + 2;
		for (let i = 0; i < noOfBats; i++) {
			const bat = player.dimension.spawnEntity("gs_ce:bat_from_horn", player.location);
			const tameable = bat.getComponent("minecraft:tameable");
			tameable.tame(player);
		}
	},
	onUse({source}) {
		const player = source;
		world.playSound("horn.call.7", player.location);
	}
};