From 2eddb833d43da2f8dfca2163e79ba32eabd56d8f Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 9 Oct 2024 02:32:47 +0200 Subject: [PATCH] Add PlayerData & ConnectionListener classes --- .../de/jeremystartm/pickshadow/Extension.java | 6 +- .../api/entity/player/PlayerData.java | 87 ++++++++++ .../api/entity/player/PlayerDataFactory.java | 160 ++++++++++++++++++ .../api/translation/LanguageString.java | 5 +- .../pickshadow/listener/ChatListener.java | 2 + .../listener/ConnectionListener.java | 65 +++++++ .../src/main/resources/translations/en.json | 2 + 7 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerData.java create mode 100644 extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerDataFactory.java create mode 100644 extension/src/main/java/de/jeremystartm/pickshadow/listener/ConnectionListener.java diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/Extension.java b/extension/src/main/java/de/jeremystartm/pickshadow/Extension.java index 13d757f..dcf0140 100644 --- a/extension/src/main/java/de/jeremystartm/pickshadow/Extension.java +++ b/extension/src/main/java/de/jeremystartm/pickshadow/Extension.java @@ -19,10 +19,12 @@ package de.jeremystartm.pickshadow; +import de.jeremystartm.pickshadow.api.entity.player.PlayerDataFactory; import de.jeremystartm.pickshadow.api.translation.TranslationManager; import de.jeremystartm.pickshadow.command.general.*; import de.jeremystartm.pickshadow.command.survival.HomeCommand; import de.jeremystartm.pickshadow.listener.ChatListener; +import de.jeremystartm.pickshadow.listener.ConnectionListener; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.utility.Miscellaneous; import lombok.Getter; @@ -78,7 +80,7 @@ public final class Extension extends JavaPlugin { try { Class.forName("net.minecraft.server.MinecraftServer"); } catch (Exception exception) { - logger.crash("This Bukkit implementation does not run on NMS.\nPSSE is designed to only run on NMS-based server software.\nYou may need to rewrite certain parts of PSSE so it can run on non-NMS server softwares.", exception); + logger.crash("This Bukkit implementation does not run on NMS.\nPSSE is designed to only run on NMS-based server software.\nYou may need to rewrite certain parts of PSSE so it can run on non-NMS server software.", exception); return; } @@ -87,6 +89,7 @@ public final class Extension extends JavaPlugin { ExtensionInformation.update(); TranslationManager.loadTranslations(); TranslationManager.processTranslations(); + PlayerDataFactory.initialize(); logger.info("Bootstrapped in " + Miscellaneous.measureExecutionTime(() -> {}) + "ms"); } @@ -112,6 +115,7 @@ public final class Extension extends JavaPlugin { new TrollCommand(); logger.verb("Registering listeners"); + Bukkit.getPluginManager().registerEvents(new ConnectionListener(), this); Bukkit.getPluginManager().registerEvents(new ChatListener(), this); }) + "ms"); } catch (Exception exception) { diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerData.java b/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerData.java new file mode 100644 index 0000000..86beb16 --- /dev/null +++ b/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerData.java @@ -0,0 +1,87 @@ +/* + * PICKSHADOW SERVER KIT SOURCE FILE + * Copyright (c) 2024 The PickShadow Server Kit authors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.jeremystartm.pickshadow.api.entity.player; + + +import de.jeremystartm.pickshadow.misc.TabListHandler; +import de.staropensource.sosengine.base.logging.LoggerInstance; +import fr.mrmicky.fastboard.adventure.FastBoard; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +@Setter +@Getter +@SuppressWarnings({ "JavadocDeclaration" }) +public final class PlayerData { + /** + * Contains the logger instance for this instance. + * + * @since v1-release0 + */ + private final LoggerInstance logger; + + /** + * Contains the associated {@link Player}. + * + * @since v1-release0 + * -- GETTER -- + * Returns the associated {@link Player}. + * + * @return associated {@link Player} + * @since v1-release0 + */ + @Setter(value = AccessLevel.NONE) + private final Player player; + + /** + * Contains the player list scoreboard. + * + * @since v1-release0 + * -- GETTER -- + * Returns the player list scoreboard. + * + * @return player list scoreboard + * @since v1-release0 + * -- SETTER -- + * Sets the player list scoreboard. + * + * @param playerListScoreboard new player list scoreboard + * @since v1-release0 + */ + @ApiStatus.Experimental + private @NotNull FastBoard playerListScoreboard; + + /** + * Creates and initializes an instance of this class. + * + * @param player {@link Player} to use + * @since v1-release0 + */ + PlayerData(Player player) { + this.player = player; + this.logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("PSSE").setMetadata(player.getName()).build(); + + //TabListHandler.getInstance().initializeTabList(this); + } +} diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerDataFactory.java b/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerDataFactory.java new file mode 100644 index 0000000..cdb9620 --- /dev/null +++ b/extension/src/main/java/de/jeremystartm/pickshadow/api/entity/player/PlayerDataFactory.java @@ -0,0 +1,160 @@ +/* + * PICKSHADOW SERVER KIT SOURCE FILE + * Copyright (c) 2024 The PickShadow Server Kit authors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.jeremystartm.pickshadow.api.entity.player; + +import de.staropensource.sosengine.base.logging.LoggerInstance; +import lombok.Getter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * A factory for {@link Player}s. + * + * @since v1-release0 + */ +@SuppressWarnings({ "JavadocDeclaration" }) +public final class PlayerDataFactory { + /** + * Contains the global instance of this class. + * + * @since v1-release0 + * -- GETTER -- + * Returns the global instance of this class. + * + * @return global instance + * @since v1-release0 + */ + @Getter + private static PlayerDataFactory instance; + + /** + * Contains the logger instance of this instance. + * + * @since v1-release0 + */ + private final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("PSSE").build(); + + /** + * Contains all registered {@link PlayerData} instances. + * + * @since v1-release0 + */ + private final @NotNull List<@NotNull PlayerData> playerList = new ArrayList<>(); + + /** + * Creates and initializes an instance of this class. + * + * @since v1-release0 + */ + private PlayerDataFactory() {} + + /** + * Initializes this factory, + * if it isn't already. + * + * @since v1-release0 + */ + public static void initialize() { + if (instance == null) + instance = new PlayerDataFactory(); + } + + /** + * Returns an already existing {@link PlayerData} instance. + * + * @param uuid player {@link UUID} + * @return {@link PlayerData} instance + * @throws NullPointerException on error + * @since v1-release0 + */ + public PlayerData get(@NotNull UUID uuid) throws NullPointerException { + for (PlayerData playerData : playerList) + if (playerData.getPlayer().getUniqueId() == uuid) + return playerData; + + throw new NullPointerException(); + } + + /** + * Returns an already existing {@link PlayerData} instance. + * + * @param username player username + * @return {@link PlayerData} instance + * @throws NullPointerException on error + * @since v1-release0 + */ + public PlayerData get(@NotNull String username) throws NullPointerException { + for (PlayerData playerData : playerList) + if (playerData.getPlayer().getName().equals(username)) + return playerData; + + throw new NullPointerException(); + } + + /** + * Returns an already existing {@link PlayerData} instance. + * + * @param player {@link Player} instance + * @return player instance + * @throws NullPointerException on error + * @since v1-release0 + */ + public @NotNull PlayerData get(@NotNull Player player) throws NullPointerException { + for (PlayerData playerData : playerList) + if (playerData.getPlayer().getUniqueId() == player.getUniqueId()) + return playerData; + + throw new NullPointerException(); + } + + /** + * Registers a new {@link PlayerData} instance. + * + * @param player {@link Player} instance + * @throws Exception on error + * @since v1-release0 + */ + public void registerPlayer(@NotNull Player player) throws Exception { + logger.verb("Registering player " + player.getName() + " (" + player.getUniqueId() + ")"); + + try { + playerList.add(new PlayerData(player)); + } catch (Exception exception) { + logger.crash("Unable to create PlayerData instance for player " + player.getName() + " (" + player.getUniqueId() + ")", exception, true); + throw exception; + } + } + + /** + * Unregisters a {@link PlayerData} instance. + * + * @param playerData {@link Player} instance + * @since v1-release0 + */ + public void unregisterPlayer(@NotNull PlayerData playerData) { + logger.verb("Unregistering player " + playerData.getPlayer().getName() + " (" + playerData.getPlayer().getUniqueId() + ")"); + + playerList.remove(playerData); + } +} diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/api/translation/LanguageString.java b/extension/src/main/java/de/jeremystartm/pickshadow/api/translation/LanguageString.java index 4de1780..9c61825 100644 --- a/extension/src/main/java/de/jeremystartm/pickshadow/api/translation/LanguageString.java +++ b/extension/src/main/java/de/jeremystartm/pickshadow/api/translation/LanguageString.java @@ -49,6 +49,9 @@ public enum LanguageString { ERROR_INVALID_ARGUMENT, ERROR_PLAYER_NOT_FOUND, + // Connection errors + CONNECTION_ERROR_REGISTRATION, + // Command /psse EXTENSIONCMD_GREETER, EXTENSIONCMD_KILLJVM, @@ -94,5 +97,5 @@ public enum LanguageString { CHATMESSAGE_SERVER, // Event for chat commands - CHATCOMMAND_ERROR_NAMESPACE + CHATCOMMAND_ERROR_NAMESPACE, } diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/listener/ChatListener.java b/extension/src/main/java/de/jeremystartm/pickshadow/listener/ChatListener.java index 40d85ab..ebb3ba9 100644 --- a/extension/src/main/java/de/jeremystartm/pickshadow/listener/ChatListener.java +++ b/extension/src/main/java/de/jeremystartm/pickshadow/listener/ChatListener.java @@ -39,6 +39,7 @@ public final class ChatListener implements Listener { /** * Handles chat messages. * + * @param event event information * @since v1-release0 */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -55,6 +56,7 @@ public final class ChatListener implements Listener { /** * Handles chat commands. * + * @param event event information * @since v1-release0 */ @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) diff --git a/extension/src/main/java/de/jeremystartm/pickshadow/listener/ConnectionListener.java b/extension/src/main/java/de/jeremystartm/pickshadow/listener/ConnectionListener.java new file mode 100644 index 0000000..5ef2610 --- /dev/null +++ b/extension/src/main/java/de/jeremystartm/pickshadow/listener/ConnectionListener.java @@ -0,0 +1,65 @@ +/* + * PICKSHADOW SERVER KIT SOURCE FILE + * Copyright (c) 2024 The PickShadow Server Kit authors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.jeremystartm.pickshadow.listener; + +import de.jeremystartm.pickshadow.api.entity.player.PlayerDataFactory; +import de.jeremystartm.pickshadow.api.translation.LanguageString; +import de.jeremystartm.pickshadow.api.translation.TranslationManager; +import net.kyori.adventure.text.minimessage.MiniMessage; +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.PlayerQuitEvent; +import org.jetbrains.annotations.NotNull; + +/** + * Listens on joins and disconnects. + * + * @since v1-release0 + */ +public final class ConnectionListener implements Listener { + /** + * Handles player joins. + * + * @param event event information + * @since v1-release0 + */ + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void handlePlayerJoin(@NotNull PlayerJoinEvent event) { + try { + PlayerDataFactory.getInstance().registerPlayer(event.getPlayer()); + } catch (Exception exception) { + event.getPlayer().kick(MiniMessage.miniMessage().deserialize(TranslationManager.get(LanguageString.CONNECTION_ERROR_REGISTRATION, event.getPlayer(), false)), PlayerKickEvent.Cause.UNKNOWN); + } + } + + /** + * Handles player disconnects. + * + * @param event event information + * @since v1-release0 + */ + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void handlePlayerJoin(@NotNull PlayerQuitEvent event) { + PlayerDataFactory.getInstance().unregisterPlayer(PlayerDataFactory.getInstance().get(event.getPlayer())); + } +} diff --git a/extension/src/main/resources/translations/en.json b/extension/src/main/resources/translations/en.json index 2d8b306..a98e982 100644 --- a/extension/src/main/resources/translations/en.json +++ b/extension/src/main/resources/translations/en.json @@ -19,6 +19,8 @@ "ERROR_INVALID_ARGUMENT": "Invalid argument(s) %argument%.", "ERROR_PLAYER_NOT_FOUND": "The player %player% could not be found.", + "CONNECTION_ERROR_REGISTRATION": "Unable to process log in", + "EXTENSIONCMD_GREETER": "This subserver is running <#d60532>PSSE \"%codename%\" @ %version% (%commit%, dirty %dirty%)\nTo view additional information, invoke some subcommands.", "EXTENSIONCMD_KILLJVM": "Bye bye!", "EXTENSIONCMD_LICENSE": "PSSE is licensed under the GNU Affero General Public License v3.",