-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTeamMessageCommand.java
More file actions
77 lines (69 loc) · 3.62 KB
/
TeamMessageCommand.java
File metadata and controls
77 lines (69 loc) · 3.62 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
71
72
73
74
75
76
77
package pro.cloudnode.smp.cloudnodemsg.command;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
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.PluginConfig;
import pro.cloudnode.smp.cloudnodemsg.error.NoPermissionError;
import pro.cloudnode.smp.cloudnodemsg.error.NotInTeamError;
import pro.cloudnode.smp.cloudnodemsg.error.NotPlayerError;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class TeamMessageCommand extends Command {
@Override
public boolean run(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String @NotNull [] args) {
if (!sender.hasPermission(Permissions.USE_TEAM)) return new NoPermissionError().send(sender);
if (!(sender instanceof final @NotNull Player player)) return new NotPlayerError().send(sender);
final @NotNull Optional<@NotNull Team> team = Optional.ofNullable(player.getScoreboard().getPlayerTeam(player));
if (team.isEmpty()) return new NotInTeamError().send(player);
if (args.length == 0) {
if (Message.hasTeamChannel(player)) {
Message.exitTeamChannel(player);
return sendMessage(player, CloudnodeMSG.getInstance().config()
.channelTeamClosed(player.getName(), team.get()));
}
else {
Message.createTeamChannel(player);
return sendMessage(player, CloudnodeMSG.getInstance().config()
.channelTeamCreated(player.getName(), team.get()));
}
}
return sendTeamMessage(player, team.get(), Component.text(String.join(" ", args)));
}
@Override
public @Nullable List<@NotNull String> onTabComplete(final @NotNull CommandSender sender, final org.bukkit.command.@NotNull Command command, final @NotNull String label, final @NotNull String @NotNull [] args) {
return new ArrayList<>();
}
/**
* Send message to online team members
*/
public static boolean sendTeamMessage(final @NotNull Player sender, final @NotNull Team team, final @NotNull Component message) {
CloudnodeMSG.getInstance()
.config()
.sound(PluginConfig.SoundEvent.TEAM_OUTGOING)
.ifPresent(sound -> sound.play(sender));
for (final @NotNull Player player : sender.getServer().getOnlinePlayers()) {
if (!sender.getUniqueId().equals(player.getUniqueId())) {
CloudnodeMSG.getInstance()
.config()
.sound(PluginConfig.SoundEvent.TEAM_INCOMING)
.ifPresent(sound -> sound.play(player));
}
if (Message.isIgnored(player, sender)) continue;
if (Optional.ofNullable(player.getScoreboard().getPlayerTeam(player)).map(t -> t.equals(team))
.orElse(false))
sendMessage(player, CloudnodeMSG.getInstance().config().team(sender.getName(), team, message));
else if (player.hasPermission(Permissions.SPY))
sendMessage(player, CloudnodeMSG.getInstance().config().teamSpy(sender.getName(), team, message));
}
sender.getServer().getConsoleSender()
.sendMessage(CloudnodeMSG.getInstance().config().teamSpy(sender.getName(), team, message));
return true;
}
}