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

const TAG_ARMOR_EFFECT = 'oct_plev_armor_effect_diving'

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

		const hasHelmet = armorEquipped.getEquipment(EquipmentSlot.Head)?.typeId == "oct_plev:diving_helmet";
		const hasChestplate = armorEquipped.getEquipment(EquipmentSlot.Chest)?.typeId == "oct_plev:diving_chestplate";
		const hasLeggings = armorEquipped.getEquipment(EquipmentSlot.Legs)?.typeId == "oct_plev:diving_leggings";
		const hasBoots = armorEquipped.getEquipment(EquipmentSlot.Feet)?.typeId == "oct_plev:diving_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)) {
			player.addTag(TAG_ARMOR_EFFECT);			
			player.runCommand("/effect @s conduit_power 100000 1 true");
		}
		else if (!hasThreeParts && player.hasTag(TAG_ARMOR_EFFECT)) {
			player.removeTag(TAG_ARMOR_EFFECT);
			player.runCommand("/effect @s conduit_power 0");
		
		}
	}
}
system.runInterval(main, 5);