# Events

## Events

Every event is an ordinary Bukkit event in `com.phoenixplugins.phoenixcurrencies.api.events`.

| Event | Fired when | Carries | Cancellable |
| ----- | ---------- | ------- | ----------- |
| `PreBalanceChangeEvent` | Before a single balance changes: admin give, take, set, reset, Vault, the API, banknotes, rollbacks | `getPlayer()`, `getCurrency()`, `getType()` (`DEPOSIT`, `WITHDRAW`, `SET`), `getAmount()` / `setAmount(BigDecimal)`, `getReason()`, `getActor()` | Yes |
| `PrePaymentEvent` | Before one player pays another, from a command, the pay menu or `transfer` in the API | `getSender()`, `getReceiver()`, `getCurrency()`, `getAmount()` / `setAmount(BigDecimal)` (what the receiver gets), `getFee()`, `getReason()` | Yes |
| `PreExchangeEvent` | Before a player exchanges one currency for another | `getPlayer()`, `getFrom()`, `getTo()`, `getPaid()` / `setPaid(BigDecimal)`, `getReceived()` / `setReceived(BigDecimal)` | Yes |
| `BalanceChangeEvent` | After a balance changed, for any reason | `getPlayer()`, `getCurrency()`, `getPreviousExact()`, `getBalanceExact()`, `getReason()` | No |
| `LeaderboardRefreshEvent` | After the leaderboards were worked out again | `getCurrencies()`, the ids that were refreshed | No |

A payment or exchange fires only its own pre event, not a `PreBalanceChangeEvent` for each side. A changed amount is cut to the currency's decimals. Cancelling makes the change return `CANCELLED` and nothing moves.

The player can be offline, so look them up with `Bukkit.getPlayer(uuid)` before treating them as online. `getReason()` is a short key such as `pay`, `give`, `exchange` or `vault`, the same kinds the translation file names under `reasons`.

## Threads

| Event | Thread |
| ----- | ------ |
| Pre events | The thread making the change, before it is made, so the change waits for your listener. `isAsynchronous()` is `true` when that is not a server tick thread: commands on offline players, Vault calls from async tasks, the API for offline players. On Folia a change made on a region thread fires there as a normal event. |
| `BalanceChangeEvent` | Paper: the main thread. Folia: the region of the player when they are online here, else the global region. |
| `LeaderboardRefreshEvent` | Paper: the main thread. Folia: the global region. |

A pre event listener must be quick and must not touch the world, entities or inventories when `isAsynchronous()` is `true`. Hop to the right thread for that, and decide the outcome from data you already have. A listener that throws stops nothing; one that fires a pre event on the wrong thread gets the change refused and a console error.

```java
@EventHandler
public void onPay(PrePaymentEvent event) {
    if (event.getCurrency().getId().equals("gems") && event.getAmount().compareTo(BigDecimal.valueOf(10_000)) > 0) {
        event.setCancelled(true);
    }
}
```

```java
@EventHandler
public void onBalanceChange(BalanceChangeEvent event) {
    Player player = Bukkit.getPlayer(event.getPlayer());
    BigDecimal goal = BigDecimal.valueOf(1000);

    if (player != null && event.getBalanceExact().compareTo(goal) >= 0
            && event.getPreviousExact().compareTo(goal) < 0) {
        player.sendMessage("You reached 1,000 gems!");
    }
}
```

Source: https://www.phoenixplugins.com/docs/phoenix-currencies/api/events