Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move YAML parsing to a PluginDescription subclass.
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
Showing
4 changed files
with
182 additions
and
194 deletions.
There are no files selected for viewing
55 changes: 0 additions & 55 deletions
55
src/main/java/org/bukkit/command/PluginCommandYamlParser.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/main/java/org/bukkit/plugin/YamlPluginDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.