/
CommandScriptContainer.java
193 lines (175 loc) · 8.86 KB
/
CommandScriptContainer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package net.aufdemrand.denizen.scripts.containers.core;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenCommand;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import java.util.*;
public class CommandScriptContainer extends ScriptContainer {
// <--[language]
// @name Command Script Containers
// @group Script Container System
// @description
// Command script containers allow you to register commands with CraftBukkit via magic.
// This, in turn, allows the command to show up in the '/help' command, with some info
// on the command.
//
// Note that existing names or aliases from other plugins will be overridden.
// If you want to run a script at the same time as an existing command, see <@link example on command event tutorial>.
//
// The following is the format for the container.
// Note that everything is optional except for "name" and "script".
//
//
// <code>
// # The name of the script doesn't matter, and will not affect the command in any way.
// Command Script Name:
//
// type: command
//
// # The name of the command. This will show up in the default list in the '/help' command
// # and will be the default method for running the command.
// name: mycmd
//
// # The description of the command. This will be shown in the '/help' command.
// # Multiple lines are acceptable, via <&nl> (the tag for a new line), but you should
// # make the first line a brief summary of what your command does.
// description: My command.
//
// # Correct usage for the command. This will show in the '/help' command.
// usage: /mycmd <<>myArg1<>>
//
// # A list of aliases for the command. These will show in the '/help' command, and
// # are alternatives to the default name.
// aliases:
// - myalias
// - mycommand
//
// # The permission node to check for permissions plugins. This will automatically
// # block players without the permission from accessing the command and help for
// # the command.
// permission: my.permission.node
//
// # The message to send to the player when they try to use the command without
// # permission. If this is not specified, the default Bukkit message will be sent.
// permission message: Sorry, <player.name>, you can't use my command because you don't have the permission '<permission>'!
//
// # The procedure-based script that will be checked when a player or the console
// # is trying to view help for this command. This must always be determined true
// # or false. If there is no script, it's assumed that all players and the console
// # should be allowed to view the help for this command.
// # Available context: <context.server> returns whether the server is viewing the help (a player if false).
// allowed help:
// - determine <player.is_op||<context.server>>
//
// # The procedure-based script that will run when a player uses tab completion to
// # predict words. This should return a dList of words that the player can tab through,
// # based on the arguments they have already typed. Leaving this node out will result
// # in using Bukkit's built-in tab completion.
// # Available context: <context.args> returns a list of input arguments.
// # <context.raw_args> returns all the arguments as raw text.
// # <context.server> returns whether the server is using tab completion (a player if false).
// # <context.alias> returns the command alias being used.
// tab complete:
// - if !<player.is_op||<context.server>> queue clear
// - determine <server.list_online_players.parse[name].include[pizza|potato|anchovy].filter[starts_with[<context.args.last>]]>
//
// # The script that will run when the command is executed.
// # No, you do not need '- determine fulfilled' or anything of the sort, since
// # the command is fully registered.
// # Available context: <context.args> returns a list of input arguments.
// # <context.raw_args> returns all the arguments as raw text.
// # <context.server> returns whether the server is running the command (a player if false).
// # <context.alias> returns the command alias being used.
// script:
// - if !<player.is_op||<context.server>> {
// - narrate "<red>You do not have permission for that command."
// - queue clear
// }
// - narrate "Yay!"
// - narrate "My command worked!"
// - narrate "And I typed '<context.raw_args>'!"
// </code>
//
// -->
public CommandScriptContainer(YamlConfiguration configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
CommandScriptHelper.registerDenizenCommand(new DenizenCommand(this));
}
public String getCommandName() {
return getString("NAME", null).toLowerCase();
}
public String getDescription() {
// Replace new lines with a space and a new line, to allow full brief descriptions in /help.
// Without this, "line<n>line"s brief description would be "lin", because who doesn't like
// random cutoff-
return TagManager.tag(null, null, getString("DESCRIPTION", "")).replace("\n", " \n");
}
public String getUsage() {
return TagManager.tag(null, null, getString("USAGE", ""));
}
public List<String> getAliases() {
List<String> aliases = getStringList("ALIASES");
return aliases != null? aliases : new ArrayList<String>();
}
public String getPermission() {
return getString("PERMISSION");
}
public String getPermissionMessage() {
return getString("PERMISSION MESSAGE");
}
public ScriptQueue runCommandScript(dPlayer player, dNPC npc, Map<String, dObject> context) {
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).addEntries(getBaseEntries(
new BukkitScriptEntryData(player, npc)));
if (context != null) {
for (Map.Entry<String, dObject> entry : context.entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
}
queue.start();
return queue;
}
public boolean runAllowedHelpProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
// Add the reqId to each of the entries for the determine command
List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "ALLOWED HELP");
long id = DetermineCommand.getNewId();
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).setReqId(id).addEntries(entries);
if (context != null) {
for (Map.Entry<String, dObject> entry : context.entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
}
queue.start();
return DetermineCommand.hasOutcome(id) && DetermineCommand.getOutcome(id).get(0).equalsIgnoreCase("true");
}
public List<String> runTabCompleteProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
// Add the reqId to each of the entries for the determine command
List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "TAB COMPLETE");
long id = DetermineCommand.getNewId();
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).setReqId(id).addEntries(entries);
if (context != null) {
for (Map.Entry<String, dObject> entry : context.entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
}
queue.start();
if (DetermineCommand.hasOutcome(id))
return dList.valueOf(DetermineCommand.getOutcome(id).get(0));
else
return new ArrayList<String>();
}
public boolean hasAllowedHelpProcedure() {
return contains("ALLOWED HELP");
}
public boolean hasTabCompleteProcedure() {
return contains("TAB COMPLETE");
}
}