Merge remote-tracking branch 'origin/main'

This commit is contained in:
Stijn Bannink 2025-01-12 20:15:28 +01:00
commit c88001373c
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A

View file

@ -10,19 +10,19 @@ In this section, you'll find additional examples of how to use the VehiclesPlus
## Example 1: Adding a Car to Someone's Garage
```java
// Give car to garage
public void giveCar(Garage garage, String vehicleType) {
// Attempt to create a vehicle
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
public void giveVehicleToGarage(Garage targetGarage, String vehicleType) {
// Attempt to create a new vehicle of the given type
StorageVehicle createdVehicle = VehiclesPlusAPI.createVehicle(vehicleType);
if (vehicle == null) {
System.err.println("Failed to create vehicle of type: " + vehicleType);
// If vehicle creation fails, log the error and exit
if (createdVehicle == null) {
Bukkit.getLogger().warning("Failed to create vehicle of type: " + vehicleType);
return; // Exit if the vehicle could not be created
}
// Add the vehicle's UUID to the garage
garage.addVehicle(vehicle.getUuid());
System.out.println("Vehicle created and added to the garage successfully.");
// Add the created vehicle's UUID to the target garage
targetGarage.addVehicle(createdVehicle.getUuid());
Bukkit.getLogger().info("Vehicle created and added to the garage successfully.");
}
```
@ -30,26 +30,22 @@ public void giveCar(Garage garage, String vehicleType) {
```java
// Give car to player's default garage
public void giveCar(Player player, String vehicleType) {
// Attempt to create a vehicle
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
public void giveVehicleToPlayerGarage(Player player, String vehicleType) {
// Attempt to create a new vehicle of the given type
StorageVehicle createdVehicle = VehiclesPlusAPI.createVehicle(vehicleType);
// Retrieve the player's default garage
Optional<Garage> optionalGarage = VehiclesPlusAPI.getGarage(player.getName());
// Retrieve the player's personal garage (will create a new garage if none exists)
Garage playerGarage = VehiclesPlusAPI.getPersonalGarage(player);
if (vehicle == null) {
System.err.println("Failed to create vehicle of type: " + vehicleType);
// If vehicle creation fails, log the error and exit
if (createdVehicle == null) {
Bukkit.getLogger().warning("Failed to create vehicle of type: " + vehicleType);
return; // Exit if the vehicle could not be created
}
if (optionalGarage.isPresent()) {
Garage garage = optionalGarage.get();
garage.addVehicle(vehicle.getUuid());
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.
}
// Add the created vehicle's UUID to the player's personal garage
playerGarage.addVehicle(createdVehicle.getUuid());
Bukkit.getLogger().info("Vehicle created and added to the player's personal garage successfully.");
}
```
@ -64,10 +60,10 @@ public void despawnFirstVehicle(Player player) {
if (firstSpawnedVehicleOptional.isPresent()) {
// Despawn the vehicle using the API's despawn reason
firstSpawnedVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
System.out.println("Vehicle despawned successfully.");
Bukkit.getLogger().info("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.");
Bukkit.getLogger().warning("Player does not have a spawned vehicle.");
}
}
```
@ -89,10 +85,10 @@ public void despawnClosestVehicle(Player executor, Player targetPlayer) {
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.");
Bukkit.getLogger().info("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.");
Bukkit.getLogger().warning("Target player does not have any spawned vehicles.");
}
}
```
@ -106,7 +102,7 @@ public void removeVehicleFromPoliceGarage(String vehicleModelName) {
// 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.");
Bukkit.getLogger().warning("Error: The 'police' garage does not exist.");
return;
}
@ -129,15 +125,15 @@ public void removeVehicleFromPoliceGarage(String vehicleModelName) {
try {
// Remove the vehicle from the garage
vehicleToRemove.remove();
System.out.println("Vehicle with model '" + vehicleModelName + "' has been successfully removed from the police garage.");
Bukkit.getLogger().info("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.");
Bukkit.getLogger().warning("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.");
Bukkit.getLogger().warning("Error: No vehicle with model '" + vehicleModelName + "' found in the police garage.");
}
}
```