Skip to content

Commit

Permalink
Cleaned up diff
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjack committed Nov 12, 2021
1 parent dea9c43 commit 1a55535
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]", ""));

}

Expand Down Expand Up @@ -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));
}

}
Expand Down Expand Up @@ -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"));
}
}
}
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;
}
}
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,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());
}
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;
}
}
}

0 comments on commit 1a55535

Please sign in to comment.