# API

{% hint style="info" %}
Please keep in mind that this wiki only shows a limited selection of methods, functions and possibilites.

For further questions regarding the development of addons, methods, or just general questions about what is possible, ask us on our discord server. Do read the entire page here though.
{% endhint %}

***

## <mark style="color:orange;">Using the API</mark>

#### Step 1:

Create a `/libs` folder in your project, and drag and drop the Vehicles jar into the folder.

#### Step 2:

Add the following to your `pom.xml` if you are using Maven:

```xml
<dependency>
  <groupId>Vehicles</groupId>
  <artifactId>Vehicles</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${pom.basedir}/src/main/java/<path>/<to>/<folder>/libs/Vehicles.jar</systemPath>
</dependency>
```

Depending on how you called your `Vehicles.jar`, you may also need to change that part in the `systemPath`.

#### Step 3:

You can now use the Vehicles API. Don't forget to add Vehicles as a dependency to your `plugin.yml`

{% hint style="warning" %}
The `Vehicle` object is a custom one. It is not the same as the `org.bukkit.entity` Vehicle. Please make sure to import the correct Vehicle:&#x20;

```java
import es.pollitoyeye.vehicles.interfaces.Vehicle;
```

{% endhint %}

***

## <mark style="color:orange;">Limitations of the API</mark>

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><mark style="color:orange;"><strong>No custom Vehicles</strong></mark></td><td>As our FAQ already hinted towards, you can't create your own Vehicles with this plugin. Not even using the API.</td><td></td></tr><tr><td><mark style="color:orange;"><strong>No automatic Vehicle movement</strong></mark></td><td>You can't force Vehicles to follow a specific path (eg: like TrainCarts). </td><td></td></tr><tr><td><mark style="color:orange;"><strong>Want something added?</strong></mark></td><td>Tell us on our discord and the developer can see what is possible. Don't ask about new Vehicles though =).</td><td></td></tr><tr><td><mark style="color:orange;"><strong>Movement tracking is resource intensive</strong></mark></td><td>Default MoveEvents from Bukkit and other checks don't work, as only the armorstand moves.<br><br>ProtocolLib offers a VehicleSteerPacket, which can register the movement.</td><td></td></tr></tbody></table>

***

## <mark style="color:orange;">Available Events</mark>

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-hidden></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><mark style="color:orange;"><strong>VehicleEnterEvent</strong></mark></td><td>This event gets fired when a player right clicks a Vehicle to mount it. You can not manually fire this event.</td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehicleExitEvent</strong></mark></td><td>This event gets fired when a player tries to dismount a Vehicle. You can not manually fire this event.</td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehiclePickupEvent</strong></mark></td><td>This event gets fired when a player tries to pick a Vehicle up. You can not manually fire this event.</td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehiclePlaceEvent</strong></mark></td><td>This event gets fire when a player tries to place a Vehicle. You can not manually fire this event.</td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehicleSpawnedEvent</strong></mark></td><td>This event gets fired AFTER a Vehicle has been spawned and AFTER the VehiclePlaceEvent has been. You can not manually fire this event.</td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehicleInventoryOpenEvent</strong></mark></td><td>This event gets fired when a player is trying to open the trunk of a Vehicle. You can not manually fire this event.<br></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>VehicleCollideEvent</strong></mark></td><td>This event gets fired whenever a Vehicle encounters a block that it can not drive up. You can not manually fire this event.</td><td></td><td></td></tr></tbody></table>

***

## <mark style="color:orange;">General API Methods</mark>

The following code selections are based off of the VehiclesMain instance:

```java
VehiclesMain mainClass = VehiclesMain.getPlugin()
mainClass.<snippet>;
```

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-hidden></th><th data-hidden></th><th data-hidden><select></select></th><th data-hidden data-type="users" data-multiple></th></tr></thead><tbody><tr><td><mark style="color:orange;"><strong>Get current mounted Vehicle from Player</strong></mark></td><td><pre class="language-java"><code class="lang-java">.getPlayerVehicle(player);
</code></pre></td><td></td><td><pre><code>VehiclesMain.getPlugin()
</code></pre></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Generate subtype from String</strong></mark></td><td><pre class="language-java"><code class="lang-java">.vehicleSubTypeFromString(VehicleType type, String subtype)
</code></pre></td><td></td><td></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Get all current mounted Vehicles as a Hashmap</strong></mark></td><td><pre class="language-java"><code class="lang-java">.playerVehicles;
</code></pre></td><td></td><td></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Get other config options:</strong></mark></td><td><pre class="language-java"><code class="lang-java">.&#x3C;Config Option here>;
</code></pre></td><td></td><td></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Theres a bit more...</strong></mark></td><td>Ask us on the Discord and we can surely help you find a solution you need!</td><td></td><td></td><td></td><td></td></tr></tbody></table>

***

The following code selections are generalised and should give insight on what info you can get/set from Vehicles.\
\
Please keep in mind, that you call these methods with a Vehicle object.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-hidden></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><mark style="color:orange;"><strong>Get the</strong></mark> <code>mainstand</code> <mark style="color:orange;"><strong>of a Vehicle</strong></mark></td><td><pre class="language-java"><code class="lang-java">Vehicle#getMainstand();
</code></pre></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Edit Vehicle stats</strong></mark></td><td><pre class="language-java"><code class="lang-java">Vehicle#setHealth(double);
Vehicle#damage(double);
Vehicle#setFuel(double);
</code></pre></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Get current Players on Vehicle</strong></mark></td><td><pre class="language-java"><code class="lang-java">Vehicle#getRidingPlayers();
</code></pre></td><td></td><td></td></tr><tr><td><mark style="color:orange;"><strong>Get Type / Subtype of Vehicle</strong></mark></td><td><pre class="language-java"><code class="lang-java">Vehicle#getType();
</code></pre></td><td></td><td></td></tr></tbody></table>

***

## <mark style="color:orange;">Code Examples</mark>

<details>

<summary><mark style="color:orange;"><strong>Spawn a new Vehicle</strong></mark></summary>

Spawning is done via the VehicleManager. Each Vehicle has its own Manager.

```java
// General VehicleManager
VehicleManager vehicleManager;

// Examples of the "Per Vehicle" Managers
VehicleManager carManager = new CarManager();
VehicleManager tankManager = new TankManager();
VehicleManager planeManager = new PlaneManager();                      

// General method and parameters
vehicleManager.spawn(location, ownerUUID, vehicleSubType);

// Example for spawning a car
carManager.spawn(new Location(Bukkit.getWorld("world"), 0, 0, 0), "5ec4c0de-17ed-4d34-bddf-c703b5ac5737", "BLACK");
```

</details>

<details>

<summary><mark style="color:orange;"><strong>Instant Mount a Player on a Vehicle</strong></mark></summary>

Due to Vehicles having a `20L` delay when all interaction is blocked with it, you need to delay the mounting as well. Mounting only works by firing a `playerInteractAtEntityEvent` manually.

<pre class="language-java"><code class="lang-java">@EventHandler
public void onVehicleSpawn(VehicleSpawnedEvent event) {
    // Grab a random armorstand to fire the event on
    ArmorStand armorStand = event.getVehicleParts().get(0);
    Location location = armorStand.getLocation();

    // Create new Vector from its location
    Vector vector = new Vector(location.getBlockX(), location.getBlockY(), location.getBlockZ());

    // Create PlayerInteractAtEntityEvent
    Player player = Bukkit.getPlayer(UUID.fromString(event.getOwner()));
<strong>    PlayerInteractAtEntityEvent playerInteractAtEntityEvent = new PlayerInteractAtEntityEvent(player, armorStand, vector, EquipmentSlot.HAND);
</strong>    
    // Schedule event to fire after the delay
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable(){
        @Override
        public void run(){
            Bukkit.getServer().getPluginManager().callEvent(playerInteractAtEntityEvent);
        }
    }, 21L);
}
</code></pre>

</details>
