> For the complete documentation index, see [llms.txt](https://tgiann.gitbook.io/tgiann/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tgiann.gitbook.io/tgiann/scripts/tgiann-core/hooks.md). Agents can also search and read this documentation through its [MCP server](https://tgiann.gitbook.io/tgiann/~gitbook/mcp).

# Hooks

Hooks of the player ownable (business) system. Other resources react to purchases, staff changes, rent and money movements, cancel them or extend them without touching tgiann-core.

{% hint style="danger" %}
All hooks work server-side only.
{% endhint %}

{% hint style="success" %}
Same pattern as the tgiann-inventory and tgiann-bank hooks. Pre hooks run before the action and cancel it by returning `false`. Post hooks run after the change was written, their return value is ignored. Query hooks return a table that is merged into the result.
{% endhint %}

## RegisterOwnableHook <a href="#registerownablehook" id="registerownablehook"></a>

```lua
local hookId = exports["tgiann-core"]:RegisterOwnableHook(eventName, function(payload) end, options)
```

* eventName: `string`
* payload: `table` every payload carries `keyName` and `businessName`
* options?: `table`
  * print?: `boolean`
    * Print to the console when triggering the event.
  * keyFilter?: `{ [string]: true }`
    * The event only triggers for the businesses whose `keyName` is one of the keys.

Return:

* hookId: `number`

{% hint style="info" %}
Hooks of a resource are removed automatically when that resource stops. Admin commands (`addOwnableLocation`, `removeOwnableLocation`), the [AddOwnableMember / RemoveOwnableMember](https://tgiann.gitbook.io/tgiann/scripts/tgiann-core/exports.md#addownablemember) exports and the money exports run the same pre hooks as the player actions, the payload then carries `admin = true` and / or `resource` (the calling resource, `source` is nil when an export runs it). A job change coming from the framework does not run pre hooks, only the post hooks.
{% endhint %}

***

## Pre hooks

### businessBuy

Triggered before a player buys a business (boss menu, `ControlPanelBuy` export, admin grant, `AddOwnableMember` export). Returning `false` cancels the purchase, nothing is charged.

* Payload: `table`
  * source?: `number` buyer (the admin on an admin grant, nil from an export)
  * citizenId: `string`
  * price: `number` `0` on an admin grant
  * priceType: `string` `"bank"`, `"cash"`, `"coin"`... or `"admin"`
  * admin?: `boolean` true when granted through the admin command or `AddOwnableMember`
  * resource?: `string` calling resource when granted from an export

**Example**

```lua
-- only characters with a business licence may buy a mechanic shop
exports["tgiann-core"]:RegisterOwnableHook("businessBuy", function(payload)
    if payload.admin then return end
    if not exports["my-licences"]:HasLicence(payload.citizenId, "business") then
        tgiCore.notif(payload.source, "You need a business licence", "error")
        return false
    end
end, { keyFilter = { mechanic_lsc = true, mechanic_bennys = true } })
```

***

### businessSellBack

Triggered before the owner gives a business back (boss menu, `ControlPanelSellBack` export). Returning `false` keeps the business.

* Payload: `table`
  * source: `number`
  * citizenId: `string` owner
  * money: `number` cash left in the business vault

**Example**

```lua
-- no selling back while money is still in the vault
exports["tgiann-core"]:RegisterOwnableHook("businessSellBack", function(payload)
    if payload.money > 0 then
        tgiCore.notif(payload.source, "Withdraw the vault first", "error")
        return false
    end
end)
```

***

### businessTransferRequest

Triggered when the owner sends a transfer offer, before the target sees it. Returning `false` stops the offer.

* Payload: `table`
  * source: `number` owner
  * fromCitizenId: `string`
  * toCitizenId: `string`
  * price: `number` `0` = free
  * priceType: `string`
  * rentMultiplier: `number` multiplier of the current rent cycle, `1` without a bonus

**Example**

```lua
-- a business bought with a rent bonus may only go to a subscriber
exports["tgiann-core"]:RegisterOwnableHook("businessTransferRequest", function(payload)
    if payload.rentMultiplier > 1 and not hasSubscription(payload.toCitizenId) then
        tgiCore.notif(payload.source, "This player can not receive a bonus business", "error")
        return false
    end
end)
```

***

### businessTransfer

Triggered when the target accepts a transfer offer, when an admin (command or `AddOwnableMember`) hands an owned business to another player, or when `RemoveOwnableMember` removes the boss of a business that keeps its staff (`toCitizenId = "0"`). Returning `false` cancels the transfer, nothing is charged.

* Payload: `table`
  * source?: `number` target (the admin on an admin grant, nil from an export)
  * fromCitizenId: `string`
  * toCitizenId: `string` `"0"` when the business goes on without a boss
  * price: `number`
  * priceType: `string` `"admin"` on an admin grant
  * rentMultiplier: `number`
  * admin?: `boolean`
  * resource?: `string` calling resource when run from an export

**Example**

```lua
-- log every ownership change to your own webhook
exports["tgiann-core"]:RegisterOwnableHook("businessTransfer", function(payload)
    myLog(("%s: %s -> %s for $%s"):format(payload.businessName, payload.fromCitizenId, payload.toCitizenId, payload.price))
end)
```

***

### businessHireEmployee

Triggered before an employee is added (boss menu, admin grant or `AddOwnableMember` export). Returning `false` cancels the hire.

* Payload: `table`
  * source?: `number` owner (the admin on an admin grant, nil from an export)
  * ownerCitizenId: `string` `"0"` when the business has staff but no boss
  * citizenId: `string` the player being hired
  * admin?: `boolean`
  * resource?: `string` calling resource when run from an export

**Example**

```lua
-- a character may work at one business of this script at a time
exports["tgiann-core"]:RegisterOwnableHook("businessHireEmployee", function(payload)
    for keyName, info in pairs(exports["tgiann-core"]:GetPlayerControlPanelKeyNames(payload.citizenId, true)) do
        if not info.owner and keyName:find("^mechanic_") then
            tgiCore.notif(payload.source, "This player already works at a mechanic shop", "error")
            return false
        end
    end
end, { keyFilter = { mechanic_lsc = true, mechanic_bennys = true } })
```

***

### businessFireEmployee

Triggered before an employee is removed from the boss menu or with the `RemoveOwnableMember` export. Returning `false` keeps the employee.

* Payload: `table`
  * source?: `number` owner, nil from an export
  * ownerCitizenId: `string`
  * citizenId: `string` employee
  * admin?: `boolean` true when removed with `RemoveOwnableMember`
  * resource?: `string` calling resource when run from an export

**Example**

```lua
-- protect employees while they are on duty
exports["tgiann-core"]:RegisterOwnableHook("businessFireEmployee", function(payload)
    if payload.admin then return end
    if exports["my-duty"]:IsOnDuty(payload.citizenId) then
        tgiCore.notif(payload.source, "You can not fire someone on duty", "error")
        return false
    end
end)
```

***

### businessPayRent

Triggered before the owner pays the rent (boss menu or `ControlPanelPayRent` export). Returning `false` cancels the payment.

* Payload: `table`
  * source: `number`
  * citizenId: `string` owner
  * rentMoney: `number`
  * priceType: `string`

**Example**

```lua
-- rent can only be paid while the business bank account is not frozen
exports["tgiann-core"]:RegisterOwnableHook("businessPayRent", function(payload)
    local iban = exports["tgiann-bank"]:GetOwnableIban(payload.keyName)
    if iban and exports["tgiann-bank"]:IsAccountFrozen(iban) then
        tgiCore.notif(payload.source, "The business account is frozen", "error")
        return false
    end
end)
```

***

### businessWithdraw

Triggered before the owner takes the vault money as cash. Returning `false` keeps the money in the vault.

* Payload: `table`
  * source: `number`
  * citizenId: `string` owner
  * amount: `number` vault balance

**Example**

```lua
-- no cash withdrawals above a limit, use the bank instead
exports["tgiann-core"]:RegisterOwnableHook("businessWithdraw", function(payload)
    if payload.amount > 250000 then
        tgiCore.notif(payload.source, "Amounts this large go through the bank", "error")
        return false
    end
end)
```

***

### businessAddMoney

Triggered when a script credits the vault through `ControlPanelAddMoney`. Returning `false` drops the income.

* Payload: `table`
  * citizenId: `string` owner
  * amount: `number`
  * resource: `string` calling resource

**Example**

```lua
-- route business income to the bank account instead of the core vault
exports["tgiann-core"]:RegisterOwnableHook("businessAddMoney", function(payload)
    if exports["tgiann-bank"]:AddOwnableMoney(payload.keyName, payload.amount, payload.resource) then
        return false -- handled, keep it out of the vault
    end
end)
```

***

### businessRemoveMoney

Triggered when a script debits the vault through `ControlPanelRemoveMoney`. Returning `false` refuses it.

* Payload: `table`
  * citizenId: `string` owner
  * amount: `number`
  * resource: `string` calling resource

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessRemoveMoney", function(payload)
    if payload.resource == "some-untrusted-script" then return false end
end)
```

***

### businessRemove

Triggered before a business is removed by the `removeOwnableLocation` command, the `DeleteControlPanel` export, or the `RemoveOwnableMember` export taking out the boss of a business without employees. Returning `false` keeps the business.

* Payload: `table`
  * source?: `number` admin, nil from an export
  * citizenIds: `string[]` owner and employees
  * isBought: `boolean`
  * resource?: `string` calling resource when removed from an export
  * admin: `boolean`

**Example**

```lua
-- never let a script delete a business that still has staff
exports["tgiann-core"]:RegisterOwnableHook("businessRemove", function(payload)
    if not payload.admin and #payload.citizenIds > 1 then return false end
end)
```

***

## Query hooks

### businessRentPeriod

Runs when a player opens the buy screen or the owner menu, buys the business, or pays the rent. Never cancels anything: return `{ multiplier?, label? }`. `multiplier` scales the rent period the player gets on that purchase / rent payment (multipliers of several hooks are multiplied together and stored per business until the next payment), `label` is shown next to the rent period in the boss menu and in tgiann-shop.

* Payload: `table`
  * source?: `number` nil when the player is offline
  * citizenId: `string`
  * rentPeriodDay: `number` base period
  * priceType: `string`

**Example**

```lua
-- subscribers rent for twice the period, everyone else sees a hint
exports["tgiann-core"]:RegisterOwnableHook("businessRentPeriod", function(payload)
    if payload.priceType == "coin" then return end
    if hasSubscription(payload.citizenId) then
        return { multiplier = 2, label = "Subscriber bonus: 2x rent period" }
    end
    return { label = "Subscribe to rent for twice as long" }
end)
```

***

## Post hooks

Every post payload carries `citizenIds`, everyone affected by the change. `"0"` is the placeholder owner of a business that has staff but no boss.

### businessBought

* Payload: `table`
  * citizenIds: `string[]` `{ owner }`
  * owner: `string`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessBought", function(payload)
    if payload.owner == "0" then return end
    exports["my-achievements"]:Grant(payload.owner, "first_business")
end)
```

***

### businessOwnerChanged

* Payload: `table`
  * citizenIds: `string[]` `{ previousOwner, owner }`
  * previousOwner: `string`
  * owner: `string`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessOwnerChanged", function(payload)
    exports["my-doorlock"]:MoveKeys(payload.keyName, payload.previousOwner, payload.owner)
end)
```

***

### businessEmployeeAdded / businessEmployeeRemoved

* Payload: `table`
  * citizenIds: `string[]` `{ employee }`
  * citizenId: `string` employee
  * owner: `string`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessEmployeeAdded", function(payload)
    exports["my-doorlock"]:GiveKey(payload.keyName, payload.citizenId)
end)

exports["tgiann-core"]:RegisterOwnableHook("businessEmployeeRemoved", function(payload)
    exports["my-doorlock"]:RemoveKey(payload.keyName, payload.citizenId)
end)
```

***

### businessClosed

The business went back on sale: sold back, rent expired, removed by an admin or script, or the last member left.

* Payload: `table`
  * citizenIds: `string[]` `{ owner, ...employees }`
  * reason: `"sellBack" | "expired" | "removed" | "closed"`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessClosed", function(payload)
    if payload.reason == "expired" then
        for _, cid in ipairs(payload.citizenIds) do
            exports["tgiann-phone"]:SendMail(cid, "Your business closed, the rent was not paid")
        end
    end
end)
```

***

### businessRentPaid

* Payload: `table`
  * citizenIds: `string[]` `{ owner }`
  * citizenId: `string`
  * rentMoney: `number`
  * priceType: `string`
  * rentTime: `number` epoch of this payment
  * canRentTime: `number` epoch from which the next payment is allowed
  * lastRentTime: `number` epoch when the business expires

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessRentPaid", function(payload)
    local iban = exports["tgiann-bank"]:GetOwnableIban(payload.keyName)
    if iban then exports["tgiann-bank"]:AddHistory(iban, "outcome", payload.rentMoney, "Rent") end
end)
```

***

### businessBalanceChanged

The vault balance changed: script income, owner withdrawal or `ControlPanelRemoveMoney`.

* Payload: `table`
  * citizenIds: `string[]` `{ owner }`
  * owner: `string`
  * delta: `number` positive on income, negative on withdrawal
  * money: `number` new balance

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessBalanceChanged", function(payload)
    if payload.delta > 0 then
        exports["tgiann-phone"]:SendNotification(payload.owner, ("%s earned $%s"):format(payload.businessName, payload.delta))
    end
end)
```

***

### businessMoneyWithdrawn

* Payload: `table`
  * citizenIds: `string[]` `{ owner }`
  * citizenId: `string`
  * source: `number`
  * amount: `number`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessMoneyWithdrawn", function(payload)
    myLog(("%s took $%s out of %s"):format(payload.citizenId, payload.amount, payload.businessName))
end)
```

***

### businessTransferDeclined

* Payload: `table`
  * citizenIds: `string[]` `{ fromCitizenId, toCitizenId }`
  * fromCitizenId: `string`
  * toCitizenId: `string`
  * price: `number`
  * priceType: `string`

**Example**

```lua
exports["tgiann-core"]:RegisterOwnableHook("businessTransferDeclined", function(payload)
    myLog(("%s declined the %s offer"):format(payload.toCitizenId, payload.businessName))
end)
```

## RemoveOwnableHooks <a href="#removeownablehooks" id="removeownablehooks"></a>

Removes a hook created by the invoking resource with the specified id.\
If no id is specified then all hooks registered by the resource are removed.

```lua
exports["tgiann-core"]:RemoveOwnableHooks(id)
```

* id?: `number`
