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

export function dropInventory(event) {
  const player = event.sourceEntity;
  const inventory = player.getComponent("inventory");
  const content = inventory.container;
  const dimension = player.dimension;

  // Set water
  player.runCommand(`/setblock ~~1~ flowing_water`);

  // Get the player's position and facing direction
  const playerLocation = player.location;
  const rotation = player.getRotation(); // Vector2: { x: pitch, y: yaw }

  // Convert yaw to radians
  const yawRadians = (rotation.y * Math.PI) / 180;

  // Calculate forward vector
  const forwardVector = {
    x: -Math.sin(yawRadians) * 3,
    y: 0,
    z: Math.cos(yawRadians) * 3,
  };

  // Spawn position slightly in front of the player
  const spawnPosition = {
    x: playerLocation.x + forwardVector.x,
    y: playerLocation.y,
    z: playerLocation.z + forwardVector.z,
  };

  // Iterate through all inventory slots
  for (let i = 0; i < content.size; i++) {
    const itemStack = content.getItem(i);
    if (itemStack) {
      dimension.spawnItem(itemStack, spawnPosition);
      content.setItem(i, null);
    }
  }
}