package net.programmer.igoodie.twitchspawn.tslanguage.action;

import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.programmer.igoodie.twitchspawn.TwitchSpawn;
import net.programmer.igoodie.twitchspawn.tslanguage.EventArguments;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLParser;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLSyntaxError;

import java.util.LinkedList;
import java.util.List;

public class ExecuteAction extends TSLAction {

    private List<String> commands;

    public ExecuteAction(List<String> words) throws TSLSyntaxError {
        this.message = TSLParser.parseMessage(words);
        List<String> actionWords = actionPart(words);

        if (actionWords.size() == 0)
            throw new TSLSyntaxError("Expected at least one command.");

        if (!actionWords.stream().allMatch(word -> word.startsWith("/")))
            throw new TSLSyntaxError("Every command must start with '/' character");

        this.commands = new LinkedList<>(words);
    }

    @Override
    protected void performAction(ServerPlayerEntity player, EventArguments args) {
        CommandSource source = player.getCommandSource()
                .withPermissionLevel(9999) // OVER 9000!
                .withFeedbackDisabled();


        for (String command : commands) {
            TwitchSpawn.SERVER.execute(() -> {
                int result = TwitchSpawn.SERVER
                        .getCommandManager()
                        .handleCommand(source, replaceExpressions(command, args));

                TwitchSpawn.LOGGER.info("Executed (Status:{}) -> {}", result, replaceExpressions(command, args));
            });
        }
    }

}

@@ -1584,24 +1584,624 @@
 d, args));%0A%0A
+                if (result %3C= 0) %7B // Wohooo we knew iGoodie liked hacky solutions. ( ? :/ )%0A                    // If it yielded an error, and not worked as expected%0A                    // Then turn on the feedback, and run it again! Brilliant! What could go wrong? :))))))%0A                    CommandSource newSource = player.getCommandSource()%0A                            .withPermissionLevel(9999);%0A                    TwitchSpawn.SERVER%0A                            .getCommandManager()%0A                            .handleCommand(newSource, replaceExpressions(command, args));%0A                %7D%0A%0A
             

add feedback for EXECUTE action errors