import './handlers/join.js';
import './handlers/world.js';
import './content/mechanics/touch_grass.js';
import { PotBlockComponent } from './content/blocks/pot.js';
import './content/mechanics/compost.js';
import { CrucibleBlockComponent, SmokeyCrucibleBlockComponent } from './content/blocks/crucible.js';
import { world, system } from '@minecraft/server';
import { FluidType } from './constants.js';
import './minecraft_vanilla_data.js';
import { CrookItemComponent } from './content/items/crook.js';
import './handlers/handlers.js';
import './compact/storage_drawers.js';
import './api/api.js';
import './content/mechanics/sneakgrow.js';
import './content/items/hammer.js';
import '@minecraft/server-ui';
import { WORLD_SETTINGS } from './settings/settings.js';
import './recipes/recipes.js';
import { LeafWormItemComponent, GummyLeafWormItemComponent } from './content/items/leaf_worm.js';
import { SickleItemComponent } from './content/items/sickle.js';
import { LeavesBlockComponent } from './content/blocks/leaves.js';
import { AutoHammerBlockComponent } from './content/blocks/auto_hammer.js';
import { GeneratorBlockCustomComponent } from './content/blocks/generator.js';
import { BasicSieveBlockComponent, AutoSieveBlockComponent } from './content/blocks/sieves.js';
import { TrowelItemComponent } from './content/items/trowel.js';
import { BookCustomItemComponent } from './book/book_componets.js';
import { sieves as U } from './ftb_bedrock_bedrock_utils_dist_bedrock_utils.js';

world.afterEvents.worldLoad.subscribe((event) => {
    for (let worldsetting of WORLD_SETTINGS) {
        worldsetting.initDefault();
    }
});
system.beforeEvents.startup.subscribe((event) => {
    const itemRegistry = event.itemComponentRegistry;
    itemRegistry.registerCustomComponent(U("crook"), new CrookItemComponent());
    itemRegistry.registerCustomComponent(U("leaf_worm"), new LeafWormItemComponent());
    itemRegistry.registerCustomComponent(U("gummy_leaf_worm"), new GummyLeafWormItemComponent());
    itemRegistry.registerCustomComponent(U("sickle"), new SickleItemComponent());
    itemRegistry.registerCustomComponent(U("trowel"), new TrowelItemComponent());
    itemRegistry.registerCustomComponent(U("book"), new BookCustomItemComponent());
    const blockRegistry = event.blockComponentRegistry;
    blockRegistry.registerCustomComponent(U("dead_leaves"), new LeavesBlockComponent());
    blockRegistry.registerCustomComponent(U("auto_hammer"), new AutoHammerBlockComponent());
    blockRegistry.registerCustomComponent(U("crucible_campfire"), new CrucibleBlockComponent([FluidType.WATER, FluidType.LAVA]));
    blockRegistry.registerCustomComponent(U("crucible_wood"), new CrucibleBlockComponent([FluidType.WATER]));
    blockRegistry.registerCustomComponent(U("generator"), new GeneratorBlockCustomComponent());
    blockRegistry.registerCustomComponent(U("basic_sieve_interact"), new BasicSieveBlockComponent());
    blockRegistry.registerCustomComponent(U("auto_sieve_interact"), new AutoSieveBlockComponent());
    blockRegistry.registerCustomComponent(U("basic_botany_pot_interact"), new PotBlockComponent());
    blockRegistry.registerCustomComponent(U("crucible_smoke"), new SmokeyCrucibleBlockComponent());
});
system.afterEvents.scriptEventReceive.subscribe(event => {
    const playerName = event.message.trim();
    const player = world.getPlayers().find(p => p.name === playerName);
    if (!player)
        return;
    const id = event.id;
    let recipe = 0;
    if (id === "ftb:book_rocks")
        recipe = 1;
    if (id === "ftb:book_base")
        recipe = 0;
    const r1 = Math.floor(Math.random() * 1000);
    const r2 = Math.floor(Math.random() * 1000);
    const r3 = Math.floor(Math.random() * 1000);
    player.playAnimation("animation.ftb_sieves.book.dummy", {
        controller: "controller.render.ftb_sieves.book_dynamic", // required to trigger pre_animation
        stopExpression: `t.player_name='${player.name}';v._r1=${r1};v._r2=${r2};v._r3=${r3};t.recipe=${recipe};return 0;`
    });
});
