Skip to content

Commit c9a111f

Browse files
authored
Generic improvements (#41)
2 parents 398e6ad + 1bd6dab commit c9a111f

File tree

14 files changed

+201
-117
lines changed

14 files changed

+201
-117
lines changed

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# CloudnodeMSG
2+
23
A Minecraft Java plugin for private messages.
34

45
Supports message channels (so you don't have to use a command for every DM) and integrates with vanish plugins.
56

67
[See the default config](https://github.com/cloudnode-pro/CloudnodeMSG/blob/main/src/main/resources/config.yml)
78

89
## Commands
10+
911
The following are all commands from this plugin.
1012

1113
***
1214

1315
### `/msg <player> <message>`
16+
1417
Send a private message to another player.
1518

1619
<dl>
@@ -21,6 +24,7 @@ Send a private message to another player.
2124
***
2225

2326
### `/msg <player>`
27+
2428
Create a private message channel to a player;
2529
i.e. all of your chat messages will be sent as private messages to this player.
2630
Run the command again to disable the message channel.
@@ -33,6 +37,7 @@ Run the command again to disable the message channel.
3337
***
3438

3539
### `/reply <message>`
40+
3641
Send a private message to the last player that messaged you.
3742

3843
<dl>
@@ -43,6 +48,7 @@ Send a private message to the last player that messaged you.
4348
***
4449

4550
### `/teammsg <message>`
51+
4652
Send a private message to your [scoreboard team](https://minecraft.fandom.com/wiki/Scoreboard#Teams).
4753

4854
<dl>
@@ -53,6 +59,7 @@ Send a private message to your [scoreboard team](https://minecraft.fandom.com/wi
5359
***
5460

5561
### `/teammsg`
62+
5663
Create a private message channel to your [scoreboard team](https://minecraft.fandom.com/wiki/Scoreboard#Teams);
5764
i.e. all of your chat messages will be sent as private team messages.
5865
Run the command again to disable the message channel.
@@ -65,7 +72,8 @@ Run the command again to disable the message channel.
6572
***
6673

6774
### `/ignore <player>`
68-
Ignore a player—you will stop seeing all of their private messages to you.
75+
76+
Ignore a player—you will stop seeing all of their messages (including public chat messages).
6977

7078
<dl>
7179
<dt>Aliases:</dt> <dd><code>/block</code></dd>
@@ -75,7 +83,8 @@ Ignore a player—you will stop seeing all of their private messages to you.
7583
***
7684

7785
### `/unignore <player>`
78-
You will stop ignoring the player and will be able to receive their private messages again.
86+
87+
You will stop ignoring the player and will be able to see their messages again.
7988

8089
<dl>
8190
<dt>Aliases:</dt> <dd><code>/unblock</code></dd>
@@ -85,6 +94,7 @@ You will stop ignoring the player and will be able to receive their private mess
8594
***
8695

8796
### `/togglemsg`
97+
8898
Enable or disable receiving private messages.
8999
When your private messages are disabled, nobody can message you, but you can still send messages.
90100

@@ -96,6 +106,7 @@ When your private messages are disabled, nobody can message you, but you can sti
96106
***
97107

98108
### `/togglemsg <player>`
109+
99110
Enable or disable receiving private messages of another player.
100111

101112
<dl>
@@ -108,6 +119,7 @@ Enable or disable receiving private messages of another player.
108119
***
109120

110121
### `/cloudnodemsg reload`
122+
111123
Reload the plugin configuration.
112124

113125
<dl>
@@ -116,6 +128,7 @@ Reload the plugin configuration.
116128
</dl>
117129

118130
## Permissions
131+
119132
Here is a list of the permissions used by this plugin.
120133

121134
| Permission | Description | Recommended Group |
@@ -138,13 +151,16 @@ with new features or changes typically released every **Tuesday**.
138151
When we merge critical bug fixes, we may publish out-of-band releases on any day of the week.
139152

140153
## Report Issues
154+
141155
Please ensure
142156
that you are using the [latest version](https://modrinth.com/plugin/5Ce4fxJB/version/latest) of CloudnodeMSG.
143157
The newest bug fixes are only available in the most recent version,
144158
and support is provided exclusively for this version.
145159

146160
If you encounter any problems with the plugin,
147-
please first check the [list of known issues](https://github.com/cloudnode-pro/CloudnodeMSG/issues?q=is%3Aopen+is%3Aissue+label%3Abug) on our GitHub repository.
161+
please first check
162+
the [list of known issues](https://github.com/cloudnode-pro/CloudnodeMSG/issues?q=is%3Aopen+is%3Aissue+label%3Abug) on
163+
our GitHub repository.
148164
If you don’t find a similar fault listed there,
149165
we encourage you to [submit a new issue](https://github.com/cloudnode-pro/CloudnodeMSG/issues/new?labels=bug).
150166
Resolving bugs is the highest priority for this project.
@@ -158,14 +174,15 @@ To suggest a new feature, please [create a new issue](https://github.com/cloudno
158174
providing a detailed description of your idea.
159175

160176
## Contributing
161-
CloudnodeMSG is licensed under the [GPL-3.0 licence](https://github.com/cloudnode-pro/CloudnodeMSG/blob/main/LICENSE).
177+
178+
CloudnodeMSG is licensed under the [GPL-3.0 licence](https://github.com/cloudnode-pro/CloudnodeMSG/blob/main/LICENSE).
162179
The source code is available on [GitHub](https://github.com/cloudnode-pro/CloudnodeMSG).
163180

164181
New contributors are most welcome to the project. If you're interested in contributing, follow these steps:
165182

166-
1. [Fork the repository](https://github.com/cloudnode-pro/CloudnodeMSG/fork)
167-
2. Create a new branch for your contributions.
168-
3. Make your changes and ensure they align with the project’s goals.
169-
4. Commit your changes with clear and descriptive messages.
170-
5. Push your changes to your fork.
171-
6. Submit a pull request.
183+
1. [Fork the repository](https://github.com/cloudnode-pro/CloudnodeMSG/fork)
184+
2. Create a new branch for your contributions.
185+
3. Make your changes and ensure they align with the project’s goals.
186+
4. Commit your changes with clear and descriptive messages.
187+
5. Push your changes to your fork.
188+
6. Submit a pull request.

src/main/java/pro/cloudnode/smp/cloudnodemsg/Message.java

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import org.bukkit.persistence.PersistentDataType;
99
import org.jetbrains.annotations.NotNull;
1010
import org.jetbrains.annotations.Nullable;
11+
import pro.cloudnode.smp.cloudnodemsg.error.ChannelOfflineError;
1112
import pro.cloudnode.smp.cloudnodemsg.error.InvalidPlayerError;
13+
import pro.cloudnode.smp.cloudnodemsg.error.PlayerHasIncomingDisabledError;
14+
import pro.cloudnode.smp.cloudnodemsg.error.PlayerNotFoundError;
1215

1316
import java.util.Arrays;
1417
import java.util.HashSet;
@@ -42,23 +45,41 @@ public Message(@NotNull OfflinePlayer sender, @NotNull OfflinePlayer recipient,
4245
}
4346

4447
public void send() throws InvalidPlayerError {
48+
send(false);
49+
}
50+
51+
public void send(final boolean channel) throws InvalidPlayerError {
4552
final @NotNull String senderUsername = playerOrServerUsername(this.sender);
4653
final @NotNull String recipientUsername = playerOrServerUsername(this.recipient);
4754

4855
final @NotNull Optional<@NotNull Player> senderPlayer = Optional.ofNullable(this.sender.getPlayer());
4956
final @NotNull Optional<@NotNull Player> recipientPlayer = Optional.ofNullable(this.recipient.getPlayer());
5057

58+
if (senderPlayer.isPresent() && (recipientPlayer.isEmpty() || (CloudnodeMSG.isVanished(recipientPlayer.get()) && !senderPlayer
59+
.get().hasPermission(Permission.SEND_VANISHED)))) {
60+
if (!channel) new PlayerNotFoundError(senderPlayer.get().getName()).send(senderPlayer.get());
61+
else {
62+
Message.exitChannel(senderPlayer.get());
63+
new ChannelOfflineError(senderPlayer.get().getName(), Optional.ofNullable(recipient.getName())
64+
.orElse("Unknown Player")).send(senderPlayer.get());
65+
}
66+
return;
67+
}
68+
69+
if (recipientPlayer.isPresent() && senderPlayer.isPresent() && !Message.isIncomingEnabled(recipientPlayer.get()) && !senderPlayer
70+
.get().hasPermission(Permission.TOGGLE_BYPASS)) {
71+
new PlayerHasIncomingDisabledError(recipientPlayer.get().getName()).send(senderPlayer.get());
72+
return;
73+
}
74+
5175
sendMessage(sender, CloudnodeMSG.getInstance().config().outgoing(senderUsername, recipientUsername, message));
5276
if (senderPlayer.isPresent() && !Message.hasChannel(senderPlayer.get(), recipient))
5377
setReplyTo(sender, recipient);
5478

5579
sendSpyMessage(sender, recipient, message);
5680

57-
if (
58-
(recipientPlayer.isPresent() && Message.isIgnored(recipientPlayer.get(), sender))
59-
&&
60-
(senderPlayer.isPresent() && !senderPlayer.get().hasPermission(Permission.IGNORE_BYPASS))
61-
) return;
81+
if ((recipientPlayer.isPresent() && Message.isIgnored(recipientPlayer.get(), sender)) && (senderPlayer.isPresent() && !senderPlayer
82+
.get().hasPermission(Permission.IGNORE_BYPASS))) return;
6283
sendMessage(recipient, CloudnodeMSG.getInstance().config()
6384
.incoming(senderUsername, recipientUsername, message));
6485
if (recipientPlayer.isPresent() && !Message.hasChannel(recipientPlayer.get(), sender))
@@ -82,17 +103,18 @@ public static void sendMessage(final @NotNull OfflinePlayer recipient, final @No
82103
* Send social spy to online players with permission
83104
*/
84105
public static void sendSpyMessage(final @NotNull OfflinePlayer sender, final @NotNull OfflinePlayer recipient, final @NotNull Component message) {
85-
final @NotNull String senderName = sender.getUniqueId().equals(console.getUniqueId()) ? CloudnodeMSG.getInstance().config().consoleName() : Optional.ofNullable(sender.getName()).orElse("Unknown Player");
86-
final @NotNull String recipientName = recipient.getUniqueId().equals(console.getUniqueId()) ? CloudnodeMSG.getInstance().config().consoleName() : Optional.ofNullable(recipient.getName()).orElse("Unknown Player");
106+
final @NotNull String senderName = sender.getUniqueId().equals(console.getUniqueId()) ? CloudnodeMSG
107+
.getInstance().config().consoleName() : Optional.ofNullable(sender.getName()).orElse("Unknown Player");
108+
final @NotNull String recipientName = recipient.getUniqueId().equals(console.getUniqueId()) ? CloudnodeMSG
109+
.getInstance().config().consoleName() : Optional.ofNullable(recipient.getName())
110+
.orElse("Unknown Player");
87111
for (final @NotNull Player player : CloudnodeMSG.getInstance().getServer().getOnlinePlayers()) {
88-
if (
89-
!player.hasPermission(Permission.SPY)
90-
|| player.getUniqueId().equals(sender.getUniqueId())
91-
|| player.getUniqueId().equals(recipient.getUniqueId())
92-
) continue;
112+
if (!player.hasPermission(Permission.SPY) || player.getUniqueId().equals(sender.getUniqueId()) || player
113+
.getUniqueId().equals(recipient.getUniqueId())) continue;
93114
sendMessage(player, CloudnodeMSG.getInstance().config().spy(senderName, recipientName, message));
94115
}
95-
if (!sender.getUniqueId().equals(console.getUniqueId()) && !recipient.getUniqueId().equals(console.getUniqueId()))
116+
if (!sender.getUniqueId().equals(console.getUniqueId()) && !recipient.getUniqueId()
117+
.equals(console.getUniqueId()))
96118
sendMessage(console, CloudnodeMSG.getInstance().config().spy(senderName, recipientName, message));
97119
}
98120

@@ -102,18 +124,17 @@ public static void sendSpyMessage(final @NotNull OfflinePlayer sender, final @No
102124

103125
public static void setReplyTo(final @NotNull OfflinePlayer sender, final @NotNull OfflinePlayer recipient) {
104126
if (sender.getUniqueId().equals(console.getUniqueId())) consoleReply = recipient.getUniqueId();
105-
else if (sender.isOnline())
106-
Objects.requireNonNull(sender.getPlayer()).getPersistentDataContainer()
107-
.set(REPLY_TO, PersistentDataType.STRING, recipient.getUniqueId().toString());
127+
else if (sender.isOnline()) Objects.requireNonNull(sender.getPlayer()).getPersistentDataContainer()
128+
.set(REPLY_TO, PersistentDataType.STRING, recipient.getUniqueId().toString());
108129
}
109130

110131
public static @NotNull Optional<@NotNull OfflinePlayer> getReplyTo(final @NotNull OfflinePlayer player) {
111132
if (player.getUniqueId().equals(console.getUniqueId())) return Optional.ofNullable(consoleReply)
112133
.map(uuid -> CloudnodeMSG.getInstance().getServer().getOfflinePlayer(uuid));
113-
if (player.isOnline())
114-
return Optional.ofNullable(Objects.requireNonNull(player.getPlayer()).getPersistentDataContainer()
115-
.get(REPLY_TO, PersistentDataType.STRING))
116-
.map(uuid -> CloudnodeMSG.getInstance().getServer().getOfflinePlayer(UUID.fromString(uuid)));
134+
if (player.isOnline()) return Optional
135+
.ofNullable(Objects.requireNonNull(player.getPlayer()).getPersistentDataContainer()
136+
.get(REPLY_TO, PersistentDataType.STRING))
137+
.map(uuid -> CloudnodeMSG.getInstance().getServer().getOfflinePlayer(UUID.fromString(uuid)));
117138
return Optional.empty();
118139
}
119140

@@ -133,15 +154,16 @@ else if (player.isOnline())
133154
* @param player The player
134155
*/
135156
public static @NotNull HashSet<@NotNull UUID> getIgnored(final @NotNull Player player) {
136-
final @NotNull Optional<@NotNull String> str = Optional.ofNullable(player.getPersistentDataContainer().get(IGNORED_PLAYERS, PersistentDataType.STRING));
137-
return str.map(s -> new HashSet<>(Arrays.stream(s.split(";")).filter(e -> !e.isEmpty()).map(UUID::fromString).toList()))
138-
.orElseGet(HashSet::new);
157+
final @NotNull Optional<@NotNull String> str = Optional.ofNullable(player.getPersistentDataContainer()
158+
.get(IGNORED_PLAYERS, PersistentDataType.STRING));
159+
return str.map(s -> new HashSet<>(Arrays.stream(s.split(";")).filter(e -> !e.isEmpty()).map(UUID::fromString)
160+
.toList())).orElseGet(HashSet::new);
139161
}
140162

141163
/**
142164
* Check if a player is ignored
143165
*
144-
* @param player The player
166+
* @param player The player
145167
* @param ignored The ignored player
146168
*/
147169
public static boolean isIgnored(final @NotNull Player player, final @NotNull OfflinePlayer ignored) {
@@ -157,19 +179,23 @@ public static boolean isIgnored(final @NotNull Player player, final @NotNull Off
157179
public static void ignore(final @NotNull Player player, final @NotNull OfflinePlayer ignore) {
158180
final @NotNull HashSet<@NotNull UUID> ignoredPlayers = getIgnored(player);
159181
ignoredPlayers.add(ignore.getUniqueId());
160-
player.getPersistentDataContainer().set(IGNORED_PLAYERS, PersistentDataType.STRING, String.join(";", ignoredPlayers.stream().map(UUID::toString).toList()));
182+
player.getPersistentDataContainer()
183+
.set(IGNORED_PLAYERS, PersistentDataType.STRING, String.join(";", ignoredPlayers.stream()
184+
.map(UUID::toString).toList()));
161185
}
162186

163187
/**
164188
* Unignore a player
165189
*
166-
* @param player The player
190+
* @param player The player
167191
* @param ignored The player to unignore
168192
*/
169193
public static void unignore(final @NotNull Player player, final @NotNull OfflinePlayer ignored) {
170194
final @NotNull HashSet<@NotNull UUID> ignoredPlayers = getIgnored(player);
171195
ignoredPlayers.remove(ignored.getUniqueId());
172-
player.getPersistentDataContainer().set(IGNORED_PLAYERS, PersistentDataType.STRING, String.join(";", ignoredPlayers.stream().map(UUID::toString).toList()));
196+
player.getPersistentDataContainer()
197+
.set(IGNORED_PLAYERS, PersistentDataType.STRING, String.join(";", ignoredPlayers.stream()
198+
.map(UUID::toString).toList()));
173199
}
174200

175201
public static final @NotNull NamespacedKey INCOMING_ENABLED = new NamespacedKey(CloudnodeMSG.getInstance(), "incoming_enabled");
@@ -208,7 +234,8 @@ public static boolean isIncomingEnabled(final @NotNull Player player) {
208234
* @param recipient The other end of the channel
209235
*/
210236
public static void createChannel(final @NotNull Player player, final @NotNull OfflinePlayer recipient) {
211-
player.getPersistentDataContainer().set(CHANNEL_RECIPIENT, PersistentDataType.STRING, recipient.getUniqueId().toString());
237+
player.getPersistentDataContainer()
238+
.set(CHANNEL_RECIPIENT, PersistentDataType.STRING, recipient.getUniqueId().toString());
212239
player.getPersistentDataContainer().remove(CHANNEL_TEAM);
213240
}
214241

@@ -227,21 +254,31 @@ public static void exitChannel(final @NotNull Player player) {
227254
* @param player The player
228255
*/
229256
public static @NotNull Optional<@NotNull OfflinePlayer> getChannel(final @NotNull Player player) {
230-
return Optional.ofNullable(player.getPersistentDataContainer().get(CHANNEL_RECIPIENT, PersistentDataType.STRING))
257+
return Optional
258+
.ofNullable(player.getPersistentDataContainer().get(CHANNEL_RECIPIENT, PersistentDataType.STRING))
231259
.map(uuid -> CloudnodeMSG.getInstance().getServer().getOfflinePlayer(UUID.fromString(uuid)));
232260
}
233261

234262
/**
235263
* Check whether player has DM channel with recipient
236264
*
237-
* @param player The player
265+
* @param player The player
238266
* @param recipient The recipient
239267
*/
240268
public static boolean hasChannel(final @NotNull Player player, final @NotNull OfflinePlayer recipient) {
241269
final @NotNull Optional<@NotNull OfflinePlayer> channel = getChannel(player);
242270
return channel.isPresent() && channel.get().getUniqueId().equals(recipient.getUniqueId());
243271
}
244272

273+
/**
274+
* Check whether player has DM channel
275+
*
276+
* @param player The player
277+
*/
278+
public static boolean hasChannel(final @NotNull Player player) {
279+
return getChannel(player).isPresent();
280+
}
281+
245282
/**
246283
* Team message channel
247284
*

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static boolean sendMessage(final @NotNull Audience recipient, final @NotN
1414
return true;
1515
}
1616

17-
public abstract boolean run(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String[] args);
17+
public abstract boolean run(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String @NotNull [] args);
1818

1919
@Override
2020
public final boolean onCommand(final @NotNull CommandSender sender, final @NotNull org.bukkit.command.Command command, final @NotNull String label, @NotNull String @NotNull [] args) {

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/IgnoreCommand.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ public boolean run(final @NotNull CommandSender sender, final @NotNull String la
3232
}
3333

3434
public static boolean ignore(final @NotNull Player player, final @NotNull OfflinePlayer target) {
35-
if (target.isOnline() && Objects.requireNonNull(target.getPlayer()).hasPermission(Permission.IGNORE_BYPASS)) return new CannotIgnoreError(Optional.ofNullable(target.getName()).orElse("Unknown Player")).send(player);
36-
if (!target.isOnline() && !target.hasPlayedBefore()) return new NeverJoinedError(Optional.ofNullable(target.getName()).orElse("Unknown Player")).send(player);
35+
if (target.isOnline() && Objects.requireNonNull(target.getPlayer()).hasPermission(Permission.IGNORE_BYPASS))
36+
return new CannotIgnoreError(Optional.ofNullable(target.getName()).orElse("Unknown Player")).send(player);
37+
if (!target.isOnline() && !target.hasPlayedBefore())
38+
return new NeverJoinedError(Optional.ofNullable(target.getName()).orElse("Unknown Player")).send(player);
3739
Message.ignore(player, target);
38-
return sendMessage(player, CloudnodeMSG.getInstance().config().ignored(Optional.ofNullable(target.getName()).orElse("Unknown Player")));
40+
return sendMessage(player, CloudnodeMSG.getInstance().config()
41+
.ignored(Optional.ofNullable(target.getName()).orElse("Unknown Player")));
3942
}
4043

4144
public static boolean unignore(final @NotNull Player player, final @NotNull OfflinePlayer target) {
4245
Message.unignore(player, target);
43-
return sendMessage(player, CloudnodeMSG.getInstance().config().unignored(Optional.ofNullable(target.getName()).orElse("Unknown Player")));
46+
return sendMessage(player, CloudnodeMSG.getInstance().config()
47+
.unignored(Optional.ofNullable(target.getName()).orElse("Unknown Player")));
4448
}
4549

4650
@Override

0 commit comments

Comments
 (0)