/
NarrateCommand.java
100 lines (83 loc) · 4.15 KB
/
NarrateCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.tags.TagManager;
import org.bukkit.Bukkit;
import java.util.Arrays;
import java.util.List;
public class NarrateCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
if (scriptEntry.getArguments().size() > 4) { // TODO: Use this more often!
throw new InvalidArgumentsException("Too many arguments! Did you forget a 'quote'?");
}
// Iterate through arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("format")
&& arg.matchesPrefix("format", "f")) {
String formatStr = arg.getValue();
FormatScriptContainer format = ScriptRegistry.getScriptContainer(formatStr);
if (format == null) {
dB.echoError("Could not find format script matching '" + formatStr + '\'');
}
scriptEntry.addObject("format", format);
}
// Add players to target list
else if (!scriptEntry.hasObject("targets")
&& arg.matchesPrefix("target", "targets", "t")) {
scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class));
}
// Use raw_value as to not accidentally strip a value before any :'s.
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new Element(TagManager.cleanOutputFully(arg.raw_value)));
}
else {
arg.reportUnhandled();
}
}
// If there are no targets, check if you can add this player
// to the targets
if (!scriptEntry.hasObject("targets")) {
scriptEntry.addObject("targets",
(((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()) : null));
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Missing any text!");
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
String text = scriptEntry.getElement("text").asString();
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
// Report to dB
dB.report(scriptEntry, getName(),
aH.debugObj("Narrating", text)
+ aH.debugList("Targets", targets)
+ (format != null ? aH.debugObj("Format", format.getName()) : ""));
if (targets == null) {
Bukkit.getServer().getConsoleSender().sendMessage(format != null ? format.getFormattedText(scriptEntry) : text);
return;
}
for (dPlayer player : targets) {
if (player != null && player.isOnline()) {
player.getPlayerEntity().sendMessage(format != null ? format.getFormattedText(scriptEntry) : text);
}
else {
dB.echoError("Narrated to non-existent or offline player!");
}
}
}
}