-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageCommand.java
More file actions
70 lines (63 loc) · 3.97 KB
/
MessageCommand.java
File metadata and controls
70 lines (63 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package pro.cloudnode.smp.cloudnodemsg.command;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.cloudnodemsg.CloudnodeMSG;
import pro.cloudnode.smp.cloudnodemsg.Message;
import pro.cloudnode.smp.cloudnodemsg.Permissions;
import pro.cloudnode.smp.cloudnodemsg.error.InvalidPlayerError;
import pro.cloudnode.smp.cloudnodemsg.error.MessageYourselfError;
import pro.cloudnode.smp.cloudnodemsg.error.NoPermissionError;
import pro.cloudnode.smp.cloudnodemsg.error.PlayerHasIncomingDisabledError;
import pro.cloudnode.smp.cloudnodemsg.error.PlayerNotFoundError;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public final class MessageCommand extends Command {
public static final @NotNull String usage = "<player> [message]";
public static final @NotNull String usageConsole = "<player> <message>";
@Override
public boolean run(final @NotNull CommandSender sender, final @NotNull String label, @NotNull String @NotNull [] args) {
if (!sender.hasPermission(Permissions.USE)) return new NoPermissionError().send(sender);
if (args.length == 0)
return sendMessage(sender, CloudnodeMSG.getInstance().config().usage(label, sender instanceof Player ? usage : usageConsole));
if (args.length == 1 && !(sender instanceof Player)) return sendMessage(sender, CloudnodeMSG.getInstance().config()
.usage(label, usageConsole.replace("<player>", args[0])));
final @NotNull Optional<@NotNull Player> recipient = Optional.ofNullable(CloudnodeMSG.getInstance().getServer()
.getPlayer(args[0]));
if (sender instanceof final @NotNull Player player && recipient.isPresent() && recipient.get().getUniqueId().equals(player.getUniqueId()))
return new MessageYourselfError().send(sender);
if (args.length == 1) {
final @NotNull Player player = (Player) sender;
if (Message.hasChannel(player)) {
final @NotNull OfflinePlayer recipientOffline = CloudnodeMSG.getInstance().getServer().getOfflinePlayer(args[0]);
if (Message.getChannel(player).map(r -> r.getUniqueId().equals(recipientOffline.getUniqueId())).orElse(false)) {
Message.exitChannel(player);
return sendMessage(player, CloudnodeMSG.getInstance().config().channelClosed(player.getName(), Optional.ofNullable(recipientOffline.getName()).orElse("Unknown Player"), label));
}
}
if (recipient.isEmpty() || (CloudnodeMSG.isVanished(recipient.get(), player) && !player.hasPermission(Permissions.SEND_VANISHED))) return new PlayerNotFoundError(args[0]).send(player);
if (!Message.isIncomingEnabled(recipient.get())) return new PlayerHasIncomingDisabledError(recipient.get().getName()).send(player);
Message.createChannel(player, recipient.get());
return sendMessage(player, CloudnodeMSG.getInstance().config().channelCreated(player.getName(), recipient.get().getName(), label));
}
try {
if (recipient.isEmpty()) return new PlayerNotFoundError(args[0]).send(sender);
new Message(Message.offlinePlayer(sender), recipient.get(), String.join(" ", Arrays.copyOfRange(args, 1, args.length))).send();
return true;
}
catch (final @NotNull InvalidPlayerError e) {
return e.log().send(sender);
}
}
@Override
public @Nullable List<@NotNull String> onTabComplete(final @NotNull CommandSender sender, final @NotNull org.bukkit.command.Command command, final @NotNull String label, final @NotNull String @NotNull [] args) {
if (!sender.hasPermission(Permissions.USE)) return new ArrayList<>();
// `null` works for list of players
if (args.length == 1) return null;
return new ArrayList<>();
}
}