2025-12-06 19:25:23 +01:00
|
|
|
package me.firephoenix.deathcounter;
|
|
|
|
|
|
|
|
|
|
import me.clip.placeholderapi.PlaceholderAPI;
|
2026-03-25 17:59:00 +01:00
|
|
|
import me.firephoenix.deathcounter.commands.DeathsCommand;
|
2025-12-06 19:25:23 +01:00
|
|
|
import me.firephoenix.deathcounter.listener.DeathListener;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
|
|
public final class DeathCounter extends JavaPlugin {
|
|
|
|
|
public String dataBaseURL;
|
|
|
|
|
public HashMap<UUID, Integer> playerDeaths = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public HashMap<UUID, Integer> getPlayerDeaths() {
|
|
|
|
|
return playerDeaths;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getDataBaseURL() {
|
|
|
|
|
return dataBaseURL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onEnable() {
|
|
|
|
|
// Plugin startup logic
|
|
|
|
|
this.saveDefaultConfig();
|
|
|
|
|
|
|
|
|
|
Bukkit.getServer().getPluginManager().registerEvents(new DeathListener(this), this);
|
|
|
|
|
|
|
|
|
|
dataBaseURL = "jdbc:sqlite:" + this.getDataPath() + "/deaths.db";
|
|
|
|
|
|
|
|
|
|
createSqLiteDatabase();
|
|
|
|
|
|
2026-03-25 17:59:00 +01:00
|
|
|
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
|
|
|
|
new DeathPlaceholderExpansion(this).register();
|
2025-12-06 19:25:23 +01:00
|
|
|
}
|
2026-03-25 17:59:00 +01:00
|
|
|
|
|
|
|
|
getCommand("deaths").setExecutor(new DeathsCommand(this));
|
2025-12-06 19:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createSqLiteDatabase() {
|
|
|
|
|
try (var conn = DriverManager.getConnection(dataBaseURL)) {
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
String sql = "CREATE TABLE IF NOT EXISTS deaths_counts ("
|
|
|
|
|
+ " id BIGINT auto_increment,"
|
|
|
|
|
+ " uuid VARCHAR(36) NOT NULL UNIQUE,"
|
|
|
|
|
+ " deaths BIGINT NOT NULL,"
|
|
|
|
|
+ " primary key (id));";
|
|
|
|
|
|
|
|
|
|
Statement statement = conn.createStatement();
|
|
|
|
|
statement.execute(sql);
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
Bukkit.getLogger().log(Level.WARNING, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int requestDeathCountForPlayer(UUID uuid) {
|
|
|
|
|
//Search our cache first
|
|
|
|
|
if (getPlayerDeaths().containsKey(uuid)) {
|
|
|
|
|
return getPlayerDeaths().get(uuid);
|
|
|
|
|
} else {
|
|
|
|
|
int deaths = 0;
|
|
|
|
|
try (var conn = DriverManager.getConnection(this.getDataBaseURL())) {
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
String getSQL = "SELECT deaths FROM deaths_counts WHERE uuid = " + "'" + uuid + "'" + ";";
|
|
|
|
|
|
|
|
|
|
Statement statement = conn.createStatement();
|
|
|
|
|
ResultSet resultSet = statement.executeQuery(getSQL);
|
|
|
|
|
|
|
|
|
|
if (resultSet.next()) {
|
|
|
|
|
deaths = resultSet.getInt(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPlayerDeaths().put(uuid, deaths);
|
|
|
|
|
|
|
|
|
|
return deaths;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDisable() {
|
|
|
|
|
// Plugin shutdown logic
|
|
|
|
|
}
|
|
|
|
|
}
|