001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.meta;
019
020import com.google.gson.JsonObject;
021
022import javax.annotation.concurrent.NotThreadSafe;
023
024/**
025 * Chat message properties, encoded in the JSON format
026 *
027 * @author The TridentSDK Team
028 * @since 0.3-alpha-DP
029 */
030@NotThreadSafe
031public final class Message {
032    private final JsonObject message;
033
034    /**
035     * Starts building a new chat message
036     */
037    public Message() {
038        this.message = new JsonObject();
039    }
040
041    /**
042     * The text to display to the messageable
043     *
044     * @param input the text
045     * @return the current message
046     */
047    public Message text(String input) {
048        this.message().addProperty("text", input);
049        return this;
050    }
051
052    /**
053     * Adds color to the message
054     *
055     * @param color the color to add
056     * @return the current message
057     */
058    public Message color(ChatColor color) {
059        this.message().addProperty("color", color.name().toLowerCase());
060
061        return this;
062    }
063
064    /**
065     * Adds an action when the message is clicked
066     *
067     * @param event the clickevent to use
068     * @return the current message
069     */
070    public Message clickEvent(ClickEvent event) {
071        JsonObject obj = new JsonObject();
072
073        obj.addProperty("action", event.action().toString());
074        obj.addProperty("value", event.value());
075
076        this.message().add("clickEvent", obj);
077        return this;
078    }
079
080    /**
081     * Adds an action when the message is hovered by the mouse
082     *
083     * @param event the hoverevent to use
084     * @return the current message
085     */
086    public Message hoverEvent(HoverEvent event) {
087        JsonObject obj = new JsonObject();
088
089        obj.addProperty("action", event.action().toString());
090        obj.addProperty("value", event.value());
091
092        this.message().add("hoverEvent", obj);
093        return this;
094    }
095
096    /**
097     * The underlying JSON formatted string of the message
098     *
099     * @return the JSON text
100     */
101    public String asJson() {
102        return MessageBuilder.GSON.toJson(this.message());
103    }
104
105    /**
106     * The object representation of the JSON
107     *
108     * @return the message in object form of JSON
109     */
110    public JsonObject message() {
111        return message;
112    }
113}