Compare commits
No commits in common. "3ee4b30621e29c5f9212e407d714fda081838e3c" and "7c6ad99b542a299a80673f601f4ae57e1639a161" have entirely different histories.
3ee4b30621
...
7c6ad99b54
2 changed files with 16 additions and 113 deletions
|
@ -2,7 +2,7 @@
|
||||||
title: Examples
|
title: Examples
|
||||||
description: Example usages of the api
|
description: Example usages of the api
|
||||||
published: false
|
published: false
|
||||||
date: 2024-12-28T22:26:48.261Z
|
date: 2024-12-28T21:29:47.658Z
|
||||||
tags: developers
|
tags: developers
|
||||||
editor: markdown
|
editor: markdown
|
||||||
dateCreated: 2024-12-28T21:11:02.102Z
|
dateCreated: 2024-12-28T21:11:02.102Z
|
||||||
|
@ -35,118 +35,21 @@ public void giveCar(Garage garage, String vehicleType) {
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Give car to player's default garage
|
// Give car to player's default garage
|
||||||
public void giveCar(Player player, String vehicleType) {
|
public void giveCar(Player player, String vehicleType) {
|
||||||
// Attempt to create a vehicle
|
// Attempt to create a vehicle
|
||||||
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
||||||
|
|
||||||
// Retrieve the player's default garage
|
// Retrieve the player's default garage
|
||||||
Optional<Garage> optionalGarage = VehiclesPlusAPI.getGarage(player.getName());
|
// Note: This method will always return a garage, even if the player does not have one.
|
||||||
|
// If the player does not have a garage, the garage will be created.
|
||||||
|
Garage garage = VehiclesPlusAPI.getPersonalGarage(player);
|
||||||
|
|
||||||
if (vehicle == null) {
|
if (vehicle == null) {
|
||||||
System.err.println("Failed to create vehicle of type: " + vehicleType);
|
System.err.println("Failed to create vehicle of type: " + vehicleType);
|
||||||
return; // Exit if the vehicle could not be created
|
return; // Exit if the vehicle could not be created
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optionalGarage.isPresent()) {
|
|
||||||
Garage garage = optionalGarage.get();
|
|
||||||
garage.addVehicle(vehicle.getUuid());
|
garage.addVehicle(vehicle.getUuid());
|
||||||
System.out.println("Vehicle created and added to the garage successfully.");
|
System.out.println("Vehicle created and added to the garage successfully.");
|
||||||
} else {
|
|
||||||
System.err.println("Garage not found for player: " + player.getName());
|
|
||||||
// Optionally, you could create a new garage for the player here if the API allows it.
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example 3: Despawning the First Vehicle of a Player
|
|
||||||
|
|
||||||
```java
|
|
||||||
public void despawnFirstVehicle(Player player) {
|
|
||||||
// Retrieve the first spawned vehicle of the player
|
|
||||||
Optional<SpawnedVehicle> firstSpawnedVehicleOptional = VehiclesPlusAPI.getVehicle(player);
|
|
||||||
|
|
||||||
// Check if a vehicle is present to despawn
|
|
||||||
if (firstSpawnedVehicleOptional.isPresent()) {
|
|
||||||
// Despawn the vehicle using the API's despawn reason
|
|
||||||
firstSpawnedVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
|
||||||
System.out.println("Vehicle despawned successfully.");
|
|
||||||
} else {
|
|
||||||
// Handle the case where the player doesn't have a spawned vehicle
|
|
||||||
System.err.println("Player does not have a spawned vehicle.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example 4: Despawning the Closest Vehicle to a Player
|
|
||||||
|
|
||||||
```java
|
|
||||||
public void despawnClosestVehicle(Player executor, Player targetPlayer) {
|
|
||||||
// Retrieve a list of all spawned vehicles belonging to the target player
|
|
||||||
@NotNull List<Vehicle> vehiclesOfTargetPlayer = VehiclesPlusAPI.getVehicles(targetPlayer);
|
|
||||||
|
|
||||||
// Find the closest spawned vehicle to the executor player
|
|
||||||
Optional<SpawnedVehicle> closestVehicleOptional = vehiclesOfTargetPlayer.stream()
|
|
||||||
.filter(vehicle -> vehicle instanceof SpawnedVehicle) // Only consider spawned vehicles
|
|
||||||
.map(vehicle -> (SpawnedVehicle) vehicle)
|
|
||||||
.min(Comparator.comparingDouble(vehicle -> vehicle.getHolder().getLocation().distance(executor.getLocation())));
|
|
||||||
|
|
||||||
// Check if the closest vehicle was found
|
|
||||||
if (closestVehicleOptional.isPresent()) {
|
|
||||||
// Despawn the closest vehicle using the API's despawn reason
|
|
||||||
closestVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
|
||||||
System.out.println("Closest vehicle despawned successfully.");
|
|
||||||
} else {
|
|
||||||
// Handle the case where no spawned vehicles are found for the target player
|
|
||||||
System.err.println("Target player does not have any spawned vehicles.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example 5: Removing a Vehicle from the Police Garage
|
|
||||||
|
|
||||||
```java
|
|
||||||
public void removeVehicleFromPoliceGarage(String vehicleModelName) {
|
|
||||||
// Retrieve the "police" garage using its name
|
|
||||||
Optional<Garage> policeGarageOptional = VehiclesPlusAPI.getGarage("police");
|
|
||||||
|
|
||||||
// Check if the police garage exists, and if not, print an error message and exit
|
|
||||||
if (policeGarageOptional.isEmpty()) {
|
|
||||||
System.err.println("Error: The 'police' garage does not exist.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the actual garage object
|
|
||||||
Garage policeGarage = policeGarageOptional.get();
|
|
||||||
|
|
||||||
// Search for the vehicle with the given model name within the "police" garage
|
|
||||||
Optional<Vehicle> vehicleToRemoveOptional = policeGarage.getVehicles().stream()
|
|
||||||
// Convert the vehicle UUID to an actual StorageVehicle object
|
|
||||||
.map(vehicleUuid -> VehiclesPlusAPI.getVehicle(vehicleUuid))
|
|
||||||
// Filter vehicles to match the given model name
|
|
||||||
.filter(vehicle -> vehicle != null && vehicle.getVehicleModel().getId().equals(vehicleModelName))
|
|
||||||
// Select the first match
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
// If a matching vehicle is found, attempt to remove it
|
|
||||||
if (vehicleToRemoveOptional.isPresent()) {
|
|
||||||
Vehicle vehicleToRemove = vehicleToRemoveOptional.get();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Remove the vehicle from the garage
|
|
||||||
vehicleToRemove.remove();
|
|
||||||
System.out.println("Vehicle with model '" + vehicleModelName + "' has been successfully removed from the police garage.");
|
|
||||||
} catch (DataStorageException e) {
|
|
||||||
// Handle any errors that occur during the removal process
|
|
||||||
System.err.println("Error: Failed to remove vehicle with model '" + vehicleModelName + "' from the garage.");
|
|
||||||
throw new RuntimeException("Error while removing vehicle", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If no matching vehicle was found, print an error message
|
|
||||||
System.err.println("Error: No vehicle with model '" + vehicleModelName + "' found in the police garage.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## More Examples
|
|
||||||
|
|
||||||
For additional usage examples, please visit the [VehiclesPlus API Examples on GitHub](https://git.sbdevelopment.tech/SBDevelopment/VehiclesPlus-v3-API-example).
|
|
|
@ -2,7 +2,7 @@
|
||||||
title: Getting Started
|
title: Getting Started
|
||||||
description: Api usage
|
description: Api usage
|
||||||
published: false
|
published: false
|
||||||
date: 2024-12-28T22:26:17.950Z
|
date: 2024-12-28T21:31:42.426Z
|
||||||
tags: developers
|
tags: developers
|
||||||
editor: markdown
|
editor: markdown
|
||||||
dateCreated: 2024-12-28T20:10:42.650Z
|
dateCreated: 2024-12-28T20:10:42.650Z
|
||||||
|
@ -54,7 +54,7 @@ dependencies {
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
For usage examples, please visit the [VehiclesPlus Examples Page](https://docs.sbdevelopment.tech/en/vehiclesplus-v3/api/examples).
|
For usage examples, please visit the [VehiclesPlus Examples Page](https://docs.sbdevelopment.tech/en/vehiclesplus-v3/api/gettingstarted).
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
|
|
Reference in a new issue