CRUD Storage
You could opt for a SQL database, and YAML files for data, but it can be difficult to design and to build SQL queries, modularize it and make it independant.
This library is designed to be a common proxy to all possibles storage methods of your plugin's data. It offers also a basic implementation in MySQL. You can use it without using any SQL language, regardless the creation of the database itself (that is in most cases not made by the authors of the plugins).
After that, you can modularize it and change conveniently your method of storage without touching your core plugin code.
You could also use it to manage your settings conveniently., but it's primary designed for mass data.
Quick walkthrough:
You must first choose one method of storage. The only avalaible storage builtin method is MySQL. It store data by a ORM api (object-relational mapping). Each field of the class represent one column in the database, and each instance of your class will represent one line in the database. You will need a model-Class made of basic data types.
It's easy-to-use, you need only, at least, 2 classes and even if there a little bit of code, it's mainly boilerplate and search-and-replace classes.
You will need at least
- an "ID" field for this storage method (as known as primary key or pk in database words) that will be unique.
- an empty constructor
- an "ID" constructor
1) Create a plain Model Class:
Java
public class PlayerData { // Example data
private int id; // Needed +
private int level; // Example
public PlayerData() {} // Needed
public PlayerData(int id) { this.id = id; } // Needed
public int getLevel() { return level; }
public void setLevel(int level) { this.level = level; }
public int getId() { return id;}
public void setId(int id) { this.id = id; }
}2) Inherit BeanCRUDful<PlayerData> and override these methods as this:
Java
import net.rafoudiablol.api.crud.sql.BeanCRUDful;
import org.mentabean.BeanConfig;
import org.mentabean.DBTypes;
import java.util.List;
public class SampleCRUDful extends BeanCRUDful<PlayerData> {
@Override
protected PlayerData createInstance(int id) {
return new PlayerData(id);
}
@Override
protected void loadConfig(List<BeanConfig> configs) {
BeanConfig config = new BeanConfig(PlayerData.class, table);
config.pk("id", DBTypes.AUTOINCREMENT);
config.field("level", DBTypes.INTEGER);
configs.add(config);
}
}You will call the methods pk() to select primary key name and field() to add a field inside your database. Important: To each field, the must be the appropriate getters and setters inside your class ! According to java convention, get/set prefix and first letter as uppercase in cameCase. It allow advanced modification because the changes are not directly injected into the fields but made by function calls.
table is the name of the table where the data is stored inside the database. If you only must store a Datatype, the field table contains then name passed by the method open().
The createInstance() method should be always the same (in exception of the return type).
3) Geta connection instance. You will need an URL, and maybe a login and a password, and then instanciate your class, and next open a connection.
SampleCRUDful storage = new SampleCRUDful();
4) Enjoy!
You can use it and automatically create the table if it not already exists.
Java
SampleCRUDful storage = new SampleCRUDful();
storage.open(DriverManager.getConnection(url, login, password), "mytablename");
storage.createTables();
PlayerData data = storage.create(); // INSERT inside the database
data.setLevel(3);
storage.update(data); // UPDATE inside the database
storage.delete(data.getId()); // DELETE from the database
// Search all players that have level 3
// You can define a method inside your Storage Class
List<PlayerData> players = storage.search("level", 3);
// Other example: Search a players from UUID...It doesn't seems like, but these statements manipulates and affects the database through the easy API.
How to implement a new Storage Method:
Inherit from CRUDful<>. You may inherit with a specific type, for a specific need. You can also inherit with genericity and provide an interface for write/read or use Reflection to get dynamially the fields, but it's maybe not the best choice. It is basically very abstract.
Java
package net.rafoudiablol.api.crud;
public interface CRUDful<T> {
T create();
T read(int id);
void update(T object);
void delete(int id);
String VERSION = "1.1";
}The Common Proxy Interface is made only by this CRUD iterface. It's the core of the library.
If the data is designed to be sorted (to be said a ranking system or a queue), you can inherit instead from SortedCRUDful<> that offer plus 2 methods: position(int id) to read the position of an object and read(int min, int max) to read all data from position min (included) to position max (excluded).
Feature:
This library provide also an implementation for MySQL database.
For more information and documentation, visit https://github.com/saoj/mentabean
Compatibility with Spigot version:
This library has no dependencies to Spigot, it can works on normal Java application. It means that this library can work with (under reserve of Java and libs compatibility) any version of your server, and does not need to be updated when a new version of Spigot is released.
Dependencies:(included)
mentabean, Simple ORM and Query Builder (slightly modified version)
https://github.com/saoj/mentabean
Similar projects
Plugin →Let Me Despawn
Improves performance by tweaking mob despawn rules. Say bye to pesky unintentional persistent mobs.
25MSueta
JourneyMap
Real-time map used for mapping in-game or your browser as you explore.
14.7MSueta
Emotecraft
Create your own emotes in Minecraft.
7.5MSueta
Simple Tpa
A simple tpa plugin which sends requests
5.4MSueta
Common Network
Forge and Fabric Unified Networking Solution
3.9MSueta
Set Home
A lightweight plugin for players to set their homes.
3.8MSueta