Fix INSTANCE being null

This commit is contained in:
Alessandro Proto 2024-08-06 12:27:00 +02:00
parent c16722fc8e
commit 17e839c1a8
3 changed files with 9 additions and 19 deletions

View file

@ -9,7 +9,7 @@ yarn_mappings=1.21+build.9
loader_version=0.15.11 loader_version=0.15.11
# Mod Properties # Mod Properties
mod_version=1.4.1 mod_version=1.4.2
maven_group=ct maven_group=ct
archives_base_name=ct-server archives_base_name=ct-server

View file

@ -6,15 +6,12 @@ import ct.server.database.PlayerTable;
import ct.server.events.PlayerWelcome; import ct.server.events.PlayerWelcome;
import ct.server.http.ServiceServer; import ct.server.http.ServiceServer;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.text.Texts;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,39 +31,38 @@ public class CtServer implements ModInitializer {
private static float currentMspt = 0; private static float currentMspt = 0;
private static int currentPlayerCount = 0; private static int currentPlayerCount = 0;
private static CtServer INSTANCE;
public static CtServer getInstance() { public static CtServer getInstance() {
return INSTANCE; return INSTANCE;
} }
private static CtServer INSTANCE;
private ServiceServer serviceServer; private ServiceServer serviceServer;
public ServiceServer serviceServer() { public ServiceServer serviceServer() {
return serviceServer; return serviceServer;
} }
private DatabaseClient database; private final DatabaseClient database = new DatabaseClient();
public DatabaseClient database() { public DatabaseClient database() {
return database; return database;
} }
private final PlayerTable playerTable = new PlayerTable(); private final PlayerTable playerTable = new PlayerTable();
public PlayerTable playerTable() { public PlayerTable playerTable() {
return playerTable; return playerTable;
} }
public CtServer() {
INSTANCE = this;
}
@Override @Override
public void onInitialize() { public void onInitialize() {
INSTANCE = this;
LOGGER.info("Starting ct-server"); LOGGER.info("Starting ct-server");
try { try {
database = new DatabaseClient(); // Jumpstart connection
database.connection();
playerTable.ensureDatabaseCreated(); playerTable.ensureDatabaseCreated();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Database error", e); LOGGER.error("Database error", e);

View file

@ -12,10 +12,4 @@ public class DatabaseClient {
} }
return connection; return connection;
} }
// Prepare a connection to the DB ready for use
// TODO: Consider creating a pool
public DatabaseClient() throws SQLException {
connection();
}
} }