Skip to content

Commit 4508866

Browse files
authored
Merge pull request #5 from AxenoDev/feat/add-orm
Feat: Add an ORM to save homes
2 parents 8990d5a + ab8e35f commit 4508866

File tree

9 files changed

+347
-52
lines changed

9 files changed

+347
-52
lines changed

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ dependencies {
4242
implementation("io.github.revxrsal:lamp.brigadier:${revxrsal_lamp_version}")
4343
implementation("io.github.revxrsal:lamp.bukkit:${revxrsal_lamp_version}")
4444

45+
implementation("com.j256.ormlite:ormlite-jdbc:${ormlite_version}")
46+
implementation("org.xerial:sqlite-jdbc:${sqlite_jdbc_version}")
47+
implementation("com.mysql:mysql-connector-j:${mysql_connector_version}")
48+
4549
compileOnly("org.projectlombok:lombok:${lombok_version}")
4650
annotationProcessor("org.projectlombok:lombok:${lombok_version}")
4751
}
@@ -82,6 +86,7 @@ modrinth {
8286
projectId = "hommr"
8387
versionNumber = version
8488
versionName = "Hommr ${version}"
89+
versionType = version.toString().contains("beta") ? "beta" : (version.toString().contains("alpha") ? "alpha" : "release")
8590
uploadFile = tasks.shadowJar
8691
gameVersions = ["${minecraft_version}"]
8792
loaders = ["paper", "purpur"]

gradle.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ yarn_mappings=1.21.8+build.1
33

44
lombok_version=1.18.38
55
revxrsal_lamp_version=4.0.0-rc.14
6-
revxrsal_paper_version=4.0.0-beta.19
6+
revxrsal_paper_version=4.0.0-beta.19
7+
8+
ormlite_version=6.1
9+
sqlite_jdbc_version=3.51.1.0
10+
mysql_connector_version=9.6.0

src/main/java/me/axeno/hommr/Hommr.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package me.axeno.hommr;
22

3+
import lombok.Getter;
34
import me.axeno.hommr.api.HommrApi;
45
import me.axeno.hommr.api.impl.HommrApiImpl;
56
import me.axeno.hommr.commands.HomeCommands;
6-
import me.axeno.hommr.listeners.PlayerListener;
77
import me.axeno.hommr.managers.HomeManager;
8-
import lombok.Getter;
98
import org.bukkit.Bukkit;
109
import org.bukkit.plugin.ServicePriority;
1110
import org.bukkit.plugin.java.JavaPlugin;
@@ -25,6 +24,11 @@ public final class Hommr extends JavaPlugin {
2524
@Getter
2625
private Lamp<BukkitCommandActor> lamp;
2726

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

4246
lamp.register(new HomeCommands());
4347

44-
Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
45-
4648
this.logLoadMessage();
4749
}
4850

51+
/**
52+
* Perform plugin shutdown tasks when the plugin is disabled.
53+
*
54+
* <p>Shuts down the HomeManager and logs a disable message to the SLF4J logger.</p>
55+
*/
4956
@Override
5057
public void onDisable() {
58+
HomeManager.shutdown();
5159
this.getSLF4JLogger().info("Hommr Disabled");
5260
}
5361

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

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

src/main/java/me/axeno/hommr/listeners/PlayerListener.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package me.axeno.hommr.managers;
2+
3+
import com.j256.ormlite.dao.Dao;
4+
import com.j256.ormlite.dao.DaoManager;
5+
import com.j256.ormlite.jdbc.JdbcConnectionSource;
6+
import com.j256.ormlite.misc.TransactionManager;
7+
import com.j256.ormlite.support.ConnectionSource;
8+
import com.j256.ormlite.table.TableUtils;
9+
import lombok.Getter;
10+
import me.axeno.hommr.Hommr;
11+
import me.axeno.hommr.models.Home;
12+
13+
import java.io.File;
14+
import java.sql.SQLException;
15+
import java.util.List;
16+
import java.util.concurrent.Callable;
17+
18+
public class DatabaseManager {
19+
20+
private ConnectionSource connectionSource;
21+
@Getter
22+
private Dao<Home, Integer> homeDao;
23+
24+
/**
25+
* Set up the database connection and DAO for Home entities based on configuration.
26+
*
27+
* Initializes the plugin data folder if necessary, creates a JDBC connection source
28+
* (MySQL when `database.type` is `"mysql"`, otherwise SQLite using a `homes.db` file),
29+
* and creates a Dao<Home, Integer> for accessing Home records. Ensures the Home table
30+
* exists in the database; failures during folder creation, table creation, or overall
31+
* initialization are logged.
32+
*/
33+
public void init() {
34+
try {
35+
File dataFolder = Hommr.getInstance().getDataFolder();
36+
if (!dataFolder.exists()) {
37+
boolean created = dataFolder.mkdirs();
38+
if (!created) {
39+
Hommr.getInstance().getLogger().warning("Could not create plugin data folder: " + dataFolder.getAbsolutePath());
40+
}
41+
}
42+
43+
String type = Hommr.getInstance().getConfig().getString("database.type", "sqlite").toLowerCase();
44+
String databaseUrl;
45+
String username = null;
46+
String password = null;
47+
48+
if (type.equals("mysql")) {
49+
databaseUrl = Hommr.getInstance().getConfig().getString("database.connection.url");
50+
username = Hommr.getInstance().getConfig().getString("database.connection.username");
51+
password = Hommr.getInstance().getConfig().getString("database.connection.password");
52+
if (databaseUrl == null || databaseUrl.isEmpty()) {
53+
throw new IllegalArgumentException("MySQL database URL is required but not configured. Please check your config.yml.");
54+
}
55+
connectionSource = new JdbcConnectionSource(databaseUrl, username, password);
56+
} else {
57+
databaseUrl = "jdbc:sqlite:" + new File(dataFolder, "homes.db").getAbsolutePath();
58+
connectionSource = new JdbcConnectionSource(databaseUrl);
59+
}
60+
61+
homeDao = DaoManager.createDao(connectionSource, Home.class);
62+
63+
TableUtils.createTableIfNotExists(connectionSource, Home.class);
64+
65+
} catch (SQLException e) {
66+
Hommr.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Failed to initialize database", e);
67+
throw new RuntimeException("Database initialization failed", e);
68+
}
69+
}
70+
71+
/**
72+
* Closes the underlying database connection source and releases related resources.
73+
*
74+
* If an error occurs while closing, the exception is caught and a warning is logged.
75+
*/
76+
public void close() {
77+
if (connectionSource != null) {
78+
try {
79+
connectionSource.close();
80+
connectionSource = null;
81+
} catch (Exception e) {
82+
Hommr.getInstance().getLogger().log(java.util.logging.Level.WARNING, "Error closing database connection", e);
83+
}
84+
}
85+
}
86+
87+
/**
88+
* Retrieve all Home records from the database.
89+
*
90+
* @return a list of all Home objects
91+
* @throws SQLException if a database access error occurs while querying for homes
92+
*/
93+
public List<Home> getAllHomes() throws SQLException {
94+
return homeDao.queryForAll();
95+
}
96+
97+
/**
98+
* Replaces all stored Home records with the provided list.
99+
*
100+
* Clears the Home table and inserts the given homes in a single batch operation.
101+
*
102+
* @param homes the list of Home objects to persist (may be empty)
103+
* @throws SQLException if an error occurs while clearing the table or saving records
104+
*/
105+
public void saveAllHomes(List<Home> homes) throws SQLException {
106+
TransactionManager.callInTransaction(connectionSource, () -> {
107+
TableUtils.clearTable(connectionSource, Home.class);
108+
for (Home home : homes) {
109+
homeDao.create(home);
110+
}
111+
return null;
112+
});
113+
}
114+
}

0 commit comments

Comments
 (0)