# API

## API

Read holograms, turn their pages, or create holograms of your own from code.

## Maven Repository

```xml
<repositories>
    <repository>
        <id>phoenixplugins</id>
        <url>https://repo.phoenixplugins.com/repository/maven-public/</url>
    </repository>
</repositories>
```

## Dependency

```xml
<dependency>
    <groupId>com.phoenixplugins.phoenixholograms</groupId>
    <artifactId>api</artifactId>
    <version>1.3.0</version>
</dependency>
```

## Getting started

Add PhoenixHolograms to your **plugin.yml** so your plugin loads after it.

```yaml
depend: [PhoenixHolograms]
```

`PhoenixHologramsAPI.get()` returns the service as an `Optional`, empty while the plugin is not enabled. Call it from the main thread. On Folia any region thread or the global region thread works; changes for a player are applied on that player's region.

```java
PhoenixHologramsAPI.get().ifPresent(holograms -> holograms.setPage(player, "lobby", 2));
```

| Method | Returns |
| ------ | ------- |
| `getIdentifiers()`, `exists(String id)` | The ids of every hologram, and whether one exists |
| `getLocation(String id)` | Where a hologram stands, as an `Optional` |
| `getPage(Player, String id)`, `getPageCount(String id)` | The page a player sees, from 1, and how many pages there are |
| `setPage(Player, String id, int page)` | Shows a player a page, from 1 |
| `refresh(String id)` | Redraws a hologram |
| `isValidIdentifier(String id)` | Whether an id is allowed |
| `create(Plugin owner, String id, Location location)` | A new hologram of your own, see below |
| `getHandle(String id)` | The handle of a hologram, as an `Optional` |
| `remove(String id)`, `removeAll(Plugin owner)` | Removes one hologram, or every hologram your plugin made |
| `createPersistent(String id, Location location)` | A new saved hologram, the same as one made with `/pholo create`, see below |
| `registerAction(String type, String syntax, HologramActionHandler handler)` | Adds a click action type, see [Custom Actions](#custom-actions) |
| `unregisterAction(String type)` | Removes an action type you added |
| `showDamageIndicator(Location at, double amount, boolean critical)` | Shows a [damage indicator](#damage-indicators) at a spot |

## Holograms Made by Code

A hologram made with `create` is never saved and never listed in the editor or `/pholo list`. It stays through `/pholo reload` and is removed when your plugin disables.

The returned `HologramHandle` changes it. Lines take the same text as the editor, placeholders and [tokens](../features/animations.md#text-tokens) included.

```java
HologramHandle handle = holograms.create(this, "my_plugin_spawn", location);

handle.setLines(Arrays.asList("&6&lSpawn", "&7Welcome, %player%"));
```

`setLocation` redraws the hologram, while `teleport` slides it to the new spot and is cheap enough to call every tick. After `setManualVisibility(true)` only the players added with `setManualViewer` see the hologram, still within its view distance. `setViewPermission` replaces the hologram's Permission view conditions with one for that node, and an empty node removes them.

## Handle Methods

Pages are counted from 1 and lines from 0, the same as `getPage` and `setLine`. A line method on a page that does not exist throws an `IllegalArgumentException`; `addLine` and `insertLine` on the page after the last one create it.

| Methods | What they change |
| ------- | ---------------- |
| `getLines`, `setLines`, `setLine(int, String)`, `setPages` | The text lines of page 1, or every page as text |
| `getPageCount`, `addPage`, `removePage` | The pages |
| `getLines(page)`, `getLine(page, index)`, `addLine`, `insertLine`, `setLine(page, index, LineSpec)`, `removeLine`, `setLinePages` | Lines of any type on any page, see [Lines](#lines) |
| `getActions`, `setActions`, `setLineActions`, `setClickPermission`, `setClickCooldown` | [Click actions](../main-features/click-actions.md) of the hologram and of one line, per `ClickType`: `LEFT`, `RIGHT`, `SHIFT_LEFT`, `SHIFT_RIGHT` or `ANY` |
| `setLineConditions`, `getViewConditions`, `setViewConditions` | [Line conditions](../main-features/pages-and-lines/line-conditions.md) and [view conditions](../main-features/visibility.md#view-conditions), see [Conditions](#conditions) |
| `getRegions`, `setRegions`, `setViewPermission`, `setVisibleTo`, `setManualVisibility`, `setManualViewer`, `setViewDistance`, `setUpdateInterval` | [Visibility](../main-features/visibility.md) |
| `getAppearance`, `setAppearance`, `setScale`, `setBackground` | [Appearance](../main-features/appearance.md), see [Appearance](#appearance) |
| `isEnabled`, `setEnabled`, `setPageInterval` | Whether it shows at all, and the page rotation |
| `getLocation`, `setLocation`, `teleport` | Where it stands |
| `moveBy(x, y, z)` | Moves it by an amount of blocks. On a hologram linked to an NPC only `y` may be set, and it changes the height above the NPC; returns `false` when the move is refused |
| `linkNpc`, `getLinkedNpc`, `unlinkNpc`, `getLinkOffsetY`, `setLinkOffsetY` | The [NPC link](../main-features/holograms/edit-hologram.md#npc-link) and the height above the NPC's name, up to 16 blocks either way |
| `getLeaderboard`, `setLeaderboard` | The [leaderboard](../features/leaderboards.md) settings, see [Leaderboards](#leaderboards) |
| `remove` | Removes a hologram made by code |

## Lines

A `LineSpec` is built from one of `LineSpec.text`, `item` (a `Material`, an `ItemStack` or a hook id such as `nexo:ruby`), `head`, `image` or `command`, then any [line option](../main-features/pages-and-lines/edit-line.md): scale, height, spin, bob, glow, rotation, offset, shadow, the [style](../main-features/pages-and-lines/line-style.md) overrides, the [effects](../main-features/pages-and-lines/line-effects.md), click actions and conditions. A style override left out follows the hologram. `toBuilder()` makes a changed copy of a line read with `getLine`.

```java
handle.addLine(1, LineSpec.item(Material.DIAMOND).spin(90).glow(true)
        .action(ClickType.RIGHT, "[MESSAGE] &bShiny!").build());
handle.addLine(1, LineSpec.text("&eHello").billboard(Billboard.FIXED).rotation(45, 0).hoverText("&aHi").build());
```

An item the server does not know throws an `IllegalArgumentException`. A command line runs as the console; your plugin decides who may set one.

## Conditions

`ConditionSpec.permission(node)`, `ConditionSpec.noPermission(node)` or `ConditionSpec.compare(placeholder, ConditionType.GREATER_OR_EQUAL, "10")`. Every condition has to pass.

## Appearance

`Appearance.builder()` with any of billboard, scale, line gap, shadow, see-through, background, opacity, alignment, line width, brightness, spin, rotation, offset, double-sided, fade ticks and the three freeze switches. `setAppearance` only changes the values that were set, so a builder with one value changes one option.

```java
handle.setAppearance(Appearance.builder().billboard(Billboard.VERTICAL).fade(10).build());
```

## Leaderboards

`setLeaderboard` takes a `LeaderboardSpec`. `LeaderboardSpec.builder(value)` ranks a [value](../features/leaderboards.md#value-source) the plugin tracks per player, with `period`, `yourRank`, the row formats, size and refresh of the editor. `null` removes the settings. The players show on [rank lines](../features/leaderboards.md#rank-lines): `LineSpec.Builder.rank(n)`, `yourRank()` and `rankText(format)` on text and head lines.

```java
handle.setLeaderboard(LeaderboardSpec.builder("statistic:PLAYER_KILLS")
        .period(LeaderboardPeriod.WEEKLY).yourRank("&7You: #{rank} {value}").build());
handle.addLine(LineSpec.head("").rank(1).build());
handle.addLine(LineSpec.text("").rank(1).build());
handle.addLine(LineSpec.text("").yourRank().build());
```

## Saved Holograms

`createPersistent` makes a hologram that belongs to the server rather than your plugin: it is saved like every other hologram, synced to other servers when [sync](../features/storage.md#cross-server-sync) is on, listed in the editor, and admins edit or delete it like any other. It takes the [defaults](../configurations/main-configuration.md#hologram-defaults) of config.yml and needs a location in a loaded world. The returned handle fills it in, for example with a leaderboard.

## Damage Indicators

`showDamageIndicator` floats a damage number up from a spot, the same as a hurt mob gets, to the players nearby. `critical` uses the critical wording. It follows the [damage indicator](../features/damage-indicators.md) settings, so nothing shows while indicators or Damage are off, in a disabled world, or for an amount of 0 or less. On Folia the number is shown on the region of the spot.

```java
holograms.showDamageIndicator(npc.getLocation().add(0, 2, 0), 7.5, false);
```

## Custom Actions

A registered type shows in the editor's action builder with a single text field, and runs on hologram and line clicks like the built-in ones. The handler gets the hologram's id, the player and everything after the type. Return `false` to stop the actions after it.

```java
holograms.registerAction("QUEST", "<quest>", (hologram, player, argument) -> quests.start(player, argument));
```

Unregister it in your `onDisable`. PhoenixCurrencies uses this for its [currency actions](../plugin-information/action-types.md#currency-actions).

> **Info:**
`HologramsLoadEvent` fires after every load of the plugin, so a plugin can make its holograms again after PhoenixHolograms was reloaded or replaced.

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