# Revscriptsys

### Script Structure

> **Constructor**:
>
> * _CreatureEvent(eventName)_
> * > **eventName**: equivalent to the `name` tag in XML.
>
> **Events**:
>
> * onLogin(player)
> * onLogout(player)
> * onThink(creature, interval)
> * onPrepareDeath(creature, killer)
> * onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
> * onKill(creature, target)
> * onAdvance(player, skill, oldLevel, newLevel)
> * onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
> * onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
> * onTextEdit(player, item, text)
> * onModalWindow(player, modalWindowId, buttonId, choiceId)
> * onExtendedOpcode(player, opcode, buffer)
>
> **Methods**:
>
> * creatureEvent:register(): Finalizes the creatureEvent's definition and registers it.
> * creatureEvent:type(eventType):
> * > **eventType**: Equivalent to the `type` tag in XML.

### Examples

```lua
local loginEvent = CreatureEvent("newLoginEvent")
loginEvent:type("login")

function loginEvent.onLogin(player)
    print(player:getName() .. " has logged in!")
    return true
end

loginEvent:register()
```

```lua
local function damageMitigator(primaryDamage, secondaryDamage, mul)
    primaryDamage = primaryDamage - (primaryDamage * mul)
    secondaryDamage = secondaryDamage - (secondaryDamage * mul)
    return primaryDamage, secondaryDamage
end

local healthChange = CreatureEvent("healthChangeMitigate")
healthChange:type("healthchange")

function healthChange.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
    -- Reduce health damage by 50%
    primaryDamage, secondaryDamage = damageMitigator(primaryDamage, secondaryDamage, 0.5)
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

healthChange:register()

local manaChange = CreatureEvent("manaChangeMitigate")
manaChange:type("manachange")

function manaChange.onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
    -- Reduce mana damage by 75%
    primaryDamage, secondaryDamage = damageMitigator(primaryDamage, secondaryDamage, 0.75)
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

manaChange:register()

local loginEvent = CreatureEvent("mitigatorRegistration")
loginEvent:type("login")

function loginEvent.onLogin(player)
    -- Register both health/mana change mitigators to every single player who logs in
    player:registerEvent("healthChangeMitigate")
    player:registerEvent("manaChangeMitigate")
    return true
end

loginEvent:register()
```
