Skip to content

Commit

Permalink
Move YAML parsing to a PluginDescription subclass.
Browse files Browse the repository at this point in the history
Plugin interfaces may have other, more intuitive means of getting
metadata for plugins in the target language.

Because Commands are integral part of Bukkit, there's no need to
separate the regular YAML parsing and command parsing in classes.
A separate method will do.
  • Loading branch information
Stéphan Kochen committed Feb 25, 2011
1 parent 226f299 commit 6f1f99c
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 194 deletions.
55 changes: 0 additions & 55 deletions src/main/java/org/bukkit/command/PluginCommandYamlParser.java

This file was deleted.

148 changes: 10 additions & 138 deletions src/main/java/org/bukkit/plugin/PluginDescription.java
@@ -1,61 +1,20 @@
package org.bukkit.plugin;

import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

/**
* Encapsulates description and other metadata on a plugin.
*/
public final class PluginDescription {
private static final Yaml yaml = new Yaml(new SafeConstructor());
private String name = null;
private String main = null;
private String version = null;
private Object commands = null;
private String description = null;
private ArrayList<String> authors = new ArrayList<String>();
private String website = null;

@SuppressWarnings("unchecked")
public PluginDescription(final InputStream stream) throws InvalidDescriptionException {
loadMap((Map<String, Object>)yaml.load(stream));
}

/**
* Loads a PluginDescriptionFile from the specified reader
* @param reader
*/
@SuppressWarnings("unchecked")
public PluginDescription(final Reader reader) throws InvalidDescriptionException {
loadMap((Map<String, Object>)yaml.load(reader));
}

/**
* Creates a new PluginDescriptionFile with the given detailed
*
* @param pluginName Name of this plugin
* @param mainClass Full location of the main class of this plugin
*/
public PluginDescription(final String pluginName, final String pluginVersion, final String mainClass) {
name = pluginName;
version = pluginVersion;
main = mainClass;
}

/**
* Saves this PluginDescriptionFile to the given writer
*
* @param writer Writer to output this file to
*/
public void save(Writer writer) {
yaml.dump(saveMap(), writer);
public abstract class PluginDescription {
protected String name;
protected String main;
protected String version;
protected Object commands;
protected String description;
protected ArrayList<String> authors;
protected String website;

protected PluginDescription() {
}

/**
Expand Down Expand Up @@ -114,91 +73,4 @@ public ArrayList<String> getAuthors() {
public String getWebsite() {
return website;
}

private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
try {
name = map.get("name").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "name is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "name is of wrong type");
}

try {
version = map.get("version").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "version is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "version is of wrong type");
}

try {
main = map.get("main").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "main is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "main is of wrong type");
}

if (map.containsKey("commands")) {
try {
commands = map.get("commands");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "commands are of wrong type");
}
}

if (map.containsKey("website")) {
try {
website = (String)map.get("website");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "website is of wrong type");
}
}

if (map.containsKey("description")) {
try {
description = (String)map.get("description");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "description is of wrong type");
}
}

if (map.containsKey("author")) {
try {
String extra = (String)map.get("author");
authors.add(extra);
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "author is of wrong type");
}
}

if (map.containsKey("authors")) {
try {
ArrayList<String> extra = (ArrayList<String>)map.get("authors");
authors.addAll(extra);
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "authors are of wrong type");
}
}
}

private Map<String, Object> saveMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("main", main);
map.put("version", version);

if (commands != null) map.put("command", commands);
if (website != null) map.put("website", website);
if (description != null) map.put("description", description);

if (authors.size() == 1) {
map.put("author", authors.get(0));
} else if (authors.size() > 1) {
map.put("authors", authors);
}

return map;
}
}
171 changes: 171 additions & 0 deletions src/main/java/org/bukkit/plugin/YamlPluginDescription.java
@@ -0,0 +1,171 @@
package org.bukkit.plugin;

import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.command.Command;
import org.bukkit.command.PluginCommand;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class YamlPluginDescription extends PluginDescription {
private static final Yaml yaml = new Yaml(new SafeConstructor());

@SuppressWarnings("unchecked")
public YamlPluginDescription(final InputStream stream) throws InvalidDescriptionException {
loadMap((Map<String, Object>)yaml.load(stream));
}

/**
* Loads a PluginDescriptionFile from the specified reader
* @param reader
*/
@SuppressWarnings("unchecked")
public YamlPluginDescription(final Reader reader) throws InvalidDescriptionException {
loadMap((Map<String, Object>)yaml.load(reader));
}

/**
* Saves this PluginDescriptionFile to the given writer
*
* @param writer Writer to output this file to
*/
public void save(Writer writer) {
yaml.dump(saveMap(), writer);
}

private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
try {
name = map.get("name").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "name is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "name is of wrong type");
}

try {
version = map.get("version").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "version is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "version is of wrong type");
}

try {
main = map.get("main").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "main is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "main is of wrong type");
}

if (map.containsKey("commands")) {
try {
commands = map.get("commands");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "commands are of wrong type");
}
}

if (map.containsKey("website")) {
try {
website = (String)map.get("website");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "website is of wrong type");
}
}

if (map.containsKey("description")) {
try {
description = (String)map.get("description");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "description is of wrong type");
}
}

if (map.containsKey("author")) {
try {
authors = new ArrayList<String>();
authors.add((String)map.get("author"));
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "author is of wrong type");
}
}

if (map.containsKey("authors")) {
try {
authors = (ArrayList<String>)map.get("authors");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "authors are of wrong type");
}
}
}

private Map<String, Object> saveMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("main", main);
map.put("version", version);

if (commands != null) map.put("command", commands);
if (website != null) map.put("website", website);
if (description != null) map.put("description", description);

if (authors.size() == 1) {
map.put("author", authors.get(0));
} else if (authors.size() > 1) {
map.put("authors", authors);
}

return map;
}

@SuppressWarnings("unchecked")
public static List<Command> parse(Plugin plugin) {
List<Command> pluginCmds = new ArrayList<Command>();
Object object = plugin.getDescription().getCommands();
if (object == null)
return pluginCmds;

Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object;

if (map != null) {
for(Entry<String, Map<String, Object>> entry : map.entrySet()) {
Command newCmd = new PluginCommand(entry.getKey(),plugin);
Object description = entry.getValue().get("description");
Object usage = entry.getValue().get("usage");
Object aliases = entry.getValue().get("aliases");

if (description != null)
newCmd.setTooltip(description.toString());

if (usage != null) {
newCmd.setUsage(usage.toString());
}

if (aliases != null) {
List<String> aliasList = new ArrayList<String>();

if (aliases instanceof List) {
for (Object o : (List<Object>)aliases) {
aliasList.add(o.toString());
}
} else {
aliasList.add(aliases.toString());
}

newCmd.setAliases(aliasList);
}

pluginCmds.add(newCmd);
}
}
return pluginCmds;
}
}

0 comments on commit 6f1f99c

Please sign in to comment.