# API reference

## Introduction

AT's application programming interface was introduced in version 1.3.0 and is entirely mcfunction-based. Its purpose is to provide a unified way to interact with AT's timer and configuration systems, thereby facilitating implementation into external projects. All API functions are located in the *at-tapi* namespace and write their result values to NBT storage.

## Methods

Inspired by [SNMP](https://en.wikipedia.org/wiki/Simple_Network_Management_Protocol), the API system can be divided into three request types, also known as methods:

* **Get:** Enables reading of timer data (timer units, configuration vales, ...)
* **Set:** Enables modifying timer data (timer units, configuration vales, ...)
* **Trap:** Notifies based on timer events (continue, pause, reset, reach full timer unit, ...)

## Result values

When invoked, API functions provide some information about the type of request as well as multiple return values, indicating whether the current request was successful or failed. This data is written to AT's main NBT storage *animated-timer:data* (path: *at.api.result*). Due to compatibility reasons (especially in cases where multiple entities are querying the API), this path cannot be changed.

In detail, the API provides the following result values:

<table><thead><tr><th width="270.0167236328125" align="center">Path</th><th width="128.4000244140625" align="center">Type</th><th align="center">Description &#x26; sample values</th></tr></thead><tbody><tr><td align="center">at.api.result.data</td><td align="center">Compound</td><td align="center">Output data of API Get function</td></tr><tr><td align="center">at.api.result.message</td><td align="center">String</td><td align="center">Brief description of request status<br><br><em>Sample values:</em><br><em>"OK", "Applied successfully", "Error: ..."</em></td></tr><tr><td align="center">at.api.result.request_context</td><td align="center">String</td><td align="center">Request target / API endpoint<br><br><em>Sample values:</em><br><em>"full_configuration", "visibility", "remove"</em></td></tr><tr><td align="center">at.api.result.request_method</td><td align="center">String</td><td align="center">Type of request<br><br><em>Possible values:</em><br><em>"get", "set", "trap"</em></td></tr><tr><td align="center">at.api.result.return_code</td><td align="center">Byte</td><td align="center">Return code of API function<br><br><em>Possible values:</em><br><em>0b - Error</em><br><em>1b - Success</em></td></tr><tr><td align="center">at.api.result.success</td><td align="center">Byte</td><td align="center">Only set if API function returns success<br><br><em>Possible values:</em><br><em>1b - Success</em></td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FraYTzZN8WdPdkd8cWj5Y%2Fapi-result-values0.png?alt=media&#x26;token=eddb76c6-44f1-4048-97f0-d27174665ce4" alt=""><figcaption></figcaption></figure>

In addition to the *return\_code* being accessible through NBT storage, it is also returned directly from all API functions. This enables */execute* structures like this to work:

`/execute if function at-tapi:set/render/caz run say Changed render type to CAZ.`

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2F9SYbBTLTCp2d3OKs12sJ%2Fapi-result-execute0.png?alt=media&#x26;token=93f13aaf-6b5f-456e-8f53-021d2199ff07" alt=""><figcaption></figcaption></figure>

## Functions - Get

As there is no situation where any of the below API Get functions will fail, they always return with 1. The term "Output" refers to the values that are saved to *at.api.result.data* during execution.

### Actionbar string

**Description:** Reads final actionbar string including render patterns. Prefix & suffix fields are excluded.

**Usage:** `/function at-tapi:get/actionbar_string`

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">actionbar</td><td align="center">String</td><td align="center">Timer: 2h 43m 12s</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FGjXMOHr5UiQF7BdDgapW%2Fapi-get-actionbar-string0.png?alt=media&#x26;token=3f018217-b57d-47f5-9299-76ad3f0a10cb" alt=""><figcaption></figcaption></figure>

### Current time

**Description:** Reads individual timer units including ticks.

**Usage:** `/function at-tapi:get/current_time`

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">tick</td><td align="center">Integer</td><td align="center">14</td></tr><tr><td align="center">second</td><td align="center">Integer</td><td align="center">58</td></tr><tr><td align="center">minute</td><td align="center">Integer</td><td align="center">3</td></tr><tr><td align="center">hour</td><td align="center">Integer</td><td align="center">22</td></tr><tr><td align="center">day</td><td align="center">Integer</td><td align="center">0</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FQy5O1QQU0vXRQYxYR2XZ%2Fapi-get-current-time0.png?alt=media&#x26;token=9ecd976d-2e49-49dc-8ecd-6237d63f07b4" alt=""><figcaption></figcaption></figure>

### Full configuration

**Description:** Reads both visual & behavioral configuration settings.

**Usage:** `/function at-tapi:get/full_configuration`

Note: Many values, especially boolean ones, internally use numbers to represent their current state. Because of this, the API system reads both its numerical value (*\_num*) as well as verbose one (*\_vrb*), each of which having their own suffixes attached.

The values featured below are just a small selection of what is actually being read by the API function:

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">mode_num</td><td align="center">Integer</td><td align="center">2</td></tr><tr><td align="center">mode_vrb</td><td align="center">String</td><td align="center">down</td></tr><tr><td align="center">pattern</td><td align="center">String</td><td align="center">Timer: %t</td></tr><tr><td align="center">prefix</td><td align="center">String</td><td align="center">Hello world!</td></tr><tr><td align="center">static_bold1_num</td><td align="center">Integer</td><td align="center">0</td></tr><tr><td align="center">static_bold1_vrb</td><td align="center">String</td><td align="center">false</td></tr><tr><td align="center">animated_animation</td><td align="center">String</td><td align="center">purple-orange</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FyEEHGf5KsRsTLENeWWbF%2Fapi-get-full-configuration0.png?alt=media&#x26;token=99b7a714-3dff-40f6-8558-6f39c9d12200" alt=""><figcaption></figcaption></figure>

### Local version

**Description:** Reads numerical patch number & version string.

**Usage:** `/function at-tapi:get/local_version`

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">numerical</td><td align="center">Integer</td><td align="center">103010</td></tr><tr><td align="center">verbose</td><td align="center">String</td><td align="center">1.3.1</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FzocLUMYIq58L67Gu4eOv%2Fapi-get-local-version0.png?alt=media&#x26;token=2c756a9c-2c49-4112-86d9-90d942585531" alt=""><figcaption></figcaption></figure>

### Restore values

**Description:** Reads timer restore values (last manually set time thats get loaded when counting down timer is being reset).

**Usage:** `/function at-tapi:get/restore_values`

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">second</td><td align="center">Integer</td><td align="center">0</td></tr><tr><td align="center">minute</td><td align="center">Integer</td><td align="center">0</td></tr><tr><td align="center">hour</td><td align="center">Integer</td><td align="center">12</td></tr><tr><td align="center">day</td><td align="center">Integer</td><td align="center">1</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2Fgnzg5E1gImVW3tqdXWQb%2Fapi-get-restore-values0.png?alt=media&#x26;token=6bd646c4-bf8b-455e-8acd-fbde26a60f73" alt=""><figcaption></figcaption></figure>

### Timer state

**Description:** Reads timer paused / counting up / counting down state.

**Usage:** `/function at-tapi:get/timer_state`

<table><thead><tr><th width="262.39990234375" align="center">Output</th><th width="168.916748046875" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">numerical</td><td align="center">Integer</td><td align="center">1</td></tr><tr><td align="center">verbose</td><td align="center">String</td><td align="center">up</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2FbUz6Rdu8vwcgCgUZEnD8%2Fapi-get-timer-state0.png?alt=media&#x26;token=045fa7f0-eede-47fe-8577-a5ee6a3f9b6e" alt=""><figcaption></figcaption></figure>

## Functions - Set

### Timer controls

<table><thead><tr><th width="279.083251953125" align="center">Description &#x26; possible errors</th><th align="center">Usage</th></tr></thead><tbody><tr><td align="center">Control timer state<br>(paused / running)</td><td align="center"><code>/function at-tapi:set/state/paused</code><br><code>/function at-tapi:set/state/running</code></td></tr><tr><td align="center">Set current time<br><br><em>Possible errors:</em><br><em>Invalid time specified</em></td><td align="center"><code>/function at-tapi:set/time {tick:0,second:0,minute:30,hour:1,day:0}</code></td></tr></tbody></table>

### Configuration

<table><thead><tr><th width="279.083251953125" align="center">Description &#x26; possible errors</th><th align="center">Usage</th></tr></thead><tbody><tr><td align="center">Control timer mode<br>(up / down)</td><td align="center"><code>/function at-tapi:set/mode/up</code><br><code>/function at-tapi:set/mode/down</code></td></tr><tr><td align="center">Control player requirement</td><td align="center"><code>/function at-tapi:set/online/ignored</code><br><code>/function at-tapi:set/online/required</code></td></tr><tr><td align="center">Change render pattern<br><br><em>Possible errors:</em><br><em>Pattern exceeds max length</em><br><em>Pattern cannot be empty</em></td><td align="center"><code>/function at-tapi:set/pattern {text:"%t"}</code></td></tr><tr><td align="center">Change timer prefix</td><td align="center"><code>/function at-tapi:set/prefix {text:"foo "}</code></td></tr><tr><td align="center">Change timer suffix</td><td align="center"><code>/function at-tapi:set/suffix {text:" bar"}</code></td></tr><tr><td align="center">Control render type</td><td align="center"><code>/function at-tapi:set/render/caz</code><br><code>/function at-tapi:set/render/saz</code><br><code>/function at-tapi:set/render/snz</code><br><code>/function at-tapi:set/render/taz</code><br><code>/function at-tapi:set/render/tnz</code></td></tr><tr><td align="center">Control style type</td><td align="center"><code>/function at-tapi:set/style/static</code><br><code>/function at-tapi:set/style/animated</code></td></tr><tr><td align="center">Control trigger scoreboards</td><td align="center"><code>/function at-tapi:set/trigger/disabled</code><br><code>/function at-tapi:set/trigger/enabled</code></td></tr><tr><td align="center">Control visibility</td><td align="center"><code>/function at-tapi:set/visibility/hidden</code><br><code>/function at-tapi:set/visibility/shown</code></td></tr></tbody></table>

### Static style type

<table><thead><tr><th width="312.583251953125" align="center">Description &#x26; possible errors</th><th align="center">Usage</th></tr></thead><tbody><tr><td align="center">Control bold style of primary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_bold1/false</code><br><code>/function at-tapi:set/static_bold1/true</code></td></tr><tr><td align="center">Control bold style of secondary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_bold2/false</code><br><code>/function at-tapi:set/static_bold2/true</code></td></tr><tr><td align="center">Change color of primary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_color1 {color:""}</code></td></tr><tr><td align="center">Change color of secondary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_color2 {color:""}</code></td></tr><tr><td align="center">Control italic style of primary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_italic1/false</code><br><code>/function at-tapi:set/static_italic1/true</code></td></tr><tr><td align="center">Control italic style of secondary zone<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/static_italic2/false</code><br><code>/function at-tapi:set/static_italic2/true</code></td></tr></tbody></table>

### Animated style type

<table><thead><tr><th width="279.083251953125" align="center">Description &#x26; possible errors</th><th align="center">Usage</th></tr></thead><tbody><tr><td align="center">Change animation<br><br><em>Possible errors:</em><br><em>Wrong style type</em><br><em>Unknown animation</em></td><td align="center"><code>/function at-tapi:set/animated_animation {name:""}</code></td></tr><tr><td align="center">Control bold style of animation<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/animated_bold/false</code><br><code>/function at-tapi:set/animated_bold/true</code></td></tr><tr><td align="center">Control italic style of animation<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/animated_italic/false</code><br><code>/function at-tapi:set/animated_italic/true</code></td></tr><tr><td align="center">Control animation direction<br><br><em>Possible errors:</em><br><em>Wrong style type</em></td><td align="center"><code>/function at-tapi:set/animated_direction/l2r</code><br><code>/function at-tapi:set/animated_direction/r2l</code></td></tr><tr><td align="center">Control animation speed<br><br><em>Possible errors:</em><br><em>Wrong style type</em><br><em>Invalid speed value</em></td><td align="center"><code>/function at-tapi:set/animated_speed {ticks:1}</code></td></tr></tbody></table>

## API Trap functionality

As mentioned before, traps provide a way to get notified based on timer events. The API Trap system consists of several trap types, each having a distinct execution behavior:

<table><thead><tr><th width="215.4332275390625" align="center">Trap type</th><th align="center">Description</th></tr></thead><tbody><tr><td align="center">reach_full_day</td><td align="center">Runs at every full day, e.g. 1d 0h 0m 0s / 8d 0h 0m 0s</td></tr><tr><td align="center">reach_full_hour</td><td align="center">Runs at every full hour, e.g. 0d 3h 0m 0s / 2d 18h 0m 0s</td></tr><tr><td align="center">reach_full_minute</td><td align="center">Runs at every full minute, e.g. 0d 0h 40m 0s / 1d 2h 53m 0s</td></tr><tr><td align="center">reach_zero</td><td align="center">Runs if timer counts down to zero: 0d 0h 0m 0s</td></tr><tr><td align="center">timer_continue</td><td align="center">Runs if timer resumes, e.g. via player input or API</td></tr><tr><td align="center">timer_pause</td><td align="center">Runs if timer pauses, e.g. via player input or API</td></tr><tr><td align="center">timer_reset</td><td align="center">Runs if timer get reset by player input</td></tr></tbody></table>

Traps need to be registered in order to be executed by AT. In practice, the registration process works as follows:

1. Create a function that should be executed as soon as the trap is triggered
2. Run the trap creation command to register it to the API (command can be found below)
3. AT will run your function automatically if the trap type is being executed

Note: Per trap type, AT is limited to 16 functions.

## Functions - Trap

### Trap modification

<table><thead><tr><th width="263.449951171875" align="center">Description &#x26; possible errors</th><th align="center">Usage</th></tr></thead><tbody><tr><td align="center">Create / register a trap<br><br><em>Possible errors:</em><br><em>Unknown trap type</em><br><em>Function limit reached</em></td><td align="center"><code>/function at-tapi:trap/add {type:"", function:""}</code></td></tr><tr><td align="center">Remove all occurrences of function in a trap type<br><br><em>Possible errors:</em><br><em>Unknown trap type</em></td><td align="center"><code>/function at-tapi:trap/remove {type:"", function:""}</code></td></tr></tbody></table>

### Viewing trap data

**Description:** Count & list all functions of a trap type.

**Usage:** `/function at-tapi:trap/list {type:"trap_type"}`

<table><thead><tr><th width="217.7332763671875" align="center">Output</th><th width="149.933349609375" align="center">Type</th><th align="center">Sample value</th></tr></thead><tbody><tr><td align="center">trap_content</td><td align="center">List</td><td align="center">["foo:bar", "test:run with storage baz"]</td></tr><tr><td align="center">function_count</td><td align="center">Integer</td><td align="center">2</td></tr></tbody></table>

<figure><img src="https://1711582085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcYhJ8xj9E8N0Z8EEyDX2%2Fuploads%2F1CksmGS3GPEYzHmU6w66%2Fapi-trap-list0.png?alt=media&#x26;token=6073420d-a29c-4523-b3ae-bb02599029c8" alt=""><figcaption></figcaption></figure>

## Examples

### Example 1: Hourly rewarding every player with a cookie

**Steps to reproduce:**

1. Create .mcfunction file in any datapack for reward function\
   This example assumes the following file path: *data/example1-cookie/function/reward.mcfunction*
2. Insert necessary commands:

```
give @a minecraft:cookie
tellraw @a "You received a cookie. Thank you for playing!"
```

3. Test function execution by running `/function example1-cookie:reward`
4. Register a new trap to conclude the example:

`/function at-tapi:trap/add {type:"reach_full_hour", function:"example1-cookie:reward"}`
