From 5ffc37f2dd2dd8843b326884cd1d1caa77bb0709 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 14 Aug 2015 06:51:06 +0200 Subject: [PATCH] Fix ChatColor casing --- .../massivecraft/massivecore/mson/Mson.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index 5566870b..afab08cb 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -44,11 +44,31 @@ public class Mson implements Serializable // GSON // -------------------------------------------- // - public static final Gson GSON = new GsonBuilder() - .disableHtmlEscaping() - .registerTypeAdapter(ChatColor.class, ADAPTER_LOWERCASE_CHAT_COLOR) - .registerTypeAdapter(MsonEventAction.class, ADAPTER_LOWERCASE_MSON_EVENT_ACTION) - .create(); + public static final Gson GSON; + + static + { + GsonBuilder builder = new GsonBuilder(); + builder.disableHtmlEscaping(); + + builder.registerTypeAdapter(MsonEventAction.class, ADAPTER_LOWERCASE_MSON_EVENT_ACTION); + builder.registerTypeAdapter(ChatColor.class, ADAPTER_LOWERCASE_CHAT_COLOR); + + // For some unknown reason, the different chat colors + // have their own instance of java.lang.Class. + // For them to be serialized properly with gson, + // we must specify the adapter for ALL of these classes. + + // However the adapter should be created with the base class + // because the base class returns true on Class#isEnum + // and returns a non-null value on Class#getEnumConstants. + for (ChatColor color : ChatColor.values()) + { + builder.registerTypeAdapter(color.getClass(), ADAPTER_LOWERCASE_CHAT_COLOR); + } + + GSON = builder.create(); + } // -------------------------------------------- // // FIELDS