3
0
Fork 0

Add more example code

This commit is contained in:
cedric 2024-12-28 23:30:02 +01:00
parent 9730ae42d4
commit deb9bd2fc7
11 changed files with 295 additions and 9 deletions

View file

@ -5,11 +5,11 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>tech.sbdevelopment</groupId> <groupId>tech.sbdevelopment</groupId>
<artifactId>vp3apitest</artifactId> <artifactId>customModel</artifactId>
<version>1.0</version> <version>1.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>vp3apitest</name> <name>CustomModel</name>
<properties> <properties>
<java.version>21</java.version> <java.version>21</java.version>
@ -59,6 +59,11 @@
<id>sonatype</id> <id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url> <url>https://oss.sonatype.org/content/groups/public/</url>
</repository> </repository>
<repository>
<id>sbdevelopment-repo-releases</id>
<name>SBDevelopment Repository</name>
<url>https://repo.sbdevelopment.tech/releases</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -71,7 +76,7 @@
<dependency> <dependency>
<groupId>nl.sbdeveloper</groupId> <groupId>nl.sbdeveloper</groupId>
<artifactId>VehiclesPlus-API</artifactId> <artifactId>VehiclesPlus-API</artifactId>
<version>3.0.0-SNAPSHOT</version> <version>3.0.2</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,10 +1,10 @@
package tech.sbdevelopment.vp3apitest; package tech.sbdevelopment.customModel;
import nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI; import nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import tech.sbdevelopment.vp3apitest.parts.Vent; import tech.sbdevelopment.customModel.parts.Vent;
public final class VP3APItest extends JavaPlugin { public final class CustomModel extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
VehiclesPlusAPI.registerHook(() -> { VehiclesPlusAPI.registerHook(() -> {

View file

@ -1,4 +1,4 @@
package tech.sbdevelopment.vp3apitest.models; package tech.sbdevelopment.customModel.models;
import nl.sbdeveloper.vehiclesplus.api.vehicles.HolderItemPosition; import nl.sbdeveloper.vehiclesplus.api.vehicles.HolderItemPosition;
import nl.sbdeveloper.vehiclesplus.api.vehicles.VehicleModel; import nl.sbdeveloper.vehiclesplus.api.vehicles.VehicleModel;
@ -12,7 +12,7 @@ import nl.sbdeveloper.vehiclesplus.utils.jackson.ColorList;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import tech.sbdevelopment.vp3apitest.parts.Vent; import tech.sbdevelopment.customModel.parts.Vent;
public class DefaultSubmarine extends DefaultVehicleModel { public class DefaultSubmarine extends DefaultVehicleModel {
@Override @Override

View file

@ -1,4 +1,4 @@
package tech.sbdevelopment.vp3apitest.parts; package tech.sbdevelopment.customModel.parts;
import nl.sbdeveloper.vehiclesplus.api.vehicles.HolderItemPosition; import nl.sbdeveloper.vehiclesplus.api.vehicles.HolderItemPosition;
import nl.sbdeveloper.vehiclesplus.api.vehicles.movement.MovementInput; import nl.sbdeveloper.vehiclesplus.api.vehicles.movement.MovementInput;

83
website example/pom.xml Normal file
View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tech.sbdevelopment</groupId>
<artifactId>websiteExample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>websiteExample</name>
<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>sbdevelopment-repo-releases</id>
<name>SBDevelopment Repository</name>
<url>https://repo.sbdevelopment.tech/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>nl.sbdeveloper</groupId>
<artifactId>VehiclesPlus-API</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,16 @@
package tech.sbdevelopment.websiteExample;
import org.bukkit.plugin.java.JavaPlugin;
public final class WebsiteExample extends JavaPlugin {
@Override
public void onEnable() {
}
@Override
public void onDisable() {
}
}

View file

@ -0,0 +1,59 @@
package tech.sbdevelopment.websiteExample.examples;
import nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI;
import nl.sbdeveloper.vehiclesplus.api.garages.Garage;
import nl.sbdeveloper.vehiclesplus.api.vehicles.Vehicle;
import nl.sbdeveloper.vehiclesplus.storage.db.exceptions.DataStorageException;
import java.util.Optional;
public class DeleteVehicle {
/**
* Removes a specific vehicle from the "police" garage based on the given model name.
* This method retrieves the police garage, searches for a vehicle with the specified model name,
* and removes the vehicle from the garage if found.
* It handles the case where the garage or vehicle doesn't exist and logs appropriate messages.
*
* @param vehicleModelName The model name of the vehicle to remove from the police garage.
*/
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(VehiclesPlusAPI::getVehicle)
// 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.");
}
}
}

View file

@ -0,0 +1,63 @@
package tech.sbdevelopment.websiteExample.examples;
import nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI;
import nl.sbdeveloper.vehiclesplus.api.events.impl.VehicleDespawnEvent;
import nl.sbdeveloper.vehiclesplus.api.vehicles.Vehicle;
import nl.sbdeveloper.vehiclesplus.api.vehicles.impl.SpawnedVehicle;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class DespawnVehicle {
/**
* Despawns the first spawned vehicle of a player.
* If the player has multiple spawned vehicles, this method will despawn the first one retrieved.
*
* @param player The player whose vehicle is to be despawned.
*/
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.");
}
}
/**
* Despawns the closest spawned vehicle to the executor player.
* The executor player is the one determining which vehicle is closest to them.
*
* @param executor The player who is looking for the closest vehicle to despawn.
* @param targetPlayer The player whose vehicles will be checked.
*/
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.");
}
}
}

View file

@ -0,0 +1,56 @@
package tech.sbdevelopment.websiteExample.examples;
import nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI;
import nl.sbdeveloper.vehiclesplus.api.garages.Garage;
import nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle;
import org.bukkit.entity.Player;
public class GiveVehicle {
/**
* Gives a vehicle to a specific garage.
* Attempts to create a vehicle of the specified type and add it to the provided garage.
*
* @param targetGarage The garage where the vehicle will be stored.
* @param vehicleType The type of vehicle to be created.
*/
public void giveVehicleToGarage(Garage targetGarage, String vehicleType) {
// Attempt to create a new vehicle of the given type
StorageVehicle createdVehicle = VehiclesPlusAPI.createVehicle(vehicleType);
// If vehicle creation fails, log the error and exit
if (createdVehicle == null) {
System.err.println("Failed to create vehicle of type: " + vehicleType);
return; // Exit if the vehicle could not be created
}
// Add the created vehicle's UUID to the target garage
targetGarage.addVehicle(createdVehicle.getUuid());
System.out.println("Vehicle created and added to the garage successfully.");
}
/**
* Gives a vehicle to the player's default garage.
* Attempts to create a vehicle of the specified type and add it to the player's personal garage.
*
* @param player The player whose default garage will receive the vehicle.
* @param vehicleType The type of vehicle to be created.
*/
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 personal garage (will create a new garage if none exists)
Garage playerGarage = VehiclesPlusAPI.getPersonalGarage(player);
// If vehicle creation fails, log the error and exit
if (createdVehicle == null) {
System.err.println("Failed to create vehicle of type: " + vehicleType);
return; // Exit if the vehicle could not be created
}
// Add the created vehicle's UUID to the player's personal garage
playerGarage.addVehicle(createdVehicle.getUuid());
System.out.println("Vehicle created and added to the player's personal garage successfully.");
}
}

View file

@ -0,0 +1,4 @@
name: websiteExample
version: '1.0-SNAPSHOT'
main: tech.sbdevelopment.websiteExample.WebsiteExample
api-version: '1.21'