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

const TAG_ARMOR_EFFECT_LEVITATION = 'oct_plev_armor_effect_god_levitation'
const TAG_ARMOR_EFFECT_SLOW_FALLING = 'oct_plev_armor_effect_god_slow_falling'

// Armor effect on god armor
function main() {
	
	for (let player of world.getAllPlayers()) {
		const armorEquipped = player.getComponent(EntityEquippableComponent.componentId)

		const hasHelmet = armorEquipped.getEquipment(EquipmentSlot.Head)?.typeId == "oct_plev:god_helmet";
		const hasChestplate = armorEquipped.getEquipment(EquipmentSlot.Chest)?.typeId == "oct_plev:god_chestplate";
		const hasLeggings = armorEquipped.getEquipment(EquipmentSlot.Legs)?.typeId == "oct_plev:god_leggings";
		const hasBoots = armorEquipped.getEquipment(EquipmentSlot.Feet)?.typeId == "oct_plev:god_boots";

		// const hasFullArmor = hasHelmet && hasChestplate && hasLeggings && hasBoots
		// const hasAnyArmor = hasHelmet || hasChestplate || hasLeggings || hasBoots
		const hasThreeParts = ((hasHelmet && hasChestplate && hasLeggings) || (hasHelmet && hasChestplate && hasBoots) || (hasHelmet && hasLeggings && hasBoots) || (hasChestplate && hasLeggings && hasBoots))

		if(hasThreeParts && !player.hasTag(TAG_ARMOR_EFFECT_LEVITATION) && player.isJumping) {
			player.addTag(TAG_ARMOR_EFFECT_LEVITATION);			
			player.runCommand("/effect @s levitation 100000 3 true");
		}
		else if (!hasThreeParts && player.hasTag(TAG_ARMOR_EFFECT_LEVITATION) || !player.isJumping) {
			player.removeTag(TAG_ARMOR_EFFECT_LEVITATION);
			player.runCommand("/effect @s levitation 0");
		
		}

		if(hasThreeParts && !player.hasTag(TAG_ARMOR_EFFECT_SLOW_FALLING)) {
			player.addTag(TAG_ARMOR_EFFECT_SLOW_FALLING);			
			player.runCommand("/effect @s slow_falling 100000 3 true");
		}
		else if (!hasThreeParts && player.hasTag(TAG_ARMOR_EFFECT_SLOW_FALLING)) {
			player.removeTag(TAG_ARMOR_EFFECT_SLOW_FALLING);
			player.runCommand("/effect @s slow_falling 0");
		
		}
	}
}
system.runInterval(main, 5);