package io.github.deathcap.bukkit2sponge.command;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.util.command.*;

import java.util.ArrayList;

public class BukkitCommand extends Command {

    private CommandMapping commandMapping;
    private static ShinyConsoleSource commandSource = new ShinyConsoleSource("console"); // TODO

    public BukkitCommand(CommandMapping commandMapping) {
        super(commandMapping.getPrimaryAlias(),
                getDescription(commandMapping),
                getUsage(commandMapping),
                new ArrayList<>(commandMapping.getAllAliases())); // TODO: remove primary alias?

        this.commandMapping = commandMapping;
    }

    private static String getDescription(CommandMapping commandMapping) {
        Optional<Text> textOptional = commandMapping.getCallable().getShortDescription(commandSource);;
        if (!textOptional.isPresent()) {
            return commandMapping.getPrimaryAlias();
        }

        return Texts.toPlain(textOptional.get());
    }

    private static String getUsage(CommandMapping commandMapping) {
        Text text = commandMapping.getCallable().getUsage(commandSource);;

        return Texts.toPlain(text);
    }

   @Override
    public boolean execute(CommandSender commandSender, String s, String[] strings) {

        //CommandSource commandSource = commandSource; // TODO: from commandSender
        String arguments = Joiner.on(' ').join(strings); // TODO: ?

        try {
            Optional<CommandResult> commandResult = commandMapping.getCallable().process(commandSource, arguments);

            return commandResult.isPresent(); // TODO
        } catch (CommandException ex) {
            ex.printStackTrace();
            return false;
        }
    }
}

@@ -1190,42 +1190,564 @@
 urn 
-Texts.toPlain(textOptional.get());
+spongeTextToString(textOptional.get());%0A    %7D%0A%0A    private static String spongeTextToString(Text text) %7B%0A        try %7B%0A            return Texts.toPlain(text);%0A        %7D catch (NullPointerException ex) %7B%0A            // Incomplete part of SpongeAPI? null factory%0A            // https://github.com/SpongePowered/SpongeAPI/commit/38acf16861b99c76c8df23f8fceabe896217318b#commitcomment-10900228%0A            if (text instanceof Text.Literal) %7B%0A                return ((Text.Literal) text).getContent();%0A            %7D%0A            return text.toString();%0A        %7D
 %0A   
@@ -1905,37 +1905,42 @@
      return 
-Texts.toPla
+spongeTextToStr
 in
+g
 (text);%0A    

Improve Sponge->Bukkit registered command descriptions