package net.glowstone.command.minecraft;

import net.glowstone.entity.GlowPlayer;
import net.glowstone.net.message.play.game.ChatMessage;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.VanillaCommand;
import org.bukkit.entity.Player;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import java.util.Collections;

public class TellrawCommand extends VanillaCommand {

    public TellrawCommand() {
        super("tellraw", "Send a private JSON message to the given player", "/tellraw <player> <raw-json-message>", Collections.emptyList());
        setPermission("minecraft.command.tellraw");
    }

    @Override
    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
        if (!testPermission(sender)) return true;
        if (args.length < 2) {
            sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
            return false;
        }

        Player player = Bukkit.getPlayerExact(args[0]);

        if (player == null || sender instanceof Player && !((Player) sender).canSee(player)) {
            sender.sendMessage("There's no player by that name online.");
        } else {
            StringBuilder message = new StringBuilder();

            for (int i = 1; i < args.length; i++) {
                if (i > 1) message.append(" ");
                message.append(args[i]);
            }

            GlowPlayer glowPlayer = (GlowPlayer) player;

            Object obj = JSONValue.parse(message.toString());
            if (!(obj instanceof JSONObject)) {
                sender.sendMessage(ChatColor.RED + "Failed to parse JSON");
            } else {
                glowPlayer.getSession().send(new ChatMessage((JSONObject) obj));
            }
        }

        return true;
    }
}

@@ -50,91 +50,91 @@
 net.
-glowstone.entity.GlowPlayer;%0Aimport net.glowstone.net.message.play.game.ChatMessage
+md_5.bungee.api.chat.BaseComponent;%0Aimport net.md_5.bungee.chat.ComponentSerializer
 ;%0Aim
@@ -307,24 +307,58 @@
 ity.Player;%0A
+import org.json.simple.JSONArray;%0A
 import org.j
@@ -413,16 +413,62 @@
 ONValue;
+%0Aimport org.json.simple.parser.ParseException;
 %0A%0Aimport
@@ -1292,24 +1292,50 @@
  online.%22);%0A
+            return false;%0A
         %7D el
@@ -1569,53 +1569,90 @@
     
-GlowPlayer glowPlayer = (GlowPlayer) player;%0A
+Object obj = null;%0A            String json = message.toString();%0A            try %7B
 %0A   
@@ -1656,30 +1656,27 @@
             
-Object
+   
  obj = JSONV
@@ -1689,74 +1689,66 @@
 arse
-(message.toString());%0A            if (!(obj instanceof JSONObject)
+WithException(json);%0A            %7D catch (ParseException e
 ) %7B%0A
@@ -1823,147 +1823,413 @@
 JSON
-%22);%0A            %7D else %7B%0A                glowPlayer.getSession().send(new ChatMessage((JSONObject) obj));%0A            %7D%0A        %7D%0A%0A
+: %22 + e.getMessage());%0A                return false;%0A            %7D%0A            if (obj instanceof JSONArray %7C%7C obj instanceof JSONObject) %7B%0A                BaseComponent%5B%5D components = ComponentSerializer.parse(json);%0A                player.sendMessage(components);%0A                return true;%0A            %7D else %7B%0A                sender.sendMessage(ChatColor.RED + %22Failed to parse JSON%22);%0A     
         
 retu
@@ -2220,33 +2220,61 @@
             
+   
 return 
-true;
+false;%0A            %7D%0A        %7D
 %0A    %7D%0A%7D%0A

Fix /tellraw command, allow for array of Text components (fixes GH-433)