import { world, system } from "@minecraft/server";
import { playerJoins } from "./events/player_joins";
import { itemLore } from "./items/item_lore";
import { itemUseController } from "./items/item_use_controller";
import { entityHitEntityController } from "./entity/entity_hit_entity_controller";
import { playerInteractEntityController } from "./entity/player_interact_entity_controller";
import { showCampBoxHint } from "./entity/camp_controller";
import { itemHold } from "./items/item_hold";
import { backpackHandler } from "./utilities/backpack_handler";
import { tentHandler } from "./utilities/tent_handler";
import { diamondPoppyHandler } from "./utilities/diamond_poppy_handler";
import { ancientLanternHandler } from "./utilities/ancient_lantern_handler";
import { ceramicPotHandler } from "./utilities/ceramic_pot_handler";
import { fireflyJarHandler } from "./utilities/firefly_jar_handler";
import { bellStaffHandler } from "./utilities/bell_staff_handler";
import { signHandler } from "./utilities/sign_handler";
import { roofHandler } from "./utilities/roof_handler";
import { flagpoleHandler } from "./utilities/flagpole_handler";
import { spikeFenceHandler } from "./utilities/spike_fence_handler";
import { miniJukeboxOnPlayerHandler } from "./utilities/mini_jukebox_handler";
import { miniJukeboxHandler } from "./utilities/mini_jukebox_entity_handler";
import { kettleHandler } from "./utilities/kettle_handler";
import { benchHandler } from "./utilities/bench_handler";
import { foodHandler } from "./utilities/food_handler";
import { flashlightHandler } from "./utilities/flashlight";
import { checkForBpBlocks } from "./utilities/bp_block";
import { singleBenchBlock } from "./utilities/single_bench";
import { guideBook } from "./items/guidebook";
let curTick = 0;
const INIT_TIME = 0;

singleBenchBlock();
new miniJukeboxHandler();
new miniJukeboxOnPlayerHandler();
new spikeFenceHandler();
new flagpoleHandler();
new signHandler();
new fireflyJarHandler();
new ceramicPotHandler();
new backpackHandler();
new tentHandler();
new diamondPoppyHandler();
new ancientLanternHandler();
new bellStaffHandler();
new roofHandler();
new kettleHandler();
new benchHandler();
new foodHandler();

// Subscribe to an event that calls every Minecraft tick
system.runInterval(() => {
  try {
    if (curTick > INIT_TIME) {
      const allPlayers = world.getAllPlayers();
      allPlayers.forEach((player) => {
        if (!player.isValid()) return;
        else {
          itemLore(player);
          itemHold(player);
          guideBook(player);
          try {
            checkForBpBlocks(player, curTick);
          } catch (error) {}
          try {
            showCampBoxHint(player);
          } catch (error) {}
          try {
            flashlightHandler(player);
          } catch (error) {}
        }
      });
    }
    curTick++;
  } catch (e) {
    console.warn(e);
  }
}, 0);

world.afterEvents.playerInteractWithEntity.subscribe(playerInteractEntityController);
world.afterEvents.playerSpawn.subscribe(playerJoins);
