Skip to content

Commit

Permalink
Add /togglestaffchatsounds command.
Browse files Browse the repository at this point in the history
  • Loading branch information
RezzedUp committed Feb 4, 2024
1 parent 6e50aec commit 4185b19
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.rezzedup</groupId>
<artifactId>discordsrv-staff-chat</artifactId>
<version>1.4.6</version>
<version>1.4.5</version>

<name>DiscordSRV-Staff-Chat</name>
<inceptionYear>2017</inceptionYear>
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/rezzedup/discordsrv/staffchat/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ static class Profile implements StaffChatProfile

static final YamlValue<Instant> LEFT_TOGGLE_DATE = YamlValue.ofInstant("toggles.left").maybe();

static final YamlValue<Boolean> MUTED_SOUNDS_TOGGLE = YamlValue.ofBoolean("toggles.muted-sounds").maybe();

private final StaffChatPlugin plugin;
private final YamlDataFile yaml;
private final UUID uuid;

private @NullOr Instant auto;
private @NullOr Instant left;
private boolean mutedSounds = false;

Profile(StaffChatPlugin plugin, YamlDataFile yaml, UUID uuid)
{
Expand All @@ -153,9 +156,11 @@ static class Profile implements StaffChatProfile

if (plugin.config().getOrDefault(StaffChatConfig.PERSIST_TOGGLES))
{
Sections.get(yaml.data(), path()).ifPresent(section -> {
Sections.get(yaml.data(), path()).ifPresent(section ->
{
auto = AUTO_TOGGLE_DATE.get(section).orElse(null);
left = LEFT_TOGGLE_DATE.get(section).orElse(null);
mutedSounds = MUTED_SOUNDS_TOGGLE.get(section).orElse(false);
});
}
}
Expand Down Expand Up @@ -211,9 +216,21 @@ public void receivesStaffChatMessages(boolean enabled)
updateStoredProfileData();
}

@Override
public boolean receivesStaffChatSounds()
{
return !mutedSounds;
}

@Override
public void receivesStaffChatSounds(boolean enabled)
{
mutedSounds = !enabled;
}

boolean hasDefaultSettings()
{
return auto == null && left == null;
return auto == null && left == null && !mutedSounds;
}

void clearStoredProfileData()
Expand All @@ -238,6 +255,7 @@ void updateStoredProfileData()

AUTO_TOGGLE_DATE.set(section, auto);
LEFT_TOGGLE_DATE.set(section, left);
MUTED_SOUNDS_TOGGLE.set(section, mutedSounds);

yaml.updated(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.rezzedup.discordsrv.staffchat.commands.ManageStaffChatCommand;
import com.rezzedup.discordsrv.staffchat.commands.StaffChatCommand;
import com.rezzedup.discordsrv.staffchat.commands.ToggleStaffChatCommand;
import com.rezzedup.discordsrv.staffchat.commands.ToggleStaffChatSoundsCommand;
import com.rezzedup.discordsrv.staffchat.config.MessagesConfig;
import com.rezzedup.discordsrv.staffchat.config.StaffChatConfig;
import com.rezzedup.discordsrv.staffchat.listeners.DiscordSrvLoadedLaterListener;
Expand Down Expand Up @@ -102,10 +103,12 @@ public void onEnable()
events().register(new PlayerPrefixedMessageListener(this));
events().register(new PlayerStaffChatToggleListener(this));

ToggleStaffChatCommand toggle = new ToggleStaffChatCommand(this);

command("staffchat", new StaffChatCommand(this));
command("managestaffchat", new ManageStaffChatCommand(this));
command("togglestaffchatsounds", new ToggleStaffChatSoundsCommand(this));

ToggleStaffChatCommand toggle = new ToggleStaffChatCommand(this);
command("leavestaffchat", toggle);
command("joinstaffchat", toggle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public interface StaffChatProfile

void receivesStaffChatMessages(boolean enabled);

boolean receivesStaffChatSounds();

void receivesStaffChatSounds(boolean enabled);

default void toggleAutomaticStaffChat()
{
automaticStaffChat(!automaticStaffChat());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License
* Copyright © 2017-2024 RezzedUp and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.rezzedup.discordsrv.staffchat.commands;

import com.rezzedup.discordsrv.staffchat.StaffChatPlugin;
import com.rezzedup.discordsrv.staffchat.StaffChatProfile;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class ToggleStaffChatSoundsCommand implements CommandExecutor
{
private final StaffChatPlugin plugin;

public ToggleStaffChatSoundsCommand(StaffChatPlugin plugin)
{
this.plugin = plugin;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args)
{
if (sender instanceof Player)
{
Player player = (Player) sender;
StaffChatProfile profile = plugin.data().getOrCreateProfile(player);
boolean toggle = !profile.receivesStaffChatSounds();
profile.receivesStaffChatSounds(toggle);

plugin.debug(getClass()).log(() -> String.format(
"Player: %s (%s) has %s receiving staff chat sounds",
player.getName(), profile.uuid(), ((toggle) ? "enabled (unmuted)" : "disabled (muted)")
));

if (toggle) { plugin.messages().notifySoundsUnmuted(player); }
else { plugin.messages().notifySoundsMuted(player); }
}
else
{
sender.sendMessage("Only players may run this command.");
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public class MessagesConfig extends YamlDataFile
YamlValue.ofString("notifications.join.others")
.defaults("%prefix% &2→&a %player% &njoined&a the staff chat");

public static final DefaultYamlValue<String> MUTE_SOUNDS_NOTIFICATION =
YamlValue.ofString("notifications.sounds.muted")
.defaults(
"%prefix% &4→&c You have &nmuted&c staff chat sounds"
);

public static final DefaultYamlValue<String> UNMUTE_SOUNDS_NOTIFICATION =
YamlValue.ofString("notifications.sounds.unmuted")
.defaults(
"%prefix% &2→&a You have &nunmuted&a staff chat sounds"
);

@AggregatedResult
public static final List<YamlValue<?>> VALUES =
Aggregates.fromThisClass().constantsOfType(YamlValue.type()).toList();
Expand Down Expand Up @@ -247,6 +259,16 @@ public void notifyJoinChat(Player joiner, boolean notifyOthers)
sendNotification(joiner, JOIN_CHAT_NOTIFICATION_SELF, others);
}

public void notifySoundsMuted(Player player)
{
sendNotification(player, MUTE_SOUNDS_NOTIFICATION, null);
}

public void notifySoundsUnmuted(Player player)
{
sendNotification(player, UNMUTE_SOUNDS_NOTIFICATION, null);
}

//
// Unconfigurable notifications
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.github.zafarkhaja.semver.Version;
import com.rezzedup.discordsrv.staffchat.StaffChatPlugin;
import com.rezzedup.discordsrv.staffchat.StaffChatProfile;
import com.rezzedup.util.constants.Aggregates;
import com.rezzedup.util.constants.annotations.AggregatedResult;
import community.leaf.configvalues.bukkit.DefaultYamlValue;
Expand Down Expand Up @@ -105,9 +106,12 @@ public class StaffChatConfig extends YamlDataFile
public static final List<YamlValue<?>> VALUES =
Aggregates.fromThisClass().constantsOfType(YamlValue.type()).toList();

private final StaffChatPlugin plugin;

public StaffChatConfig(StaffChatPlugin plugin)
{
super(plugin.directory(), "staff-chat.config.yml", Load.LATER);
this.plugin = plugin;

reloadsWith(() ->
{
Expand All @@ -119,7 +123,7 @@ public StaffChatConfig(StaffChatPlugin plugin)
}

Version existing = get(VERSION).orElse(Configs.NO_VERSION);
boolean isOutdated = existing.lessThan(plugin.version());
boolean isOutdated = existing.isLowerThan(plugin.version());

if (isOutdated)
{
Expand Down Expand Up @@ -148,12 +152,19 @@ private void playSound(
{
if (!getOrDefault(enabled)) { return; }

player.playSound(
player.getLocation().add(0, 0.5, 0),
getOrDefault(sound),
getOrDefault(volume),
getOrDefault(pitch)
);
boolean sounds = plugin.data().getProfile(player)
.map(StaffChatProfile::receivesStaffChatSounds)
.orElse(true);

if (sounds)
{
player.playSound(
player.getLocation().add(0, 0.5, 0),
getOrDefault(sound),
getOrDefault(volume),
getOrDefault(pitch)
);
}
}

public void playMessageSound(Player player)
Expand Down
16 changes: 12 additions & 4 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: ${project.name}
version: ${project.version}

author: RezzedUp
description: Staff chat plugin that hooks into DiscordSRV.
website: https://modrinth.com/plugin/discordsrv-staff-chat

main: com.rezzedup.discordsrv.staffchat.StaffChatPlugin
api-version: 1.13
Expand All @@ -15,6 +18,11 @@ commands:
usage: |-
/<command> - toggle automatic staff chat
/<command> <message> - send a message to the staff chat
managestaffchat:
aliases: [ discordsrv-staff-chat, discordsrvstaffchat, discordstaffchat, discordadminchat, manageadminchat ]
description: Manage and get information about DiscordSRV-Staff-Chat
permission: staffchat.manage
usage: /<command>
leavestaffchat:
aliases: [leaveadminchat]
description: Leave the staff chat (stop receiving messages).
Expand All @@ -25,10 +33,10 @@ commands:
description: Rejoin the staff chat (receive messages again).
permission: staffchat.access
usage: /<command>
managestaffchat:
aliases: [discordsrv-staff-chat, discordsrvstaffchat, discordstaffchat, discordadminchat, manageadminchat]
description: Manage and get information about DiscordSRV-Staff-Chat
permission: staffchat.manage
togglestaffchatsounds:
aliases: [toggleadminchatsounds]
description: Mute or unmute staff chat sounds for yourself.
permission: staffchat.access
usage: /<command>

permissions:
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/staff-chat.config.header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Installation (Discord Setup):

- 1) Install this plugin and DiscordSRV
Get DiscordSRV here: https://www.spigotmc.org/resources/discordsrv.18494/
Get DiscordSRV here: https://modrinth.com/plugin/discordsrv

- 2) Add a "staff-chat" channel to DiscordSRV's config
(in: /plugins/DiscordSRV/config.yml)
Expand All @@ -35,8 +35,8 @@

---

This config can be reloaded in-game with: '/managestaffchat reload'
(Be sure to give yourself permission to manage the staff chat with: 'staffchat.manage')
This config can be reloaded in-game with: `/managestaffchat reload`
(Be sure to give yourself permission to manage the staff chat with: `staffchat.manage`)

---

Expand Down

0 comments on commit 4185b19

Please sign in to comment.