2019-12-08 09:26:24 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Player Analytics (Plan).
|
|
|
|
*
|
|
|
|
* Plan is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License v3 as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Plan is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package com.djrapitops.plan.gathering.listeners.nukkit;
|
|
|
|
|
|
|
|
import cn.nukkit.Player;
|
|
|
|
import cn.nukkit.event.EventHandler;
|
|
|
|
import cn.nukkit.event.EventPriority;
|
|
|
|
import cn.nukkit.event.Listener;
|
|
|
|
import cn.nukkit.event.player.PlayerJoinEvent;
|
|
|
|
import cn.nukkit.event.player.PlayerKickEvent;
|
|
|
|
import cn.nukkit.event.player.PlayerLoginEvent;
|
|
|
|
import cn.nukkit.event.player.PlayerQuitEvent;
|
2022-08-14 19:35:32 +02:00
|
|
|
import com.djrapitops.plan.gathering.domain.NukkitPlayerData;
|
|
|
|
import com.djrapitops.plan.gathering.domain.event.PlayerJoin;
|
|
|
|
import com.djrapitops.plan.gathering.domain.event.PlayerLeave;
|
|
|
|
import com.djrapitops.plan.gathering.events.PlayerJoinEventConsumer;
|
|
|
|
import com.djrapitops.plan.gathering.events.PlayerLeaveEventConsumer;
|
2019-12-08 09:26:24 +01:00
|
|
|
import com.djrapitops.plan.gathering.listeners.Status;
|
|
|
|
import com.djrapitops.plan.identification.ServerInfo;
|
2021-03-12 17:03:00 +01:00
|
|
|
import com.djrapitops.plan.identification.ServerUUID;
|
2019-12-08 09:26:24 +01:00
|
|
|
import com.djrapitops.plan.storage.database.DBSystem;
|
2022-08-14 19:35:32 +02:00
|
|
|
import com.djrapitops.plan.storage.database.transactions.events.BanStatusTransaction;
|
|
|
|
import com.djrapitops.plan.storage.database.transactions.events.KickStoreTransaction;
|
2024-03-10 09:25:42 +01:00
|
|
|
import com.djrapitops.plan.storage.database.transactions.events.StoreAllowlistBounceTransaction;
|
2020-05-15 11:20:29 +02:00
|
|
|
import com.djrapitops.plan.utilities.logging.ErrorContext;
|
2020-05-14 15:57:29 +02:00
|
|
|
import com.djrapitops.plan.utilities.logging.ErrorLogger;
|
2019-12-08 09:26:24 +01:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event Listener for PlayerJoin, PlayerQuit and PlayerKickEvents.
|
|
|
|
*
|
2021-02-13 14:16:03 +01:00
|
|
|
* @author AuroraLS3
|
2019-12-08 09:26:24 +01:00
|
|
|
*/
|
|
|
|
public class PlayerOnlineListener implements Listener {
|
|
|
|
|
2022-08-14 19:35:32 +02:00
|
|
|
private final PlayerJoinEventConsumer joinEventConsumer;
|
|
|
|
private final PlayerLeaveEventConsumer leaveEventConsumer;
|
|
|
|
|
2019-12-08 09:26:24 +01:00
|
|
|
private final ServerInfo serverInfo;
|
|
|
|
private final DBSystem dbSystem;
|
2020-05-14 15:57:29 +02:00
|
|
|
private final ErrorLogger errorLogger;
|
2019-12-08 09:26:24 +01:00
|
|
|
private final Status status;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
public PlayerOnlineListener(
|
2022-08-14 19:35:32 +02:00
|
|
|
PlayerJoinEventConsumer joinEventConsumer, PlayerLeaveEventConsumer leaveEventConsumer,
|
2019-12-08 09:26:24 +01:00
|
|
|
ServerInfo serverInfo,
|
|
|
|
DBSystem dbSystem,
|
|
|
|
Status status,
|
2020-05-14 15:57:29 +02:00
|
|
|
ErrorLogger errorLogger
|
2019-12-08 09:26:24 +01:00
|
|
|
) {
|
2022-08-14 19:35:32 +02:00
|
|
|
this.joinEventConsumer = joinEventConsumer;
|
|
|
|
this.leaveEventConsumer = leaveEventConsumer;
|
2019-12-08 09:26:24 +01:00
|
|
|
this.serverInfo = serverInfo;
|
|
|
|
this.dbSystem = dbSystem;
|
|
|
|
this.status = status;
|
2020-05-14 15:57:29 +02:00
|
|
|
this.errorLogger = errorLogger;
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerLogin(PlayerLoginEvent event) {
|
|
|
|
try {
|
2021-04-17 07:24:42 +02:00
|
|
|
Player player = event.getPlayer();
|
|
|
|
UUID playerUUID = player.getUniqueId();
|
|
|
|
ServerUUID serverUUID = serverInfo.getServerUUID();
|
|
|
|
dbSystem.getDatabase().executeTransaction(new BanStatusTransaction(playerUUID, serverUUID, player::isBanned));
|
2019-12-08 09:26:24 +01:00
|
|
|
} catch (Exception e) {
|
2021-03-09 10:36:07 +01:00
|
|
|
errorLogger.error(e, ErrorContext.builder().related(event).build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PlayerKickEvent Listener.
|
|
|
|
* <p>
|
|
|
|
* Adds processing information to the ProcessingQueue.
|
|
|
|
* After KickEvent, the QuitEvent is automatically called.
|
|
|
|
*
|
|
|
|
* @param event Fired event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerKick(PlayerKickEvent event) {
|
|
|
|
try {
|
2024-03-10 09:25:42 +01:00
|
|
|
if (event.getReasonEnum() == PlayerKickEvent.Reason.NOT_WHITELISTED) {
|
|
|
|
dbSystem.getDatabase().executeTransaction(new StoreAllowlistBounceTransaction(
|
|
|
|
event.getPlayer().getUniqueId(),
|
|
|
|
event.getPlayer().getName(),
|
|
|
|
serverInfo.getServerUUID(), System.currentTimeMillis())
|
|
|
|
);
|
|
|
|
}
|
2020-06-20 20:25:47 +02:00
|
|
|
if (status.areKicksNotCounted() || event.isCancelled()) {
|
2019-12-08 09:26:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
UUID uuid = event.getPlayer().getUniqueId();
|
2021-03-16 09:41:17 +01:00
|
|
|
if (NukkitAFKListener.afkTracker.isAfk(uuid)) {
|
2019-12-08 09:26:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(uuid));
|
|
|
|
} catch (Exception e) {
|
2021-03-09 10:36:07 +01:00
|
|
|
errorLogger.error(e, ErrorContext.builder().related(event).build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
|
|
|
try {
|
|
|
|
actOnJoinEvent(event);
|
|
|
|
} catch (Exception e) {
|
2021-03-09 10:36:07 +01:00
|
|
|
errorLogger.error(e, ErrorContext.builder().related(event).build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void actOnJoinEvent(PlayerJoinEvent event) {
|
2022-08-14 19:35:32 +02:00
|
|
|
long time = System.currentTimeMillis();
|
2019-12-08 09:26:24 +01:00
|
|
|
Player player = event.getPlayer();
|
|
|
|
UUID playerUUID = player.getUniqueId();
|
2022-08-14 19:35:32 +02:00
|
|
|
if (playerUUID == null) return; // Can be null when player is not signed in to xbox live
|
2019-12-08 09:26:24 +01:00
|
|
|
|
2021-03-16 09:41:17 +01:00
|
|
|
NukkitAFKListener.afkTracker.performedAction(playerUUID, time);
|
2019-12-08 09:26:24 +01:00
|
|
|
|
2022-08-14 19:35:32 +02:00
|
|
|
joinEventConsumer.onJoinGameServer(PlayerJoin.builder()
|
|
|
|
.server(serverInfo.getServer())
|
|
|
|
.player(new NukkitPlayerData(player))
|
|
|
|
.time(time)
|
|
|
|
.build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL)
|
|
|
|
public void beforePlayerQuit(PlayerQuitEvent event) {
|
2022-08-14 19:35:32 +02:00
|
|
|
if (event.getPlayer().getUniqueId() == null) return; // Can be null when player is not signed in to xbox live
|
|
|
|
|
|
|
|
leaveEventConsumer.beforeLeave(PlayerLeave.builder()
|
|
|
|
.server(serverInfo.getServer())
|
|
|
|
.player(new NukkitPlayerData(event.getPlayer()))
|
|
|
|
.time(System.currentTimeMillis())
|
|
|
|
.build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
|
|
try {
|
|
|
|
actOnQuitEvent(event);
|
|
|
|
} catch (Exception e) {
|
2021-03-09 10:36:07 +01:00
|
|
|
errorLogger.error(e, ErrorContext.builder().related(event).build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void actOnQuitEvent(PlayerQuitEvent event) {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
UUID playerUUID = player.getUniqueId();
|
2020-02-04 10:17:13 +01:00
|
|
|
if (playerUUID == null) return; // Can be null when player is not signed in to xbox live
|
|
|
|
|
2021-03-16 09:41:17 +01:00
|
|
|
NukkitAFKListener.afkTracker.loggedOut(playerUUID, time);
|
2019-12-08 09:26:24 +01:00
|
|
|
|
2022-08-14 19:35:32 +02:00
|
|
|
leaveEventConsumer.onLeaveGameServer(PlayerLeave.builder()
|
|
|
|
.server(serverInfo.getServer())
|
|
|
|
.player(new NukkitPlayerData(event.getPlayer()))
|
|
|
|
.time(System.currentTimeMillis())
|
|
|
|
.build());
|
2019-12-08 09:26:24 +01:00
|
|
|
}
|
|
|
|
}
|