# Server Scripting

Everything inside of the settings file can be customized to create different game modes. Apart from current settings in the file, more ways to customize the private servers will be added soon.&#x20;

Rule files in Tribals are written in [JSON ](https://en.wikipedia.org/wiki/JSON)file format, including settings.json file.&#x20;

### Settings (settings.json) file structure

* [Game mode](#game-mode)
* [Mode settings](#mode-settings)
* [Mode name](#mode-name)
* [Events](#events)
* [Actors](#actors)
* [Methods](#methods) (onCancel)
* [Bypass methods](#bypass-methods)
* [Scripts](#scripts)
* [Creating a PVE server example ](#example-script)

<div align="left" data-full-width="true"><figure><img src="/files/KkOrgpQhCW7JsO21tUMg" alt=""><figcaption><p>Settings.json file, "PVP Deathmatch" game mode settings</p></figcaption></figure></div>

### Game Mode

It defines which of the specified `modeSetting` will be set as the current game mode.

<div align="left"><figure><img src="/files/GgRGFm47PyepYZWtmJSJ" alt=""><figcaption><p>"Test" is the specified game mode.</p></figcaption></figure></div>

### Mode Settings

`modeSettings` is the parent category of the game modes in your server. All of your game modes will be a child element of this element. You can share your modes with other players, and they can easily add them under their `modeSettings` area.&#x20;

### Mode Name

The name of your mode. Usually defines the general gameplay rules of your server. For example, official Tribals servers have PVP, PVE, and Test type of game modes. But you can name your script as anything! Make sure its name can only contain the letters in the English alphabet, and numbers.

### Events

Events are the things that we listen if they triggered or not. When triggered, it brings back the information about the thing it triggered with. We can use the information to set an outcome of the triggered event. We can listen actors, set methods, and add scripts under the event we listen.&#x20;

* **`onInteract`**: When player interacts with it.
* **`onSpawn`**: When player spawns.
* **`onAttack`**: When player attacks to it.
* **`onKill`**: When player kills it.
* **`onDropItem`**: When player drops item.
* **`onCraft`**: When player crafts a something
* **`onBuild`**: When player builds something

### Actors

The things that determines what triggered this event. We can prevent specific actors to trigger in any event, via [`onCancel`](#methods) method.&#x20;

* Building
* Player
* Animal

### Methods

**`onCancel`** is the only method available for now. When specified, it will cancel the triggered event, based on the terms you specify. An error message can be set when event cancelled.&#x20;

This effect can be overridden with bypass methods.&#x20;

Also, the things listed **after** the **`onCancel`** method will also override the cancel.&#x20;

<div align="left"><figure><img src="/files/CVhH097YGf0rV7rvpcSJ" alt=""><figcaption><p>Plants can be destroyed, while the rest of the "Building" elements can't be damaged</p></figcaption></figure></div>

### Bypass Methods

Bypasses the `onCancel` method, based on the specified terms. The bypass methods are only for the building elements. More methods will be added soon.

**It will bypass the event if:**

* `isResource`: The "building" is a resource
* `decaying`: The building is decaying
* `isProtected`: The building is protected
* `authorized`: Player is authorized to the building
* `owner`: The building is owned by the player

### Scripts

Scripts are premade commands that does something. Can be use under [events](#events), [actors](#actors), and in objects (item or building)

* Clearall: clears the player inventory\
  Usage: `"script": ["clearall"]`
* Heal: fully heals the player health, hunger and thirst.\
  Usage: `"script": ["heal"]`
* Destroy: Destroys the object/building\
  Usage: `"script": ["destroy"]`
* Add Item: Adds the specified item and quantity. \
  Usage: `"script": ["addItem shit 1"]`
* Add Items: Used to add items to specific slots, with specified quantity. It should be used after "clearall" to avoid errors. Make sure the last item doesn't have a comma after it. \
  Usage: \
  `"addItems": [`\
  &#x20; `{` \
  &#x20;   `"name": "Pistol",` \
  &#x20;   `"amount": 1,` \
  &#x20;   `"slot": 1` \
  &#x20; `},` \
  &#x20; `{` \
  &#x20;   `"name": "PistolAmmo",` \
  &#x20;   `"amount": 99,` \
  &#x20;   `"slot": 2` \
  &#x20; `}` \
  `]`
* Any of the script can be run at the same time. \
  Usage: `"script": ["heal", "clearall", "addItem shit 1", "destroy"]`

### Example Script:

Let's create a "PVE" game mode from settings.json file

1. **`modeSettings`** is where you will write scripts for different modes. It's the parent category of your game modes.&#x20;
2. The next line is for the name of your script. It can include any letter in the English alphabet. In this case, we'll use "PvE".&#x20;

<div align="left"><figure><img src="/files/KpCsVtnBks8InONlqCoK" alt=""><figcaption><p>The name of the game mode script</p></figcaption></figure></div>

3. Next, you'll create the event you want to listen. In this example, we will use `onInteract` event. It will be triggered, when player "interacts" with this object.&#x20;

<div align="left"><figure><img src="/files/iMtIG3pw0ZztQ8Y7dyec" alt=""><figcaption><p>The event we listen to interact with other things</p></figcaption></figure></div>

4. Under `onInteract` event, we will define exactly what this script is for; `Building`, `Player`, or `Animal`. In this example, I use `Building`:

<div align="left"><figure><img src="/files/H8c0f0pVJgkuxTPxBVNu" alt=""><figcaption><p>We can define specific things that will happen when player interacts <br>with something. In this case, player interacts with a Building.</p></figcaption></figure></div>

5. We want to cancel building, so we'll use `onCancel` event to prevent buildings from interacted. Before that, we can specify things that'll bypass the `onCancel` event. In this case the owner of the house, resources, and authorized players can be interacted with each other.<br>

   <figure><img src="/files/FolMIMEw8vW9SfHv7zgH" alt=""><figcaption><p>A script that prevents the building elements from being interacted. <br>Thanks to bypass; it will allow owners to be interact with their buildings, resources can still be interacted, and authorized users may interact with the buildings element they authorized to. </p></figcaption></figure>
6. If we want to add more building elements that will bypass `onCancel` event, we can list them under it. For this example, we should add the Toolbox to the items that can be interacted, so players can interact with toolbox to get authorized.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.tribals.io/tribals.io-wiki/community-servers/server-scripting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
