Skip to content

Commit

Permalink
Add support for using displayname in connection message
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Sep 21, 2024
1 parent e054f45 commit 3fb49e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/java/cn/dreeam/surf/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Config {
public static String limitOffhandSwapMessage;

// Misc / Connection / Nether
public static boolean connectionMessageEnabled, connectionFirstJoinEnabled, connectionPreventKickEnabled, netherEnabled, netherTopBottomDoDamage;
public static boolean connectionMessageEnabled, connectionMessageUseDisplayName, connectionFirstJoinEnabled, connectionPreventKickEnabled, netherEnabled, netherTopBottomDoDamage;
public static int netherTopLayer, netherBottomLayer;
public static String connectionPlayerJoin, connectionPlayerLeave, connectionFirstJoinMessage, netherTopMessage, netherBottomMessage;
public static List<String> connectionKickReasons;
Expand Down Expand Up @@ -94,6 +94,8 @@ public static void initConfig() {
connectionMessageEnabled = Surf.configManager().getBoolean("connection-meesage.enabled", false, """
These are the connection messages for when a player joins / leaves
Use & for colours and %player% as a placeholder for the players name""");
connectionMessageUseDisplayName = Surf.configManager().getBoolean("connection-meesage.use-display-name", true, """
Whether use display name in connection messages.""");

connectionPlayerJoin = Surf.configManager().getString("connection-meesage.player-join", "&7[&a+&7] &8%player%");
connectionPlayerLeave = Surf.configManager().getString("connection-meesage.player-leave", "&7[&c-&7] &8%player%");
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/cn/dreeam/surf/modules/misc/ConnectionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.dreeam.surf.config.Config;
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.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -50,8 +51,16 @@ public void onKick(PlayerKickEvent event) {
}

private Component getConnectionMessage(Player player, String message) {
message = message.replace("%player%", player.getName());
Component connecntionComponent;

return LegacyComponentSerializer.legacyAmpersand().deserialize(message);
if (Config.connectionMessageUseDisplayName && !player.displayName().equals(Component.empty())) {
connecntionComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(message)
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(player.displayName()).build());
} else {
message = message.replace("%player%", player.getName());
connecntionComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(message);
}

return connecntionComponent;
}
}

0 comments on commit 3fb49e9

Please sign in to comment.