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

const GunReloadComponent = {
    onUse({ itemStack, source }) {
        if (typeId === "fw:10mm_pistol_emp") {
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=12..}] run scoreboard players add @s 10mm_pistol 12").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=11}] run scoreboard players add @s 10mm_pistol 11").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=10}] run scoreboard players add @s 10mm_pistol 10").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=9}] run scoreboard players add @s 10mm_pistol 9").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=8}] run scoreboard players add @s 10mm_pistol 8").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=7}] run scoreboard players add @s 10mm_pistol 7").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=6}] run scoreboard players add @s 10mm_pistol 6").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=5}] run scoreboard players add @s 10mm_pistol 5").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=4}] run scoreboard players add @s 10mm_pistol 4").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=3}] run scoreboard players add @s 10mm_pistol 3").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=2}] run scoreboard players add @s 10mm_pistol 2").catch(() => {});
            // Bullet add
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=1}] run scoreboard players add @s 10mm_pistol 1").catch(() => {});
            });
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=1}] clear @s 10mm_ammunition 1").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=2}] clear @s 10mm_ammunition 2").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=3}] clear @s 10mm_ammunition 3").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=4}] clear @s 10mm_ammunition 4").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=5}] clear @s 10mm_ammunition 5").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=6}] clear @s 10mm_ammunition 6").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=7}] clear @s 10mm_ammunition 7").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=8}] clear @s 10mm_ammunition 8").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=9}] clear @s 10mm_ammunition 9").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=10}] clear @s 10mm_ammunition 10").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=11}] clear @s 10mm_ammunition 11").catch(() => {});
            // Ammo clear
            source.runCommandAsync("execute as @s[hasitem={item=fw:10mm_ammunition,quantity=12..}] clear @s 10mm_ammunition 12").catch(() => {});
        }
    },
};

world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("fw:gun_reload", GunReloadComponent);
});
