fix player online
This commit is contained in:
2
pom.xml
2
pom.xml
@ -66,7 +66,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>1.21.4-R0.1-SNAPSHOT</version>
|
<version>1.21.10-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@ -68,8 +68,18 @@ public final class popa extends JavaPlugin implements Listener {
|
|||||||
// Инициализируем Gson для работы с JSON
|
// Инициализируем Gson для работы с JSON
|
||||||
gson = new Gson();
|
gson = new Gson();
|
||||||
|
|
||||||
|
// Регистрируем события
|
||||||
getServer().getPluginManager().registerEvents(this, this);
|
getServer().getPluginManager().registerEvents(this, this);
|
||||||
|
|
||||||
|
// ИНИЦИАЛИЗАЦИЯ УЖЕ ОНЛАЙН-ИГРОКОВ (после registerEvents)
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
UUID id = player.getUniqueId();
|
||||||
|
playerLoginTimes.put(id, now);
|
||||||
|
playerNames.put(id, player.getName());
|
||||||
|
lastActivityTimes.put(id, now);
|
||||||
|
}
|
||||||
|
|
||||||
// Запускаем планировщик для проверки команд
|
// Запускаем планировщик для проверки команд
|
||||||
scheduler = Executors.newSingleThreadScheduledExecutor();
|
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
scheduler.scheduleAtFixedRate(this::checkAndExecuteCommands, 5, 5, TimeUnit.SECONDS);
|
scheduler.scheduleAtFixedRate(this::checkAndExecuteCommands, 5, 5, TimeUnit.SECONDS);
|
||||||
@ -133,16 +143,20 @@ public final class popa extends JavaPlugin implements Listener {
|
|||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
UUID playerId = player.getUniqueId();
|
UUID playerId = player.getUniqueId();
|
||||||
String playerName = player.getName();
|
String playerName = player.getName();
|
||||||
lastActivityTimes.put(playerId, System.currentTimeMillis());
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
playerLoginTimes.put(playerId, System.currentTimeMillis());
|
// Считаем, что игрок активен с момента входа
|
||||||
|
playerLoginTimes.put(playerId, now);
|
||||||
playerNames.put(playerId, playerName);
|
playerNames.put(playerId, playerName);
|
||||||
|
lastActivityTimes.put(playerId, now);
|
||||||
|
|
||||||
String joinPayload = String.format(
|
String joinPayload = String.format(
|
||||||
"{\"event_type\":\"player_join\",\"player_id\":\"%s\",\"player_name\":\"%s\",\"server_ip\":\"%s\"}",
|
"{\"event_type\":\"player_join\",\"player_id\":\"%s\",\"player_name\":\"%s\",\"server_ip\":\"%s\"}",
|
||||||
playerId, playerName, serverIp
|
playerId, playerName, serverIp
|
||||||
);
|
);
|
||||||
sendEventToBackend(joinPayload);
|
sendEventToBackend(joinPayload);
|
||||||
|
|
||||||
|
// сразу обновим онлайн-лист на бэкенде
|
||||||
sendOnlinePlayersUpdate();
|
sendOnlinePlayersUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,8 +181,11 @@ public final class popa extends JavaPlugin implements Listener {
|
|||||||
);
|
);
|
||||||
sendEventToBackend(quitPayload);
|
sendEventToBackend(quitPayload);
|
||||||
|
|
||||||
|
// Полностью чистим все карты
|
||||||
playerLoginTimes.remove(playerId);
|
playerLoginTimes.remove(playerId);
|
||||||
playerNames.remove(playerId);
|
playerNames.remove(playerId);
|
||||||
|
lastActivityTimes.remove(playerId);
|
||||||
|
|
||||||
sendOnlinePlayersUpdate();
|
sendOnlinePlayersUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,15 +199,14 @@ public final class popa extends JavaPlugin implements Listener {
|
|||||||
String playerName = playerNames.get(playerId);
|
String playerName = playerNames.get(playerId);
|
||||||
Long lastActivity = lastActivityTimes.get(playerId);
|
Long lastActivity = lastActivityTimes.get(playerId);
|
||||||
|
|
||||||
// Если время последней активности не записано, пропускаем игрока
|
// Если нет последней активности — считаем, что игрок "не виден" бэкенду
|
||||||
if (lastActivity == null) {
|
if (lastActivity == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Вычисляем, сколько секунд игрок неактивен
|
|
||||||
long inactiveSeconds = (currentTime - lastActivity) / 1000;
|
long inactiveSeconds = (currentTime - lastActivity) / 1000;
|
||||||
|
|
||||||
// Если игрок неактивен больше минуты (60 секунд), пропускаем его
|
// Больше минуты не двигался — не отправляем в список активных
|
||||||
if (inactiveSeconds > 60) {
|
if (inactiveSeconds > 60) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user