import { world, system } from "@minecraft/server";
import { deleteItem } from "./ui";

system.beforeEvents.watchdogTerminate.subscribe(e => e.cancel = true);

world.afterEvents.playerInteractWithBlock.subscribe(evd => {
    const { block, player, itemStack: item } = evd;
    if (block.typeId == 'trash:trash_can') {
        deleteItem(player, item);
    }
});

system.runInterval(async () => {
    for (const player of world.getPlayers()) {
        const item = player.getComponent('inventory').container.getItem(player.selectedSlot);
        if (item) {
            if (item.typeId == 'trash:replacer') {
                await player.runCommandAsync(`clear @s trash:replacer`);
            }
        }
    }
});