Initial version
This commit is contained in:
commit
d9c6d84ad3
7 changed files with 399 additions and 0 deletions
89
src/main/java/me/firephoenix/deathcounter/DeathCounter.java
Normal file
89
src/main/java/me/firephoenix/deathcounter/DeathCounter.java
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package me.firephoenix.deathcounter;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
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();
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { //
|
||||
new DeathPlaceholderExpansion(this).register(); //
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue