Skip to content

Commit d29225e

Browse files
committed
Feat:Implmented notification sound for incoming pm msgs
1 parent 17a6248 commit d29225e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/main/java/pro/cloudnode/smp/cloudnodemsg/Message.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.command.CommandSender;
88
import org.bukkit.entity.Player;
99
import org.bukkit.persistence.PersistentDataType;
10+
import org.bukkit.Sound;
1011
import org.jetbrains.annotations.NotNull;
1112
import org.jetbrains.annotations.Nullable;
1213
import pro.cloudnode.smp.cloudnodemsg.error.ChannelOfflineError;
@@ -85,6 +86,34 @@ public void send(final @NotNull Context context) throws InvalidPlayerError {
8586
sendMessage(recipient, CloudnodeMSG.getInstance().config()
8687
.incoming(senderUsername, recipientUsername, message));
8788

89+
// Play PM notification sound for the recipient (if online and enabled in config)
90+
recipientPlayer.ifPresent(recPlayer -> {
91+
try {
92+
// Don't play sound if recipient is the same as sender (e.g., self-message)
93+
if (sender.getUniqueId().equals(recipient.getUniqueId())) return;
94+
95+
if (!CloudnodeMSG.getInstance().getConfig().getBoolean("pm-sound.enabled", true)) return;
96+
97+
final String soundName = CloudnodeMSG.getInstance().getConfig().getString("pm-sound.sound", "BLOCK_NOTE_BLOCK_PLING");
98+
final float volume = (float) CloudnodeMSG.getInstance().getConfig().getDouble("pm-sound.volume", 1.0);
99+
final float pitch = (float) CloudnodeMSG.getInstance().getConfig().getDouble("pm-sound.pitch", 1.0);
100+
101+
try {
102+
final Sound sound = Sound.valueOf(soundName);
103+
recPlayer.playSound(recPlayer.getLocation(), sound, volume, pitch);
104+
} catch (IllegalArgumentException | NoSuchFieldError | NullPointerException e) {
105+
// fallback to a safe default if configured sound isn't available on this server version
106+
try {
107+
recPlayer.playSound(recPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, volume, pitch);
108+
} catch (Throwable ignored) {
109+
// silently ignore if no sound is available
110+
}
111+
}
112+
} catch (Throwable ignored) {
113+
// Ensure message sending never fails because of sound errors
114+
}
115+
});
116+
88117
if (sender.getUniqueId().equals(console.getUniqueId()) || (senderPlayer.isPresent() && !Message.hasChannel(senderPlayer.get(), recipient)))
89118
setReplyTo(sender, recipient);
90119
if (recipient.getUniqueId().equals(console.getUniqueId()) || (recipientPlayer.isPresent() && !Message.hasChannel(recipientPlayer.get(), sender)))
@@ -331,4 +360,4 @@ public static enum Context {
331360
*/
332361
REPLY;
333362
}
334-
}
363+
}

src/main/resources/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,15 @@ errors:
152152

153153
# Trying to message a team, but not in one
154154
not-in-team: "<red>(!) You are not in a team.</red>"
155+
156+
157+
pm-sound:
158+
# Enable or disable playing a sound when a private message is received
159+
enabled: true
160+
# Sound to play on private message (Minecraft sound identifier)
161+
# Examples: ENTITY_ALLAY_AMBIENT_WITH_ITEM, ENTITY_PLAYER_LEVELUP, etc.
162+
sound: BLOCK_NOTE_BLOCK_PLINGt
163+
# Volume of the sound (float). 1.0 is normal volume, increase to be louder.
164+
volume: 1.0
165+
# Pitch of the sound (float). 1.0 is normal pitch, lower for deeper, higher for sharper.
166+
pitch: 1.0

0 commit comments

Comments
 (0)