From 90cddc894c9a4a1e42e16ef8334fda6b43ad56c6 Mon Sep 17 00:00:00 2001 From: Glare Date: Thu, 18 Nov 2021 14:28:30 -0600 Subject: [PATCH] Implemented sounds for channels and toggling (#34) * Cleaned up diff * Documentation on how to add a sound to a channel * Added missing null check before trying to play a sound * Warn when invalid sound is being used * Set channel sound to null if invalid * Only run if channel sound not null * Implemented suggested changes * One more change --- resources/Channels.yml | 5 ++ .../TownyChat/Command/ChannelCommand.java | 27 ++++++ .../bukkit/TownyChat/channels/Channel.java | 85 ++++++++++++++++++- .../TownyChat/channels/StandardChannel.java | 16 ++++ .../config/ConfigurationHandler.java | 5 +- 5 files changed, 135 insertions(+), 3 deletions(-) diff --git a/resources/Channels.yml b/resources/Channels.yml index 1651389..d50f559 100644 --- a/resources/Channels.yml +++ b/resources/Channels.yml @@ -31,6 +31,11 @@ # DarkPurple = &9, LightGreen = &a, LightBlue = &b # Rose = &c, LightPurple = &d, Yellow = &e, White = &f +# Sounds +# ------ +# Each channel can have a sound by adding sound: SOUND_TO_USE +# Sounds can be found here: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html + Channels: general: commands: [g] diff --git a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java index b6b586d..a118ea3 100644 --- a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java +++ b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java @@ -7,6 +7,7 @@ import com.palmergames.bukkit.TownyChat.events.PlayerJoinChatChannelEvent; import com.palmergames.bukkit.TownyChat.util.TownyUtil; import com.palmergames.bukkit.towny.TownyMessaging; import com.palmergames.bukkit.towny.command.BaseCommand; +import com.palmergames.bukkit.towny.object.Translatable; import com.palmergames.bukkit.towny.object.Translation; import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.util.ChatTools; @@ -39,6 +40,7 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor { channel_help.add(ChatTools.formatCommand("", "/channel", "mute [Channel] [Player]", "")); channel_help.add(ChatTools.formatCommand("", "/channel", "unmute [Channel] [Player]", "")); channel_help.add(ChatTools.formatCommand("", "/channel", "mutelist [Channel]", "")); + channel_help.add(ChatTools.formatCommand("", "/channel", "soundtoggle [Channel]", "")); } @@ -106,6 +108,8 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor { parseChannelMuteList(player, StringMgmt.remFirstArg(split)); } else if (split[0].equalsIgnoreCase("list")) { parseChannelList(player); + } else if (split[0].equalsIgnoreCase("soundtoggle")) { + parseChannelSoundToggle(player, StringMgmt.remFirstArg(split)); } } @@ -388,4 +392,27 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor { TownyMessaging.sendMessage(player, Translation.of("tc_you_joined_channel", chan.getName())); } + + public static void parseChannelSoundToggle(Player player, String[] split) { + if (split.length == 0) { + for (String line : channel_help) { + player.sendMessage(line); + } + return; + } + Channel chan = plugin.getChannelsHandler().getChannel(split[0]); + + if (chan == null) { + TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_no_channel_called_channel", split[0])); + return; + } + + if (chan.isSoundMuted(player)) { + chan.unmuteSound(player); + TownyMessaging.sendMessage(player, Translatable.of("tc_player_channel_sound_unmuted")); + } else { + chan.muteSound(player); + TownyMessaging.sendMessage(player, Translatable.of("tc_player_channel_sound_muted")); + } + } } diff --git a/src/com/palmergames/bukkit/TownyChat/channels/Channel.java b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java index 85f33fc..d5f69d4 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/Channel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java @@ -21,7 +21,7 @@ public abstract class Channel { private String name; private List commands; private channelTypes type; - private String channelTag, messageColour, permission, leavePermission; + private String channelTag, messageColour, permission, leavePermission, channelSound; private double range; private boolean hooked=false; private boolean autojoin=true; @@ -209,6 +209,18 @@ public abstract class Channel { mutedPlayers.remove(name); return true; } + + public boolean isSoundMuted(Player player) { + return playerIgnoringSoundMeta(player); + } + + public void muteSound(Player player) { + playerAddSoundIgnoreMeta(player); + } + + public void unmuteSound(Player player) { + playerRemoveSoundIgnoreMeta(player); + } /* * Get name of permissions node to leave a the channel @@ -256,6 +268,14 @@ public abstract class Channel { public void setSpam_time(double spamtime) { this.spamtime = spamtime; } + + public String getChannelSound() { + return channelSound; + } + + public void setChannelSound(String channelSound) { + this.channelSound = channelSound; + } /** * Test if this player is spamming chat. @@ -349,4 +369,65 @@ public abstract class Channel { } return false; } -} \ No newline at end of file + + private void playerAddSoundIgnoreMeta(Player player) { + StringDataField icdf = new StringDataField("townychat_soundOffChannels", "", "TownyChat Channels with Sound Toggle Off"); + Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); + if (resident == null) + return; + + if (resident.hasMeta(icdf.getKey())) { + CustomDataField cdf = resident.getMetadata(icdf.getKey()); + if (cdf instanceof StringDataField) { + StringDataField sdf = (StringDataField) cdf; + sdf.setValue(sdf.getValue().concat("\uFF0C " + this.getName())); + TownyUniverse.getInstance().getDataSource().saveResident(resident); + } + } else { + resident.addMetaData(new StringDataField("townychat_soundOffChannels", this.getName(), "TownyChat Channels with Sound Toggle Off")); + } + } + + private void playerRemoveSoundIgnoreMeta(Player player) { + StringDataField icdf = new StringDataField("townychat_soundOffChannels", "", "TownyChat Channels with Sound Toggle Off"); + Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); + if (resident == null || !resident.hasMeta(icdf.getKey())) + return; + + CustomDataField cdf = resident.getMetadata(icdf.getKey()); + if (cdf instanceof StringDataField) { + StringDataField sdf = (StringDataField) cdf; + String newValues = ""; + String[] values = sdf.getValue().split("\uFF0C "); + for (String chanName : values) + if (!chanName.equalsIgnoreCase(this.getName())) + if (newValues.isEmpty()) + newValues = chanName; + else + newValues += "\uFF0C " + chanName; + + if (!newValues.isEmpty()) { + sdf.setValue(newValues); + TownyUniverse.getInstance().getDataSource().saveResident(resident); + } else { + resident.removeMetaData(icdf); + } + } + } + + private boolean playerIgnoringSoundMeta(Player player) { + StringDataField idf = new StringDataField("townychat_soundOffChannels", "", "TownyChat Channels with Sound Toggle Off"); + Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); + if (resident != null && resident.hasMeta(idf.getKey())) { + CustomDataField cdf = resident.getMetadata(idf.getKey()); + if (cdf instanceof StringDataField) { + StringDataField sdf = (StringDataField) cdf; + String[] split = sdf.getValue().split("\uFF0C "); + for (String string : split) + if (string.equalsIgnoreCase(this.getName())) + return true; + } + } + return false; + } +} diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index 06f32cd..c8200d2 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -22,6 +22,7 @@ import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.dynmap.DynmapAPI; @@ -161,6 +162,21 @@ public class StandardChannel extends Channel { sendSpyMessage(event, channelType); } + if (getChannelSound() != null) { + for (Player recipient : event.getRecipients()) { + if (!isSoundMuted(player)) { + try { + recipient.playSound(player.getLocation(), Sound.valueOf(getChannelSound()), 1.0f, 1.0f); + } catch (IllegalArgumentException ex) { + plugin.getLogger().warning("Channel " + this.getName() + " has an invalid sound configured."); + setChannelSound(null); + break; + } + } + } + } + + if (notifyjoin) { TownyMessaging.sendMessage(player, "You join " + Colors.White + getName()); } diff --git a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java index 171827d..55f0dc9 100644 --- a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java +++ b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java @@ -137,6 +137,9 @@ public class ConfigurationHandler { if (key.equalsIgnoreCase("range")) channel.setRange(Double.valueOf(element.toString())); + + if (key.equalsIgnoreCase("sound")) + channel.setChannelSound(String.valueOf(element)); } // If no leave permission is set, create a default permission name @@ -302,4 +305,4 @@ public class ConfigurationHandler { } return false; } -} \ No newline at end of file +}