import * as Minecraft from "mojang-minecraft";
import { getItems, findItem } from "./lib/inventory.js";
import { getScore } from "./lib/getScore.js";

let worldHasPlayer = false 

Minecraft.world.events.playerJoin.subscribe(player => {
	let players = Minecraft.world.getPlayers()
	
	for (let player of players) {
		player.runCommand("execute @s ~~~ function joined")
	}
	
	worldHasPlayer = true
})

const TICKS_IN_TWO_SECONDS = 20 * 2

Minecraft.world.events.tick.subscribe(eventData => {

	const { currentTick } = eventData

	if (!worldHasPlayer || currentTick % TICKS_IN_TWO_SECONDS !== 0) return;
	
	let players = Minecraft.world.getPlayers()
	
    for (let player of players) {
	    let exLim = getScore('exhaust_lim', player, {minimun: 1, maximum: 10000})
	    player.runCommand(`scoreboard players add @s[scores={exhaust=!${exLim}}] exhaust 1`);
    }
})
