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

const playerControllerMap = new Map();
playerControllerMap.clear();
const books = ["ftb_bees:bee_book"];
class PlayerController {
    constructor(player) {
        this.player = player;
        this.jumpBoiIsOn = false;
        this.justJumped = false;
    }
    tick() {
        if (!this.player) {
            return;
        }
        if (this.player.isJumping && !this.justJumped) {
            if (this.jumpBoiIsOn) {
                this.justJumped = true;
                this.player.applyKnockback(0, 0, 1, 1);
            }
            this.playerJumped();
        }
        if (!this.player.isJumping && this.player.isOnGround && this.justJumped) {
            this.justJumped = false;
        }
    }
    playerJumped() {
        let player = this.player;
        // Const get the players mainhand item
        const equippable = player.getComponent("minecraft:equippable");
        const itemStack = equippable.getEquipment(EquipmentSlot.Mainhand);
        if (!itemStack) {
            return;
        }
        books.forEach((e) => this.closeBlook(itemStack, e));
    }
    /**
     * Closes a book in the players hand
     */
    closeBlook(itemStack, bookName) {
        if (!itemStack.typeId.startsWith(bookName + "_")) {
            return;
        }
        this.player.runCommand("playsound item.book.page_turn @a ~~~");
        this.player.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 ${bookName}`);
    }
}
system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        if (!playerControllerMap.has(player.id)) {
            const controller = new PlayerController(player);
            playerControllerMap.set(player.id, controller);
        }
        else {
            playerControllerMap.get(player.id)?.tick();
        }
    }
});
