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

world.afterEvents.itemUse.subscribe((event) => {
    const itemStack = event.itemStack;
    const source = event.source;
    
    if (itemStack.typeId === 'addon:p226') {
        if (source.isSneaking) {
            source.runCommandAsync('execute as @s[scores={p226=1..}, has_property={addon:p226_reload=0}] run function p226/p226_scope');
        } else {
            source.runCommandAsync('execute as @s[scores={p226=1..}, has_property={addon:p226_reload=0}] run function p226/p226_noscope');
        }
        
        system.runTimeout(() => {
            if (source.isValid()) {
                source.runCommandAsync('execute as @s run fill ~3 ~3 ~3 ~-3 ~-3 ~-3 air replace block:custom_light').catch(() => {});
            }
        }, 5);
    }
});