From 1a5553577674f0ff3c79b8138e672e90a9863efd Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:23:08 -0600 Subject: [PATCH 1/8] Cleaned up diff --- .../TownyChat/Command/ChannelCommand.java | 21 +++++ .../bukkit/TownyChat/channels/Channel.java | 85 ++++++++++++++++++- .../TownyChat/channels/StandardChannel.java | 7 ++ .../config/ConfigurationHandler.java | 5 +- 4 files changed, 115 insertions(+), 3 deletions(-) diff --git a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java index b6b586d..85f25f5 100644 --- a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java +++ b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java @@ -39,6 +39,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 +107,8 @@ private void parseChannelCommand(Player player, String[] split) { 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 +391,22 @@ public static void parseChannelJoin(Player player, String[] split) { 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.isSoundMuted(player)) { + chan.unmuteSound(player); + TownyMessaging.sendMessage(player, Translation.of("tc_player_channel_sound_unmuted")); + } else { + chan.muteSound(player); + TownyMessaging.sendMessage(player, Translation.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 boolean unmute(String name) { 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 double getSpam_time() { 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 @@ private boolean playerIgnoringThisChannel(Player player) { } 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..335520e 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -22,6 +22,7 @@ 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,12 @@ public void chatProcess(AsyncPlayerChatEvent event) { sendSpyMessage(event, channelType); } + for (Player recipient : event.getRecipients()) { + if (!isSoundMuted(player)) { + recipient.playSound(player.getLocation(), Sound.valueOf(getChannelSound()), 1.0f, 1.0f); + } + } + 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 @@ else if (element instanceof String) 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 boolean loadConfig(String filepath, String defaultRes) { } return false; } -} \ No newline at end of file +} From 694d763caf6cb2fd9d782f53b42793141cf28b45 Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:29:24 -0600 Subject: [PATCH 2/8] Documentation on how to add a sound to a channel --- resources/Channels.yml | 5 +++++ 1 file changed, 5 insertions(+) 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] From 83baa6dae1a49d45d813c8b67a8a6904579391de Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:31:12 -0600 Subject: [PATCH 3/8] Added missing null check before trying to play a sound --- .../palmergames/bukkit/TownyChat/channels/StandardChannel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index 335520e..27a0728 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -163,7 +163,7 @@ public void chatProcess(AsyncPlayerChatEvent event) { } for (Player recipient : event.getRecipients()) { - if (!isSoundMuted(player)) { + if (getChannelSound() != null && !isSoundMuted(player)) { recipient.playSound(player.getLocation(), Sound.valueOf(getChannelSound()), 1.0f, 1.0f); } } From 5e21e683c60a7d275a32f12006e0059cdd598173 Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:44:34 -0600 Subject: [PATCH 4/8] Warn when invalid sound is being used --- .../bukkit/TownyChat/channels/StandardChannel.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index 27a0728..e627aaf 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -163,8 +163,14 @@ public void chatProcess(AsyncPlayerChatEvent event) { } for (Player recipient : event.getRecipients()) { - if (getChannelSound() != null && !isSoundMuted(player)) { - recipient.playSound(player.getLocation(), Sound.valueOf(getChannelSound()), 1.0f, 1.0f); + if (getChannelSound() == null) break; + 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."); + break; + } } } From a992cf0a73459d62d109c20cbe7cbdc2af9723bb Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:45:17 -0600 Subject: [PATCH 5/8] Set channel sound to null if invalid --- .../palmergames/bukkit/TownyChat/channels/StandardChannel.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index e627aaf..36cdbe5 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -169,6 +169,7 @@ public void chatProcess(AsyncPlayerChatEvent event) { 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; } } From 34696e6f5578103a59cd307f19d7933242747afd Mon Sep 17 00:00:00 2001 From: darbyjack Date: Fri, 12 Nov 2021 14:49:49 -0600 Subject: [PATCH 6/8] Only run if channel sound not null --- .../TownyChat/channels/StandardChannel.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index 36cdbe5..c8200d2 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -162,18 +162,20 @@ public void chatProcess(AsyncPlayerChatEvent event) { sendSpyMessage(event, channelType); } - for (Player recipient : event.getRecipients()) { - if (getChannelSound() == null) break; - 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 (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()); From 5a5f20a2738e5737856cfaf2abb18d83be8d6615 Mon Sep 17 00:00:00 2001 From: darbyjack Date: Mon, 15 Nov 2021 13:05:24 -0600 Subject: [PATCH 7/8] Implemented suggested changes --- .../bukkit/TownyChat/Command/ChannelCommand.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java index 85f25f5..19d523c 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.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; @@ -401,12 +402,17 @@ public static void parseChannelSoundToggle(Player player, String[] split) { } Channel chan = plugin.getChannelsHandler().getChannel(split[0]); + if (chan == null) { + TownyMessaging.sendErrorMsg(player, Translation.of("tc_err_no_channel_called_channel", split[0])); + return; + } + if (chan.isSoundMuted(player)) { chan.unmuteSound(player); - TownyMessaging.sendMessage(player, Translation.of("tc_player_channel_sound_unmuted")); + TownyMessaging.sendMessage(player, Translatable.of("tc_player_channel_sound_unmuted")); } else { chan.muteSound(player); - TownyMessaging.sendMessage(player, Translation.of("tc_player_channel_sound_muted")); + TownyMessaging.sendMessage(player, Translatable.of("tc_player_channel_sound_muted")); } } } From c23e3b568304b670c40f250a1a71670dcaa61bc9 Mon Sep 17 00:00:00 2001 From: darbyjack Date: Mon, 15 Nov 2021 13:09:46 -0600 Subject: [PATCH 8/8] One more change --- .../palmergames/bukkit/TownyChat/Command/ChannelCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java index 19d523c..a118ea3 100644 --- a/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java +++ b/src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java @@ -403,7 +403,7 @@ public static void parseChannelSoundToggle(Player player, String[] split) { Channel chan = plugin.getChannelsHandler().getChannel(split[0]); if (chan == null) { - TownyMessaging.sendErrorMsg(player, Translation.of("tc_err_no_channel_called_channel", split[0])); + TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_no_channel_called_channel", split[0])); return; }