import {
  world,
  system,
  EntityDamageCause
} from "@minecraft/server";


world.afterEvents.entityHitEntity.subscribe((event) => {

  const damagingEntity = event.damagingEntity;

  if(damagingEntity.typeId === 'we:bruudlin') {

    let rider = damagingEntity.getComponent('minecraft:rideable')

    if (!rider) {
      console.warn('Entity does not have rideable component');
      return;
    }

    rider.addRider(event.hitEntity);

  }

});

system.runInterval(() => {
  for (const player of world.getPlayers()) {
    let mount = player.getComponent('minecraft:riding')

if (!mount) {
    
    return;
  }
    
    if(mount.entityRidingOn.typeId === 'we:bruudlin') {
      player.camera.fade({fadeColor: {blue: 1.0, green: 0.83, red: 0.01}, fadeTime: {fadeInTime: 0, fadeOutTime : 0.5, holdTime: 0}});
      player.applyDamage(3, {cause: 'drowning', damagingEntity: mount});
    }
  }
}, 12);

