|
1 | 1 | package pro.cloudnode.smp.cloudnodemsg.command; |
2 | 2 |
|
| 3 | +import org.bukkit.OfflinePlayer; |
3 | 4 | import org.bukkit.command.CommandSender; |
4 | 5 | import org.bukkit.entity.Player; |
5 | 6 | import org.jetbrains.annotations.NotNull; |
|
18 | 19 | import java.util.Optional; |
19 | 20 |
|
20 | 21 | public final class MessageCommand extends Command { |
21 | | - public static final @NotNull String usage = "<player> <message>"; |
| 22 | + public static final @NotNull String usage = "<player> [message]"; |
| 23 | + public static final @NotNull String usageConsole = "<player> <message>"; |
22 | 24 |
|
23 | 25 | @Override |
24 | 26 | public boolean run(final @NotNull CommandSender sender, final @NotNull String label, @NotNull String @NotNull [] args) { |
25 | 27 | if (!sender.hasPermission(Permission.USE)) return new NoPermissionError().send(sender); |
26 | | - if (args.length == 0) return sendMessage(sender, CloudnodeMSG.getInstance().config().usage(label, usage)); |
27 | | - if (args.length == 1) return sendMessage(sender, CloudnodeMSG.getInstance().config() |
28 | | - .usage(label, usage.replace("<player>", args[0]))); |
| 28 | + if (args.length == 0) { |
| 29 | + return sendMessage(sender, CloudnodeMSG.getInstance().config().usage(label, sender instanceof Player ? usage : usageConsole)); |
| 30 | + } |
| 31 | + if (args.length == 1 && !(sender instanceof Player)) return sendMessage(sender, CloudnodeMSG.getInstance().config() |
| 32 | + .usage(label, usageConsole.replace("<player>", args[0]))); |
29 | 33 |
|
30 | 34 | final @NotNull Optional<@NotNull Player> recipient = Optional.ofNullable(CloudnodeMSG.getInstance().getServer() |
31 | 35 | .getPlayer(args[0])); |
32 | 36 | if (recipient.isEmpty() || (CloudnodeMSG.isVanished(recipient.get()) && !sender.hasPermission(Permission.SEND_VANISHED))) return new PlayerNotFoundError(args[0]).send(sender); |
33 | 37 | if (sender instanceof final @NotNull Player player && recipient.get().getUniqueId().equals(player.getUniqueId())) |
34 | 38 | return new MessageYourselfError().send(sender); |
35 | 39 |
|
| 40 | + if (args.length == 1) { |
| 41 | + final @NotNull Player player = (Player) sender; |
| 42 | + if (Message.getChannel(player).map(r -> r.getUniqueId().equals(recipient.get().getUniqueId())).orElse(false)) { |
| 43 | + Message.exitChannel(player); |
| 44 | + return sendMessage(player, CloudnodeMSG.getInstance().config().channelClosed(player.getName(), recipient.get().getName(), label)); |
| 45 | + } |
| 46 | + Message.createChannel(player, recipient.get()); |
| 47 | + return sendMessage(player, CloudnodeMSG.getInstance().config().channelCreated(player.getName(), recipient.get().getName(), label)); |
| 48 | + } |
| 49 | + |
36 | 50 | try { |
37 | 51 | new Message(Message.offlinePlayer(sender), recipient.get(), String.join(" ", Arrays.copyOfRange(args, 1, args.length))).send(); |
38 | 52 | return true; |
|
0 commit comments