Fix Pattern serialization

By: Thinkofdeath <thinkofdeath@spigotmc.org>
This commit is contained in:
Bukkit/Spigot 2014-12-04 10:10:54 +00:00
parent d6048829a5
commit dbfed2c8bf

View File

@ -1,11 +1,18 @@
package org.bukkit.block.banner;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.bukkit.DyeColor;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
@SerializableAs("Pattern")
public class Pattern {
public class Pattern implements ConfigurationSerializable {
private static final String COLOR = "color";
private static final String PATTERN = "pattern";
private final DyeColor color;
private final PatternType pattern;
@ -20,6 +27,32 @@ public class Pattern {
this.color = color;
this.pattern = pattern;
}
/**
* Constructor for deserialization.
*
* @param map the map to deserialize from
*/
public Pattern(Map<String, Object> map) {
color = DyeColor.valueOf(getString(map, COLOR));
pattern = PatternType.getByIdentifier(getString(map, PATTERN));
}
private static String getString(Map<?,?> map, Object key) {
Object str = map.get(key);
if (str instanceof String) {
return (String) str;
}
throw new NoSuchElementException(map + " does not contain " + key);
}
@Override
public Map<String, Object> serialize() {
return ImmutableMap.<String, Object>of(
COLOR, color.toString(),
PATTERN, pattern.getIdentifier()
);
}
/**
* Returns the color of the pattern