-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
AnnounceCommand.java
161 lines (143 loc) · 7.01 KB
/
AnnounceCommand.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.denizenscript.denizen.scripts.commands.server;
import com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class AnnounceCommand extends AbstractCommand {
public AnnounceCommand() {
setName("announce");
setSyntax("announce [<text>] (to_ops/to_console/to_flagged:<flag_name>) (format:<name>)");
setRequiredArguments(1, 3);
isProcedural = true;
}
// <--[command]
// @Name Announce
// @Syntax announce [<text>] (to_ops/to_console/to_flagged:<flag_name>) (format:<name>)
// @Required 1
// @Maximum 3
// @Short Announces a message for everyone online to read.
// @Group server
//
// @Description
// Announce sends a raw message to players.
// Simply using announce with text will send the message to all online players using the Spigot broadcast system.
// Specifying the 'to_ops' argument will narrow down the players in which the message is sent to ops only.
// Alternatively, using the 'to_flagged' argument will send the message to only players that have the specified flag.
// You can also use the 'to_console' argument to make it so it only shows in the server console.
//
// Announce can also utilize a format script with the 'format' argument. See <@link language Format Script Containers>.
// //
// Note that the default announce mode (that shows for all players) relies on the Spigot broadcast system, which requires the permission "bukkit.broadcast.user" to see broadcasts.
//
// @Tags
// None
//
// @Usage
// Use to send an important message to your players.
// - announce 'Warning! This server will restart in 5 minutes!'
//
// @Usage
// Use to send a message to a specific 'group' of players.
// - announce to_flagged:clan_subang '[<player.name>] Best clan ever!'
//
// @Usage
// Use to easily send a message to all online ops.
// - announce to_ops '<player.name> requires help!'
//
// @Usage
// Use to send a message to just the console (Primarily for debugging / logging).
// - announce to_console 'Warning- <player.name> broke a mob spawner at location <player.location>'
// -->
enum AnnounceType {ALL, TO_OPS, TO_FLAGGED, TO_CONSOLE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Users tend to forget quotes sometimes on commands like this, so
// let's check if there are more argument than usual.
if (scriptEntry.getArguments().size() > 3) {
throw new InvalidArgumentsException("Too many arguments! Did you forget a 'quote'?");
}
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("type")
&& arg.matches("to_ops")) {
scriptEntry.addObject("type", AnnounceType.TO_OPS);
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("to_console")) {
scriptEntry.addObject("type", AnnounceType.TO_CONSOLE);
}
else if (!scriptEntry.hasObject("type")
&& arg.matchesPrefix("to_flagged")) {
scriptEntry.addObject("type", AnnounceType.TO_FLAGGED);
scriptEntry.addObject("flag", arg.asElement());
}
else if (!scriptEntry.hasObject("format")
&& arg.matchesPrefix("format")) {
FormatScriptContainer format;
String formatStr = arg.getValue();
format = ScriptRegistry.getScriptContainer(formatStr);
if (format == null) {
Debug.echoError("Could not find format script matching '" + formatStr + '\'');
}
scriptEntry.addObject("format", format);
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new ElementTag(arg.raw_value));
}
}
// If text is missing, alert the console.
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Missing text argument!");
}
scriptEntry.defaultObject("type", AnnounceType.ALL);
}
@Override
public void execute(ScriptEntry scriptEntry) {
if (scriptEntry.getResidingQueue().procedural) {
Debug.echoError("'Announce' should not be used in a procedure script. Consider the 'debug' command instead.");
}
ElementTag text = scriptEntry.getElement("text");
AnnounceType type = (AnnounceType) scriptEntry.getObject("type");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
ElementTag flag = scriptEntry.getElement("flag");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugObj("Message", text)
+ (format != null ? ArgumentHelper.debugObj("Format", format.getName()) : "")
+ ArgumentHelper.debugObj("Type", type.name())
+ (flag != null ? ArgumentHelper.debugObj("Flag_Name", flag) : ""));
}
String message = format != null ? format.getFormattedText(text.asString(), scriptEntry) : text.asString();
// Use Bukkit to broadcast the message to everybody in the server.
if (type == AnnounceType.ALL) {
DenizenAPI.getCurrentInstance().getServer().spigot().broadcast(FormattedTextHelper.parse(message));
}
else if (type == AnnounceType.TO_OPS) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.isOp()) {
player.spigot().sendMessage(FormattedTextHelper.parse(message));
}
}
}
else if (type == AnnounceType.TO_FLAGGED) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (FlagManager.playerHasFlag(PlayerTag.mirrorBukkitPlayer(player), flag.asString())) {
player.spigot().sendMessage(FormattedTextHelper.parse(message));
}
}
}
else if (type == AnnounceType.TO_CONSOLE) {
Bukkit.getServer().getConsoleSender().sendMessage(message);
}
}
}