fix: also update last seen entry when bot disconnects

This commit is contained in:
ChomeNS
2025-05-03 14:14:04 +07:00
parent 059664ca0c
commit 4951286575
3 changed files with 32 additions and 18 deletions

View File

@@ -1 +1 @@
3065
3066

View File

@@ -146,6 +146,7 @@ public class Bot extends SessionAdapter {
this.world = new WorldPlugin(this);
this.core = new CorePlugin(this);
this.team = new TeamPlugin(this);
this.playersDatabase = new PlayersDatabasePlugin(this);
this.players = new PlayersPlugin(this);
this.tabComplete = new TabCompletePlugin(this);
this.commandHandler = new CommandHandlerPlugin(this);
@@ -170,7 +171,6 @@ public class Bot extends SessionAdapter {
// this.screenshare = new ScreensharePlugin(this);
this.clearChatNameAnnouncer = new ClearChatNameAnnouncerPlugin(this);
this.whitelist = new WhitelistPlugin(this);
this.playersDatabase = new PlayersDatabasePlugin(this);
this.ipFilter = new IPFilterPlugin(this);
this.rainbowArmor = new RainbowArmorPlugin(this);
}

View File

@@ -10,6 +10,7 @@ import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -182,27 +183,40 @@ public class PlayersDatabasePlugin implements Listener {
}
@Override
public void onPlayerLeft (final PlayerEntry target) {
public void disconnected (final DisconnectedEvent event) {
DatabasePlugin.EXECUTOR_SERVICE.submit(() -> {
try {
final PreparedStatement updatePlayerStatement = Main.database.connection.prepareStatement(UPDATE_PLAYER);
updatePlayerStatement.setString(1, "$.lastSeen");
updatePlayerStatement.setString(2, "$.lastSeen");
final ObjectNode lastSeenObject = getLastSeenObject();
updatePlayerStatement.setString(3, objectMapper.writeValueAsString(lastSeenObject));
updatePlayerStatement.setString(4, target.profile.getName());
updatePlayerStatement.executeUpdate();
} catch (final SQLException | JsonProcessingException e) {
bot.logger.error(e);
synchronized (bot.players.list) {
for (final PlayerEntry target : bot.players.list) {
updateLastSeenEntry(target);
}
}
});
}
@Override
public void onPlayerLeft (final PlayerEntry target) {
DatabasePlugin.EXECUTOR_SERVICE.submit(() -> updateLastSeenEntry(target));
}
private void updateLastSeenEntry (final PlayerEntry target) {
try {
final PreparedStatement updatePlayerStatement = Main.database.connection.prepareStatement(UPDATE_PLAYER);
updatePlayerStatement.setString(1, "$.lastSeen");
updatePlayerStatement.setString(2, "$.lastSeen");
final ObjectNode lastSeenObject = getLastSeenObject();
updatePlayerStatement.setString(3, objectMapper.writeValueAsString(lastSeenObject));
updatePlayerStatement.setString(4, target.profile.getName());
updatePlayerStatement.executeUpdate();
} catch (final SQLException | JsonProcessingException e) {
bot.logger.error(e);
}
}
private ObjectNode getLastSeenObject () {
final ObjectNode object = JsonNodeFactory.instance.objectNode();