1 package org.bukkit.plugin;
3 import java.io.InputStream;
6 import java.util.HashMap;
10 import org.bukkit.permissions.Permission;
11 import org.bukkit.permissions.PermissionDefault;
12 import org.yaml.snakeyaml.Yaml;
13 import org.yaml.snakeyaml.constructor.SafeConstructor;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
22 private static final Yaml yaml =
new Yaml(
new SafeConstructor());
23 private String name = null;
24 private String main = null;
25 private String classLoaderOf = null;
26 private List<String> depend = null;
27 private List<String> softDepend = null;
28 private List<String> loadBefore = null;
29 private String version = null;
30 private Map<String, Map<String, Object>> commands = null;
31 private String description = null;
32 private List<String> authors = null;
33 private String website = null;
34 private String prefix = null;
35 private boolean database =
false;
37 private List<Permission> permissions = null;
38 private Map<?, ?> lazyPermissions = null;
42 loadMap((Map<?, ?>) yaml.load(stream));
52 loadMap((Map<?, ?>) yaml.load(reader));
64 version = pluginVersion;
73 public void save(Writer writer) {
74 yaml.dump(saveMap(), writer);
101 return name +
" v" + version;
159 this.database = database;
163 if (permissions == null) {
164 if (lazyPermissions == null) {
167 permissions = ImmutableList.copyOf(
Permission.
loadPermissions(lazyPermissions,
"Permission node '%s' in plugin description file for " +
getFullName() +
" is invalid", defaultPerm));
168 lazyPermissions = null;
179 return classLoaderOf;
188 name = map.get(
"name").toString();
190 if (!name.matches(
"^[A-Za-z0-9 _.-]+$")) {
193 }
catch (NullPointerException ex) {
194 throw new InvalidDescriptionException(ex,
"name is not defined");
195 }
catch (ClassCastException ex) {
196 throw new InvalidDescriptionException(ex,
"name is of wrong type");
200 version = map.get(
"version").toString();
201 }
catch (NullPointerException ex) {
202 throw new InvalidDescriptionException(ex,
"version is not defined");
203 }
catch (ClassCastException ex) {
204 throw new InvalidDescriptionException(ex,
"version is of wrong type");
208 main = map.get(
"main").toString();
209 if (main.startsWith(
"org.bukkit.")) {
210 throw new InvalidDescriptionException(
"main may not be within the org.bukkit namespace");
212 }
catch (NullPointerException ex) {
213 throw new InvalidDescriptionException(ex,
"main is not defined");
214 }
catch (ClassCastException ex) {
215 throw new InvalidDescriptionException(ex,
"main is of wrong type");
218 if (map.get(
"commands") != null) {
219 ImmutableMap.Builder<String, Map<String, Object>> commandsBuilder = ImmutableMap.<String, Map<String, Object>>builder();
221 for (Map.Entry<?, ?> command : ((Map<?, ?>) map.get(
"commands")).entrySet()) {
222 ImmutableMap.Builder<String, Object> commandBuilder = ImmutableMap.<String, Object>builder();
223 if (command.getValue() != null) {
224 for (Map.Entry<?, ?> commandEntry : ((Map<?, ?>) command.getValue()).entrySet()) {
225 if (commandEntry.getValue() instanceof Iterable) {
227 ImmutableList.Builder<Object> commandSubList = ImmutableList.<Object>builder();
228 for (Object commandSubListItem : (Iterable<?>) commandEntry.getValue()) {
229 if (commandSubListItem != null) {
230 commandSubList.add(commandSubListItem);
233 commandBuilder.put(commandEntry.getKey().toString(), commandSubList.build());
234 }
else if (commandEntry.getValue() != null) {
235 commandBuilder.put(commandEntry.getKey().toString(), commandEntry.getValue());
239 commandsBuilder.put(command.getKey().toString(), commandBuilder.build());
241 }
catch (ClassCastException ex) {
242 throw new InvalidDescriptionException(ex,
"commands are of wrong type");
244 commands = commandsBuilder.build();
247 if (map.get(
"class-loader-of") != null) {
248 classLoaderOf = map.get(
"class-loader-of").toString();
251 if (map.get(
"depend") != null) {
252 ImmutableList.Builder<String> dependBuilder = ImmutableList.<String>builder();
254 for (Object dependency : (Iterable<?>) map.get(
"depend")) {
255 dependBuilder.add(dependency.toString());
257 }
catch (ClassCastException ex) {
258 throw new InvalidDescriptionException(ex,
"depend is of wrong type");
259 }
catch (NullPointerException e) {
260 throw new InvalidDescriptionException(e,
"invalid dependency format");
262 depend = dependBuilder.build();
265 if (map.get(
"softdepend") != null) {
266 ImmutableList.Builder<String> softDependBuilder = ImmutableList.<String>builder();
268 for (Object dependency : (Iterable<?>) map.get(
"softdepend")) {
269 softDependBuilder.add(dependency.toString());
271 }
catch (ClassCastException ex) {
272 throw new InvalidDescriptionException(ex,
"softdepend is of wrong type");
273 }
catch (NullPointerException ex) {
274 throw new InvalidDescriptionException(ex,
"invalid soft-dependency format");
276 softDepend = softDependBuilder.build();
279 if (map.get(
"loadbefore") != null) {
280 ImmutableList.Builder<String> loadBeforeBuilder = ImmutableList.<String>builder();
282 for (Object predependency : (Iterable<?>) map.get(
"loadbefore")) {
283 loadBeforeBuilder.add(predependency.toString());
285 }
catch (ClassCastException ex) {
286 throw new InvalidDescriptionException(ex,
"loadbefore is of wrong type");
287 }
catch (NullPointerException ex) {
288 throw new InvalidDescriptionException(ex,
"invalid load-before format");
290 loadBefore = loadBeforeBuilder.build();
293 if (map.get(
"database") != null) {
295 database = (Boolean) map.get(
"database");
296 }
catch (ClassCastException ex) {
297 throw new InvalidDescriptionException(ex,
"database is of wrong type");
301 if (map.get(
"website") != null) {
302 website = map.get(
"website").toString();
305 if (map.get(
"description") != null) {
306 description = map.get(
"description").toString();
309 if (map.get(
"load") != null) {
311 order = PluginLoadOrder.valueOf(((String) map.get(
"load")).toUpperCase().replaceAll(
"\\W",
""));
312 }
catch (ClassCastException ex) {
313 throw new InvalidDescriptionException(ex,
"load is of wrong type");
314 }
catch (IllegalArgumentException ex) {
315 throw new InvalidDescriptionException(ex,
"load is not a valid choice");
319 if (map.get(
"authors") != null) {
320 ImmutableList.Builder<String> authorsBuilder = ImmutableList.<String>builder();
321 if (map.get(
"author") != null) {
322 authorsBuilder.add(map.get(
"author").toString());
325 for (Object o : (Iterable<?>) map.get(
"authors")) {
326 authorsBuilder.add(o.toString());
328 }
catch (ClassCastException ex) {
329 throw new InvalidDescriptionException(ex,
"authors are of wrong type");
330 }
catch (NullPointerException ex) {
331 throw new InvalidDescriptionException(ex,
"authors are improperly defined");
333 authors = authorsBuilder.build();
334 }
else if (map.get(
"author") != null) {
335 authors = ImmutableList.of(map.get(
"author").toString());
337 authors = ImmutableList.<String>of();
340 if (map.get(
"default-permission") != null) {
342 defaultPerm = PermissionDefault.
getByName(map.get(
"default-permission").toString());
343 }
catch (ClassCastException ex) {
344 throw new InvalidDescriptionException(ex,
"default-permission is of wrong type");
345 }
catch (IllegalArgumentException ex) {
346 throw new InvalidDescriptionException(ex,
"default-permission is not a valid choice");
351 lazyPermissions = (Map<?, ?>) map.get(
"permissions");
352 }
catch (ClassCastException ex) {
353 throw new InvalidDescriptionException(ex,
"permissions are of the wrong type");
356 if (map.get(
"prefix") != null) {
357 prefix = map.get(
"prefix").toString();
361 private Map<String, Object> saveMap() {
362 Map<String, Object> map =
new HashMap<String, Object>();
364 map.put(
"name", name);
365 map.put(
"main", main);
366 map.put(
"version", version);
367 map.put(
"database", database);
368 map.put(
"order", order.toString());
369 map.put(
"default-permission", defaultPerm.
toString());
371 if (commands != null) {
372 map.put(
"command", commands);
374 if (depend != null) {
375 map.put(
"depend", depend);
377 if (softDepend != null) {
378 map.put(
"softdepend", softDepend);
380 if (website != null) {
381 map.put(
"website", website);
383 if (description != null) {
384 map.put(
"description", description);
387 if (authors.size() == 1) {
388 map.put(
"author", authors.get(0));
389 }
else if (authors.size() > 1) {
390 map.put(
"authors", authors);
393 if (classLoaderOf != null) {
394 map.put(
"class-loader-of", classLoaderOf);
397 if (prefix != null) {
398 map.put(
"prefix", prefix);