Plan/Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/listeners/bukkit/PlayerOnlineListener.java

191 lines
7.6 KiB
Java
Raw Normal View History

/*
* 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.bukkit;
import com.djrapitops.plan.gathering.cache.JoinAddressCache;
import com.djrapitops.plan.gathering.domain.BukkitPlayerData;
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;
import com.djrapitops.plan.gathering.listeners.Status;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.transactions.events.BanStatusTransaction;
import com.djrapitops.plan.storage.database.transactions.events.KickStoreTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreAllowlistBounceTransaction;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
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
*/
public class PlayerOnlineListener implements Listener {
private final PlayerJoinEventConsumer playerJoinEventConsumer;
private final PlayerLeaveEventConsumer playerLeaveEventConsumer;
private final JoinAddressCache joinAddressCache;
private final ServerInfo serverInfo;
private final DBSystem dbSystem;
private final ErrorLogger errorLogger;
[Merge] Module split (#786) * Moved project files to 'common'-module This is done so that refactoring into multiple smaller modules is easier as the IDE will not attempt to move tests incorrectly when moving things between different modules * Created 'bukkit' module Following classes were removed during the operation: - BukkitServerInfo (Renamed to ServerServerInfo) - SpongeServerInfo (could use ServerServerInfo) - Hastebin (not viable on every platform, unused) - HastebinTest - MockPlayers (unused, bukkit specific) Changes to classes: - Renamed Importer to BukkitImporter (contained bukkit related impl.) - Extracted Importer interface from BukkitImporter - Turned BukkitPlanModule and BukkitClassBindingModule to interfaces by using @Binds annotation - Added Status class since PlayersOnlineListener had a boolean, that stated if kicks were counted. This commit completes split partially and was not pushed on commit. * Created 'sponge' module Changes to classes: - Turned ServerSuperClassBindingModule, SuperClassBindingModule, SpongePlanModule and SpongeClassBindingModule to interfaces by using @Binds annotation - Renamed SystemObjectBindingModule to SystemObjectProvidingModule - Removed sponge related calls in ServerProperties - Made EmptyImportSystem injectable This commit completes split partially and was not pushed on commit. * Changed shade configuration Further tweaks required. * Created 'bungeecord' module Changes to classes: - Removed use of BungeePingTimerTask in VelocityTaskSystem - Removed use of RedisBungee in VelocityServerProperties - Fixed bukkit command.commands.RegisterCommandFilter package Changes to project structure: - Dependency versions now in main pom via dependencyManagement - Repositories now defined in main pom * Created 'velocity' module Changes to classes: - Made RawDataResponse use Gson via reflection since it is no longer available in dependencies of 'common' module * Created 'plugin' module This module is for creating a single deployment artifact and testing of system interactions. Fixes to tests: - Reflection no longer fails to initialize if Bukkit.getServer() is null - PingCountTimerBukkit no longer fails to be created if Reflection fails - Removed unnecessary @AfterClass from H2Test - Jar resource path fixes to Mocker * Shading configuration org.slf4j classes are relocated in 'common', 'bukkit' & 'bungeecord'. In 'sponge' & 'velocity' they are not relocated, allowing injection as plugin logger, while using slf4j-nop for HikariCP logging. This allows single release artifact. * Removed 'Icon' from .gitignore * Attempt to fix test resources Because all jar resources are located in 'common', an attempt to fetch them is made to common/target/Plan-common.jar, which for some reason is a bad path. This attempts to remedy that by creating a temporary file from an InputStream, read with Class#getResourceAsStream Ignored HTTPSWebServerAuthTest as the certificate path was invalid for some reason.
2018-11-11 11:55:09 +01:00
private final Status status;
@Inject
public PlayerOnlineListener(
PlayerJoinEventConsumer playerJoinEventConsumer,
PlayerLeaveEventConsumer playerLeaveEventConsumer,
JoinAddressCache joinAddressCache,
ServerInfo serverInfo,
DBSystem dbSystem,
[Merge] Module split (#786) * Moved project files to 'common'-module This is done so that refactoring into multiple smaller modules is easier as the IDE will not attempt to move tests incorrectly when moving things between different modules * Created 'bukkit' module Following classes were removed during the operation: - BukkitServerInfo (Renamed to ServerServerInfo) - SpongeServerInfo (could use ServerServerInfo) - Hastebin (not viable on every platform, unused) - HastebinTest - MockPlayers (unused, bukkit specific) Changes to classes: - Renamed Importer to BukkitImporter (contained bukkit related impl.) - Extracted Importer interface from BukkitImporter - Turned BukkitPlanModule and BukkitClassBindingModule to interfaces by using @Binds annotation - Added Status class since PlayersOnlineListener had a boolean, that stated if kicks were counted. This commit completes split partially and was not pushed on commit. * Created 'sponge' module Changes to classes: - Turned ServerSuperClassBindingModule, SuperClassBindingModule, SpongePlanModule and SpongeClassBindingModule to interfaces by using @Binds annotation - Renamed SystemObjectBindingModule to SystemObjectProvidingModule - Removed sponge related calls in ServerProperties - Made EmptyImportSystem injectable This commit completes split partially and was not pushed on commit. * Changed shade configuration Further tweaks required. * Created 'bungeecord' module Changes to classes: - Removed use of BungeePingTimerTask in VelocityTaskSystem - Removed use of RedisBungee in VelocityServerProperties - Fixed bukkit command.commands.RegisterCommandFilter package Changes to project structure: - Dependency versions now in main pom via dependencyManagement - Repositories now defined in main pom * Created 'velocity' module Changes to classes: - Made RawDataResponse use Gson via reflection since it is no longer available in dependencies of 'common' module * Created 'plugin' module This module is for creating a single deployment artifact and testing of system interactions. Fixes to tests: - Reflection no longer fails to initialize if Bukkit.getServer() is null - PingCountTimerBukkit no longer fails to be created if Reflection fails - Removed unnecessary @AfterClass from H2Test - Jar resource path fixes to Mocker * Shading configuration org.slf4j classes are relocated in 'common', 'bukkit' & 'bungeecord'. In 'sponge' & 'velocity' they are not relocated, allowing injection as plugin logger, while using slf4j-nop for HikariCP logging. This allows single release artifact. * Removed 'Icon' from .gitignore * Attempt to fix test resources Because all jar resources are located in 'common', an attempt to fetch them is made to common/target/Plan-common.jar, which for some reason is a bad path. This attempts to remedy that by creating a temporary file from an InputStream, read with Class#getResourceAsStream Ignored HTTPSWebServerAuthTest as the certificate path was invalid for some reason.
2018-11-11 11:55:09 +01:00
Status status,
ErrorLogger errorLogger
) {
this.playerJoinEventConsumer = playerJoinEventConsumer;
this.playerLeaveEventConsumer = playerLeaveEventConsumer;
this.joinAddressCache = joinAddressCache;
this.serverInfo = serverInfo;
this.dbSystem = dbSystem;
[Merge] Module split (#786) * Moved project files to 'common'-module This is done so that refactoring into multiple smaller modules is easier as the IDE will not attempt to move tests incorrectly when moving things between different modules * Created 'bukkit' module Following classes were removed during the operation: - BukkitServerInfo (Renamed to ServerServerInfo) - SpongeServerInfo (could use ServerServerInfo) - Hastebin (not viable on every platform, unused) - HastebinTest - MockPlayers (unused, bukkit specific) Changes to classes: - Renamed Importer to BukkitImporter (contained bukkit related impl.) - Extracted Importer interface from BukkitImporter - Turned BukkitPlanModule and BukkitClassBindingModule to interfaces by using @Binds annotation - Added Status class since PlayersOnlineListener had a boolean, that stated if kicks were counted. This commit completes split partially and was not pushed on commit. * Created 'sponge' module Changes to classes: - Turned ServerSuperClassBindingModule, SuperClassBindingModule, SpongePlanModule and SpongeClassBindingModule to interfaces by using @Binds annotation - Renamed SystemObjectBindingModule to SystemObjectProvidingModule - Removed sponge related calls in ServerProperties - Made EmptyImportSystem injectable This commit completes split partially and was not pushed on commit. * Changed shade configuration Further tweaks required. * Created 'bungeecord' module Changes to classes: - Removed use of BungeePingTimerTask in VelocityTaskSystem - Removed use of RedisBungee in VelocityServerProperties - Fixed bukkit command.commands.RegisterCommandFilter package Changes to project structure: - Dependency versions now in main pom via dependencyManagement - Repositories now defined in main pom * Created 'velocity' module Changes to classes: - Made RawDataResponse use Gson via reflection since it is no longer available in dependencies of 'common' module * Created 'plugin' module This module is for creating a single deployment artifact and testing of system interactions. Fixes to tests: - Reflection no longer fails to initialize if Bukkit.getServer() is null - PingCountTimerBukkit no longer fails to be created if Reflection fails - Removed unnecessary @AfterClass from H2Test - Jar resource path fixes to Mocker * Shading configuration org.slf4j classes are relocated in 'common', 'bukkit' & 'bungeecord'. In 'sponge' & 'velocity' they are not relocated, allowing injection as plugin logger, while using slf4j-nop for HikariCP logging. This allows single release artifact. * Removed 'Icon' from .gitignore * Attempt to fix test resources Because all jar resources are located in 'common', an attempt to fetch them is made to common/target/Plan-common.jar, which for some reason is a bad path. This attempts to remedy that by creating a temporary file from an InputStream, read with Class#getResourceAsStream Ignored HTTPSWebServerAuthTest as the certificate path was invalid for some reason.
2018-11-11 11:55:09 +01:00
this.status = status;
this.errorLogger = errorLogger;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogin(PlayerLoginEvent event) {
try {
UUID playerUUID = event.getPlayer().getUniqueId();
ServerUUID serverUUID = serverInfo.getServerUUID();
boolean banned = PlayerLoginEvent.Result.KICK_BANNED == event.getResult();
boolean notWhitelisted = PlayerLoginEvent.Result.KICK_WHITELIST == event.getResult();
if (notWhitelisted) {
dbSystem.getDatabase().executeTransaction(new StoreAllowlistBounceTransaction(playerUUID, event.getPlayer().getName(), serverUUID, System.currentTimeMillis()));
}
String address = event.getHostname();
if (!address.isEmpty()) {
if (address.contains(":")) {
address = address.substring(0, address.lastIndexOf(':'));
}
if (address.contains("\u0000")) {
address = address.substring(0, address.indexOf('\u0000'));
}
2022-11-11 20:20:57 +01:00
if (address.contains("fml")) {
address = address.substring(0, address.lastIndexOf("fml"));
}
joinAddressCache.put(playerUUID, address);
}
dbSystem.getDatabase().executeTransaction(new BanStatusTransaction(playerUUID, serverUUID, banned));
} catch (Exception e) {
errorLogger.error(e, ErrorContext.builder().related(event, event.getResult()).build());
}
}
/**
* 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 {
if (status.areKicksNotCounted() || event.isCancelled()) {
return;
}
UUID uuid = event.getPlayer().getUniqueId();
if (BukkitAFKListener.afkTracker.isAfk(uuid)) {
return;
}
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(uuid));
} catch (Exception e) {
errorLogger.error(e, ErrorContext.builder().related(event).build());
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
try {
actOnJoinEvent(event);
} catch (Exception e) {
errorLogger.error(e, ErrorContext.builder().related(event).build());
}
}
private void actOnJoinEvent(PlayerJoinEvent event) {
long time = System.currentTimeMillis();
UUID playerUUID = event.getPlayer().getUniqueId();
BukkitAFKListener.afkTracker.performedAction(playerUUID, time);
playerJoinEventConsumer.onJoinGameServer(PlayerJoin.builder()
.server(serverInfo.getServer())
.player(new BukkitPlayerData(event.getPlayer(), joinAddressCache.getNullableString(playerUUID)))
.time(time)
.build());
}
@EventHandler(priority = EventPriority.NORMAL)
public void beforePlayerQuit(PlayerQuitEvent event) {
try {
playerLeaveEventConsumer.beforeLeave(PlayerLeave.builder()
.server(serverInfo.getServer())
.player(new BukkitPlayerData(event.getPlayer(), null))
.time(System.currentTimeMillis())
.build());
} catch (Exception e) {
errorLogger.error(e, ErrorContext.builder().related(event).build());
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
try {
actOnQuitEvent(event);
} catch (Exception e) {
errorLogger.error(e, ErrorContext.builder().related(event).build());
}
}
private void actOnQuitEvent(PlayerQuitEvent event) {
long time = System.currentTimeMillis();
UUID playerUUID = event.getPlayer().getUniqueId();
BukkitAFKListener.afkTracker.loggedOut(playerUUID, time);
playerLeaveEventConsumer.onLeaveGameServer(PlayerLeave.builder()
.server(serverInfo.getServer())
.player(new BukkitPlayerData(event.getPlayer(), null))
.time(System.currentTimeMillis())
.build());
}
}