/
DenizenCommand.java
211 lines (199 loc) · 8.5 KB
/
DenizenCommand.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.denizenscript.denizen.utilities.command.scripted;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.CommandScriptContainer;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.tags.TagManager;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.ChatColor;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.CommandMinecart;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DenizenCommand extends Command {
private CommandScriptContainer script;
public DenizenCommand(CommandScriptContainer script) {
super(script.getCommandName(), script.getDescription(), script.getUsage(), script.getAliases());
String permission = script.getPermission();
if (permission != null && !permission.equals("")) {
this.setPermission(permission);
String permissionMessage = script.getPermissionMessage();
if (permissionMessage != null && !permissionMessage.equals("")) {
this.setPermissionMessage(permissionMessage);
}
}
this.script = script;
}
public boolean canSeeHelp(CommandSender commandSender) {
if (!script.hasAllowedHelpProcedure()) {
return true;
}
if (!testPermissionSilent(commandSender)) {
return false;
}
Map<String, ObjectTag> context = new HashMap<>();
PlayerTag player = null;
NPCTag npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (!EntityTag.isNPC(pl)) {
player = PlayerTag.mirrorBukkitPlayer(pl);
}
context.put("server", new ElementTag(false));
}
else {
context.put("server", new ElementTag(true));
}
return script.runAllowedHelpProcedure(player, npc, context);
}
@Override
public boolean testPermission(CommandSender target) {
if (testPermissionSilent(target)) {
return true;
}
String permissionMessage = getPermissionMessage();
if (permissionMessage == null) {
target.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.");
}
else if (permissionMessage.length() != 0) {
PlayerTag player = null;
NPCTag npc = null;
if (target instanceof Player) {
Player pl = (Player) target;
if (EntityTag.isCitizensNPC(pl)) {
npc = NPCTag.fromEntity(pl);
}
else {
player = PlayerTag.mirrorBukkitPlayer(pl);
}
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(target);
if (citizen != null) {
npc = new NPCTag(citizen);
}
}
// <permission> is built into Bukkit... let's keep it here
for (String line : TagManager.tag(permissionMessage.replace("<permission>", getPermission()),
new BukkitTagContext(player, npc, null, false, new ScriptTag(script))).split("\n")) {
target.sendMessage(line);
}
}
return false;
}
@Override
public boolean execute(CommandSender commandSender, String commandLabel, String[] arguments) {
if (!testPermission(commandSender)) {
return true;
}
Map<String, ObjectTag> context = new HashMap<>();
String raw_args = "";
if (arguments.length > 0) {
StringBuilder rawArgsBuilder = new StringBuilder();
for (String arg : arguments) {
rawArgsBuilder.append(arg).append(' ');
}
raw_args = rawArgsBuilder.substring(0, rawArgsBuilder.length() - 1);
}
List<String> args = Arrays.asList(ArgumentHelper.buildArgs(raw_args));
context.put("args", new ListTag(args));
context.put("raw_args", new ElementTag(raw_args));
context.put("alias", new ElementTag(commandLabel));
PlayerTag player = null;
NPCTag npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (EntityTag.isCitizensNPC(pl)) {
npc = NPCTag.fromEntity(pl);
}
else {
player = PlayerTag.mirrorBukkitPlayer(pl);
}
context.put("server", new ElementTag(false));
context.put("source_type", new ElementTag("player"));
}
else {
if (commandSender instanceof BlockCommandSender) {
context.put("command_block_location", new LocationTag(((BlockCommandSender) commandSender).getBlock().getLocation()));
context.put("server", new ElementTag(false));
context.put("source_type", new ElementTag("command_block"));
}
else if (commandSender instanceof CommandMinecart) {
context.put("command_minecart", new EntityTag((CommandMinecart) commandSender));
context.put("server", new ElementTag(false));
context.put("source_type", new ElementTag("command_minecart"));
}
else {
context.put("server", new ElementTag(true));
context.put("source_type", new ElementTag("server"));
}
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
if (citizen != null) {
npc = new NPCTag(citizen);
}
}
script.runCommandScript(player, npc, context);
return true;
}
@Override
public boolean isRegistered() {
return true;
}
@Override
public List<String> tabComplete(CommandSender commandSender, String alias, String[] arguments) {
if (!script.hasTabCompleteProcedure()) {
return super.tabComplete(commandSender, alias, arguments);
}
Map<String, ObjectTag> context = new HashMap<>();
String raw_args = "";
if (arguments.length > 0) {
StringBuilder rawArgsBuilder = new StringBuilder();
for (String arg : arguments) {
rawArgsBuilder.append(arg).append(' ');
}
raw_args = rawArgsBuilder.substring(0, rawArgsBuilder.length() - 1);
}
List<String> args = Arrays.asList(ArgumentHelper.buildArgs(raw_args));
context.put("args", new ListTag(args));
context.put("raw_args", new ElementTag(raw_args));
context.put("alias", new ElementTag(alias));
PlayerTag player = null;
NPCTag npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (EntityTag.isCitizensNPC(pl)) {
npc = NPCTag.fromEntity(pl);
}
else {
player = PlayerTag.mirrorBukkitPlayer(pl);
}
context.put("server", new ElementTag(false));
}
else {
context.put("server", new ElementTag(true));
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
if (citizen != null) {
npc = new NPCTag(citizen);
}
}
return script.runTabCompleteProcedure(player, npc, context);
}
}