Skip to content

Commit

Permalink
fix crash when sqlib loads after depending mod
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar committed Nov 15, 2023
1 parent 8027b70 commit f3dd3e2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
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.
```java
// 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("userdata")
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/mrnavastar/sqlib/SQLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import me.mrnavastar.sqlib.database.Database;
import me.mrnavastar.sqlib.database.MySQLDatabase;
import me.mrnavastar.sqlib.database.SQLiteDatabase;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;

Expand All @@ -20,7 +20,7 @@
import java.util.HashMap;
import java.util.Objects;

public class SQLib implements ModInitializer {
public class SQLib implements PreLaunchEntrypoint {

public static final String MOD_ID = "SQLib";
public static final Gson GSON = new Gson();
Expand All @@ -30,7 +30,7 @@ public class SQLib implements ModInitializer {
private static SQLibConfig config = new SQLibConfig();

@Override
public void onInitialize() {
public void onPreLaunch() {
ServerLifecycleEvents.SERVER_STOPPING.register(server -> databaseRegistry.forEach((key, database) -> database.close()));
new File(FabricLoader.getInstance().getGameDir() + "/sqlib").mkdirs();

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"icon": "assets/sqlib/icon.png",
"environment": "*",
"entrypoints": {
"main": ["me.mrnavastar.sqlib.SQLib"]
"preLaunch": ["me.mrnavastar.sqlib.SQLib"]
},
"depends": {
"minecraft": ">=1.16.5"
Expand Down

0 comments on commit f3dd3e2

Please sign in to comment.