Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/main/java/pro/cloudnode/smp/cloudnodemsg/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.cloudnodemsg.error.ChannelOfflineError;
Expand Down Expand Up @@ -85,6 +86,34 @@ public void send(final @NotNull Context context) throws InvalidPlayerError {
sendMessage(recipient, CloudnodeMSG.getInstance().config()
.incoming(senderUsername, recipientUsername, message));

// Play PM notification sound for the recipient (if online and enabled in config)
recipientPlayer.ifPresent(recPlayer -> {
try {
// Don't play sound if recipient is the same as sender (e.g., self-message)
if (sender.getUniqueId().equals(recipient.getUniqueId())) return;

if (!CloudnodeMSG.getInstance().getConfig().getBoolean("pm-sound.enabled", true)) return;

final String soundName = CloudnodeMSG.getInstance().getConfig().getString("pm-sound.sound", "BLOCK_NOTE_BLOCK_PLING");
final float volume = (float) CloudnodeMSG.getInstance().getConfig().getDouble("pm-sound.volume", 1.0);
final float pitch = (float) CloudnodeMSG.getInstance().getConfig().getDouble("pm-sound.pitch", 1.0);
Comment on lines +95 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the pro.cloudnode.smp.cloudnodemsg.PluginConfig class for accessing the config

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure, my bad didn't noticed it


try {
final Sound sound = Sound.valueOf(soundName);
recPlayer.playSound(recPlayer.getLocation(), sound, volume, pitch);
} catch (IllegalArgumentException | NoSuchFieldError | NullPointerException e) {
// fallback to a safe default if configured sound isn't available on this server version
try {
recPlayer.playSound(recPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, volume, pitch);
} catch (Throwable ignored) {
// silently ignore if no sound is available
}
}
Comment on lines +104 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to log and do nothing, so e.g. something like

Suggested change
} catch (IllegalArgumentException | NoSuchFieldError | NullPointerException e) {
// fallback to a safe default if configured sound isn't available on this server version
try {
recPlayer.playSound(recPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, volume, pitch);
} catch (Throwable ignored) {
// silently ignore if no sound is available
}
}
} catch (Throwable e) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Failed to play sound", e);
}

} catch (Throwable ignored) {
// Ensure message sending never fails because of sound errors
}
Comment on lines +112 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/catch should not be needed as it would only cover the config access

});

if (sender.getUniqueId().equals(console.getUniqueId()) || (senderPlayer.isPresent() && !Message.hasChannel(senderPlayer.get(), recipient)))
setReplyTo(sender, recipient);
if (recipient.getUniqueId().equals(console.getUniqueId()) || (recipientPlayer.isPresent() && !Message.hasChannel(recipientPlayer.get(), sender)))
Expand Down Expand Up @@ -331,4 +360,4 @@ public static enum Context {
*/
REPLY;
}
}
}
Comment on lines -334 to +363
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty line at the end of the file should not be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry but, I don't see any empty lines there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 335 should be an empty line, you can just add it manually.

If you’re using IntelliJ, it’s possible to enable this setting:
image

12 changes: 12 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,15 @@ errors:

# Trying to message a team, but not in one
not-in-team: "<red>(!) You are not in a team.</red>"


pm-sound:
Comment on lines +155 to +157
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should also be a way to do this for team messages, and make the config like.

sound:
  private:
    #
  team:
    #

Also I think it would be best to move this above the errors section so it’s not buried by all the errors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, sounds reasonable, my personal intention was just to notify players about pm notification only since team message can be more spammy sometimes, but still yeah config might be a better choice for server owners to pick

# Enable or disable playing a sound when a private message is received
enabled: true
# Sound to play on private message (Minecraft sound identifier)
# Examples: ENTITY_ALLAY_AMBIENT_WITH_ITEM, ENTITY_PLAYER_LEVELUP, etc.
sound: BLOCK_NOTE_BLOCK_PLINGt
# Volume of the sound (float). 1.0 is normal volume, increase to be louder.
volume: 1.0
# Pitch of the sound (float). 1.0 is normal pitch, lower for deeper, higher for sharper.
pitch: 1.0