-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ActionBarCommand.java
93 lines (75 loc) · 3.78 KB
/
ActionBarCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
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 java.util.Arrays;
import java.util.List;
public class ActionBarCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (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);
}
if (arg.matchesPrefix("targets", "target")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class, scriptEntry));
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new Element(TagManager.cleanOutputFully(arg.raw_value)));
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}
if (!scriptEntry.hasObject("targets") && !((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
throw new InvalidArgumentsException("Must specify target(s).");
}
if (!scriptEntry.hasObject("targets")) {
BukkitScriptEntryData data = (BukkitScriptEntryData) scriptEntry.entryData;
if (!data.hasPlayer()) {
throw new InvalidArgumentsException("Must specify valid player Targets!");
}
else {
scriptEntry.addObject("targets",
Arrays.asList(data.getPlayer()));
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element text = scriptEntry.getElement("text");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), text.debug() + aH.debugList("Targets", targets));
}
if (format != null) {
text = new Element(format.getFormattedText(scriptEntry));
}
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
NMSHandler.getInstance().getPacketHelper().sendActionBarMessage(player.getPlayerEntity(), text.asString());
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Tried to send action bar message to non-existent or offline player!");
}
}
}
}