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

world.afterEvents.playerInteractWithEntity.subscribe(event => {
	const {target: entity, itemStack, player} = event;
	if (!entity || !player) return;
	if (itemStack && itemStack.hasTag('whs:whistle') && !itemStack.getDynamicProperty('whs:entityId') && player.isSneaking && !entity.getDynamicProperty('whs:mountBind') && entity.getComponent('rideable') && entity.getComponent("is_tamed")) {
		player.dimension.playSound('random.whistle', player.location);
		entity.setDynamicProperty('whs:mountBind', true);
		entity.addTag(`${player.name}_${entity.id}`);
		const lore = itemStack.getLore();
		lore.push(`§r§7${player.name}\'s Whistle`, `§r§7Entity Id: ${entity.id}`);
		itemStack.setLore(lore);
		itemStack.setDynamicProperty('whs:entityId', entity.id);
		player.getComponent('equippable').setEquipment('Mainhand', itemStack);
	}
});
