/
DenizenCommand.java
162 lines (150 loc) · 6.65 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
package net.aufdemrand.denizen.utilities;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.containers.core.CommandScriptContainer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.*;
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, dObject> context = new HashMap<String, dObject>();
dPlayer player = null;
dNPC npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (Depends.citizens == null || !CitizensAPI.getNPCRegistry().isNPC(pl))
player = dPlayer.mirrorBukkitPlayer(pl);
context.put("server", Element.FALSE);
}
else {
context.put("server", Element.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) {
dPlayer player = null;
dNPC npc = null;
if (target instanceof Player) {
Player pl = (Player) target;
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(pl))
npc = dNPC.fromEntity(pl);
else
player = dPlayer.mirrorBukkitPlayer(pl);
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(target);
if (citizen != null)
npc = dNPC.mirrorCitizensNPC(citizen);
}
// <permission> is built into Bukkit... let's keep it here
for (String line : TagManager.tag(player, npc, permissionMessage.replace("<permission>", getPermission()))
.split("\n")) {
target.sendMessage(line);
}
}
return false;
}
@Override
public boolean execute(CommandSender commandSender, String commandLabel, String[] arguments) {
Map<String, dObject> context = new HashMap<String, dObject>();
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(aH.buildArgs(raw_args));
context.put("args", new dList(args));
context.put("raw_args", new Element(raw_args));
dPlayer player = null;
dNPC npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(pl))
npc = dNPC.fromEntity(pl);
else
player = dPlayer.mirrorBukkitPlayer(pl);
context.put("server", Element.FALSE);
}
else {
context.put("server", Element.TRUE);
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
if (citizen != null)
npc = dNPC.mirrorCitizensNPC(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, dObject> context = new HashMap<String, dObject>();
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(aH.buildArgs(raw_args));
context.put("args", new dList(args));
context.put("raw_args", new Element(raw_args));
context.put("alias", new Element(alias));
dPlayer player = null;
dNPC npc = null;
if (commandSender instanceof Player) {
Player pl = (Player) commandSender;
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(pl))
npc = dNPC.fromEntity(pl);
else
player = dPlayer.mirrorBukkitPlayer(pl);
context.put("server", Element.FALSE);
}
else {
context.put("server", Element.TRUE);
}
if (Depends.citizens != null && npc == null) {
NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
if (citizen != null)
npc = dNPC.mirrorCitizensNPC(citizen);
}
return script.runTabCompleteProcedure(player, npc, context);
}
}