Skip to content

Commit

Permalink
Reload command added
Browse files Browse the repository at this point in the history
  • Loading branch information
axolotlagatsuma committed Aug 19, 2024
1 parent 4556ff8 commit 2ff26b9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
Binary file added .github/assets/bteme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<a href="https://buildtheearth.net/teams/me/"><img style="width: 250px; display: block; margin-left: auto; margin-right: auto;" src="./.github/assets/bteme.png" title="BTE Middle East Logo"></a>
<div style="text-align: center;">
<h1>Database Connector & File Reader</h1>
<p>A **Kotlin** plugin that connect to a database and copies files from that database</p>
</div>

### Contributing
- Help me optimize the plugin if it's possible. (please)
Original file line number Diff line number Diff line change
@@ -1,50 +1,71 @@
package org.axolotlagatsuma.databaseconnector

import org.bukkit.scheduler.BukkitRunnable
import org.bukkit.scheduler.BukkitTask
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.io.FileOutputStream
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException

class DatabaseConnector : JavaPlugin() {
class DatabaseConnector : JavaPlugin(), CommandExecutor {

private lateinit var databaseManager: DatabaseManager

override fun onEnable() {
// Initialize the database manager with configuration
databaseManager = DatabaseManager(
config.getString("database.url")!!,
config.getString("database.username")!!,
config.getString("database.password")!!,
config.getInt("database.poolSize"),
config.getString("database.tableName")!!,
config.getString("targetDirectory")!!
)
// Load initial configuration
saveDefaultConfig()
loadConfiguration()

// Register the db command
this.getCommand("db")?.setExecutor(this)

// Schedule the task to run every minute (1200 ticks)
val scheduler = server.scheduler
val task: BukkitTask = object : BukkitRunnable() {
override fun run() {
databaseManager.copyFilesToDirectory()
}
}.runTaskTimer(this, 0L, 1200L)
scheduler.runTaskTimer(this, Runnable {
databaseManager.copyFilesToDirectory()
}, 0L, 1200L)

logger.info("Plugin Enabled")
}


override fun onDisable() {
databaseManager.close()
logger.info("Plugin Disabled")
}

private fun loadConfiguration() {
reloadConfig()
databaseManager = DatabaseManager(
config.getString("database.url")!!,
config.getString("database.username")!!,
config.getString("database.password")!!,
config.getInt("database.poolSize"),
config.getString("database.tableName")!!,
config.getString("targetDirectory")!!
)
}

override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<String>): Boolean {
if (command.name.equals("db", ignoreCase = true)) {
if (args.isNotEmpty() && args[0].equals("reload", ignoreCase = true)) {
if (sender.hasPermission("databasemanager.reload")) {
loadConfiguration()
sender.sendMessage("§aConfiguration reloaded successfully.")
} else {
sender.sendMessage("§cYou do not have permission to execute this command.")
}
return true
} else {
sender.sendMessage("§cUsage: /$label reload")
return false
}
}
return false
}

class DatabaseManager(
private val url: String,
private val username: String,
Expand Down Expand Up @@ -107,18 +128,16 @@ class DatabaseConnector : JavaPlugin() {
val targetFile = File(directory, "$fileName.yml")

try {
FileOutputStream(targetFile).use { outputStream ->
outputStream.write(content.toByteArray())
}
targetFile.writeText(content)
} catch (e: Exception) {
Bukkit.getLogger().severe("Failed to write file: $fileName. Error: ${e.message}")
Bukkit.getServer().logger.severe("Failed to write file: $fileName. Error: ${e.message}")
}
}
}
}
}
} catch (e: SQLException) {
Bukkit.getLogger().severe("Failed to retrieve files from database: ${e.message}")
Bukkit.getServer().logger.severe("Failed to retrieve files from database: ${e.message}")
}
}

Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: DatabaseConnector
version: '1.0.1'
version: '1.0.2'
main: org.axolotlagatsuma.databaseconnector.DatabaseConnector
api-version: '1.21'
prefix: DBConnector
Expand All @@ -13,7 +13,7 @@ permissions:
default: op
commands:
db:
description: Let's you reload the database connection.
description: Let's you reload the plugin configuration.
aliases:
- db
- dbm
Expand Down

0 comments on commit 2ff26b9

Please sign in to comment.