Class DataManager
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum representing the types of databases supported. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate com.zaxxer.hikari.HikariDataSourceThe data source used for database connections.private final GravesThe main plugin instance associated with Graves.private DataManager.TypeThe type of the component or event. -
Constructor Summary
ConstructorsConstructorDescriptionDataManager(Graves plugin) Initializes the DataManager with the specified plugin instance and sets up the database connection. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBlockData(BlockData blockData) Adds block data to the database.private voidaddColumnIfNotExists(String tableName, String columnName, String columnDefinition) Adds a column to a table if it does not exist.voidaddEntityData(EntityData entityData) Adds entity data to the database.voidAdds a grave to the database.voidaddHologramData(HologramData hologramData) Adds hologram data to the database.private voidAdjusts the grave table for the target database if necessary for MSSQL.private voidalterColumnIfExists(String tableName, String columnName, String columnDefinition) Alters an existing column’s definition if the column already exists in the given table.private voidChecks if the Database is locked and attempts to unlock the database.private voidvoidCloses the database connection.private voidconfigureH2(com.zaxxer.hikari.HikariConfig config) Configures the H2 data source.private voidconfigureMSSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for Microsoft SQL Server (MSSQL).private voidconfigureMySQLOrMariaDB(com.zaxxer.hikari.HikariConfig config, DataManager.Type type) Configures the HikariConfig for MySQL or MariaDB.private voidconfigurePostgreSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for PostgreSQL.private voidconfigureSQLite(com.zaxxer.hikari.HikariConfig config) Configures the SQLite data source.private voidCreates the entity data map table if it does not exist.Returns the table name for the specified entity data type.private voidexecuteUpdate(String sql, Object[] parameters) Executes an update SQL statement with parameters.private voidexecuteUpdateMainThread(String sql, Object[] parameters) Executes an update SQL statement with parameters using the main thread.getChunkData(org.bukkit.Location location) Retrieves chunk data for a specified location.getColumnList(String tableName) Retrieves a list of columns for a specified table.private ConnectionRetrieves a connection from the data source.Retrieves the version of the database connection type.Retrieves the versions of supported databases.private StringRetrieves the configured storage prefix for GravesX from the plugin configuration.getType()private voidHandles unlocking for Microsoft SQL Server databases using COMMIT.private voidHandles unlocking for MySQL and MariaDB databases.private voidHandles unlocking for PostgreSQL/H2 databases using COMMIT or ROLLBACK.private voidHandles unlocking for SQLite databases using COMMIT or Rollback.booleanhasChunkData(org.bukkit.Location location) Checks if chunk data exists for a specified location.booleanhasGraveAtLocation(org.bukkit.Location location) Deprecated, for removal: This API element is subject to removal in a future version.Unused API.private booleanChecks if the database connection is active.private booleanisIntegrationEnabled(String integration) Checks if the integration is enabled.private booleanisVersionGreaterThan(String version) private voidKeeps the database connection alive by periodically executing a query.private voidload()Loads data from the database asynchronously.voidLoads the block map from the database (Folia-safe)private voidloadEntityDataMap(String table, EntityData.Type type) Loads entity data from the database.private voidloadEntityMap(String table, EntityData.Type type) Loads an entity map from the database.voidLoads the grave map from the database.voidLoads the hologram map from the database.private voidLoads database tables.voidloadType(DataManager.Type type) Loads the database type and sets up the data source.private StringmapSQLiteTypeToH2(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding H2 type.private StringmapSQLiteTypeToMSSQL(String sqliteType, String columnName) Maps a SQLite column type (including length specifiers) to the corresponding MSSQL type.private StringmapSQLiteTypeToMySQL(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding MySQL/MariaDB type.private StringmapSQLiteTypeToPostgreSQL(String sqliteType, String columnName) Maps a SQLite column type (including optional length/precision) to the corresponding PostgreSQL type.private StringmapSQLiteTypeToTargetDB(String sqliteType, String columnName) Maps a SQLite data type to the appropriate type for the configured target database (MySQL/MariaDB, PostgreSQL, H2, or MSSQL).private voidmigrate()Migrates data from the plugin’s SQLite schema into the configured target database.private voidMigrates root data to a sub-data directory.voidreload()Reloads the data manager with the current type.voidreload(DataManager.Type type) Reloads the data manager with the specified type.voidremoveBlockData(org.bukkit.Location location) Removes block data from the database.voidremoveChunkData(ChunkData chunkData) Removes chunk data.voidremoveEntityData(EntityData entityData) Removes entity data from the database.voidremoveEntityData(List<EntityData> entityDataList) Removes a list of entity data from the database.voidremoveGrave(Grave grave) Removes a grave from the database.voidremoveGrave(UUID uuid) voidremoveHologramData(Grave grave) Removes hologram entries from the database based on the grave UUID.resultSetToGrave(ResultSet resultSet) Converts a ResultSet to a Grave object.private static StringSanitizes custom grave providers.voidSets up the block table in the database.private voidsetupEntityTable(String name) Sets up an entity table in the database.private voidSets up entity tables.voidSets up the grave table in the database.voidSets up the hologram table in the database.booleantableExists(String tableName) Checks if a table exists in the database.private booleanTests the database connection.voidupdateGrave(Grave grave, String column, int integer) Updates a grave in the database.voidupdateGrave(Grave grave, String column, String string) Updates a grave in the database.voidupdateGraveMainThread(Grave grave, String column, String string) Updates a grave in the database using the main thread.
-
Field Details
-
plugin
The main plugin instance associated with Graves.This
Gravesinstance represents the core plugin that this Graves is part of. It provides access to the plugin's functionality, configuration, and other services. -
type
The type of the component or event.This
DataManager.Typerepresents the specific type or category of this component or event. -
dataSource
private com.zaxxer.hikari.HikariDataSource dataSourceThe data source used for database connections.This
HikariDataSourceprovides the connection pool for interacting with the database.
-
-
Constructor Details
-
DataManager
Initializes the DataManager with the specified plugin instance and sets up the database connection.- Parameters:
plugin- the Graves plugin instance.
-
-
Method Details
-
getType
-
load
private void load()Loads data from the database asynchronously. -
isIntegrationEnabled
Checks if the integration is enabled.- Parameters:
integration- The name of the integration.- Returns:
- true if enabled, false otherwise.
-
loadTables
Loads database tables.- Throws:
SQLException- if an SQL error occurs.
-
setupEntityTables
Sets up entity tables.- Throws:
SQLException- if an SQL error occurs.
-
sanitizeKey
Sanitizes custom grave providers. -
reload
public void reload()Reloads the data manager with the current type. -
reload
Reloads the data manager with the specified type.- Parameters:
type- the type of database.
-
loadType
Loads the database type and sets up the data source.- Parameters:
type- the type of database.
-
checkMariaDBasMySQL
private void checkMariaDBasMySQL() -
isVersionGreaterThan
-
configureSQLite
private void configureSQLite(com.zaxxer.hikari.HikariConfig config) Configures the SQLite data source.- Parameters:
config- the HikariConfig to configure.
-
configurePostgreSQL
private void configurePostgreSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for PostgreSQL.- Parameters:
config- the HikariConfig to configure.
-
configureH2
private void configureH2(com.zaxxer.hikari.HikariConfig config) Configures the H2 data source.- Parameters:
config- the HikariConfig to configure.
-
configureMySQLOrMariaDB
Configures the HikariConfig for MySQL or MariaDB.- Parameters:
config- the HikariConfig to configure.type- the type of database (MYSQL or MARIADB).
-
configureMSSQL
private void configureMSSQL(com.zaxxer.hikari.HikariConfig config) Configures the HikariConfig for Microsoft SQL Server (MSSQL).- Parameters:
config- the HikariConfig to configure.
-
migrateRootDataSubData
private void migrateRootDataSubData()Migrates root data to a sub-data directory. -
hasChunkData
public boolean hasChunkData(org.bukkit.Location location) Checks if chunk data exists for a specified location.- Parameters:
location- the location to check.- Returns:
- true if chunk data exists, false otherwise.
-
getChunkData
Retrieves chunk data for a specified location.- Parameters:
location- the location to retrieve chunk data for.- Returns:
- the chunk data.
-
removeChunkData
Removes chunk data.- Parameters:
chunkData- the chunk data to remove.
-
getColumnList
Retrieves a list of columns for a specified table.- Parameters:
tableName- the table name.- Returns:
- the list of columns.
-
tableExists
Checks if a table exists in the database.- Parameters:
tableName- the table name.- Returns:
- true if the table exists, false otherwise.
-
addColumnIfNotExists
private void addColumnIfNotExists(String tableName, String columnName, String columnDefinition) throws SQLException Adds a column to a table if it does not exist.- Parameters:
tableName- the table name.columnName- the column name.columnDefinition- the column definition.- Throws:
SQLException- if an SQL error occurs.
-
alterColumnIfExists
private void alterColumnIfExists(String tableName, String columnName, String columnDefinition) throws SQLException Alters an existing column’s definition if the column already exists in the given table.- Parameters:
tableName- the name of the table to modifycolumnName- the name of the column to altercolumnDefinition- the SQL definition to apply (type, length, etc.)- Throws:
SQLException- if an SQL error occurs during the ALTER TABLE operation
-
setupGraveTable
Sets up the grave table in the database.- Throws:
SQLException- if an SQL error occurs.
-
setupBlockTable
Sets up the block table in the database.- Throws:
SQLException- if an SQL error occurs.
-
setupHologramTable
Sets up the hologram table in the database.- Throws:
SQLException- if an SQL error occurs.
-
setupEntityTable
Sets up an entity table in the database.- Parameters:
name- the name of the table.- Throws:
SQLException- if an SQL error occurs.
-
loadGraveMap
public void loadGraveMap()Loads the grave map from the database. -
loadBlockMap
public void loadBlockMap()Loads the block map from the database (Folia-safe). - Reads DB asynchronously. - Batches rows per chunk and applies them on the chunk's region thread using plugin.getGravesXScheduler().execute(anchorLocation, ...). -
loadEntityMap
Loads an entity map from the database.- Parameters:
table- the table name.type- the type of entity data.
-
loadHologramMap
public void loadHologramMap()Loads the hologram map from the database. -
createEntityDataMapTable
Creates the entity data map table if it does not exist.- Parameters:
name- the name of the table.
-
loadEntityDataMap
Loads entity data from the database.- Parameters:
table- the table name.type- the type of entity data.
-
addBlockData
Adds block data to the database.- Parameters:
blockData- the block data to add.
-
removeBlockData
public void removeBlockData(org.bukkit.Location location) Removes block data from the database.Uses GravesXScheduler (UniversalScheduler) to run the in-memory cache update at the block's location (Folia-safe). The database delete runs asynchronously.
- Parameters:
location- the location of the block data to remove.
-
addHologramData
Adds hologram data to the database.Uses GravesXScheduler (UniversalScheduler) to execute the in-memory cache update at the hologram's location (Folia-safe). The database write is performed asynchronously.
- Parameters:
hologramData- the hologram data to add.
-
removeHologramData
Removes hologram entries from the database based on the grave UUID. This is a fallback if entity data is not loaded in memory.- Parameters:
grave- the grave to remove hologram data.
-
addEntityData
Adds entity data to the database.- Parameters:
entityData- the entity data to add.
-
removeEntityData
Removes entity data from the database.- Parameters:
entityData- the entity data to remove.
-
removeEntityData
Removes a list of entity data from the database.- Parameters:
entityDataList- the list of entity data to remove.
-
hasGraveAtLocation
@Deprecated(since="4.9.9.1", forRemoval=true) @ScheduledForRemoval(inVersion="4.9.11.1") public boolean hasGraveAtLocation(org.bukkit.Location location) Deprecated, for removal: This API element is subject to removal in a future version.Unused API. Deprecated as of 4.9.9.1 and scheduled for removal in 4.9.11.1.Checks whether a grave exists at the given location.Unused. This method is deprecated and will be removed.
- Parameters:
location- the location to check- Returns:
trueif a grave exists at the location;falseotherwise
-
entityDataTypeTable
Returns the table name for the specified entity data type.- Parameters:
type- the entity data type.- Returns:
- the table name.
-
addGrave
Adds a grave to the database.- Parameters:
grave- the grave to add.
-
removeGrave
Removes a grave from the database.- Parameters:
grave- the grave to remove.
-
removeGrave
-
updateGrave
Updates a grave in the database.- Parameters:
grave- the grave to update.column- the column to update.integer- the new integer value for the column.
-
updateGrave
Updates a grave in the database.- Parameters:
grave- the grave to update.column- the column to update.string- the new value for the column.
-
updateGraveMainThread
Updates a grave in the database using the main thread.- Parameters:
grave- the grave to update.column- the column to update.string- the new value for the column.
-
resultSetToGrave
Converts a ResultSet to a Grave object.- Parameters:
resultSet- the ResultSet to convert.- Returns:
- the Grave object, or null if an error occurs.
-
isConnected
private boolean isConnected()Checks if the database connection is active.- Returns:
- true if the connection is active, false otherwise.
-
getConnection
Retrieves a connection from the data source.- Returns:
- the database connection, or
nullif unavailable.
-
getDatabaseVersions
Retrieves the versions of supported databases.- Returns:
- a map of database types and their versions as integers.
- Throws:
SQLException- if a database access error occurs.
-
getDatabaseVersion
Retrieves the version of the database connection type.- Returns:
- the database version as a string.
-
closeConnection
public void closeConnection()Closes the database connection. -
executeUpdate
Executes an update SQL statement with parameters.- Parameters:
sql- the SQL statement.parameters- the parameters for the SQL statement.- Throws:
SQLException- if a database access error occurs.
-
executeUpdateMainThread
@Experimental private void executeUpdateMainThread(String sql, Object[] parameters) throws SQLException Executes an update SQL statement with parameters using the main thread.- Parameters:
sql- the SQL statement.parameters- the parameters for the SQL statement.- Throws:
SQLException- if a database access error occurs.
-
testDatabaseConnection
private boolean testDatabaseConnection()Tests the database connection.- Returns:
- true if the connection is successful, false otherwise.
-
migrate
private void migrate()Migrates data from the plugin’s SQLite schema into the configured target database.- Reads each table from the SQLite file in
plugin/data/data.db. - Recreates it in the target database with mapped column types. - Copies all rows. - Renames the SQLite file to data.old.db on success. -
mapSQLiteTypeToTargetDB
Maps a SQLite data type to the appropriate type for the configured target database (MySQL/MariaDB, PostgreSQL, H2, or MSSQL).- Parameters:
sqliteType- the SQLite type declaration (e.g. "VARCHAR(255)", "INT")columnName- the column name, used for special-case mappings- Returns:
- the target database type declaration, or null if no mapping is available
-
mapSQLiteTypeToMySQL
Maps a SQLite column type (including optional length/precision) to the corresponding MySQL/MariaDB type.- Parameters:
sqliteType- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName- the column name, used for special‐casing certain fields- Returns:
- the MySQL/MariaDB type declaration, or null if no mapping is available
-
mapSQLiteTypeToPostgreSQL
Maps a SQLite column type (including optional length/precision) to the corresponding PostgreSQL type.- Parameters:
sqliteType- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName- the column name, used for special casing certain fields- Returns:
- the PostgreSQL type declaration, or null if no mapping is available
-
mapSQLiteTypeToH2
Maps a SQLite column type (including optional length/precision) to the corresponding H2 type.- Parameters:
sqliteType- the SQLite type declaration (e.g. "VARCHAR(100)", "INT", "NUMERIC(8,2)")columnName- the column name, used for special casing certain fields- Returns:
- the H2 type declaration, or null if no mapping is available
-
mapSQLiteTypeToMSSQL
Maps a SQLite column type (including length specifiers) to the corresponding MSSQL type.- Parameters:
sqliteType- the SQLite type declaration (e.g. "VARCHAR(255)", "INT", "NUMERIC(10,2)")columnName- the column name, used for special casing certain fields- Returns:
- the MSSQL type declaration, or null if no mapping is available
-
adjustGraveTableForTargetDB
Adjusts the grave table for the target database if necessary for MSSQL.- Throws:
SQLException
-
keepConnectionAlive
private void keepConnectionAlive()Keeps the database connection alive by periodically executing a query. -
checkAndUnlockDatabase
private void checkAndUnlockDatabase()Checks if the Database is locked and attempts to unlock the database. -
handleUnlockSQLite
private void handleUnlockSQLite()Handles unlocking for SQLite databases using COMMIT or Rollback. -
handleUnlockMySQL
private void handleUnlockMySQL()Handles unlocking for MySQL and MariaDB databases. -
handleUnlockPostgreSQLandH2
private void handleUnlockPostgreSQLandH2()Handles unlocking for PostgreSQL/H2 databases using COMMIT or ROLLBACK. -
handleUnlockMSSQL
private void handleUnlockMSSQL()Handles unlocking for Microsoft SQL Server databases using COMMIT. -
getStoragePrefix
Retrieves the configured storage prefix for GravesX from the plugin configuration.If the prefix is not set or is empty/whitespace, this method returns an empty string. If a valid prefix is provided, it appends an underscore to the end (e.g.,
"gravesx_").While this is intended to help namespace GravesX database tables, note that shared databases (e.g., running multiple plugin instances in the same DB) are not supported.
- Returns:
- the formatted table prefix with an underscore, or an empty string if no prefix is set.
-