Class DataManager

java.lang.Object
com.ranull.graves.manager.DataManager

public final class DataManager extends Object
Manages data storage and retrieval for the Graves plugin.
  • Field Details

    • plugin

      private final Graves plugin
      The main plugin instance associated with Graves.

      This Graves instance represents the core plugin that this Graves is part of. It provides access to the plugin's functionality, configuration, and other services.

    • type

      private DataManager.Type type
      The type of the component or event.

      This DataManager.Type represents the specific type or category of this component or event.

    • dataSource

      private com.zaxxer.hikari.HikariDataSource dataSource
      The data source used for database connections.

      This HikariDataSource provides the connection pool for interacting with the database.

  • Constructor Details

    • DataManager

      public DataManager(Graves plugin)
      Initializes the DataManager with the specified plugin instance and sets up the database connection.
      Parameters:
      plugin - the Graves plugin instance.
  • Method Details

    • getType

      public String getType()
    • load

      private void load()
      Loads data from the database asynchronously.
    • isIntegrationEnabled

      private boolean isIntegrationEnabled(String integration)
      Checks if the integration is enabled.
      Parameters:
      integration - The name of the integration.
      Returns:
      true if enabled, false otherwise.
    • loadTables

      private void loadTables() throws SQLException
      Loads database tables.
      Throws:
      SQLException - if an SQL error occurs.
    • setupEntityTables

      private void setupEntityTables() throws SQLException
      Sets up entity tables.
      Throws:
      SQLException - if an SQL error occurs.
    • reload

      public void reload()
      Reloads the data manager with the current type.
    • reload

      public void reload(DataManager.Type type)
      Reloads the data manager with the specified type.
      Parameters:
      type - the type of database.
    • loadType

      public void loadType(DataManager.Type type)
      Loads the database type and sets up the data source.
      Parameters:
      type - the type of database.
    • checkMariaDBasMySQL

      private void checkMariaDBasMySQL()
    • isVersionGreaterThan

      private boolean isVersionGreaterThan(String version)
    • 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.
    • addPostgreSQLProperties

      private void addPostgreSQLProperties(com.zaxxer.hikari.HikariConfig config, boolean ssl)
      Adds PostgreSQL-specific properties to the HikariConfig.
      Parameters:
      config - the HikariConfig to which properties will be added.
      ssl - whether SSL is enabled.
    • configureH2

      private void configureH2(com.zaxxer.hikari.HikariConfig config)
      Configures the H2 data source.
      Parameters:
      config - the HikariConfig to configure.
    • configureMySQLOrMariaDB

      private void configureMySQLOrMariaDB(com.zaxxer.hikari.HikariConfig config, DataManager.Type type)
      Configures the HikariConfig for MySQL or MariaDB.
      Parameters:
      config - the HikariConfig to configure.
      type - the type of database (MYSQL or MARIADB).
    • addMySQLProperties

      private void addMySQLProperties(com.zaxxer.hikari.HikariConfig config, boolean useSSL, boolean allowPublicKeyRetrieval, boolean verifyServerCertificate)
      Adds MySQL or MariaDB-specific properties to the HikariConfig.
      Parameters:
      config - the HikariConfig to which properties will be added.
      useSSL - whether SSL is enabled.
      allowPublicKeyRetrieval - whether to allow public key retrieval.
      verifyServerCertificate - whether to verify the server certificate.
    • 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

      public ChunkData getChunkData(org.bukkit.Location location)
      Retrieves chunk data for a specified location.
      Parameters:
      location - the location to retrieve chunk data for.
      Returns:
      the chunk data.
    • removeChunkData

      public void removeChunkData(ChunkData chunkData)
      Removes chunk data.
      Parameters:
      chunkData - the chunk data to remove.
    • getColumnList

      public List<String> getColumnList(String tableName)
      Retrieves a list of columns for a specified table.
      Parameters:
      tableName - the table name.
      Returns:
      the list of columns.
    • tableExists

      public boolean tableExists(String tableName)
      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 modify
      columnName - the name of the column to alter
      columnDefinition - the SQL definition to apply (type, length, etc.)
      Throws:
      SQLException - if an SQL error occurs during the ALTER TABLE operation
    • setupGraveTable

      public void setupGraveTable() throws SQLException
      Sets up the grave table in the database.
      Throws:
      SQLException - if an SQL error occurs.
    • setupBlockTable

      public void setupBlockTable() throws SQLException
      Sets up the block table in the database.
      Throws:
      SQLException - if an SQL error occurs.
    • setupHologramTable

      public void setupHologramTable() throws SQLException
      Sets up the hologram table in the database.
      Throws:
      SQLException - if an SQL error occurs.
    • setupEntityTable

      private void setupEntityTable(String name) throws SQLException
      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.
    • loadEntityMap

      private void loadEntityMap(String table, EntityData.Type type)
      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

      private void createEntityDataMapTable(String name)
      Creates the entity data map table if it does not exist.
      Parameters:
      name - the name of the table.
    • loadEntityDataMap

      private void loadEntityDataMap(String table, EntityData.Type type)
      Loads entity data from the database.
      Parameters:
      table - the table name.
      type - the type of entity data.
    • addBlockData

      public void addBlockData(BlockData blockData)
      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.
      Parameters:
      location - the location of the block data to remove.
    • addHologramData

      public void addHologramData(HologramData hologramData)
      Adds hologram data to the database.
      Parameters:
      hologramData - the hologram data to add.
    • removeHologramData

      public void removeHologramData(Grave grave)
      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.
    • removeHologramData

      @Deprecated(forRemoval=true) public void removeHologramData(List<EntityData> entityDataList)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use to properly remove grave hologram data Removes hologram data from the database.
      Parameters:
      entityDataList - the list of entity data to remove.
    • addEntityData

      public void addEntityData(EntityData entityData)
      Adds entity data to the database.
      Parameters:
      entityData - the entity data to add.
    • removeEntityData

      public void removeEntityData(EntityData entityData)
      Removes entity data from the database.
      Parameters:
      entityData - the entity data to remove.
    • removeEntityData

      public void removeEntityData(List<EntityData> entityDataList)
      Removes a list of entity data from the database.
      Parameters:
      entityDataList - the list of entity data to remove.
    • hasGraveAtLocation

      public boolean hasGraveAtLocation(org.bukkit.Location location)
    • entityDataTypeTable

      public String entityDataTypeTable(EntityData.Type type)
      Returns the table name for the specified entity data type.
      Parameters:
      type - the entity data type.
      Returns:
      the table name.
    • addGrave

      public void addGrave(Grave grave)
      Adds a grave to the database.
      Parameters:
      grave - the grave to add.
    • removeGrave

      public void removeGrave(Grave grave)
      Removes a grave from the database.
      Parameters:
      grave - the grave to remove.
    • removeGrave

      public void removeGrave(UUID uuid)
    • updateGrave

      public void updateGrave(Grave grave, String column, int integer)
      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

      public void updateGrave(Grave grave, String column, String string)
      Updates a grave in the database.
      Parameters:
      grave - the grave to update.
      column - the column to update.
      string - the new value for the column.
    • resultSetToGrave

      public Grave resultSetToGrave(ResultSet resultSet)
      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

      private Connection getConnection()
      Retrieves a connection from the data source.
      Returns:
      the database connection.
    • getDatabaseVersions

      public Map<String,Map<String,Integer>> getDatabaseVersions() throws SQLException
      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

      public String getDatabaseVersion() throws SQLException
      Retrieves the version of the database connection type.
      Returns:
      the database version as a string.
      Throws:
      SQLException - if a database access error occurs.
    • closeConnection

      public void closeConnection()
      Closes the database connection.
    • executeBatch

      private void executeBatch(Statement statement)
      Executes a batch of SQL statements.
      Parameters:
      statement - the statement containing the batch.
    • executeUpdate

      private void executeUpdate(String sql, Object[] parameters) throws SQLException
      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.
    • executeQuery

      private ResultSet executeQuery(String sql) throws SQLException
      Executes a query SQL statement.
      Parameters:
      sql - the SQL statement.
      Returns:
      the ResultSet of the query.
      Throws:
      SQLException
    • executeQuery

      private ResultSet executeQuery(String sql, Object[] params) throws SQLException
      Executes a query SQL statement with parameters.
      Parameters:
      sql - the SQL statement with placeholders (e.g., ? for parameters).
      params - the parameters to be set in the prepared statement.
      Returns:
      the ResultSet of the query.
      Throws:
      SQLException - if an SQL error occurs.
    • closeConnection

      private void closeConnection(Connection connection)
      Closes a database connection.
      Parameters:
      connection - the connection to close.
    • closeStatement

      private void closeStatement(Statement statement)
      Closes a statement.
      Parameters:
      statement - the statement to close.
    • closeResultSet

      private void closeResultSet(ResultSet resultSet)
      Closes a ResultSet.
      Parameters:
      resultSet - the ResultSet to close.
    • testDatabaseConnection

      private boolean testDatabaseConnection()
      Tests the MySQL 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

      private String mapSQLiteTypeToTargetDB(String sqliteType, String columnName)
      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

      private String mapSQLiteTypeToMySQL(String sqliteType, String columnName)
      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

      private String mapSQLiteTypeToPostgreSQL(String sqliteType, String columnName)
      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

      private String mapSQLiteTypeToH2(String sqliteType, String columnName)
      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

      private String mapSQLiteTypeToMSSQL(String sqliteType, String columnName)
      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

      private void adjustGraveTableForTargetDB() throws SQLException
      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

      @Experimental private String 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.