import { ItemStack } from "@minecraft/server";
import * as utils from "./utils";

export async function onScriptEventReceive(event) {
	const source = event.sourceEntity;

    if (!source || !source.isValid()) {
        return;
    };
    
    const player = await utils.getEntityNearEntity("minecraft:player", source);

    if (!player || !player.isValid()) {
        source.triggerEvent("goe_bos:detect");
        return;
    };

    const playerInventory = player.getComponent("minecraft:inventory").container;
    if (playerInventory.emptySlotsCount == 0) {
        source.triggerEvent("goe_bos:detect");
        return;
    };

    player.playSound("goe_bos:reward_dropping");
    player.dimension.spawnItem(new ItemStack(event.message), player.location);
    source.remove();
}