/
ActionBarCommand.java
122 lines (103 loc) · 4.9 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
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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.nms.NMSHandler;
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.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.tags.TagManager;
import java.util.Arrays;
import java.util.List;
public class ActionBarCommand extends AbstractCommand {
// <--[command]
// @Name ActionBar
// @Syntax actionbar [<text>] (targets:<player>|...) (format:<name>)
// @Required 1
// @Short Sends a message to a player's action bar.
// @group player
//
// @Description
// Sends a message to the target's action bar area. If no target is specified it will default to the attached
// player. Accepts the 'format:<name>' argument, which will reformat the text according to the specified
// format script.
//
// @Tags
// None
//
// @Usage
// Use to send a message to the player's action bar.
// - actionbar "Hey there <player.name>!"
//
// @Usage
// Use to send a message to a list of players.
// - actionbar "Hey, welcome to the server!" targets:<[thatplayer]>|<[player]>|<[someplayer]>
//
// @Usage
// Use to send a message to a list of players, with a formatted message.
// - actionbar "Hey there!" targets:<[thatplayer]>|<[player]> format:ServerChat
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("format", "f")) {
String formatStr = arg.getValue();
FormatScriptContainer format = ScriptRegistry.getScriptContainer(formatStr);
if (format == null) {
Debug.echoError("Could not find format script matching '" + formatStr + '\'');
}
scriptEntry.addObject("format", format);
}
if (arg.matchesPrefix("targets", "target")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new ElementTag(TagManager.cleanOutputFully(arg.raw_value)));
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}
if (!scriptEntry.hasObject("targets") && !Utilities.entryHasPlayer(scriptEntry)) {
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) {
ElementTag text = scriptEntry.getElement("text");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), text.debug() + ArgumentHelper.debugList("Targets", targets));
}
if (format != null) {
text = new ElementTag(format.getFormattedText(text.asString(), scriptEntry));
}
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
NMSHandler.getPacketHelper().sendActionBarMessage(player.getPlayerEntity(), text.asString());
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Tried to send action bar message to non-existent or offline player!");
}
}
}
}