/
CommandBook.java
274 lines (231 loc) · 10.1 KB
/
CommandBook.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// $Id$
/*
* CommandBook
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.commandbook;
import com.google.common.base.Joiner;
import com.sk89q.commandbook.component.session.SessionComponent;
import com.sk89q.commandbook.config.LegacyCommandBookConfigurationMigrator;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.InjectComponentAnnotationHandler;
import com.zachsthings.libcomponents.bukkit.BasePlugin;
import com.zachsthings.libcomponents.bukkit.DefaultsFileYAMLProcessor;
import com.zachsthings.libcomponents.bukkit.YAMLNodeConfigurationNode;
import com.zachsthings.libcomponents.bukkit.YAMLProcessorConfigurationFile;
import com.zachsthings.libcomponents.config.ConfigurationFile;
import com.zachsthings.libcomponents.loader.ClassLoaderComponentLoader;
import com.zachsthings.libcomponents.loader.ConfigListedComponentLoader;
import com.zachsthings.libcomponents.loader.JarFilesComponentLoader;
import com.zachsthings.libcomponents.loader.StaticComponentLoader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.enginehub.piston.CommandManager;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Base plugin class for CommandBook.
*
* @author sk89q
*/
public final class CommandBook extends BasePlugin {
private static CommandBook instance;
private PlatformCommandManager commandManager = new PlatformCommandManager(this);
private Map<String, Integer> itemNames;
public boolean broadcastChanges;
public boolean useDisplayNames;
public boolean lookupWithDisplayNames;
public boolean crappyWrapperCompat;
public CommandBook() {
super();
instance = this;
}
public static CommandBook inst() {
return instance;
}
public static Logger logger() {
return inst().getLogger();
}
public static void registerEvents(Listener listener) {
server().getPluginManager().registerEvents(listener, inst());
}
public static void registerComponentCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
inst().commandManager.registerComponentCommands(op);
}
private void publishPistonCommands() {
commandManager.registerCommandsWith(this);
}
@Override
public void onEnable() {
super.onEnable();
publishPistonCommands();
}
public void registerComponentLoaders() {
// -- Component loaders
final File configDir = new File(getDataFolder(), "config/");
componentManager.addComponentLoader(new StaticComponentLoader(getLogger(), configDir, new SessionComponent()) {
@Override
public ConfigurationFile createConfigurationNode(File file) {
return new YAMLProcessorConfigurationFile(new YAMLProcessor(file, true, YAMLFormat.EXTENDED));
}
});
final YAMLProcessor jarComponentAliases = new DefaultsFileYAMLProcessor("components.yml", false);
try {
jarComponentAliases.load();
} catch (IOException e) {
getLogger().severe("Error loading component aliases!");
e.printStackTrace();
} catch (YAMLException e) {
getLogger().severe("Error loading component aliases!");
e.printStackTrace();
}
componentManager.addComponentLoader(new ConfigListedComponentLoader(getLogger(),
new YAMLNodeConfigurationNode(config),
new YAMLNodeConfigurationNode(jarComponentAliases), configDir));
for (String dir : config.getStringList("component-class-dirs", Arrays.asList("component-classes"))) {
final File classesDir = new File(getDataFolder(), dir);
if (!classesDir.exists() || !classesDir.isDirectory()) {
classesDir.mkdirs();
}
componentManager.addComponentLoader(new ClassLoaderComponentLoader(getLogger(), classesDir, configDir) {
@Override
public ConfigurationFile createConfigurationNode(File file) {
return new YAMLProcessorConfigurationFile(new YAMLProcessor(file, true, YAMLFormat.EXTENDED));
}
});
}
for (String dir : config.getStringList("component-jar-dirs", Arrays.asList("component-jars"))) {
final File classesDir = new File(getDataFolder(), dir);
if (!classesDir.exists() || !classesDir.isDirectory()) {
classesDir.mkdirs();
}
componentManager.addComponentLoader(new JarFilesComponentLoader(getLogger(), classesDir, configDir) {
@Override
public ConfigurationFile createConfigurationNode(File file) {
return new YAMLProcessorConfigurationFile(new YAMLProcessor(file, true, YAMLFormat.EXTENDED));
}
});
}
// -- Annotation handlers
componentManager.registerAnnotationHandler(InjectComponent.class, new InjectComponentAnnotationHandler(componentManager));
}
// FIXME: Backport to WorldEdit/common lib
private String rebuildArguments(String commandLabel, String[] args) {
int plSep = commandLabel.indexOf(":");
if (plSep >= 0 && plSep < commandLabel.length() + 1) {
commandLabel = commandLabel.substring(plSep + 1);
}
StringBuilder sb = new StringBuilder("/").append(commandLabel);
if (args.length > 0) {
sb.append(" ");
}
return Joiner.on(" ").appendTo(sb, args).toString();
}
/**
* Called on a command.
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
commandManager.handleCommand(sender, rebuildArguments(commandLabel, args));
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String commandLabel, String[] args) {
String arguments = rebuildArguments(commandLabel, args);
return CommandUtil.fixSuggestions(arguments, commandManager.handleCommandSuggestion(sender, arguments));
}
/**
* Loads the configuration.
*/
@Override
public YAMLProcessor createConfiguration() {
final File configFile = new File(getDataFolder(), "config.yml");
YAMLProcessor config = new YAMLProcessor(configFile, true, YAMLFormat.EXTENDED);
YAMLProcessor comments = new DefaultsFileYAMLProcessor("config-comments.yml", false);
try {
if (!configFile.exists()) {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
}
config.load();
comments.load();
} catch (Exception e) {
getLogger().log(Level.WARNING, "Error loading configuration: ", e);
}
for (Map.Entry<String, Object> e : comments.getMap().entrySet()) {
if (e.getValue() != null) {
config.setComment(e.getKey(), e.getValue().toString());
}
}
// Migrate the old configuration, if we need to
final String result = new LegacyCommandBookConfigurationMigrator(configFile, config).migrate();
if (result != null) {
logger().severe("Error migrating CommandBook configuration: " + result);
}
return config;
}
@Override
public void populateConfiguration(YAMLProcessor config) {
loadItemList(config);
useDisplayNames = config.getBoolean("use-display-names", true);
lookupWithDisplayNames = config.getBoolean("lookup-with-display-names", true);
broadcastChanges = config.getBoolean("broadcast-changes", true);
crappyWrapperCompat = config.getBoolean("crappy-wrapper-compat", true);
if (crappyWrapperCompat) {
getLogger().info("Maximum wrapper compatibility is enabled. " +
"Some features have been disabled to be compatible with " +
"poorly written server wrappers.");
}
}
/**
* Loads the item list.
*
* @param config The {@link YAMLProcessor} to load from
*/
protected void loadItemList(YAMLProcessor config) {
// Load item names aliases list
Object itemNamesTemp = config.getProperty("item-names");
if (itemNamesTemp != null && itemNamesTemp instanceof Map) {
itemNames = new HashMap<String, Integer>();
try {
Map<?, ?> temp = (Map<?, ?>) itemNamesTemp;
for (Map.Entry<?, ?> entry : temp.entrySet()) {
String name = entry.getKey().toString().toLowerCase();
// Check if the item ID is a number
if (entry.getValue() instanceof Integer) {
itemNames.put(name, (Integer) entry.getValue());
}
}
} catch (ClassCastException ignore) {
}
} else {
itemNames = new HashMap<String, Integer>();
}
}
public Map<String, Integer> getItemNames() {
return Collections.unmodifiableMap(itemNames);
}
}