# ServerAllocationEvent events

> 3 FeatherPanel plugin events on ServerAllocationEvent (2 emitted by the core): the event names, the arguments each listener gets, where they fire and a PHP listener for each.

Source: https://featherpanel.com/docs/plugins/events/server-allocation/

Part of the [plugin events reference](https://featherpanel.com/docs/plugins/events/). Listeners get the payload values as positional arguments in the order shown, cannot cancel the action, and must catch their own exceptions. 1 of these events are declared but no code emits them yet; they are marked not emitted.

## Setup

Import `ServerAllocationEvent` in your entry class and register listeners in `processEvents()`:

```php
<?php

namespace App\Addons\myplugin;

use App\App;
use App\Plugins\AppPlugin;
use App\Plugins\PluginEvents;
use App\Plugins\Events\Events\ServerAllocationEvent;

class MyPlugin implements AppPlugin
{
    public static function processEvents(PluginEvents $event): void
    {
        $event->on(ServerAllocationEvent::onServerAllocationDeleted(), function ($userUuid, $serverUuid, $allocationId): void {
            App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:user:server:allocation:deleted ' . json_encode(['allocation_id' => $allocationId]));
        });
    }

    public static function pluginInstall(): void
    {
    }

    public static function pluginUninstall(): void
    {
    }
}
```

## `ServerAllocationEvent::onServerAllocationDeleted()`

- Name: `featherpanel:user:server:allocation:deleted`
- emitted at 2 sites
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/ServerAllocationEvent.php#L28)

Docblock: string server uuid, int allocation id. (the emitter passes 3 values, see the table)

Same event name as `ServerEvent::onServerAllocationDeleted()`. A listener on either method receives the emits of both.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `user_uuid` | `$userUuid` | not declared |
| 2 | `server_uuid` | `$serverUuid` | not declared |
| 3 | `allocation_id` | `$allocationId` | `int` |

When it fires (2 sites)

-   [`ServerAllocationController::deleteAllocation`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/User/Server/ServerAllocationController.php#L274)[`DELETE /api/user/servers/{uuidShort}/allocations/{allocationId}`](https://featherpanel.com/docs/api/reference/user/server/allocations/#delete-user-servers-uuidShort-allocations-allocationId)
-   [`DeleteAllocationTool::execute`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Services/Chatbot/Tools/DeleteAllocationTool.php#L181)

```php
$event->on(ServerAllocationEvent::onServerAllocationDeleted(), function ($userUuid, $serverUuid, $allocationId): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:user:server:allocation:deleted ' . json_encode(['allocation_id' => $allocationId]));
});
```

## `ServerAllocationEvent::onServerAllocationError()`

- Name: `featherpanel:user:server:allocation:error`
- not emitted
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/ServerAllocationEvent.php#L45)

Docblock: string error message, array context.

```php
$event->on(ServerAllocationEvent::onServerAllocationError(), function (...$args): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:user:server:allocation:error ' . json_encode($args));
});
```

No code emits this event yet, so the arguments are unknown. The listener accepts any.

## `ServerAllocationEvent::onServerAllocationSetPrimary()`

- Name: `featherpanel:user:server:allocation:set_primary`
- emitted at 2 sites
- 2 payload shapes
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/ServerAllocationEvent.php#L36)

Docblock: string server uuid, int allocation id, bool is\_primary.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `user_uuid` | `$userUuid` | `string`from docblock |
| 2 | `server_uuid` | `$serverUuid` | `int`from docblock |
| 3 | `allocation_id` | `$allocationId` | `int` |

| Arguments passed | Emitted from |
| --- | --- |
| `user_uuid`, `server_uuid`, `allocation_id` | 
`ServerAllocationController::setPrimaryAllocation`

 |
| `user_uuid`, `server_uuid`, `allocation_id`, `is_primary` | 

`SetPrimaryAllocationTool::execute`

 |

When it fires (2 sites)

-   [`ServerAllocationController::setPrimaryAllocation`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/User/Server/ServerAllocationController.php#L415)[`POST /api/user/servers/{uuidShort}/allocations/{allocationId}/primary`](https://featherpanel.com/docs/api/reference/user/server/allocations/#post-user-servers-uuidShort-allocations-allocationId-primary)
-   [`SetPrimaryAllocationTool::execute`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Services/Chatbot/Tools/SetPrimaryAllocationTool.php#L172)

```php
$event->on(ServerAllocationEvent::onServerAllocationSetPrimary(), function ($userUuid, $serverUuid, $allocationId, ...$rest): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:user:server:allocation:set_primary ' . json_encode(['user_uuid' => $userUuid, 'server_uuid' => $serverUuid, 'allocation_id' => $allocationId]));
});
```
