import {world, system, Player} from "@minecraft/server";
import { CombatManager } from "../classes/combatManager.js";
import { debug, Check, getCooldownTime, clamp, healthParticle, toColor } from "../mathAndCalculations.js";

// Handles the entire combat. 
//* Very important!
world.afterEvents.entityHitEntity.subscribe((event) => {
	if (world.getDynamicProperty("addon_toggle") == false) return;
    const player = event.damagingEntity;
    const target = event.hitEntity;
    
    if (!(player instanceof Player)) {
    	// The 'player' here is actually not a player. It's for disabling shield knockback on non-player mobs. Don't get confused!
	    const { stats } = player.getItemStats();
	    const currentTime = Date.now();
    	const shieldBlock = Check.shieldBlock(currentTime, player, target, stats);
    	if (shieldBlock) player.applyKnockback(0,0,0,0);
	    return;
    }
    
	player.__leftClick = true;
    if (target?.isValid()) CombatManager.attack({ player, target });
});
