Add join and leave messages

This commit is contained in:
JeremyStar™ 2024-10-09 10:50:04 +02:00
parent 13fcf0cbc3
commit 595cf46e27
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 58 additions and 2 deletions

View file

@ -51,6 +51,9 @@ public enum LanguageString {
ERROR_PLAYER_NOT_FOUND,
// Connection
CONNECTION_JOIN,
CONNECTION_JOIN_WELCOME,
CONNECTION_DISCONNECT,
CONNECTION_ERROR_REGISTRATION,
CONNECTION_ERROR_TABLISTHANDLER,

View file

@ -23,11 +23,13 @@ import de.jeremystartm.pickshadow.ExtensionInformation;
import de.jeremystartm.pickshadow.api.command.CommandBase;
import de.jeremystartm.pickshadow.api.command.completion.StaticTabCompletion;
import de.jeremystartm.pickshadow.api.command.TabCompletion;
import de.jeremystartm.pickshadow.api.entity.player.PlayerDataFactory;
import de.jeremystartm.pickshadow.api.translation.LanguageString;
import de.jeremystartm.pickshadow.api.translation.TranslationManager;
import lombok.Getter;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
@ -54,6 +56,7 @@ public final class ExtensionCommand extends CommandBase {
.add("psse", 0, "license", "pickshadow.command.extension")
.add("psse", 0, "source", "pickshadow.command.extension")
.add("psse", 0, "killjvm", "pickshadow.command.extension.advanced")
.add("psse", 0, "debug", "pickshadow.command.extension.advanced")
.copy("psse", "pickshadow")
.copy("psse", "server");
@ -94,6 +97,14 @@ public final class ExtensionCommand extends CommandBase {
sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_KILLJVM, sender, true));
Runtime.getRuntime().halt(0);
}
case "debug" -> {
if (checkPermission(sender, "pickshadow.command.extension.advanced")) return;
if (sender instanceof Player player)
sender.sendMessage(PlayerDataFactory.getInstance().get(player).toJSON());
else
sender.sendRichMessage(TranslationManager.get(LanguageString.ERROR_NOT_A_PLAYER, sender, true));
}
default -> sender.sendRichMessage(TranslationManager.get(LanguageString.ERROR_INVALID_ARGUMENT, sender, true, entry("argument", arguments[0])));
}
if (arguments[0].equals("killjvm"))

View file

@ -19,10 +19,14 @@
package de.jeremystartm.pickshadow.listener;
import de.jeremystartm.pickshadow.Extension;
import de.jeremystartm.pickshadow.api.entity.player.PlayerDataFactory;
import de.jeremystartm.pickshadow.api.translation.LanguageString;
import de.jeremystartm.pickshadow.api.translation.TranslationManager;
import de.jeremystartm.pickshadow.command.general.ClearChatCommand;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -31,6 +35,8 @@ import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/**
* Listens on joins and disconnects.
*
@ -38,19 +44,46 @@ import org.jetbrains.annotations.NotNull;
*/
public final class ConnectionListener implements Listener {
/**
* Handles player joins.
* Handles player joins early.
*
* @param event event information
* @since v1-release0
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void handlePlayerJoin(@NotNull PlayerJoinEvent event) {
private void handlePlayerJoinEarly(@NotNull PlayerJoinEvent event) {
try {
// Register player
PlayerDataFactory.getInstance().registerPlayer(event.getPlayer());
// Set join message
event.joinMessage(MiniMessage.miniMessage().deserialize(TranslationManager.get(
LanguageString.CONNECTION_JOIN,
event.getPlayer(),
false,
entry("player", event.getPlayer().getName())
)));
// Schedule late join event
event.getPlayer().getScheduler().execute(Extension.getInstance(), () -> handlePlayerJoinLate(event.getPlayer()), null, 1);
} catch (Exception exception) {
event.getPlayer().kick(MiniMessage.miniMessage().deserialize(TranslationManager.get(LanguageString.CONNECTION_ERROR_REGISTRATION, event.getPlayer(), false)), PlayerKickEvent.Cause.UNKNOWN);
}
}
/**
* Handles player joins late.
*
* @param player {@link Player} which joined
* @since v1-release0
*/
private void handlePlayerJoinLate(@NotNull Player player) {
// Clear chat and display welcome message
player.sendRichMessage(ClearChatCommand.NEWLINES + "\n" + TranslationManager.get(
LanguageString.CONNECTION_JOIN_WELCOME,
player, false,
entry("player", player.getName()),
entry("minecraftVersion", Bukkit.getMinecraftVersion())
));
}
/**
* Handles player disconnects.
@ -60,6 +93,15 @@ public final class ConnectionListener implements Listener {
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void handlePlayerJoin(@NotNull PlayerQuitEvent event) {
// Unregister player
PlayerDataFactory.getInstance().unregisterPlayer(PlayerDataFactory.getInstance().get(event.getPlayer()));
// Set leave message
event.quitMessage(MiniMessage.miniMessage().deserialize(TranslationManager.get(
LanguageString.CONNECTION_DISCONNECT,
event.getPlayer(),
false,
entry("player", event.getPlayer().getName())
)));
}
}