/
AnnounceCommand.java
107 lines (89 loc) · 4.06 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
/**
* <p>Announces a message to the server.</p>
*
* <b>dScript Usage:</b><br>
* <pre>ANNOUNCE ['message to announce']</pre>
*
* <ol><tt>Arguments: [] - Required</ol></tt>
*
* <ol><tt>['message to announce']</tt><br>
* The message to send to the server. This will be seen by all Players.</ol>
*
*
* <br><b>Example Usage:</b><br>
* <ol><tt>
* - ANNOUNCE 'Today is Christmas!' <br>
* - ANNOUNCE "<PLAYER.NAME> has completed '<FLAG.P:currentQuest>'!" <br>
* - ANNOUNCE "<GOLD>$$$ <WHITE>- Make some quick cash at our <RED>MINEA-SINO<WHITE>!"
* </ol></tt>
*
* @author Jeremy Schroeder
*
*/
public class AnnounceCommand extends AbstractCommand {
enum AnnounceType { ALL, TO_OPS }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize fields
String text = null;
AnnounceType announceType = AnnounceType.ALL;
FormatScriptContainer format = null;
// 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(Messages.ERROR_LOTS_OF_ARGUMENTS);
// Should only be one argument, since PlAYER: and NPCID: are handled
// internally. Let's get that argument and set it as the text.
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesArg("TO_OPS", arg)) {
announceType = AnnounceType.TO_OPS;
} else if (aH.matchesArg("FORMAT", arg)) {
String formatStr = aH.getStringFrom(arg);
format = ScriptRegistry.getScriptContainerAs(formatStr, FormatScriptContainer.class);
if(format != null) dB.echoDebug("... format set to: " + formatStr);
else dB.echoError("... could not find format for: " + formatStr);
}
else text = aH.getStringFrom(arg);
}
// If text is missing, alert the console.
if (text == null)
throw new InvalidArgumentsException(Messages.ERROR_NO_TEXT);
// Add objects that need to be passed to execute() to the scriptEntry
scriptEntry.addObject("text", text);
scriptEntry.addObject("type", announceType);
scriptEntry.addObject("format", format);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
String text = (String) scriptEntry.getObject("text");
AnnounceType type = (AnnounceType) scriptEntry.getObject("type");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
// Report to dB
dB.report(getName(),
aH.debugObj("Message", text)
+ (format != null ? aH.debugObj("Format", format.getName()) : "")
+ aH.debugObj("Type", type.name()));
String message = format != null ? format.getFormattedText(scriptEntry) : text;
// Use Bukkit to broadcast the message to everybody in the server.
if (type == AnnounceType.ALL) {
denizen.getServer().broadcastMessage(message);
} else if (type == AnnounceType.TO_OPS) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.isOp()) player.sendMessage(message);
}
}
}
}