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

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