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

world.afterEvents.itemUse.subscribe((event) => {
    const itemStack = event.itemStack;
    const source = event.source;
    
    if (itemStack.typeId === 'addon:m17') {
        if (source.isSneaking) {
            source.runCommandAsync('execute as @s[scores={m17=1..}, has_property={addon:m17_reload=0}] run function m17/m17_scope');
        } else {
            source.runCommandAsync('execute as @s[scores={m17=1..}, has_property={addon:m17_reload=0}] run function m17/m17_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);
    }
});