Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented sounds for channels and toggling #34

Merged
merged 8 commits into from
Nov 18, 2021
Merged
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
5 changes: 5 additions & 0 deletions resources/Channels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]", ""));

}

Expand Down Expand Up @@ -106,6 +108,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));
}

}
Expand Down Expand Up @@ -388,4 +392,27 @@ 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]);
LlmDl marked this conversation as resolved.
Show resolved Hide resolved

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"));
}
}
}
85 changes: 83 additions & 2 deletions src/com/palmergames/bukkit/TownyChat/channels/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class Channel {
private String name;
private List<String> 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -349,4 +369,65 @@ private boolean playerIgnoringThisChannel(Player player) {
}
return false;
}
}

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;
}
}
16 changes: 16 additions & 0 deletions src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,6 +162,21 @@ public void chatProcess(AsyncPlayerChatEvent event) {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -302,4 +305,4 @@ public boolean loadConfig(String filepath, String defaultRes) {
}
return false;
}
}
}