From dbfed2c8bfc3cbc0fc4b420e37463a9126aa0bfd Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Thu, 4 Dec 2014 10:10:54 +0000 Subject: [PATCH] Fix Pattern serialization By: Thinkofdeath --- .../java/org/bukkit/block/banner/Pattern.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java b/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java index ba51c1fcd3..95b0f3c8b5 100644 --- a/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java +++ b/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java @@ -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 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 serialize() { + return ImmutableMap.of( + COLOR, color.toString(), + PATTERN, pattern.getIdentifier() + ); + } /** * Returns the color of the pattern