Execute commands sequentially on a single thread executor

This commit is contained in:
Luck 2017-12-30 12:16:34 +00:00
parent e09c5622f7
commit b9dfd7db54
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 18 additions and 18 deletions

View File

@ -196,7 +196,8 @@ public class Importer implements Runnable {
@Getter
private static class ImportCommand extends DummySender {
private static final Splitter SPACE_SPLIT = Splitter.on(" ");
private static final Splitter ARGUMENT_SPLITTER = Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings();
private static final Splitter SPACE_SPLITTER = Splitter.on(" ");
private final CommandManager commandManager;
private final int id;
@ -231,13 +232,9 @@ public class Importer implements Runnable {
}
try {
CommandResult result = commandManager.onCommand(
this,
"lp",
CommandManager.stripQuotes(Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(getCommand()))
).get();
List<String> args = CommandManager.stripQuotes(ARGUMENT_SPLITTER.splitToList(getCommand()));
CommandResult result = commandManager.onCommand(this, "lp", args, Runnable::run).get();
setResult(result);
} catch (Exception e) {
setResult(CommandResult.FAILURE);
e.printStackTrace();
@ -253,7 +250,7 @@ public class Importer implements Runnable {
return null;
}
String targetUser = SPACE_SPLIT.split(subCmd).iterator().next();
String targetUser = SPACE_SPLITTER.split(subCmd).iterator().next();
return "u:" + targetUser;
}
@ -263,7 +260,7 @@ public class Importer implements Runnable {
return null;
}
String targetGroup = SPACE_SPLIT.split(subCmd).iterator().next();
String targetGroup = SPACE_SPLITTER.split(subCmd).iterator().next();
return "g:" + targetGroup;
}
@ -273,7 +270,7 @@ public class Importer implements Runnable {
return null;
}
String targetTrack = SPACE_SPLIT.split(subCmd).iterator().next();
String targetTrack = SPACE_SPLITTER.split(subCmd).iterator().next();
return "t:" + targetTrack;
}

View File

@ -72,6 +72,9 @@ import java.util.ListIterator;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -90,6 +93,9 @@ public class CommandManager {
@Getter
private final LuckPermsPlugin plugin;
// the default executor to run commands on
private final ExecutorService executor = Executors.newSingleThreadExecutor();
@Getter
private final List<Command> mainCommands;
@ -126,14 +132,11 @@ public class CommandManager {
.build();
}
/**
* Generic on command method to be called from the command executor object of the platform
* Unlike {@link #execute(Sender, String, List)}, this method is called in a new thread
* @param sender who sent the command
* @param label the command label used
* @param args the arguments provided
*/
public CompletableFuture<CommandResult> onCommand(Sender sender, String label, List<String> args) {
return onCommand(sender, label, args, executor);
}
public CompletableFuture<CommandResult> onCommand(Sender sender, String label, List<String> args, Executor executor) {
return CompletableFuture.supplyAsync(() -> {
try {
return execute(sender, label, args);
@ -142,7 +145,7 @@ public class CommandManager {
e.printStackTrace();
return null;
}
}, plugin.getScheduler().async());
}, executor);
}
@SuppressWarnings("unchecked")