Add a system for creating task chains

This will make it easier to use the asynchronous coordinator.
This commit is contained in:
Alexander Söderberg 2020-10-06 15:50:16 +02:00 committed by Alexander Söderberg
parent 8e52bf705c
commit aaa6386ca3
13 changed files with 660 additions and 25 deletions

View file

@ -288,7 +288,7 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
@NonNull ParserParameters> {
@Override
public @NonNull ParserParameters apply(@NonNull final Greedy greedy, @NonNull final TypeToken<?> typeToken) {
public @NonNull ParserParameters apply(final @NonNull Greedy greedy, final @NonNull TypeToken<?> typeToken) {
return ParserParameters.single(StandardParameters.GREEDY, true);
}

View file

@ -1,6 +1,7 @@
dependencies {
api project(':cloud-core')
api project(':cloud-brigadier')
api project(':cloud-tasks')
compileOnly 'org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT'
compileOnly 'me.lucko:commodore:1.9'
}

View file

@ -38,6 +38,8 @@ import cloud.commandframework.bukkit.parsers.selector.MultiplePlayerSelectorArgu
import cloud.commandframework.bukkit.parsers.selector.SingleEntitySelectorArgument;
import cloud.commandframework.bukkit.parsers.selector.SinglePlayerSelectorArgument;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.tasks.TaskFactory;
import cloud.commandframework.tasks.TaskRecipe;
import io.leangen.geantyref.TypeToken;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -73,6 +75,9 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
private final Function<CommandSender, C> commandSenderMapper;
private final Function<C, CommandSender> backwardsCommandSenderMapper;
private final BukkitSynchronizer bukkitSynchronizer;
private final TaskFactory taskFactory;
private boolean splitAliases = false;
/**
@ -96,6 +101,9 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
this.commandSenderMapper = commandSenderMapper;
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
this.bukkitSynchronizer = new BukkitSynchronizer(owningPlugin);
this.taskFactory = new TaskFactory(this.bukkitSynchronizer);
/* Try to determine the Minecraft version */
int version = -1;
try {
@ -142,6 +150,15 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
new MultiplePlayerSelectorArgument.MultiplePlayerSelectorParser<>());
}
/**
* Create a new task recipe. This can be used to create chains of synchronous/asynchronous method calls
*
* @return Task recipe
*/
public @NonNull TaskRecipe taskRecipe() {
return this.taskFactory.recipe();
}
/**
* Get the plugin that owns the manager
*

View file

@ -0,0 +1,76 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.bukkit;
import cloud.commandframework.tasks.TaskConsumer;
import cloud.commandframework.tasks.TaskFunction;
import cloud.commandframework.tasks.TaskSynchronizer;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.concurrent.CompletableFuture;
final class BukkitSynchronizer implements TaskSynchronizer {
private final Plugin plugin;
BukkitSynchronizer(final @NonNull Plugin plugin) {
this.plugin = plugin;
}
@Override
public <I> CompletableFuture<Void> runSynchronous(final @NonNull I input, final @NonNull TaskConsumer<I> consumer) {
final CompletableFuture<Void> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> {
consumer.accept(input);
future.complete(null);
});
return future;
}
@Override
public <I, O> CompletableFuture<O> runSynchronous(final @NonNull I input, final @NonNull TaskFunction<I, O> function) {
final CompletableFuture<O> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> future.complete(function.apply(input)));
return future;
}
@Override
public <I> CompletableFuture<Void> runAsynchronous(final @NonNull I input, final @NonNull TaskConsumer<I> consumer) {
final CompletableFuture<Void> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
consumer.accept(input);
future.complete(null);
});
return future;
}
@Override
public <I, O> CompletableFuture<O> runAsynchronous(final @NonNull I input, final @NonNull TaskFunction<I, O> function) {
final CompletableFuture<O> future = new CompletableFuture<>();
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> future.complete(function.apply(input)));
return future;
}
}

View file

@ -0,0 +1,44 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Consumer;
/**
* Task step that does not produce any output
*
* @param <I> Input type
*/
@FunctionalInterface
public interface TaskConsumer<I> extends Consumer<@NonNull I>, TaskRecipeStep {
/**
* {@inheritDoc}
*/
@Override
void accept(@NonNull I input);
}

View file

@ -0,0 +1,53 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Factory that produces {@link TaskRecipe recipes}
*/
public final class TaskFactory {
private final TaskSynchronizer synchronizer;
/**
* Construct a new task factory
*
* @param synchronizer Synchronizer that will be passed onto the task recipes
*/
public TaskFactory(final @NonNull TaskSynchronizer synchronizer) {
this.synchronizer = synchronizer;
}
/**
* Create a new {@link TaskRecipe}
*
* @return New recipe instance
*/
public @NonNull TaskRecipe recipe() {
return new TaskRecipe(this.synchronizer);
}
}

View file

@ -0,0 +1,54 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Function;
/**
* Task step that produces output from given input
*
* @param <I> Input type
* @param <O> Output type
*/
public interface TaskFunction<I, O> extends Function<@NonNull I, @NonNull O>, TaskRecipeStep {
/**
* {@inheritDoc}
*/
@Override
@NonNull O apply(@NonNull I input);
/**
* Equivalent to {@link Function#identity()}
*
* @param <I> Input type
* @return Function that maps the input to itself
*/
static <I> TaskFunction<I, I> identity() {
return i -> i;
}
}

View file

@ -0,0 +1,237 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
/**
* A task recipe is a chain of tasks with optional synchronization steps,
* that can be used to produce some sort of result from some input
*/
@SuppressWarnings("ALL")
public final class TaskRecipe {
private final TaskSynchronizer synchronizer;
private final LinkedHashMap<TaskRecipeStep, Boolean> recipeSteps = new LinkedHashMap<>();
TaskRecipe(final @NonNull TaskSynchronizer synchronizer) {
this.synchronizer = synchronizer;
}
/**
* Begin the recipe. This step always runs asynchronously.
*
* @param input Input
* @param <I> Input type
* @return Function that maps the input to itself
*/
public <I> @NonNull TaskRecipeComponentOutputting<I, I> begin(final @NonNull I input) {
this.addAsynchronous(TaskFunction.identity());
return new TaskRecipeComponentOutputting<>(input);
}
private void addAsynchronous(final TaskRecipeStep taskRecipeStep) {
this.recipeSteps.put(taskRecipeStep, false);
}
private void addSynchronous(final TaskRecipeStep taskRecipeStep) {
this.recipeSteps.put(taskRecipeStep, true);
}
private void execute(final @NonNull Object initialInput, final @NonNull Runnable callback) {
final Iterator<Map.Entry<TaskRecipeStep, Boolean>> iterator = new LinkedHashMap<>(this.recipeSteps).entrySet().iterator();
CompletableFuture completableFuture = CompletableFuture.completedFuture(initialInput);
completableFuture.whenComplete(this.execute(iterator, callback));
}
private BiConsumer execute(final @NonNull Iterator<Map.Entry<TaskRecipeStep, Boolean>> iterator,
final @NonNull Runnable callback) {
return (o, o2) -> {
if (iterator.hasNext()) {
final Map.Entry<TaskRecipeStep, Boolean> entry = iterator.next();
final boolean synchronous = entry.getValue();
CompletableFuture other;
if (entry.getKey() instanceof TaskFunction) {
final TaskFunction function = (TaskFunction<?, ?>) entry.getKey();
if (synchronous) {
other = this.synchronizer.runSynchronous(o, function);
} else {
other = this.synchronizer.runAsynchronous(o, function);
}
} else {
final TaskConsumer consumer = (TaskConsumer<?>) entry.getKey();
if (synchronous) {
other = this.synchronizer.runSynchronous(o, consumer);
} else {
other = this.synchronizer.runAsynchronous(o, consumer);
}
}
other.whenComplete(this.execute(iterator, callback));
} else {
callback.run();
}
};
}
/**
* Represents a partial recipe
*
* @param <I> Input type
* @param <O> Output type
*/
public final class TaskRecipeComponentOutputting<I, O> {
private final Object initialInput;
private TaskRecipeComponentOutputting(final @NonNull Object initialInput) {
this.initialInput = initialInput;
}
/**
* Add a new synchronous step, consuming the input of the earlier step
*
* @param function Function mapping the input to some output
* @param <T> Output type
* @return New task recipe component
*/
public <T> TaskRecipeComponentOutputting<O, T> synchronous(final @NonNull TaskFunction<O, T> function) {
addSynchronous(function);
return new TaskRecipeComponentOutputting<>(this.initialInput);
}
/**
* Add a new asynchronous step, consuming the input of the earlier step
*
* @param function Function mapping the input to some output
* @param <T> Output type
* @return New task recipe component
*/
public <T> TaskRecipeComponentOutputting<O, T> asynchronous(final @NonNull TaskFunction<O, T> function) {
addAsynchronous(function);
return new TaskRecipeComponentOutputting<>(this.initialInput);
}
/**
* Add a new synchronous step, consuming the input of the earlier step
*
* @param consumer Consumer that consumes the input
* @return New task recipe component
*/
public TaskRecipeComponentVoid<O> synchronous(final @NonNull TaskConsumer<O> consumer) {
addSynchronous(consumer);
return new TaskRecipeComponentVoid<>(initialInput);
}
/**
* Add a new asynchronous step, consuming the input of the earlier step
*
* @param consumer Consumer that consumes the input
* @return New task recipe component
*/
public TaskRecipeComponentVoid<O> asynchronous(final @NonNull TaskConsumer<O> consumer) {
addSynchronous(consumer);
return new TaskRecipeComponentVoid<>(initialInput);
}
/**
* Execute the recipe
*
* @param callback Callback function
*/
public void execute(final @NonNull Runnable callback) {
TaskRecipe.this.execute(initialInput, callback);
}
/**
* Execute the recipe
*/
public void execute() {
this.execute(() -> {});
}
}
/**
* Represents a partial recipe
*
* @param <I> Input type
*/
public final class TaskRecipeComponentVoid<I> {
private final Object initialInput;
private TaskRecipeComponentVoid(final @NonNull Object initialInput) {
this.initialInput = initialInput;
}
/**
* Add a new synchronous step, consuming the input of the earlier step
*
* @param consumer Consumer that consumes the input
* @return New task recipe component
*/
public TaskRecipeComponentVoid<I> synchronous(final @NonNull TaskConsumer<I> consumer) {
addSynchronous(consumer);
return new TaskRecipeComponentVoid<>(initialInput);
}
/**
* Add a new asynchronous step, consuming the input of the earlier step
*
* @param consumer Consumer that consumes the input
* @return New task recipe component
*/
public TaskRecipeComponentVoid<I> asynchronous(final @NonNull TaskConsumer<I> consumer) {
addSynchronous(consumer);
return new TaskRecipeComponentVoid<>(initialInput);
}
/**
* Execute the recipe
*
* @param callback Callback function
*/
public void execute(final @NonNull Runnable callback) {
TaskRecipe.this.execute(initialInput, callback);
}
/**
* Execute the recipe
*/
public void execute() {
this.execute(() -> {});
}
}
}

View file

@ -0,0 +1,30 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
/**
* Part of a {@link TaskRecipe}
*/
public interface TaskRecipeStep {
}

View file

@ -0,0 +1,77 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.tasks;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.concurrent.CompletableFuture;
/**
* Utility responsible for synchronizing {@link TaskRecipeStep task recipe steps}
*/
public interface TaskSynchronizer {
/**
* Accept input into the consumer synchronously
*
* @param input Input to pass to the consumer
* @param consumer Consumer of the input
* @param <I> Input type
* @return Future that completes when the consumer is done
*/
<I> CompletableFuture<Void> runSynchronous(@NonNull I input, @NonNull TaskConsumer<I> consumer);
/**
* Produce output from accepted input synchronously
*
* @param input Input to pass to the function
* @param function Function that produces the output
* @param <I> Input type
* @param <O> Output type
* @return Future that completes with the output
*/
<I, O> CompletableFuture<O> runSynchronous(@NonNull I input, @NonNull TaskFunction<I, O> function);
/**
* Accept input into the consumer asynchronously
*
* @param input Input to pass to the consumer
* @param consumer Consumer of the input
* @param <I> Input type
* @return Future that completes when the consumer is done
*/
<I> CompletableFuture<Void> runAsynchronous(@NonNull I input, @NonNull TaskConsumer<I> consumer);
/**
* Produce output from accepted input asynchronously
*
* @param input Input to pass to the function
* @param function Function that produces the output
* @param <I> Input type
* @param <O> Output type
* @return Future that completes with the output
*/
<I, O> CompletableFuture<O> runAsynchronous(@NonNull I input, @NonNull TaskFunction<I, O> function);
}

View file

@ -0,0 +1,28 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
/**
* Cloud task library
*/
package cloud.commandframework.tasks;

View file

@ -44,6 +44,7 @@ import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.bukkit.arguments.selector.SingleEntitySelector;
import cloud.commandframework.bukkit.parsers.WorldArgument;
import cloud.commandframework.bukkit.parsers.selector.SingleEntitySelectorArgument;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
import cloud.commandframework.meta.CommandMeta;
@ -53,6 +54,7 @@ import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
@ -86,13 +88,13 @@ public final class ExamplePlugin extends JavaPlugin {
// This is a function that will provide a command execution coordinator that parses and executes commands
// asynchronously
//
// final Function<CommandTree<CommandSender>, CommandExecutionCoordinator<CommandSender>> executionCoordinatorFunction =
// AsynchronousCommandExecutionCoordinator.<CommandSender>newBuilder().build();
//
// However, in this example it is fine for us to run everything synchronously
//
final Function<CommandTree<CommandSender>, CommandExecutionCoordinator<CommandSender>> executionCoordinatorFunction =
CommandExecutionCoordinator.simpleCoordinator();
AsynchronousCommandExecutionCoordinator.<CommandSender>newBuilder().build();
//
// However, in many cases it is fine for to run everything synchronously:
//
// final Function<CommandTree<CommandSender>, CommandExecutionCoordinator<CommandSender>> executionCoordinatorFunction =
// CommandExecutionCoordinator.simpleCoordinator();
//
// This function maps the command sender type of our choice to the bukkit command sender.
// However, in this example we use the Bukkit command sender, and so we just need to map it
@ -207,20 +209,23 @@ public final class ExamplePlugin extends JavaPlugin {
Triplet.of(Integer.class, Integer.class, Integer.class),
triplet -> new Vector(triplet.getFirst(), triplet.getSecond(), triplet.getThird()),
Description.of("Coordinates"))
.handler(context -> {
final Player player = (Player) context.getSender();
final World world = context.get(worldArgument);
final Vector coords = context.get("coords");
.handler(context -> manager.taskRecipe().begin(context)
.synchronous(commandContext -> {
final Player player = (Player) commandContext.getSender();
final World world = commandContext.get(worldArgument);
final Vector coords = commandContext.get("coords");
final Location location = coords.toLocation(world);
player.teleport(location);
}))
}).execute()))
.command(builder.literal("teleport")
.literal("entity")
.withSenderType(Player.class)
.argument(SingleEntitySelectorArgument.of("entity"),
Description.of("Entity to teleport"))
.literal("here")
.handler(commandContext -> {
.handler(
context -> manager.taskRecipe().begin(context)
.synchronous(commandContext -> {
final Player player = (Player) commandContext.getSender();
final SingleEntitySelector singleEntitySelector = commandContext.get("entity");
if (singleEntitySelector.hasAny()) {
@ -229,7 +234,20 @@ public final class ExamplePlugin extends JavaPlugin {
} else {
player.sendMessage(ChatColor.RED + "No entity matched your query.");
}
}));
}).execute()
));
manager.command(builder.literal("tasktest")
.handler(context -> manager.taskRecipe()
.begin(context)
.asynchronous(c -> {
c.getSender().sendMessage("ASYNC: " + !Bukkit.isPrimaryThread());
return c;
})
.synchronous(c -> {
c.getSender().sendMessage("SYNC: " + Bukkit.isPrimaryThread());
})
.execute(() -> context.getSender().sendMessage("DONE!"))
));
}
@CommandMethod("example help [query]")

View file

@ -13,6 +13,7 @@ include(':cloud-cloudburst')
include(':cloud-javacord')
include(':cloud-jda')
include(':example-bukkit')
include(':cloud-tasks')
project(':cloud-bukkit').projectDir = file('cloud-minecraft/cloud-bukkit')
project(':cloud-paper').projectDir = file('cloud-minecraft/cloud-paper')
project(':cloud-brigadier').projectDir = file('cloud-minecraft/cloud-brigadier')
@ -23,4 +24,3 @@ project(':cloud-cloudburst').projectDir = file('cloud-minecraft/cloud-cloudburst
project(':cloud-javacord').projectDir = file('cloud-discord/cloud-javacord')
project(':cloud-jda').projectDir = file('cloud-discord/cloud-jda')
project(':example-bukkit').projectDir = file('examples/example-bukkit')