Repositories
Sometimes you need to globally store some data that should be wiped when the server stops / restarts.
Vital provides a class called VitalRepository, which can be used for exactly that.
The repository implementation is inspired by the Spring Boot repository solutions and uses similar syntaxes.
Essentially, a repository is nothing more than a class (instance) that stores a Map<ID, Entity> of all of your custom data entities.
A custom data entity may be a class that contains specific data for each entry, like MyCustomPlayer or MyCustomHologram, etc.
Each entity must always be uniquely identifiable by some sort of ID; a common approach to keep ids unique is to use a UUID.
To make your own repository, create a class that extends from VitalRepository.
// This annotation SHOULD be used so you can inject this repository into any other Spring bean (other @Component annotated methods, classes, etc.)
@Component
public class MyRepository extends VitalRepository<MyCustomEntity, UUID> {
}
This repository can now manage instances of MyCustomEntity, where each entity will be identified by a UUID.
Now let's create that entity class.
public class MyCustomEntity extends VitalEntity<UUID> {
private UUID uuid;
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
}
You can now freely add your own data to this entity class.
public class MyCustomEntity extends VitalEntity<UUID> {
private UUID uuid;
private String myCustomString;
private boolean myCustomBoolean;
// ...
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
// Other getters and setters...
}
Using repositories
Now that we have a repository that can manage our very own custom entity, we can now start using it to store and retrieve entities.
Since we annotated our repository with @Component, we can freely inject it into any other Spring-managed bean (All other classes annotated with @Component or other annotations that are also annotated with @Component).
Let's say we are building a command and now need to store something in this repository, we can simply declare our repository as a constructor parameter for our command.
And since the @VitalCommand.Info annotation is indirectly also annotated with @Component, our command is also a Spring bean.
@VitalCommand.Info
public class MyCommand extends VitalCommand.Spigot {
private final MyRepository myRepository;
public MyCommand(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
Now if we want to store something, we can simply use the save(T) method.
myRepository.save(new MyCustomEntity());
If we want to retrieve an entity, we can use the findById(Class, ID) method.
myRepository.findById(MyCustomEntity.class, theIdOfMyCustomEntity);
If we want to delete something, we can use the delete(T) method.
myRepository.delete(myCustomEntity);
Lifecycles
| Method | Usage |
|---|---|
onSave(T) | Perform an action when an entity is saved to this repository. |
onDelete(T) | Perform an action when an entity is deleted from this repository. |