Skip to content

Commit 5198e2c

Browse files
committed
Fix commands, add configurable sounds for messages
1 parent 4894894 commit 5198e2c

13 files changed

Lines changed: 199 additions & 38 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>simplexity</groupId>
88
<artifactId>SimplePMs</artifactId>
9-
<version>2.2.0</version>
9+
<version>2.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimplePMs</name>

src/main/java/simplexity/simplepms/SimplePMs.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class SimplePMs extends JavaPlugin {
3434
public static HashSet<Player> getPlayers() {
3535
return players;
3636
}
37+
3738
public static Set<Player> getSpyingPlayers() {
3839
return spyingPlayers;
3940
}

src/main/java/simplexity/simplepms/commands/Block.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import org.jetbrains.annotations.NotNull;
99
import simplexity.simplepms.config.Message;
1010
import simplexity.simplepms.logic.Util;
11-
import simplexity.simplepms.saving.SqlHandler;
11+
import simplexity.simplepms.objects.PlayerBlock;
12+
import simplexity.simplepms.saving.Cache;
1213

1314
import java.util.Arrays;
14-
import java.util.UUID;
1515

1616
public class Block implements CommandExecutor {
1717
@Override
@@ -31,10 +31,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3131
Placeholder.parsed("name", playerToBlockString));
3232
return false;
3333
}
34-
UUID uuid = player.getUniqueId();
35-
UUID blockPlayerUUID = playerToBlock.getUniqueId();
36-
String reason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
37-
SqlHandler.getInstance().addBlockedPlayer(uuid, blockPlayerUUID, reason);
34+
String blockReason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
35+
PlayerBlock playerBlock = new PlayerBlock(
36+
player.getUniqueId(),
37+
playerToBlock.getName(),
38+
playerToBlock.getUniqueId(),
39+
blockReason);
40+
Cache.addBlockedUser(player.getUniqueId(), playerBlock);
3841
player.sendRichMessage(Message.BLOCKED_PLAYER.getMessage(),
3942
Placeholder.parsed("name", playerToBlockString));
4043
return true;

src/main/java/simplexity/simplepms/commands/Blocklist.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2828
}
2929
UUID uuid = player.getUniqueId();
3030
List<PlayerBlock> blockList = Cache.getBlockList(uuid);
31-
if (blockList.isEmpty()) {
31+
if (blockList == null || blockList.isEmpty()) {
3232
player.sendRichMessage(Message.BLOCKLIST_EMPTY.getMessage());
3333
return true;
3434
}

src/main/java/simplexity/simplepms/commands/MessageToggle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2020
return false;
2121
}
2222
UUID uuid = player.getUniqueId();
23-
PlayerSettings playerSettings = SqlHandler.getInstance().getSettings(uuid);
23+
PlayerSettings playerSettings = Cache.getPlayerSettings(uuid);
2424
if (playerSettings.messagesDisabled()) {
2525
Cache.updateMessageSettings(uuid, false);
2626
player.sendRichMessage(Message.MESSAGES_ENABLED.getMessage());
2727
return true;
2828
}
29-
Cache.updateMessageSettings(uuid, false);
29+
Cache.updateMessageSettings(uuid, true);
3030
player.sendRichMessage(Message.MESSAGES_DISABLED.getMessage());
3131
return true;
3232
}

src/main/java/simplexity/simplepms/commands/Unblock.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
import simplexity.simplepms.config.Message;
1111
import simplexity.simplepms.objects.PlayerBlock;
1212
import simplexity.simplepms.saving.Cache;
13-
import simplexity.simplepms.saving.SqlHandler;
1413

1514
import java.util.ArrayList;
1615
import java.util.List;
17-
import java.util.UUID;
1816

1917
public class Unblock implements TabExecutor {
2018
@Override
@@ -29,21 +27,21 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2927
}
3028
String blockString = args[0];
3129
List<PlayerBlock> playerBlockList = Cache.getBlockList(player.getUniqueId());
32-
UUID blockedPlayerUUID = blockedPlayerUUID(blockString, playerBlockList);
33-
if (blockedPlayerUUID == null) {
30+
PlayerBlock blockedPlayer = getBlockedPlayer(blockString, playerBlockList);
31+
if (blockedPlayer == null) {
3432
player.sendRichMessage(Message.PLAYER_NOT_BLOCKED.getMessage());
3533
return false;
3634
}
37-
SqlHandler.getInstance().removeBlockedPlayer(player.getUniqueId(), blockedPlayerUUID);
35+
Cache.removeBlockedUser(player.getUniqueId(), blockedPlayer);
3836
player.sendRichMessage(Message.NO_LONGER_BLOCKING.getMessage(),
3937
Placeholder.parsed("name", blockString));
4038
return true;
4139
}
4240

43-
private UUID blockedPlayerUUID(String playerBlocked, List<PlayerBlock> playerBlockList) {
41+
private PlayerBlock getBlockedPlayer(String playerBlocked, List<PlayerBlock> playerBlockList) {
4442
for (PlayerBlock playerBlock : playerBlockList) {
4543
if (playerBlock.blockedPlayerName().equals(playerBlocked)) {
46-
return playerBlock.blockedPlayerUUID();
44+
return playerBlock;
4745
}
4846
}
4947
return null;

src/main/java/simplexity/simplepms/config/ConfigHandler.java

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package simplexity.simplepms.config;
22

3+
import org.bukkit.Sound;
34
import org.bukkit.configuration.file.FileConfiguration;
45
import simplexity.simplepms.SimplePMs;
56
import simplexity.simplepms.saving.SqlHandler;
67

78
import java.util.ArrayList;
89
import java.util.HashSet;
910
import java.util.List;
11+
import java.util.logging.Logger;
1012

1113
public class ConfigHandler {
1214
private static ConfigHandler instance;
@@ -16,8 +18,11 @@ public static ConfigHandler getInstance() {
1618
return instance;
1719
}
1820

21+
private final Logger logger = SimplePMs.getInstance().getLogger();
1922
private boolean mysqlEnabled, playersSendToConsole, playersSendToHiddenPlayers, consoleHasSocialSpy,
20-
commandSpyEnabled, consoleHasCommandSpy;
23+
commandSpyEnabled, consoleHasCommandSpy, receiveSoundEnabled, sendSoundEnabled, spySoundEnabled;
24+
private Sound receiveSound, sendSound, spySound;
25+
private float receivePitch, receiveVolume, sendPitch, sendVolume, spyPitch, spyVolume;
2126
private String mysqlIp, mysqlName, mysqlUsername, mysqlPassword, normalFormat, socialSpyFormat;
2227
private List<String> validNamesForConsole = new ArrayList<>();
2328
private final HashSet<String> commandsToSpy = new HashSet<>();
@@ -43,13 +48,63 @@ public void loadConfigValues() {
4348
validNamesForConsole = config.getStringList("valid-console-names");
4449
consoleHasSocialSpy = config.getBoolean("console-has-social-spy", true);
4550
consoleHasCommandSpy = config.getBoolean("console-has-command-spy", false);
51+
receiveSoundEnabled = config.getBoolean("sounds.received.enabled", false);
52+
sendSoundEnabled = config.getBoolean("sounds.sent.enabled", false);
53+
spySoundEnabled = config.getBoolean("sounds.spy.enabled", false);
54+
if (receiveSoundEnabled) loadReceiveSoundInfo(config);
55+
if (sendSoundEnabled) loadSendSoundInfo(config);
56+
if (spySoundEnabled) loadSpySoundInfo(config);
4657
}
4758

4859
private void updateHashSet(HashSet<String> set, List<String> list) {
4960
set.clear();
5061
set.addAll(list);
5162
}
5263

64+
private void loadReceiveSoundInfo(FileConfiguration config) {
65+
String soundString = config.getString("sounds.received.sound", "BLOCK_NOTE_BLOCK_XYLOPHONE");
66+
receiveSound = getValidSound(soundString, Sound.BLOCK_NOTE_BLOCK_XYLOPHONE);
67+
receivePitch = getValidFloat(config.getDouble("sounds.received.pitch", 1.8));
68+
receiveVolume = getValidFloat(config.getDouble("sounds.received.volume", 0.5));
69+
}
70+
71+
private void loadSendSoundInfo(FileConfiguration config){
72+
String soundString = config.getString("sounds.sent.sound", "ENTITY_ALLAY_ITEM_THROWN");
73+
sendSound = getValidSound(soundString, Sound.ENTITY_ALLAY_ITEM_THROWN);
74+
sendPitch = getValidFloat(config.getDouble("sounds.sent.pitch", 1.8));
75+
sendVolume = getValidFloat(config.getDouble("sounds.sent.volume", 0.5));
76+
}
77+
78+
private void loadSpySoundInfo(FileConfiguration config){
79+
String soundString = config.getString("sounds.spy.sound", "ENTITY_ITEM_FRAME_ROTATE_ITEM");
80+
spySound = getValidSound(soundString, Sound.ENTITY_ITEM_FRAME_ROTATE_ITEM);
81+
spyPitch = getValidFloat(config.getDouble("sounds.spy.pitch", 1.8));
82+
spyVolume = getValidFloat(config.getDouble("sounds.spy.volume", 0.5));
83+
}
84+
85+
private Sound getValidSound(String soundString, Sound defaultSound){
86+
Sound sound;
87+
try {
88+
sound = Sound.valueOf(soundString);
89+
} catch (IllegalArgumentException exception) {
90+
String warning = Message.SOUND_NOT_VALID.getMessage().replace("%sound-string%", soundString);
91+
String warning2 = Message.USING_DEFAULT_SOUND.getMessage().replace("%default-sound%", defaultSound.name());
92+
logger.warning(warning);
93+
logger.warning(warning2);
94+
sound = defaultSound;
95+
}
96+
return sound;
97+
}
98+
99+
private float getValidFloat(double numberToCheck){
100+
if (numberToCheck <= 2 && numberToCheck >= 0) return (float) numberToCheck;
101+
String warning = Message.OUT_OF_RANGE.getMessage().replace("%number%", String.valueOf(numberToCheck));
102+
logger.warning(warning);
103+
logger.warning(Message.USING_DEFAULT_NUMBER.getMessage());
104+
return 1.0f;
105+
}
106+
107+
53108
public boolean isMysqlEnabled() {
54109
return mysqlEnabled;
55110
}
@@ -98,10 +153,59 @@ public boolean isCommandSpyEnabled() {
98153
return commandSpyEnabled;
99154
}
100155

156+
public boolean receivingMessagePlaysSound(){
157+
return receiveSoundEnabled;
158+
}
159+
160+
public boolean sendingMessagePlaysSound(){
161+
return sendSoundEnabled;
162+
}
163+
164+
public boolean messagePlaysSoundForSpy(){
165+
return spySoundEnabled;
166+
}
167+
101168
public String getNormalFormat() {
102169
return normalFormat;
103170
}
171+
104172
public String getSocialSpyFormat() {
105173
return socialSpyFormat;
106174
}
175+
176+
public Sound getReceiveSound() {
177+
return receiveSound;
178+
}
179+
180+
public Sound getSendSound() {
181+
return sendSound;
182+
}
183+
184+
public Sound getSpySound() {
185+
return spySound;
186+
}
187+
188+
public float getReceivePitch() {
189+
return receivePitch;
190+
}
191+
192+
public float getReceiveVolume() {
193+
return receiveVolume;
194+
}
195+
196+
public float getSendPitch() {
197+
return sendPitch;
198+
}
199+
200+
public float getSendVolume() {
201+
return sendVolume;
202+
}
203+
204+
public float getSpyPitch() {
205+
return spyPitch;
206+
}
207+
208+
public float getSpyVolume() {
209+
return spyVolume;
210+
}
107211
}

src/main/java/simplexity/simplepms/config/Message.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public enum Message {
3030
YOUR_MESSAGES_CURRENTLY_DISABLED("error.your-messages-currently-disabled", "<red>Sorry, you cannot send a message to someone while your messages are disabled.</red>"),
3131
CANNOT_MESSAGE_SOMEONE_YOU_BLOCKED("error.cannot-message-someone-you-blocked", "<red>Sorry, you cannot message someone you currently have blocked.</red>"),
3232
CANNOT_MESSAGE_CONSOLE("error.cannot-message-console", "<red>Sorry, you cannot message the server</red>"),
33-
SOMETHING_WENT_WRONG("error.something-wrong", "<red>Sorry, something went wrong, please check console for more information</red>");
33+
SOMETHING_WENT_WRONG("error.something-wrong", "<red>Sorry, something went wrong, please check console for more information</red>"),
34+
SOUND_NOT_VALID("console-error.sound-not-valid", "Warning! The sound you have input: '%sound-string%' is invalid! Please be sure you use a sound that is listed on https://jd.papermc.io/paper/1.21.4/org/bukkit/Sound.html"),
35+
USING_DEFAULT_SOUND("console-error.using-default-sound", "Using %default-sound% until a valid sound is provided"),
36+
OUT_OF_RANGE("console-error.float-out-of-range", "The number %number% is out of range! Volume and pitch values must be a number between 0 and 2!"),
37+
USING_DEFAULT_NUMBER("console-error.using-default-float", "Setting to 1.0 until a valid value is provided");
38+
3439

3540
private final String path;
3641
private String message;

src/main/java/simplexity/simplepms/logic/Messaging.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,46 @@
66
import simplexity.simplepms.SimplePMs;
77
import simplexity.simplepms.config.ConfigHandler;
88
import simplexity.simplepms.config.Message;
9-
import simplexity.simplepms.events.PrivateMessageEvent;
109

1110
public class Messaging {
1211

1312
private static final String CONSOLE_SPY = "message.admin.console-spy";
1413
private static final String SOCIAL_SPY_BYPASS = "message.bypass.social-spy";
1514
private static final String COMMAND_SPY_BYPASS = "message.bypass.command-spy";
1615

17-
public static void sendMessage(PrivateMessageEvent privateMessageEvent, CommandSender initiator, CommandSender target, String messageContent) {
16+
public static void sendMessage(CommandSender initiator, CommandSender target, String messageContent) {
17+
handleMessageSend(initiator, target, messageContent);
18+
handleMessageReceive(initiator, target, messageContent);
19+
handleSocialSpy(initiator, target, messageContent);
20+
PreProcessing.lastMessaged.put(initiator, target);
21+
PreProcessing.lastMessaged.put(target, initiator);
22+
}
23+
24+
private static void handleMessageSend(CommandSender initiator, CommandSender target, String messageContent) {
1825
initiator.sendMessage(Util.getInstance().parseMessage(
1926
Message.FORMAT_SENT.getMessage(),
2027
initiator, target, messageContent, false));
28+
if (!ConfigHandler.getInstance().sendingMessagePlaysSound()) return;
29+
if (!(initiator instanceof Player player)) return;
30+
player.playSound(player,
31+
ConfigHandler.getInstance().getSendSound(),
32+
ConfigHandler.getInstance().getSendVolume(),
33+
ConfigHandler.getInstance().getSendPitch());
34+
}
35+
36+
private static void handleMessageReceive(CommandSender initiator, CommandSender target, String messageContent) {
2137
target.sendMessage(Util.getInstance().parseMessage(
2238
Message.FORMAT_RECEIVED.getMessage(),
2339
initiator, target, messageContent, false));
24-
handleSocialSpy(initiator, target, messageContent);
25-
PreProcessing.lastMessaged.put(initiator, target);
26-
PreProcessing.lastMessaged.put(target, initiator);
40+
if (!ConfigHandler.getInstance().receivingMessagePlaysSound()) return;
41+
if (!(target instanceof Player player)) return;
42+
player.playSound(player,
43+
ConfigHandler.getInstance().getReceiveSound(),
44+
ConfigHandler.getInstance().getReceiveVolume(),
45+
ConfigHandler.getInstance().getReceivePitch());
2746
}
2847

48+
2949
public static void sendCommandSpy(CommandSender initiator, String command, String messageContent) {
3050
if (initiator.hasPermission(COMMAND_SPY_BYPASS)) return;
3151
Component parsedMessage = Util.getInstance().parseMessage(
@@ -34,6 +54,7 @@ public static void sendCommandSpy(CommandSender initiator, String command, Strin
3454
for (Player spyingPlayer : SimplePMs.getSpyingPlayers()) {
3555
if (initiator.equals(spyingPlayer)) continue;
3656
spyingPlayer.sendMessage(parsedMessage);
57+
playSpySound(spyingPlayer);
3758
}
3859
if (ConfigHandler.getInstance().doesConsoleHaveCommandSpy()) {
3960
sendConsoleMessage(parsedMessage);
@@ -45,10 +66,11 @@ public static void handleSocialSpy(CommandSender initiator, CommandSender target
4566
Player initiatorPlayer = Util.getInstance().getPlayerFromCommandSender(initiator);
4667
Player targetPlayer = Util.getInstance().getPlayerFromCommandSender(target);
4768
if (initiatorPlayer == null || targetPlayer == null) consoleSpy = true;
48-
if (consoleSpy) {
49-
sendConsoleSpy(initiator, target, messageContent);
50-
} else {
69+
if (!consoleSpy) {
70+
if (initiatorPlayer.hasPermission(SOCIAL_SPY_BYPASS) || targetPlayer.hasPermission(SOCIAL_SPY_BYPASS)) return;
5171
sendSocialSpy(initiator, target, messageContent);
72+
} else {
73+
sendConsoleSpy(initiator, target, messageContent);
5274
}
5375
}
5476

@@ -61,6 +83,7 @@ private static void sendConsoleSpy(CommandSender initiator, CommandSender target
6183
if (initiator.equals(spyingPlayer) || target.equals(spyingPlayer)) continue;
6284
if (!spyingPlayer.hasPermission(CONSOLE_SPY)) continue;
6385
spyingPlayer.sendMessage(parsedMessage);
86+
playSpySound(spyingPlayer);
6487
}
6588
if (ConfigHandler.getInstance().doesConsoleHaveSocialSpy()) {
6689
sendConsoleMessage(parsedMessage);
@@ -75,15 +98,24 @@ private static void sendSocialSpy(CommandSender initiator, CommandSender target,
7598
for (Player spyingPlayer : SimplePMs.getSpyingPlayers()) {
7699
if (initiator.equals(spyingPlayer) || target.equals(spyingPlayer)) continue;
77100
spyingPlayer.sendMessage(parsedMessage);
101+
playSpySound(spyingPlayer);
78102
}
79103
if (ConfigHandler.getInstance().doesConsoleHaveSocialSpy()) {
80104
sendConsoleMessage(parsedMessage);
81105
}
82106
}
83107

84-
private static void sendConsoleMessage(Component message){
108+
private static void sendConsoleMessage(Component message) {
85109
SimplePMs.getPMConsoleSender().sendMessage(message);
86110
}
87111

112+
private static void playSpySound(Player spyingPlayer) {
113+
if (!ConfigHandler.getInstance().messagePlaysSoundForSpy()) return;
114+
spyingPlayer.playSound(spyingPlayer,
115+
ConfigHandler.getInstance().getSpySound(),
116+
ConfigHandler.getInstance().getSpyVolume(),
117+
ConfigHandler.getInstance().getSpyPitch());
118+
}
119+
88120

89121
}

src/main/java/simplexity/simplepms/logic/PreProcessing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void callPMEvent(CommandSender initiator, CommandSender target, String me
111111
PrivateMessageEvent messageEvent = new PrivateMessageEvent(initiator, target, messageContent, SimplePMs.getSpyingPlayers());
112112
SimplePMs.getInstance().getServer().getPluginManager().callEvent(messageEvent);
113113
if (!messageEvent.isCancelled()) {
114-
Messaging.sendMessage(messageEvent, initiator, target, messageContent);
114+
Messaging.sendMessage(initiator, target, messageContent);
115115
}
116116

117117
}

0 commit comments

Comments
 (0)