Add PlayerData & ConnectionListener classes
This commit is contained in:
parent
d86f859b3a
commit
2eddb833d4
7 changed files with 325 additions and 2 deletions
|
@ -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) {
|
||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
|
@ -19,6 +19,8 @@
|
|||
"ERROR_INVALID_ARGUMENT": "<error>Invalid argument(s) <mention>%argument%</mention>.</error>",
|
||||
"ERROR_PLAYER_NOT_FOUND": "<error>The player <mention>%player%</mention> could not be found.</error>",
|
||||
|
||||
"CONNECTION_ERROR_REGISTRATION": "<error>Unable to process log in",
|
||||
|
||||
"EXTENSIONCMD_GREETER": "<generic>This subserver is running <#d60532><bold>PSSE</bold> \"<italic>%codename%</italic>\" <italic>@ %version% (%commit%, dirty %dirty%)</italic></generic>\n<generic>To view additional information, invoke some subcommands.",
|
||||
"EXTENSIONCMD_KILLJVM": "<generic>Bye bye!",
|
||||
"EXTENSIONCMD_LICENSE": "<generic>PSSE is licensed under the <mention>GNU Affero General Public License v3</mention>.",
|
||||
|
|
Loading…
Reference in a new issue