-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e44c9b8
commit b7d024f
Showing
2 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/pl/mymc/mycoins/events/PlayerTimeTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |