SQLib
SQLib is a simplified database library specifically adapted for use in Minecraft mods. It offers developers a convenient way to store various data types without the need to dive deep into SQL complexities.
Main Concept
This library is not a full-fledged SQL wrapper and does not provide access to all SQL features. Its main goal is to simplify the data storage process in your mods, making it as intuitive and efficient as possible.
Configuration Setup
On first launch, the mod automatically creates a configuration file that allows you to configure database parameters for all mods using SQLib. By default, it uses an SQLite database located in the sqlib directory.
Supported Data Types
The library provides three main categories of data types:
Standard Java Types:
- Byte, Byte[], Bool, Short, Int, Float, Double
- Long, String, Char, Date, Color, UUID, URI, URL
Minecraft-specific Types:
- Vec3i, BlockPos, ChunkPos, Text, Identifier
- Sound, Json, NbtElement
Adventure Types:
- Key, Component
The developer constantly expands the list of supported types while working on their projects. If you need an additional data type, you can create a corresponding request.
Installation
Add to build.gradle file:
repositories {
maven { url "https://api.modrinth.com/maven" }
}
dependencies {
modImplementation("maven.modrinth:sqlib:3.2.2")
}
Usage Example
// Do not call SQLib.getDatabase() in an early mod initializer - this may cause a crash
// Calling in a regular mod initializer or later is safe
Database db = SQLib.getDatabase();
DataStore store = db.dataStore("myModId", "userdata");
DataContainer playerData = store.createDataContainer();
playerData.put(JavaTypes.STRING, "username", "CoolGuy123");
playerData.put(MinecraftTypes.BLOCKPOS, "home", new BlockPos(304, 62, 37));
playerData.put(MinecraftTypes.NBT, "nbt", new NbtCompound());
System.out.println(playerdata.get(JavaTypes.STRING, "username"));
System.out.println(playerdata.get(MinecraftTypes.BLOCKPOS, "home"));
System.out.println(playerdata.get(MinecraftTypes.NBT, "nbt"));
Custom Database Management
Postgres db = new Postgres("name", "192.168.1.69", "3306", "cooluser", "radman");
// OR
MySQL db = new MySQL("name", "192.168.1.69", "3306", "cooluser", "radman");
// OR
SQLite db = new SQLite("name", "some/dir");
Transaction Support
This approach allows combining SQL commands into a single operation to speed up reading/writing large amounts of data.
DataStore store = db.dataStore("modId", "userdata");
DataContainer playerData = table.createDataContainer();
playerData.transaction().put("username", "CoolGuy123").put("home", new BlockPos(304, 62, 37).commit();
Creating Custom Types
You can add your own data types by following the implementation examples in the JavaTypes, MinecraftTypes and AdventureTypes classes. After creation, they can be used just like built-in SQLib types.
// SQLPrimitive is the base type for serialization, two lambda functions handle serialization and deserialization
public static final SQLibType<JsonElement> JSON = new SQLibType<>(SQLPrimitive.STRING, JsonElement::toString, JsonParser::parseString);
// You can also extend existing types:
public static final SQLibType<Identifier> IDENTIFIER = new SQLibType<>(SQLPrimitive.STRING, Identifier::toString, Identifier::tryParse);
public static final SQLibType<SoundEvent> SOUND = new SQLibType<>(IDENTIFIER, SoundEvent::getId, SoundEvent::of);