library DamageBlockSpell requires DamageEvent initializer init
globals
//configuration constants (I use capitals for these)
private integer BLOCK_ABILITY = 'A001' //rawcode of the ability, see this in the object editor by pressing ctrl+d
private integer BLOCK_BUFF = 'B001' //rawcode of the buff
private integer BLOCK_TIMES = 3 //how many times to block
private real BLOCK_MINIMUM = 5.00
//non-constants
private integer BlockCount //doesn't need to be initialized
public trigger CastTrigger //will be initialized below, only primitives can be init'd in globals blocks
endglobals
private function BlockDamage takes nothing returns nothing
local boolean B_min //splitting up your conditions into multiple booleans can sometimes be useful
local boolean B_type //but because this funtion runs ANY time ANY unit takes damage,
local boolean B_controller //we don't want to calculate these booleans unless we need to
if GetUnitAbilityLevel(PDDS.target, BLOCK_BUFF) > 0 then
set B_min = PDDS.amount >= BLOCK_MINIMUM
set B_type = IsUnitType(PDDS.source, UNIT_TYPE_HERO)
set B_controller = GetPlayerController(GetOwningPlayer(PDDS.source)) == MAP_CONTROL_USER
if B_min and (B_type or B_controller) then
set BlockCount = BlockCount+1
set PDDS.amount = 0.00
if BlockCount >= BLOCK_TIMES then
call UnitRemoveAbility(PDDS.target, BLOCK_BUFF)
endif
endif
endif
endfunction
private function onCast takes nothing returns nothing
if GetSpellAbilityId() == BLOCK_ABILITY then
set BlockCount = 0
endif
endfunction
private function init takes nothing returns nothing
set CastTrigger = CreateTrigger() //set up the trigger to detect spell casts, which is necessary because the spell block count will break without this if a unit dies with the buff active (unlikely) or it is dispelled
call TriggerRegisterAnyUnitEventBJ(CastTrigger, EVENT_PLAYER_UNIT_SPELL_EFFECT) //this BJ saves you lots of time
call TriggerAddAction(CastTrigger, function onCast)
call AddDamageHandler(function BlockDamage) //The PDD call that tells BlockDamage to run when things take damage
endfunction
endlibrary