Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
c88001373c
1 changed files with 28 additions and 32 deletions
|
@ -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
|
## Example 1: Adding a Car to Someone's Garage
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Give car to garage
|
public void giveVehicleToGarage(Garage targetGarage, String vehicleType) {
|
||||||
public void giveCar(Garage garage, String vehicleType) {
|
// Attempt to create a new vehicle of the given type
|
||||||
// Attempt to create a vehicle
|
StorageVehicle createdVehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
||||||
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
|
||||||
|
|
||||||
if (vehicle == null) {
|
// If vehicle creation fails, log the error and exit
|
||||||
System.err.println("Failed to create vehicle of type: " + vehicleType);
|
if (createdVehicle == null) {
|
||||||
|
Bukkit.getLogger().warning("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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the vehicle's UUID to the garage
|
// Add the created vehicle's UUID to the target garage
|
||||||
garage.addVehicle(vehicle.getUuid());
|
targetGarage.addVehicle(createdVehicle.getUuid());
|
||||||
System.out.println("Vehicle created and added to the garage successfully.");
|
Bukkit.getLogger().info("Vehicle created and added to the garage successfully.");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -30,26 +30,22 @@ 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 giveVehicleToPlayerGarage(Player player, String vehicleType) {
|
||||||
// Attempt to create a vehicle
|
// Attempt to create a new vehicle of the given type
|
||||||
StorageVehicle vehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
StorageVehicle createdVehicle = VehiclesPlusAPI.createVehicle(vehicleType);
|
||||||
|
|
||||||
// Retrieve the player's default garage
|
// Retrieve the player's personal garage (will create a new garage if none exists)
|
||||||
Optional<Garage> optionalGarage = VehiclesPlusAPI.getGarage(player.getName());
|
Garage playerGarage = VehiclesPlusAPI.getPersonalGarage(player);
|
||||||
|
|
||||||
if (vehicle == null) {
|
// If vehicle creation fails, log the error and exit
|
||||||
System.err.println("Failed to create vehicle of type: " + vehicleType);
|
if (createdVehicle == null) {
|
||||||
|
Bukkit.getLogger().warning("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()) {
|
// Add the created vehicle's UUID to the player's personal garage
|
||||||
Garage garage = optionalGarage.get();
|
playerGarage.addVehicle(createdVehicle.getUuid());
|
||||||
garage.addVehicle(vehicle.getUuid());
|
Bukkit.getLogger().info("Vehicle created and added to the player's personal 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.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -64,10 +60,10 @@ public void despawnFirstVehicle(Player player) {
|
||||||
if (firstSpawnedVehicleOptional.isPresent()) {
|
if (firstSpawnedVehicleOptional.isPresent()) {
|
||||||
// Despawn the vehicle using the API's despawn reason
|
// Despawn the vehicle using the API's despawn reason
|
||||||
firstSpawnedVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
firstSpawnedVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
||||||
System.out.println("Vehicle despawned successfully.");
|
Bukkit.getLogger().info("Vehicle despawned successfully.");
|
||||||
} else {
|
} else {
|
||||||
// Handle the case where the player doesn't have a spawned vehicle
|
// 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()) {
|
if (closestVehicleOptional.isPresent()) {
|
||||||
// Despawn the closest vehicle using the API's despawn reason
|
// Despawn the closest vehicle using the API's despawn reason
|
||||||
closestVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
closestVehicleOptional.get().despawn(VehicleDespawnEvent.DespawnReason.API);
|
||||||
System.out.println("Closest vehicle despawned successfully.");
|
Bukkit.getLogger().info("Closest vehicle despawned successfully.");
|
||||||
} else {
|
} else {
|
||||||
// Handle the case where no spawned vehicles are found for the target player
|
// 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
|
// Check if the police garage exists, and if not, print an error message and exit
|
||||||
if (policeGarageOptional.isEmpty()) {
|
if (policeGarageOptional.isEmpty()) {
|
||||||
System.err.println("Error: The 'police' garage does not exist.");
|
Bukkit.getLogger().warning("Error: The 'police' garage does not exist.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,15 +125,15 @@ public void removeVehicleFromPoliceGarage(String vehicleModelName) {
|
||||||
try {
|
try {
|
||||||
// Remove the vehicle from the garage
|
// Remove the vehicle from the garage
|
||||||
vehicleToRemove.remove();
|
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) {
|
} catch (DataStorageException e) {
|
||||||
// Handle any errors that occur during the removal process
|
// 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);
|
throw new RuntimeException("Error while removing vehicle", e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If no matching vehicle was found, print an error message
|
// 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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue