/SQLib

The easiest way to store data for all your minecraft needs!

Primary LanguageJavaCreative Commons Zero v1.0 UniversalCC0-1.0

Maintenance PRs Welcome

name

ko-fi

SQLib

SQLib is the easiest way to store data for all your minecraft needs! A simple sql wrapper made with a focus on minecraft use cases.

Important Note:

This library is not a full-fledged sql wrapper, and does not provide full access to many sql features. The main focus of this library is to provide an easy and simple way to store data in your mods. If you are looking for a more advanced database I recommend taking a look at something like Nitrite.

Config

The mod generates a config on first time start that allows you to configure the database used by all mods relying on sqlib. The default database is a sqlite database running in the sqlib directory.

Supported Datatypes

The datatypes can be accessed with the SQLDataType class. I tend to add support for new types as I run into them in my projects. If you would like one added, pleade make an issue!

Standard Minecraft
String BlockPos
Int ChunkPos
Double NbtElement
Long Json
Bool MutableText
UUID Identifier

Setup

In your build.gradle include:

repositories {
    maven { url "https://api.modrinth.com/maven" }
}

dependencies {
  modImplementation("maven.modrinth:sqlib:2.2.13")
}

Developer Usage

This example uses the built-in database managed by sqlib. for 99% of mods, using the built-in database is good, however further down are examples for custom database management.

// Do not call SQLib.getDatabase() in a early mod initializer. 
// Doing so will likely crash your mod if your mod.
// Calling in or after the regular mod initializer is ok.
Database database = SQLib.getDatabase();

Table table = database.createTable("myModId", "userdata")
        .addColumn("username", SQLDataType.STRING)
        .addColumn("home", SQLDataType.BLOCKPOS)
        .addColumn("nbt", SQLDataType.NBT)
        .finish();
        
DataContainer playerData = table.createDataContainer(UUID.randomUUID());
playerData.put("username", "CoolGuy123")
          .put("home", new BlockPos(304, 62, 37)
          .put("nbt", new NbtCompound());

System.out.println(playerdata.getString("username"));
System.out.println(playerdata.getBlockPos("home"));
System.out.println(playerdata.getNbt("nbt"));

Custom Database Management

MySQLDatabase database = new MySQLDatabase("modId", "mydata", "192.168.1.69", "3306", "cooluser", "radman");
// OR
SQLiteDatabase database = new SQLiteDatabase("modId", "mydata", "some/dir");

Auto Incrementing Tables

You can make a table that auto increments its id with the following:

Table table = SQLib.getDatabase().createTable("modId", "towns")
        .setAutoIncrement()
        .addColumn("city", SQLDataType.STRING)
        .addColumn("portal", SQLDataType.BLOCKPOS)
        .finish();

DataContainer data = table.createDataContainerAutoID();
int id = data.getIdAsInt();

Transaction Support

This approach will bach sql commands into one command for faster read/writes of large amounts of data. You can begin and end a transaction at anytime.

Table table = data.createTable("modId", "userdata")
        .addColumn("username", SQLDataType.STRING)
        .addColumn("home", SQLDataType.BLOCKPOS)
        .addColumn("nbt", SQLDataType.NBT)
        .finish();

table.beginTransaction();
DataContainer playerData = table.createDataContainer(UUID.randomUUID());
playerData.put("username", "CoolGuy123");
playerData.put("home", new BlockPos(304, 62, 37);
table.endTransaction();

playerData.put("nbt", new NbtCompound());

Custom SQL Commands

If you need to do more complex things than the api allows for, you can run custom SQL commands.

MySQLDatabase database = new MySQLDatabase("modId", "mydata", "192.168.1.69", "3306", "cooluser", "radman");
Table table = data.createTable("modId", "userdata")
        .addColumn("username", SQLDataType.STRING)
        .addColumn("home", SQLDataType.BLOCKPOS)
        .addColumn("nbt", SQLDataType.NBT)
        .finish();

PreparedStatment stmt = database.executeCommand("SELECT ID FROM userdata WHERE username = ?", false, "bobross");
ResultSet result = stmt.getResultSet();

// Handle result
        
// Very important!! Your database connection may crash later if you never close it & give it back to the connection pool!
stmt.close();
stmt.getConnection().close();

//If you just want to run a command and not handle the result do the following. It will autoclose for you.
database.executeCommand("DELETE FROM userdata WHERE ID = ?", true, "bobross");