package com.namelessmc.plugin.spigot;

import com.namelessmc.java_api.NamelessException;
import com.namelessmc.java_api.modules.websend.WebsendCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.List;
import java.util.logging.Logger;

public class WebsendCommandExecutor implements Runnable {

	@Override
	public void run() {
		NamelessPlugin inst = NamelessPlugin.getInstance();
		FileConfiguration config = inst.getConfig();
		Logger log = inst.getLogger();
		final int serverId = config.getInt("server-id");
		if (serverId <= 0) {
			log.warning("Websend is enabled but 'server-id' in config.yml is not set properly.");
			return;
		}

		Bukkit.getScheduler().runTaskAsynchronously(inst, () -> {
			inst.getNamelessApi().ifPresent(api -> {
				try {
					final List<WebsendCommand> commands = api.websend().getCommands(serverId);
					if (commands.isEmpty()) {
						return;
					}

					Bukkit.getScheduler().runTask(inst, () -> {
						for (final WebsendCommand command : commands) {
							try {
								Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.getCommandLine());
							} catch (final CommandException e) {
								// If we don't catch exceptions here, the plugin might never mark commands as executed
								// which means it can run some commands over and over again (with side effects)
								e.printStackTrace();
							}
						}

						Bukkit.getScheduler().runTaskAsynchronously(inst, () -> {
							inst.getNamelessApi().ifPresent(api2 -> {
								try {
									api2.websend().markCommandsExecuted(serverId, commands);
								} catch (NamelessException e) {
									log.severe("Error while marking websend commands as executed.");
									log.severe("This is bad, websend may run commands multiple times!");
									e.printStackTrace();
								}
							});
						});
					});
				} catch (NamelessException e) {
					log.severe("Error retrieving websend commands");
					e.printStackTrace();
				}
			});
		});
	}

}

@@ -1240,613 +1240,55 @@
 %09// 
-If we don't catch exceptions here, the plugin might never mark commands as executed%0A%09%09%09%09%09%09%09%09// which means it can run some commands over and over again (with side effects)%0A%09%09%09%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%0A%09%09%09%09%09%09Bukkit.getScheduler().runTaskAsynchronously(inst, () -%3E %7B%0A%09%09%09%09%09%09%09inst.getNamelessApi().ifPresent(api2 -%3E %7B%0A%09%09%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09%09%09api2.websend().markCommandsExecuted(serverId, commands);%0A%09%09%09%09%09%09%09%09%7D catch (NamelessException e) %7B%0A%09%09%09%09%09%09%09%09%09log.severe(%22Error while marking websend commands as executed.%22);%0A%09%09%09%09%09%09%09%09%09log.severe(%22This is bad, websend may run commands multiple times!%22);%0A%09
+continue executing other commands if one fails%0A
 %09%09%09%09
@@ -1323,17 +1323,16 @@
 %09%09%09%09
-%09
 %7D%0A%09%09%09%09%09%09
 %09%7D);
@@ -1331,22 +1331,9 @@
 %09%09%09%09
-%09%7D);%0A%09%09%09%09%09%09%7D);
+%7D
 %0A%09%09%09

remove marking commands as done