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

const fill_level = 10; // Levels players needs to fill up the xp jar

world.afterEvents.itemUse.subscribe(async (event) => {
	const { itemStack, source } = event;
	const player = source;
	const item = itemStack;

	if (item.typeId === "oct_plev:xp_jar") {
		if (player.level >= fill_level) {
			player.addLevels(-fill_level);
			player.runCommand("/clear @s oct_plev:xp_jar 0 1");
			const item = new ItemStack("oct_plev:xp_jar_full");
			player.getComponent("inventory").container.addItem(item);

			player.runCommand("/playsound bottle.dragonbreath @s ~~~ 1 1.2");
		}
		else {
			player.runCommand(`/title @s actionbar §cYou need ${fill_level} levels to fill the Experience Jar!`);
			player.runCommand("/playsound bottle.dragonbreath @s ~~~ 1 1.8")
		}
	}

	if (item.typeId === "oct_plev:xp_jar_full") {
		player.addLevels(fill_level);
		player.runCommand("/clear @s oct_plev:xp_jar_full 0 1");
		const item = new ItemStack("oct_plev:xp_jar");
		player.getComponent("inventory").container.addItem(item);

		player.runCommand("/playsound bottle.dragonbreath @s ~~~ 1 1");
		player.runCommand("/playsound random.levelup @s ~~~ 1 1");
	}
});