Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ dependencies {
implementation("io.github.revxrsal:lamp.brigadier:${revxrsal_lamp_version}")
implementation("io.github.revxrsal:lamp.bukkit:${revxrsal_lamp_version}")

implementation("com.j256.ormlite:ormlite-jdbc:${ormlite_version}")
implementation("org.xerial:sqlite-jdbc:${sqlite_jdbc_version}")
implementation("com.mysql:mysql-connector-j:${mysql_connector_version}")

compileOnly("org.projectlombok:lombok:${lombok_version}")
annotationProcessor("org.projectlombok:lombok:${lombok_version}")
}
Expand Down Expand Up @@ -82,6 +86,7 @@ modrinth {
projectId = "hommr"
versionNumber = version
versionName = "Hommr ${version}"
versionType = version.toString().contains("beta") ? "beta" : (version.toString().contains("alpha") ? "alpha" : "release")
uploadFile = tasks.shadowJar
gameVersions = ["${minecraft_version}"]
loaders = ["paper", "purpur"]
Expand Down
6 changes: 5 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ yarn_mappings=1.21.8+build.1

lombok_version=1.18.38
revxrsal_lamp_version=4.0.0-rc.14
revxrsal_paper_version=4.0.0-beta.19
revxrsal_paper_version=4.0.0-beta.19

ormlite_version=6.1
sqlite_jdbc_version=3.51.1.0
mysql_connector_version=9.6.0
26 changes: 20 additions & 6 deletions src/main/java/me/axeno/hommr/Hommr.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package me.axeno.hommr;

import lombok.Getter;
import me.axeno.hommr.api.HommrApi;
import me.axeno.hommr.api.impl.HommrApiImpl;
import me.axeno.hommr.commands.HomeCommands;
import me.axeno.hommr.listeners.PlayerListener;
import me.axeno.hommr.managers.HomeManager;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -25,6 +24,11 @@ public final class Hommr extends JavaPlugin {
@Getter
private Lamp<BukkitCommandActor> lamp;

/**
* Initializes the plugin on enable: sets the singleton instance, ensures default configuration,
* starts the HomeManager, creates and registers the Hommr API service, builds the command lamp,
* registers plugin commands, and emits the startup log banner.
*/
@Override
public void onEnable() {
instance = this;
Expand All @@ -41,19 +45,30 @@ public void onEnable() {

lamp.register(new HomeCommands());

Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);

this.logLoadMessage();
}

/**
* Perform plugin shutdown tasks when the plugin is disabled.
*
* <p>Shuts down the HomeManager and logs a disable message to the SLF4J logger.</p>
*/
@Override
public void onDisable() {
HomeManager.shutdown();
this.getSLF4JLogger().info("Hommr Disabled");
}

/**
* Logs a stylized startup banner containing plugin, Java, and server information.
*
* <p>Emits a multi-line ASCII banner to the plugin logger that includes the plugin
* version, the running Java version, and the server name/version.</p>
*/
private void logLoadMessage() {
Logger logger = this.getSLF4JLogger();

@SuppressWarnings("UnstableApiUsage")
String pluginVersion = this.getPluginMeta().getVersion();
String javaVersion = System.getProperty("java.version");
String server = String.format("%s %s", Bukkit.getName(), Bukkit.getVersion());
Expand All @@ -65,5 +80,4 @@ private void logLoadMessage() {
logger.info("\u001B[1;34m |_||_|\\___/|_| |_|_| |_|_|_\\ \u001B[0m");
logger.info("\u001B[1;34m \u001B[0m");
}
}

}
13 changes: 0 additions & 13 deletions src/main/java/me/axeno/hommr/listeners/PlayerListener.java

This file was deleted.

114 changes: 114 additions & 0 deletions src/main/java/me/axeno/hommr/managers/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package me.axeno.hommr.managers;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import lombok.Getter;
import me.axeno.hommr.Hommr;
import me.axeno.hommr.models.Home;

import java.io.File;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

public class DatabaseManager {

private ConnectionSource connectionSource;
@Getter
private Dao<Home, Integer> homeDao;

/**
* Set up the database connection and DAO for Home entities based on configuration.
*
* Initializes the plugin data folder if necessary, creates a JDBC connection source
* (MySQL when `database.type` is `"mysql"`, otherwise SQLite using a `homes.db` file),
* and creates a Dao<Home, Integer> for accessing Home records. Ensures the Home table
* exists in the database; failures during folder creation, table creation, or overall
* initialization are logged.
*/
public void init() {
try {
File dataFolder = Hommr.getInstance().getDataFolder();
if (!dataFolder.exists()) {
boolean created = dataFolder.mkdirs();
if (!created) {
Hommr.getInstance().getLogger().warning("Could not create plugin data folder: " + dataFolder.getAbsolutePath());
}
}

String type = Hommr.getInstance().getConfig().getString("database.type", "sqlite").toLowerCase();
String databaseUrl;
String username = null;
String password = null;

if (type.equals("mysql")) {
databaseUrl = Hommr.getInstance().getConfig().getString("database.connection.url");
username = Hommr.getInstance().getConfig().getString("database.connection.username");
password = Hommr.getInstance().getConfig().getString("database.connection.password");
if (databaseUrl == null || databaseUrl.isEmpty()) {
throw new IllegalArgumentException("MySQL database URL is required but not configured. Please check your config.yml.");
}
connectionSource = new JdbcConnectionSource(databaseUrl, username, password);
} else {
databaseUrl = "jdbc:sqlite:" + new File(dataFolder, "homes.db").getAbsolutePath();
connectionSource = new JdbcConnectionSource(databaseUrl);
}
Comment thread
AxenoDev marked this conversation as resolved.

homeDao = DaoManager.createDao(connectionSource, Home.class);

TableUtils.createTableIfNotExists(connectionSource, Home.class);

} catch (SQLException e) {
Hommr.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Failed to initialize database", e);
throw new RuntimeException("Database initialization failed", e);
}
}
Comment thread
AxenoDev marked this conversation as resolved.

/**
* Closes the underlying database connection source and releases related resources.
*
* If an error occurs while closing, the exception is caught and a warning is logged.
*/
public void close() {
if (connectionSource != null) {
try {
connectionSource.close();
connectionSource = null;
} catch (Exception e) {
Hommr.getInstance().getLogger().log(java.util.logging.Level.WARNING, "Error closing database connection", e);
}
}
}

/**
* Retrieve all Home records from the database.
*
* @return a list of all Home objects
* @throws SQLException if a database access error occurs while querying for homes
*/
public List<Home> getAllHomes() throws SQLException {
return homeDao.queryForAll();
}

/**
* Replaces all stored Home records with the provided list.
*
* Clears the Home table and inserts the given homes in a single batch operation.
*
* @param homes the list of Home objects to persist (may be empty)
* @throws SQLException if an error occurs while clearing the table or saving records
*/
public void saveAllHomes(List<Home> homes) throws SQLException {
TransactionManager.callInTransaction(connectionSource, () -> {
TableUtils.clearTable(connectionSource, Home.class);
for (Home home : homes) {
homeDao.create(home);
}
return null;
});
}
Comment thread
AxenoDev marked this conversation as resolved.
}
Loading