Java tutorial
/** * This file is a component of Quartz Powered, this license makes sure any work * associated with Quartz Powered, must follow the conditions of the license included. * * The MIT License (MIT) * * Copyright (c) 2015 Quartz Powered * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.quartzpowered.protocol.data.chat.component.serialize; import com.google.common.base.Preconditions; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonObject; import com.google.gson.JsonSerializationContext; import org.quartzpowered.protocol.data.chat.ChatColor; import org.quartzpowered.protocol.data.chat.component.BaseComponent; import org.quartzpowered.protocol.data.chat.component.ClickEvent; import org.quartzpowered.protocol.data.chat.component.HoverEvent; import java.util.Arrays; import java.util.HashSet; public class BaseComponentSerializer { protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context) { if (object.has("color")) { component.setColor(ChatColor.valueOf(object.get("color").getAsString().toUpperCase())); } if (object.has("bold")) { component.setBold(object.get("bold").getAsBoolean()); } if (object.has("italic")) { component.setItalic(object.get("italic").getAsBoolean()); } if (object.has("underlined")) { component.setUnderlined(object.get("underlined").getAsBoolean()); } if (object.has("strikethrough")) { component.setStrikethrough(object.get("strikethrough").getAsBoolean()); } if (object.has("obfuscated")) { component.setObfuscated(object.get("obfuscated").getAsBoolean()); } if (object.has("extra")) { component.setExtra(Arrays.<BaseComponent>asList( context.<BaseComponent[]>deserialize(object.get("extra"), BaseComponent[].class))); } //Events if (object.has("clickEvent")) { JsonObject event = object.getAsJsonObject("clickEvent"); component.setClickEvent( new ClickEvent(ClickEvent.Action.valueOf(event.get("action").getAsString().toUpperCase()), event.get("value").getAsString())); } if (object.has("hoverEvent")) { JsonObject event = object.getAsJsonObject("hoverEvent"); BaseComponent[] res; if (event.get("value").isJsonArray()) { res = context.deserialize(event.get("value"), BaseComponent[].class); } else { res = new BaseComponent[] { context.<BaseComponent>deserialize(event.get("value"), BaseComponent.class) }; } component.setHoverEvent(new HoverEvent( HoverEvent.Action.valueOf(event.get("action").getAsString().toUpperCase()), res)); } } protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context) { boolean first = false; if (ComponentSerializer.serializedComponents.get() == null) { first = true; ComponentSerializer.serializedComponents.set(new HashSet<BaseComponent>()); } try { Preconditions.checkArgument(!ComponentSerializer.serializedComponents.get().contains(component), "Component loop"); ComponentSerializer.serializedComponents.get().add(component); if (component.getColorRaw() != null) { object.addProperty("color", component.getColorRaw().getName()); } if (component.isBoldRaw() != null) { object.addProperty("bold", component.isBoldRaw()); } if (component.isItalicRaw() != null) { object.addProperty("italic", component.isItalicRaw()); } if (component.isUnderlinedRaw() != null) { object.addProperty("underlined", component.isUnderlinedRaw()); } if (component.isStrikethroughRaw() != null) { object.addProperty("strikethrough", component.isStrikethroughRaw()); } if (component.isObfuscatedRaw() != null) { object.addProperty("obfuscated", component.isObfuscatedRaw()); } if (component.getExtra() != null) { object.add("extra", context.serialize(component.getExtra())); } //Events if (component.getClickEvent() != null) { JsonObject clickEvent = new JsonObject(); clickEvent.addProperty("action", component.getClickEvent().getAction().toString().toLowerCase()); clickEvent.addProperty("value", component.getClickEvent().getValue()); object.add("clickEvent", clickEvent); } if (component.getHoverEvent() != null) { JsonObject hoverEvent = new JsonObject(); hoverEvent.addProperty("action", component.getHoverEvent().getAction().toString().toLowerCase()); hoverEvent.add("value", context.serialize(component.getHoverEvent().getValue())); object.add("hoverEvent", hoverEvent); } } finally { ComponentSerializer.serializedComponents.get().remove(component); if (first) { ComponentSerializer.serializedComponents.set(null); } } } }