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

    world.beforeEvents.worldInitialize.subscribe(function (pch) {

    pch.blockComponentRegistry.registerCustomComponent('peach:rider_couch', {
        onPlayerInteract: function (e) {
            let {
              x,
              y,
              z
            } = e.block.location;
            const equippable = e.player.getComponent("equippable");
            const mainhand = equippable ? equippable.getEquipment("Mainhand") : null;
            if (mainhand && (mainhand.typeId === 'peach:white_cushion')) {
              e.cancel = true;
              return;
            }
            if (mainhand && mainhand.typeId.startsWith("ef:brush_")) {
              e.cancel = true;
              return;
            }
            if (e.player.isSneaking) return;
            let blockStr = e.block.x + " " + e.block.y + " " + e.block.z;
            let yaw = 0;
            let seatId = 'peach:south_sit';
            try {
              const face = e.block.permutation.getState('minecraft:cardinal_direction');
              if (face === 'north') { yaw = 0; seatId = 'peach:south_sit'; }
              else if (face === 'east') { yaw = 0; seatId = 'peach:east_sit'; }
              else if (face === 'south') { yaw = 0; seatId = 'peach:north_sit'; }
              else if (face === 'west') { yaw = 0; seatId = 'peach:west_sit'; }
            } catch {}
            e.dimension.runCommand(`summon ${seatId} ${e.block.x} ${e.block.y} ${e.block.z}`);
            e.dimension.runCommand(`execute positioned ${e.block.x} ${e.block.y} ${e.block.z} as @e[type=${seatId},c=1] run tp @s ${e.block.x} ${e.block.y} ${e.block.z} ${yaw} 0`);
            e.player.runCommand(`execute at @e[type=player] positioned ${x} ${y} ${z} run ride @s start_riding @e[type=${seatId},c=1] teleport_rider`);
          },
          onPlayerDestroy: function (e) {
            if (!e.player) return;
            let playerLoc = e.player.location;
            playerLoc.x -= 0.5;
            playerLoc.z -= 0.5;
      
            if (playerLoc.x != e.block.location.x) return;
            if (playerLoc.y != e.block.location.y) return;
            if (playerLoc.z != e.block.location.z) return;
      
            e.player.runCommand("ride @s stop_riding");
          },
          onPlace: function (e) {
            if (!e.block) return;
            let block = e.block.above();
            if (!block) return;
          }
        });
          });
    
