net.jselby.pc.player.ChatMessage.java Source code

Java tutorial

Introduction

Here is the source code for net.jselby.pc.player.ChatMessage.java

Source

/*
 * PoweredCube3
 * Copyright (C) 2014 James
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.jselby.pc.player;

import net.jselby.pc.network.ChatColor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;

/**
 * Represents a SimpleJSON chat message, which can be sent via the
 * PacketOutChatMessage packet
 */
public class ChatMessage {
    public JSONObject json;

    /**
     * Creates a ChatMessage from a already existing JSONObject.
     *
     * @param json The chat messages contents
     */
    public ChatMessage(JSONObject json) {
        this.json = json;
    }

    /**
     * Converts a string to a basic ChatMessage
     *
     * @param message The String message
     * @return A Chat message
     */
    public static ChatMessage convertToJson(String message) {
        JSONObject obj = new JSONObject();

        // Parse message, looking for colors etc.
        ArrayList<JSONObject> objects = new ArrayList<JSONObject>();

        ChatColor color = ChatColor.WHITE;
        String currentMessage = "";

        boolean nextByteAsColor = false;

        for (char bytes : message.toCharArray()) {
            if (nextByteAsColor) {
                color = ChatColor.getByChar(bytes);
                nextByteAsColor = false;
                continue;
            }
            if (bytes == ChatColor.COLOR_CHAR) { // Special color symbol
                if (currentMessage.length() > 0) {
                    JSONObject currentMessageAsJSON = new JSONObject();
                    currentMessageAsJSON.put("text", currentMessage);
                    currentMessageAsJSON.put("color", color.name().toLowerCase());
                    objects.add(currentMessageAsJSON);
                }
                color = ChatColor.WHITE;
                currentMessage = "";
                nextByteAsColor = true;
            } else {
                currentMessage += bytes;
            }
        }

        if (currentMessage.length() > 0) {
            JSONObject rest = new JSONObject();
            rest.put("text", currentMessage);
            rest.put("color", color.name().toLowerCase());
            objects.add(rest);
        }

        JSONArray array = new JSONArray();

        for (JSONObject jsonObject : objects) {
            array.add(jsonObject);
        }

        obj.put("extra", array);
        obj.put("text", "");

        return new ChatMessage(obj);
    }
}