API

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.


Using the API

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:

<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

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. eg.:

import es.pollitoyeye.vehicles.interfaces.Vehicle;

Limitations of the API

No custom Vehicles

As our FAQ already hinted towards, you can't create your own Vehicles with this plugin. Not even using the API.

No automatic Vehicle movement

You can't force Vehicles to follow a specific path (eg: like TrainCarts).

Want something added?

Tell us on our discord and the developer can see what is possible. Don't ask about new Vehicles though =).


Available Events

VehicleEnterEvent

This event gets fired when a player right clicks a Vehicle to mount it. You can not manually fire this event.

Methods
Event#getVehicleType()
Event#getVehicleSubType()
Event#getPlayer()
Event#getMainArmorStand()
Event#getHandlers()
Event#isCancelled()
Event#setCancelled(boolean)

VehicleExitEvent

This event gets fired when a player tries to dismount a Vehicle. You can not manually fire this event.

Methods
Event#getVehicle()
Event#getPlayer()
Event#getHandlers()
Event#removeOnDismountSet()
Event#setRemoveOnDismount(boolean)
Event#isCancelled()
Event#setCancelled(boolean)

VehiclePickupEvent

This event gets fired when a player tries to pick a Vehicle up. You can not manually fire this event.

Methods
Event#getVehicleType()
Event#getVehicleSubType()
Event#getPlayer()
Event#getBlockLocation()
Event#getHandlers()
Event#getOwner()
Event#isCancelled()
Event#isOverridePermissionEnabled()
Event#setCancelled(boolean)
Event#setOverridePermission(boolean)
Event#setPlayer(Player)

VehiclePlaceEvent

This event gets fire when a player tries to place a Vehicle. You can not manually fire this event.

Methods
Event#getType()
Event#getSubType()
Event#getHandlers()
Event#getOwner()
Event#isCancelled()
Event#setCancelled(boolean)
Event#getLocation()

VehicleSpawnedEvent

This event gets fired AFTER a Vehicle has been spawned and AFTER the VehiclePlaceEvent has been. You can not manually fire this event.

Methods
Event#getVehicleType()
Event#getVehicleSubType()
Event#getHandlers()
Event#getOwner()
Event#getVehicleParts()
Event#getBlockLocation()

VehicleInventoryOpenEvent

This event gets fired when a player is trying to open the trunk of a Vehicle. You can not manually fire this event.

Methods
Event#getHandlers()
Event#getPlayer()
Event#getVehicle()
Event#isCancelled()
Event#setCancelled()

VehicleCollideEvent

This event gets fired whenever a Vehicle encounters a block it can not drive up. You can not manually fire this event.

Methods
Event#getVehicle()
Event#getHandlers()

General API Methods

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

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

Get current mounted Vehicle from Player

.getPlayerVehicle(player);

Generate subtype from String

.vehicleSubTypeFromString(VehicleType type, String subtype)

Get all current mounted Vehicles as a Hashmap

.playerVehicles;

Get other config options:

.<Config Option here>;

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.

Get the mainstand of a Vehicle

Vehicle#getMainstand();

Edit Vehicle stats

Vehicle#setHealth(double);
Vehicle#damage(double);
Vehicle#setFuel(double);

Get current Players on Vehicle

Vehicle#getRidingPlayers();

Get Type / Subtype of Vehicle

Vehicle#getType();

Code Examples

Spawn a new Vehicle

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

// 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 of spawning a car
carManager.spawn(new Location(Bukkit.getWorld("world"), 0, 0, 0), "5ec4c0de-17ed-4d34-bddf-c703b5ac5737", "BLACK");
Instant Mount a Player on a Vehicle

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 yourself.

@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()));
    PlayerInteractAtEntityEvent playerInteractAtEntityEvent = new PlayerInteractAtEntityEvent(player, armorStand, vector, EquipmentSlot.HAND);
    
    // 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);
}

Last updated