Skip to content

Commit

Permalink
Add PlayerTimeTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
WieszczY85 committed Jun 23, 2024
1 parent e44c9b8 commit b7d024f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/main/java/pl/mymc/mycoins/My_Coin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pl.mymc.mycoins;

import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerQuitEvent;
import pl.mymc.mycoins.databases.MySQLDatabaseHandler;
import pl.mymc.mycoins.events.PlayerTimeTracker;
import pl.mymc.mycoins.helpers.MyCoinsDependencyManager;
import pl.mymc.mycoins.helpers.MyCoinsLogger;
import pl.mymc.mycoins.helpers.MyCoinsVersionChecker;
Expand All @@ -13,12 +16,14 @@

import java.net.URLClassLoader;
import java.sql.SQLException;
import java.sql.Statement;

public final class My_Coin extends JavaPlugin {
public MyCoinsLogger logger;
private MyCoinsLogger logger;
private static Economy econ = null;
private static Permission perms = null;
private MySQLDatabaseHandler dbHandler;
private PlayerTimeTracker timeTracker;

@Override
public void onLoad() {
Expand Down Expand Up @@ -69,6 +74,27 @@ public void onEnable() {
} catch (SQLException | ClassNotFoundException e) {
logger.err("Nie udało się połączyć z bazą danych!" + e.getMessage());
}
try {
Statement statement = dbHandler.getConnection().createStatement();
logger.info("Sprawdzanie tableli " + pluginName + " w bazie danych");
String createTable = "CREATE TABLE IF NOT EXISTS `My-Coins` (" +
"`id` int(11) NOT NULL AUTO_INCREMENT," +
"`player` varchar(255) NOT NULL," +
"`uuid` varchar(255) NOT NULL," +
"`data` date NOT NULL," +
"`joinTime` time NOT NULL," +
"`quitTime` time NOT NULL," +
"`totalTime` time NOT NULL," +
"PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;";

statement.execute(createTable);
logger.info("Zakończono operacje na tabeli");
} catch (SQLException e) {
logger.err("Nie udało się utworzyć tabeli w bazie danych!" + e.getMessage());
}
timeTracker = new PlayerTimeTracker(dbHandler, logger);
getServer().getPluginManager().registerEvents(timeTracker, this);
logger.pluginStart();
logger.checkServerType();
MyCoinsVersionChecker.checkVersion(pluginName, currentVersion, logger, checkForUpdates, autoDownloadUpdates);
Expand Down Expand Up @@ -102,7 +128,11 @@ public static Permission getPermissions() {

@Override
public void onDisable() {
logger.err(getDescription().getName() +" Disabled Version "+ getDescription().getVersion());
timeTracker = new PlayerTimeTracker(dbHandler, logger);
getServer().getPluginManager().registerEvents(timeTracker, this);
for (Player player : Bukkit.getOnlinePlayers()) {
timeTracker.onPlayerQuit(new PlayerQuitEvent(player, ""));
}
try {
if (dbHandler.getConnection() != null && !dbHandler.getConnection().isClosed()) {
dbHandler.getConnection().close();
Expand All @@ -111,5 +141,6 @@ public void onDisable() {
} catch (SQLException e) {
logger.err("Nie udało się zamknąć połączenia z bazą danych!" + e.getMessage());
}
logger.err(getDescription().getName() +" Disabled Version "+ getDescription().getVersion());
}
}
60 changes: 60 additions & 0 deletions src/main/java/pl/mymc/mycoins/events/PlayerTimeTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pl.mymc.mycoins.events;

import pl.mymc.mycoins.databases.MySQLDatabaseHandler;
import pl.mymc.mycoins.helpers.MyCoinsLogger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;


import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Instant;

public class PlayerTimeTracker implements Listener {
private final MySQLDatabaseHandler dbHandler;
private final MyCoinsLogger logger;

public PlayerTimeTracker(MySQLDatabaseHandler dbHandler, MyCoinsLogger logger) {
this.dbHandler = dbHandler;
this.logger = logger;
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
String playerName = event.getPlayer().getName();
String playerUUID = event.getPlayer().getUniqueId().toString();
long joinTime = Instant.now().getEpochSecond();

try {
PreparedStatement statement = dbHandler.getConnection().prepareStatement("INSERT INTO `My-Coins` (player, uuid, joinTime) VALUES (?, ?, ?);");
statement.setString(1, playerName);
statement.setString(2, playerUUID);
statement.setLong(3, joinTime);
statement.executeUpdate();
} catch (SQLException e) {
logger.err("Błąd zapisu wejścia gracza do bazy danych" + e.getMessage());
}
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
String playerName = event.getPlayer().getName();
String playerUUID = event.getPlayer().getUniqueId().toString();
long quitTime = Instant.now().getEpochSecond();

try {
PreparedStatement statement = dbHandler.getConnection().prepareStatement("UPDATE `My-Coins` SET quitTime = ? WHERE uuid = ? AND quitTime IS NULL;");
statement.setLong(1, quitTime);
statement.setString(2, playerUUID);
statement.executeUpdate();

statement = dbHandler.getConnection().prepareStatement("UPDATE `My-Coins` SET totalTime = quitTime - joinTime WHERE uuid = ?;");
statement.setString(1, playerUUID);
statement.executeUpdate();
} catch (SQLException e) {
logger.err("Błąd zapisu wyjścia gracza do bazy danych" + e.getMessage());
}
}
}

0 comments on commit b7d024f

Please sign in to comment.