Skip to content

Commit d6a74f8

Browse files
authored
Fix replying to/from console (#44)
2 parents de842fe + 3aeb0fe commit d6a74f8

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pro.cloudnode.smp.cloudnodemsg;
22

3+
import net.kyori.adventure.audience.Audience;
34
import net.kyori.adventure.text.Component;
45
import org.bukkit.NamespacedKey;
56
import org.bukkit.OfflinePlayer;
@@ -12,6 +13,7 @@
1213
import pro.cloudnode.smp.cloudnodemsg.error.InvalidPlayerError;
1314
import pro.cloudnode.smp.cloudnodemsg.error.PlayerHasIncomingDisabledError;
1415
import pro.cloudnode.smp.cloudnodemsg.error.PlayerNotFoundError;
16+
import pro.cloudnode.smp.cloudnodemsg.error.ReplyOfflineError;
1517

1618
import java.util.Arrays;
1719
import java.util.HashSet;
@@ -45,23 +47,27 @@ public Message(@NotNull OfflinePlayer sender, @NotNull OfflinePlayer recipient,
4547
}
4648

4749
public void send() throws InvalidPlayerError {
48-
send(false);
50+
send(Context.REGULAR);
4951
}
5052

51-
public void send(final boolean channel) throws InvalidPlayerError {
53+
public void send(final @NotNull Context context) throws InvalidPlayerError {
5254
final @NotNull String senderUsername = playerOrServerUsername(this.sender);
5355
final @NotNull String recipientUsername = playerOrServerUsername(this.recipient);
5456

5557
final @NotNull Optional<@NotNull Player> senderPlayer = Optional.ofNullable(this.sender.getPlayer());
5658
final @NotNull Optional<@NotNull Player> recipientPlayer = Optional.ofNullable(this.recipient.getPlayer());
5759

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());
60+
if (!recipient.getUniqueId().equals(console.getUniqueId()) && recipientPlayer.isEmpty() || (recipientPlayer.isPresent() && senderPlayer.isPresent() && CloudnodeMSG.isVanished(recipientPlayer.get()) && !senderPlayer.get().hasPermission(Permission.SEND_VANISHED))) {
61+
if (context == Context.CHANNEL) {
62+
final @NotNull Player player = Objects.requireNonNull(sender.getPlayer());
63+
Message.exitChannel(player);
64+
new ChannelOfflineError(player.getName(), Optional.ofNullable(recipient.getName())
65+
.orElse("Unknown Player")).send(player);
66+
}
6167
else {
62-
Message.exitChannel(senderPlayer.get());
63-
new ChannelOfflineError(senderPlayer.get().getName(), Optional.ofNullable(recipient.getName())
64-
.orElse("Unknown Player")).send(senderPlayer.get());
68+
final @NotNull Audience senderAudience = senderPlayer.isPresent() ? senderPlayer.get() : CloudnodeMSG.getInstance().getServer().getConsoleSender();
69+
if (context == Context.REPLY) new ReplyOfflineError(recipientUsername).send(senderAudience);
70+
else new PlayerNotFoundError(recipientUsername).send(senderAudience);
6571
}
6672
return;
6773
}
@@ -72,17 +78,16 @@ public void send(final boolean channel) throws InvalidPlayerError {
7278
return;
7379
}
7480

75-
sendMessage(sender, CloudnodeMSG.getInstance().config().outgoing(senderUsername, recipientUsername, message));
76-
if (senderPlayer.isPresent() && !Message.hasChannel(senderPlayer.get(), recipient))
77-
setReplyTo(sender, recipient);
78-
7981
sendSpyMessage(sender, recipient, message);
80-
82+
sendMessage(sender, CloudnodeMSG.getInstance().config().outgoing(senderUsername, recipientUsername, message));
8183
if ((recipientPlayer.isPresent() && Message.isIgnored(recipientPlayer.get(), sender)) && (senderPlayer.isPresent() && !senderPlayer
8284
.get().hasPermission(Permission.IGNORE_BYPASS))) return;
8385
sendMessage(recipient, CloudnodeMSG.getInstance().config()
8486
.incoming(senderUsername, recipientUsername, message));
85-
if (recipientPlayer.isPresent() && !Message.hasChannel(recipientPlayer.get(), sender))
87+
88+
if (sender.getUniqueId().equals(console.getUniqueId()) || (senderPlayer.isPresent() && !Message.hasChannel(senderPlayer.get(), recipient)))
89+
setReplyTo(sender, recipient);
90+
if (recipient.getUniqueId().equals(console.getUniqueId()) || (recipientPlayer.isPresent() && !Message.hasChannel(recipientPlayer.get(), sender)))
8691
setReplyTo(recipient, sender);
8792
}
8893

@@ -306,4 +311,24 @@ public static void exitTeamChannel(final @NotNull Player player) {
306311
public static boolean hasTeamChannel(final @NotNull Player player) {
307312
return player.getPersistentDataContainer().getOrDefault(CHANNEL_TEAM, PersistentDataType.BOOLEAN, false);
308313
}
314+
315+
/**
316+
* The context in which this message is sent
317+
*/
318+
public static enum Context {
319+
/**
320+
* Message sent via command (i.e. no special context)
321+
*/
322+
REGULAR,
323+
324+
/**
325+
* Message sent via messaging channel
326+
*/
327+
CHANNEL,
328+
329+
/**
330+
* Message sent as a reply
331+
*/
332+
REPLY;
333+
}
309334
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
import org.bukkit.command.CommandSender;
55
import org.jetbrains.annotations.NotNull;
66
import pro.cloudnode.smp.cloudnodemsg.CloudnodeMSG;
7+
import pro.cloudnode.smp.cloudnodemsg.Message;
78
import pro.cloudnode.smp.cloudnodemsg.Permission;
89
import pro.cloudnode.smp.cloudnodemsg.error.InvalidPlayerError;
910
import pro.cloudnode.smp.cloudnodemsg.error.NoPermissionError;
1011
import pro.cloudnode.smp.cloudnodemsg.error.NobodyReplyError;
11-
import pro.cloudnode.smp.cloudnodemsg.error.ReplyOfflineError;
12-
import pro.cloudnode.smp.cloudnodemsg.error.PlayerHasIncomingDisabledError;
13-
import pro.cloudnode.smp.cloudnodemsg.Message;
1412

1513
import java.util.ArrayList;
1614
import java.util.List;
17-
import java.util.Objects;
1815
import java.util.Optional;
1916

2017
public final class ReplyCommand extends Command {
@@ -27,18 +24,9 @@ public boolean run(final @NotNull CommandSender sender, final @NotNull String la
2724

2825
final @NotNull Optional<@NotNull OfflinePlayer> recipient = Message.getReplyTo(Message.offlinePlayer(sender));
2926
if (recipient.isEmpty()) return new NobodyReplyError().send(sender);
30-
if (!recipient.get().getUniqueId()
31-
.equals(Message.console.getUniqueId()) && !Message.isIncomingEnabled(Objects.requireNonNull(recipient
32-
.get().getPlayer())) && !sender.hasPermission(Permission.TOGGLE_BYPASS))
33-
return new PlayerHasIncomingDisabledError(Objects.requireNonNull(recipient.get().getName())).send(sender);
34-
if (!recipient.get().getUniqueId().equals(Message.console.getUniqueId()) && (!recipient.get()
35-
.isOnline() || (CloudnodeMSG.isVanished(Objects.requireNonNull(recipient.get()
36-
.getPlayer())) && !sender.hasPermission(Permission.SEND_VANISHED))))
37-
return new ReplyOfflineError(Optional.ofNullable(recipient.get().getName())
38-
.orElse("Unknown Player")).send(sender);
3927

4028
try {
41-
new Message(Message.offlinePlayer(sender), recipient.get(), String.join(" ", args)).send();
29+
new Message(Message.offlinePlayer(sender), recipient.get(), String.join(" ", args)).send(Message.Context.REPLY);
4230
return true;
4331
}
4432
catch (final @NotNull InvalidPlayerError e) {

src/main/java/pro/cloudnode/smp/cloudnodemsg/listener/AsyncChatListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void channels(final @NotNull AsyncChatEvent event) {
4848
if (channelRecipient.isEmpty()) return;
4949
event.setCancelled(true);
5050
try {
51-
new Message(sender, channelRecipient.get(), event.message()).send(true);
51+
new Message(sender, channelRecipient.get(), event.message()).send(Message.Context.CHANNEL);
5252
}
5353
catch (final @NotNull InvalidPlayerError e) {
5454
e.log().send(sender);

0 commit comments

Comments
 (0)