Skip to content

Commit

Permalink
Improve compatibility of connection message
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Sep 21, 2024
1 parent 3afa3cb commit 63a43c1
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions src/main/java/cn/dreeam/surf/modules/misc/ConnectionEvent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cn.dreeam.surf.modules.misc;

import cn.dreeam.surf.Surf;
import cn.dreeam.surf.config.Config;
import cn.dreeam.surf.util.MessageUtil;
import cn.dreeam.surf.util.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -20,22 +22,37 @@ public void onJoin(PlayerJoinEvent event) {
if (!Config.connectionMessageEnabled) return;

Player player = event.getPlayer();
Component message = Config.connectionFirstJoinEnabled && !player.hasPlayedBefore()
? getConnectionMessage(player, Config.connectionFirstJoinMessage)
: getConnectionMessage(player, Config.connectionPlayerJoin);

event.setJoinMessage(null);
Surf.getInstance().adventure().all().sendMessage(message);
if (Util.isNewerAndEqual(16, 0)) {
Component message = Config.connectionFirstJoinEnabled && !player.hasPlayedBefore()
? getConnectionMessageModern(player, Config.connectionFirstJoinMessage)
: getConnectionMessageModern(player, Config.connectionPlayerJoin);

event.setJoinMessage(null);
broadcastConnectionMessage(message);
} else {
String message = Config.connectionFirstJoinEnabled && !player.hasPlayedBefore()
? getConnectionMessageLegacy(player, Config.connectionFirstJoinMessage)
: getConnectionMessageLegacy(player, Config.connectionPlayerJoin);

event.setJoinMessage(message);
}
}

@EventHandler
public void onLeave(PlayerQuitEvent event) {
if (!Config.connectionMessageEnabled) return;

Component message = getConnectionMessage(event.getPlayer(), Config.connectionPlayerLeave);
if (Util.isNewerAndEqual(16, 0)) {
Component message = getConnectionMessageModern(event.getPlayer(), Config.connectionPlayerLeave);

event.setQuitMessage(null);
Surf.getInstance().adventure().all().sendMessage(message);
event.setQuitMessage(null);
broadcastConnectionMessage(message);
} else {
String message = getConnectionMessageLegacy(event.getPlayer(), Config.connectionPlayerLeave);

event.setQuitMessage(message);
}
}

@EventHandler
Expand All @@ -46,11 +63,11 @@ public void onKick(PlayerKickEvent event) {

if (Config.connectionKickReasons.contains(reason)) {
event.setCancelled(true);
Util.println("Cancelled a kick for " + event.getPlayer().getName() + ", Reason: " + reason);
MessageUtil.println("Cancelled a kick for " + event.getPlayer().getName() + ", Reason: " + reason);
}
}

private Component getConnectionMessage(Player player, String message) {
private Component getConnectionMessageModern(Player player, String message) {
Component connecntionComponent;

if (Config.connectionMessageUseDisplayName && !player.displayName().equals(Component.empty())) {
Expand All @@ -63,4 +80,25 @@ private Component getConnectionMessage(Player player, String message) {

return connecntionComponent;
}
}

private String getConnectionMessageLegacy(Player player, String message) {
String displayName = player.getDisplayName();
message = ChatColor.translateAlternateColorCodes('&', message);

if (Config.connectionMessageUseDisplayName && !displayName.isEmpty()) {
message = message.replace("%player%", displayName);
} else {
message = message.replace("%player%", player.getName());
}

return message;
}

private static void broadcastConnectionMessage(Component message) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player == null) continue;

player.sendMessage(message);
}
}
}

0 comments on commit 63a43c1

Please sign in to comment.